mysql使用者的管理

來源:互聯網
上載者:User

標籤:mysql使用者的管理   mysql   mysql管理   

mysql使用者的管理

一、查看當前的串連帳號資訊

1.1、查看當前資料庫的以串連的帳號資訊

使用命令:show processlist

MySQL [(none)]> show processlist;

+--------+-------------+---------------------+--------+---------+------+----------+------------------+

| Id     | User        | Host                | db     | Command | Time | State    | Info             |

+--------+-------------+---------------------+--------+---------+------+----------+------------------+

| 232091 | zabbix      | 172.17.207.88:558 | zabbix | Sleep   |   20 |          | NULL             |

1.2、查看當前使用的是什麼帳號登入

使用命令select user()命令進行查看

MySQL [(none)]> select user();

+--------------------+

| user()             |

+--------------------+

| [email protected] |

+--------------------+

1 row in set (0.00 sec)

MySQL [(none)]>

二、建立使用者

2.1、新使用者的建立

使用creat user命令建立使用者並建立密碼

列子:create user 'zhang'@'localhost' identified by 'zhang';

  • 建立zhang使用者可以使用任意地址訪問並設定密碼為zhang

MySQL [(none)]> create user 'zhang'@'%' identified by 'zhang';

Query OK, 0 rows affected (0.01 sec)

MySQL [(none)]>

  • 設定完成後查看是否建立成功

MySQL [(none)]> select user,host from mysql.user;

+-------------+----------------+

| user        | host           |

+-------------+----------------+

| jumpserver  | %              |

| root        | %              |

| wordpress   | %              |

| zabbix      | 39.106.3.162 |

| %           | localhost      |

| zhang       | localhost      |

+-------------+----------------+

9 rows in set (0.01 sec)

MySQL [(none)]>

  • 使用新建立的使用者zhang進行登入並查看資料庫

[[email protected] ~]# mysql -uzhang -h120.26.32.14 -p

Enter password:

Welcome to the MariaDB monitor.  Commands end with ; or \g.

Your MySQL connection id is 1204

Server version: 5.6.35 Source distribution

Copyright (c) 2000, 2017, Oracle, MariaDB Corporation Ab and others.

Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.

  • 查看資料庫

MySQL [(none)]> show databases;

+--------------------+

| Database           |

+--------------------+

| information_schema |

| test               |

+--------------------+

2 rows in set (0.02 sec)

MySQL [(none)]>

三、刪除資料庫帳號

使用drop user 命令刪除使用者

MySQL [(none)]> drop user 'zhang'@'localhost';

Query OK, 0 rows affected (0.00 sec)

MySQL [(none)]>

四、重新命名使用者

4.1、使用rename user命令進行修改重新命名使用者

MySQL [(none)]> rename user 'zhang'@'%c' to 'zhang'@'%' ;

Query OK, 0 rows affected (0.02 sec)

MySQL [(none)]> select user,host from mysql.user;

+-------------+-------------------+

| user        | host              |

+-------------+-------------------+

| root        | %                 |

| user_name   | %                 |

| xuchangming | %                 |

| zhang       | %                 |

| root        | 127.0.0.1         |

| root        | ::1               |

|             | instance-jvfp1b6r |

| root        | instance-jvfp1b6r |

| root        | localhost         |

| xuchangming | localhost         |

+-------------+-------------------+

10 rows in set (0.01 sec)

MySQL [(none)]>

五、授權帳號

5.1、使用grant 命令進行授權帳號

命令格式為:

grant 許可權 privileges on 庫.表 to ‘帳號’@‘ip’ [identified by ‘登入密碼’];

庫表許可權說明:

on *.*     :管理員權限,任何資料庫都可以操作

on db_name.* :指定對某個庫進行操作,只有某個庫的許可權

on db_name.tables_name:指定某一個庫中的一個表有操作許可權

on db_name.routine_name:指定某個庫的預存程序或者儲存函數

5.2、使用命令 show grants命令查看許可權

SQL [(none)]> show grants;

+--------------------------------------------------------------------------------------------------------------------------------+

