1. 程式人生 > >mysql用戶的管理

mysql用戶的管理

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進行登錄並查看數據庫

[root@iZ2zegql6fupnhn8aei0bgZ ~]# 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 root@% |

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

| 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)]>

登錄查看

[root@iZ2zegql6fupnhn8aei0bgZ ~]# 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)]>

[root@iZ2zegql6fupnhn8aei0bgZ ~]# 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 zhang@% |

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

| 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 zhang@% |

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

| 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 zhang@% |

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

| 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 zhang@% |

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

| 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 zhang@% |

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

| 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]>

九、找回密碼

[root@iZ2ze2rrr9fg73mstmet9tZ ~]# /usr/local/mysql/bin/mysqld_safe --skip-grant-tables &

[root@iZ2ze2rrr9fg73mstmet9tZ ~]#mysql

清空root密碼

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


mysql用戶的管理