MySQL-权限及安全控制

一、简介

1.1 内容

实践MySQL的权限及安全控制的相关操作。

1.2 知识点

MySQL的权限及安全控制

二、内容

2.1****添加用户

(1) 添加两个新用户,Hans的密码为hans131,Rose的密码为rose123

1
2
3
mysql> create user
-> 'Hans'@'localhost' identified by ' hans131',
-> 'Rose'@'localhost' identified by ' rose123‘;

(2) 添加一个新用户,用户名为Pool,密码为136792,密码不指定明文。

①使用password()函数获取密码’136792’的散列值。

1
2
3
4
5
6
mysql> select password('136792');
+-------------------------------------------+
| password('136792') |
+-------------------------------------------+
| *BB3D238C77F04751017773C8CFFFB291BFF7427C |
+-------------------------------------------+

②执行create user语句创建用户Pool。

1
2
mysql> create user 'Pool'@'localhost'
-> identified by password'*BB3D238C77F04751017773C8CFFFB291BFF7427C‘;

(3) 使用grant语句创建一个新用户test11,主机名为localhost,密码为test131,并授予所有数据表的select和update权限。

1
2
mysql> grant select,update on *.* to 'test11'@'localhost'
-> identified by ‘test131’;

其中,*.*表示对用户test131设置全局权限。利用“select user from user;”语句可以验证新用户的添加是否成功。

2.2****删除普通用户

如果存在一个或是多个帐户被闲置,应当考虑将其删除,确保不会用于可能的违法的活动。

(1) 使用drop命令删除用户Pool

1
mysql>drop user Pool@localhost;

(2) 使用delete删除用户test11

1
mysql>delete from mysql.user where host='localhost'and user='test11';

2.3 修改用户名称

将用户Hans和Rose的名字分别修改为king1和king1

1
2
3
mysql> rename user
-> 'Hans'@'localhost' to 'king1'@'localhost',
-> 'Rose'@'localhost' to 'king2'@'localhost';

2.4****修改用户密码

(1) root用户修改自己的密码,将root用户的密码修改为“rootpwd”

1
2
mysql>mysqladmin –u root –p password "rootpwd"; 
Enter password: *******

(2) 将用户test11的密码修改为test12。

mysql>set password for ‘test11‘@’localhost’ =password(‘test12’);

2.5****权限管理

(1) 使用grant语句创建一个新用户grantuser,密码为grantpass。用户grantuser对所有的数据有查询、插入权限,并授予grant权限。

1
2
3
mysql> grant select,insert on *.* to 'grantuser'@'localhost'
-> identified by 'grantpass'
-> with grant option;

(2) 使用grant语句将teaching数据库中student表的delete权限授予用户grantuser。

1
2
mysql> grant delete on teaching.student 
-> to 'grantuser'@'localhost‘;

(3) 授予grantuser在student表上的studentno列和sname列的update权限。

1
2
3
mysql> grant update(studentno, sname)
-> on student
-> to grantuser@localhost;

(4) 授予用户grantuser为teaching数据库创建存储过程和存储函数权限。

1
2
mysql> grant create routine on teaching.* 
-> to grantuser@localhost;

(5) 授予grantuser用户select, insert, update, delete, create,drop权限,同时允许将其本身权限转移给其他用户

1
2
3
mysql> grant select,insert,update,delete,create,drop
-> on teaching.* to grantuser@localhost
-> with grant option;

(6) 收回grantuser用户对teaching数据库中student表的update权限。

1
2
mysql> revoke update on teaching.student 
-> from grantuser@localhost;

(7) 使用show grants语句查看grantuser用户的权限信息

1
mysql>show grants for grantuser@localhost; 

(8) 授予grantuser每小时只能处理一条select语句的权限。

1
2
3
4
mysql> grant select
-> on teaching.student
-> to grantuser@localhost
-> with max_queries_per_hour 1;

(9) 授予grantuser每小时可以发出的查询数20次,每小时可以连接数据库5次,每小时可以发出的更新数为10次。

1
2
3
4
5
mysql> grant all on *.* to grantuser@localhost
-> identified by 'grantpass'
-> with max_queries_per_hour 20
-> max_updateS_per_hour 10
-> max_connections_per_hour 5;