| Grants for [email protected]%                                                                                                              |

+--------------------------------------------------------------------------------------------------------------------------------+

| GRANT ALL PRIVILEGES ON *.* TO 'root'@'%' IDENTIFIED BY PASSWORD '*0FC3121124C80F34B383F5FCA33F0D68B6AFA1C0' WITH GRANT OPTION |

+--------------------------------------------------------------------------------------------------------------------------------+

1 row in set (0.01 sec)

MySQL [(none)]>

5.3、列子

5.3.1、授權所有許可權【管理員權限】給某一個帳號

建立boos使用者並設定登入密碼為boss,對所有庫和表授權所有操作並允許所有地址串連

MySQL [(none)]> grant all privileges on *.* to 'boos'@'%' identified  by 'boss';

Query OK, 0 rows affected (0.02 sec)

MySQL [(none)]>

登入查看

[[email protected] ~]# mysql -uboos -h120.76.32.14 -p

Enter password:

Welcome to the MariaDB monitor.  Commands end with ; or \g.

Your MySQL connection id is 1217

Server version: 5.6.35 Source distribution

Copyright (c) 2000, 2017, Oracle, MariaDB Corporation Ab and others.

Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.

MySQL [(none)]> show databases;

+--------------------+

| Database           |

+--------------------+

| information_schema |

| 測試               |

| ceshi              |

| employees          |

| mysql              |

| performance_schema |

| test               |

+--------------------+

7 rows in set (0.01 sec)

MySQL [(none)]>

MySQL [(none)]> select user();

+---------------------+

| user()              |

+---------------------+

| [email protected] |

+---------------------+

1 row in set (0.02 sec)

MySQL [(none)]>

5.3.2、授權所有許可權給某一帳號只針對某一個資料庫

建立帳號zhang並添加密碼zhang,修改許可權對 ceshi庫的所有操作

MySQL [(none)]> grant all  privileges on ceshi.* to 'zhang'@'%' identified by 'zhang' ;

Query OK, 0 rows affected (0.02 sec)

MySQL [(none)]>

[[email protected] ~]# mysql -uzhang -h120.76.32.14 -p

Enter password:

Welcome to the MariaDB monitor.  Commands end with ; or \g.

Your MySQL connection id is 1458

Server version: 5.6.35 Source distribution

Copyright (c) 2000, 2017, Oracle, MariaDB Corporation Ab and others.

Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.

MySQL [(none)]> show databases;

+--------------------+

| Database           |

+--------------------+

| information_schema |

| ceshi              |

| test               |

+--------------------+

3 rows in set (0.02 sec)

MySQL [(none)]> show grants;

+-------------------------------------------------------------------+

| Grants for [email protected]%                                                |

+-------------------------------------------------------------------+

| GRANT USAGE ON *.* TO 'zhang'@'%' IDENTIFIED BY PASSWORD <secret> |

| GRANT ALL PRIVILEGES ON `ceshi`.* TO 'zhang'@'%'                  |

+-------------------------------------------------------------------+

2 rows in set (0.01 sec)

MySQL [(none)]> use test;

Database changed

MySQL [test]> show tables;

Empty set (0.02 sec)

5.3.3、授權某一個許可權給某一個帳號,只針對有一個資料庫進行操作

建立帳號zhang並運行所有ip地址串連並建立密碼zhang,設定許可權為只對ceshi資料庫進行select查詢

MySQL [(none)]> grant select  on ceshi.* to 'zhang'@'%' identified by 'zhang';

Query OK, 0 rows affected (0.02 sec)

MySQL [(none)]>

MySQL [(none)]> show grants;

+-------------------------------------------------------------------+

| Grants for [email protected]%                                                |

+-------------------------------------------------------------------+

| GRANT USAGE ON *.* TO 'zhang'@'%' IDENTIFIED BY PASSWORD <secret> |

| GRANT SELECT ON `ceshi`.* TO 'zhang'@'%'                          |

+-------------------------------------------------------------------+

2 rows in set (0.02 sec)

MySQL [(none)]>

使用create 建立表進行測試,是否有許可權建立,如下顯示則沒有建立成功,表示沒有許可權

MySQL [ceshi]> create table t1;

ERROR 1142 (42000): CREATE command denied to user 'zhang'@'120.76.32.14' for table 't1'

MySQL [ceshi]>

在zhang帳號中添加create建立許可權

MySQL [(none)]> grant create  on ceshi.* to 'zhang'@'%' identified by 'zhang';

Query OK, 0 rows affected (0.02 sec)

查看此帳號許可權

MySQL [(none)]> show grants for 'zhang'@'%';

+------------------------------------------------------------------------------------------------------+

| Grants for [email protected]%                                                                                   |

+------------------------------------------------------------------------------------------------------+

| GRANT USAGE ON *.* TO 'zhang'@'%' IDENTIFIED BY PASSWORD '*5D83A6402DF44A7D8EC2B8861B19F8A2F4F3EA2F' |

| GRANT SELECT, CREATE ON `ceshi`.* TO 'zhang'@'%'                                                     |

+------------------------------------------------------------------------------------------------------+

2 rows in set (0.01 sec)

MySQL [(none)]>

5.3.4、授權某一列

MySQL [ceshi]> grant select(table_name,engine) on test.t to 'zhang'@'localhost';

六、撤銷許可權

格式命令:revoke 許可權 on 庫.表 from 'user'@'host';

查看zhang使用者目前的許可權列表

MySQL [ceshi]> show grants for  'zhang'@'%';

+------------------------------------------------------------------------------------------------------+

| Grants for [email protected]%                                                                                   |

+------------------------------------------------------------------------------------------------------+

| GRANT USAGE ON *.* TO 'zhang'@'%' IDENTIFIED BY PASSWORD '*5D83A6402DF44A7D8EC2B8861B19F8A2F4F3EA2F' |

| GRANT SELECT, CREATE ON `ceshi`.* TO 'zhang'@'%'                                                     |

+------------------------------------------------------------------------------------------------------+

2 rows in set (0.02 sec)

MySQL [ceshi]>

把zhang使用者的create的許可權刪掉,使其不能使用create建立

MySQL [ceshi]> revoke create on ceshi.* from 'zhang'@'%';

Query OK, 0 rows affected (0.02 sec)

MySQL [ceshi]> show grants for  'zhang'@'%';

+------------------------------------------------------------------------------------------------------+

| Grants for [email protected]%                                                                                   |

+------------------------------------------------------------------------------------------------------+

| GRANT USAGE ON *.* TO 'zhang'@'%' IDENTIFIED BY PASSWORD '*5D83A6402DF44A7D8EC2B8861B19F8A2F4F3EA2F' |

| GRANT SELECT ON `ceshi`.* TO 'zhang'@'%'                                                             |

+------------------------------------------------------------------------------------------------------+

2 rows in set (0.01 sec)

MySQL [ceshi]>

七、修改帳號的密碼

格式命令:set password for 'user'@'host' = password('NEW-password');

MySQL [ceshi]> set password for 'zhang'@'%' = password('boss');

Query OK, 0 rows affected (0.02 sec)

八、如何對一個帳號進行資源限制

資源可以包括為:

resource_option: {

| MAX_QUERIES_PER_HOUR count

| MAX_UPDATES_PER_HOUR count

| MAX_CONNECTIONS_PER_HOUR count

| MAX_USER_CONNECTIONS count

每一個小時的連結次數

每一個帳號每一個小時的查詢多少次

每一個帳號每一個小時更新多少次

每一個帳號每一個小時並發連結多少次

8.1、每一個小時不能超過2次查詢

MySQL [ceshi]> grant all privileges on *.* to 'boss'@'%' with  MAX_QUERIES_PER_HOUR 2;

Query OK, 0 rows affected (0.02 sec)

MySQL [ceshi]>

九、找回密碼

[[email protected] ~]# /usr/local/mysql/bin/mysqld_safe --skip-grant-tables &

[[email protected] ~]#mysql

清空root密碼

MySQL [ceshi]> update user set password='' where user='root' and host='localhost'


mysql使用者的管理

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.