在Mysql 中,权限设置非常重要,分配权限可以清晰的划分责任,管理人员只需要关注自己的任务即可,最重要的是保证系统数据安全。
1.授予权限
(1)权限控制主要是出于安全因素,因此需要遵循以下几个原则:
a. 只授予能满足需要的最小权限,为了防止用户误操作和干坏事。比如用户只需要查询,只需赋予 serlect 权限就可以了,不用给用户 uodate 、insert 、delete 权限。
b. 创建用户的时候限制用户的登录主机,一般是限制成指定IP 或者内网 IP.
c. 初始化数据库时删除没有密码的用户。安装完数据库时会自动创建一些用户,这些用户没有密码。
e. 为每个用户设置满足密码复杂度的密码。
f. 定期清理不需要的用户。收回权限或者删除用户。
(2)授予权限使用 GRANT 命令,命令格式如下:GRANT 权限列表 ON 库名.表名 TO 用户名@主机地址 [IDENTIFIED BY ‘密码‘].
命令格式很明确,是指定用户允许它操作某些表,对于这些表拥有相应的操作权限。[root@bogon ~]# mysql -uroot -p
Enter password:
Welcome to the MySQL monitor. Commands end with ; or \g.mysql> grant select on school.info to'user03'@'localhost'identified by '123abc';
Query OK, 0 rows affected, 1 warning (0.00 sec)
使用户 user03 可以在主机 localhost 连接,密码是 123abc ,它拥有对数据库表 school.info 的 select 权限mysql> quit
Bye
[root@bogon ~]# mysql -u user03 –p //使用user03 登录,进行验证
Enter password:
Welcome to the MySQL monitor. Commands end with ; or \g.mysql> select * from school.info; //select 语句可以正常执行
+----+----------+-------+-------+
| id | name | score | hobby |
+----+----------+-------+-------+
| 1 | zhangsan | 30.00 | 1 |
| 2 | lisi | 74.00 | 2 |
| 3 | wangwu | 86.00 | 3 |
| 4 | zhaoliu | 95.00 | 4 |
+----+----------+-------+-------+
4 rows in set (0.00 sec)
mysql> insert into school.info (id,name,score,hobby) values (6,'lili',78,3);
ERROR 1142 (42000): INSERT command denied to user 'user03'@'localhost' for table 'info' //执行 insert 语句没有足够权限
mysql>
使用GRANT 时有些问题需要注意:
(1)当用户名和主机名在数据库中不存在时,用户和主机名被创建,也就是 user 表中多了一个用户数据,和使用创建新用户命令效果相同,登录密码是后面指定的密码。
(2)当用户名和主机名在数据库中已经存在,后面设置的新密码可以覆盖旧密码,相当于修改密码的功能。[root@localhost ~]# mysql -u root -p
Enter password:
Welcome to the MySQL monitor. Commands end with ; or \g.
mysql> use mysql;
Reading table information for completion of table and column names
You can turn off this feature to get a quicker startup with -A
Database changed
mysql> select User,authentication_string,Host from user;
+-----------+-------------------------------------------+-----------+
| User | authentication_string | Host |
+-----------+-------------------------------------------+-----------+
| root | *3620754A963ECB3D7296097F9DA00C1FA5476B03 | localhost |
| mysql.sys | *3620754A963ECB3D7296097F9DA00C1FA5476B03 | localhost |
| root | *3620754A963ECB3D7296097F9DA00C1FA5476B03 | % |
| user01 | *3620754A963ECB3D7296097F9DA00C1FA5476B03 | localhost |
| user03 | *3620754A963ECB3D7296097F9DA00C1FA5476B03 | localhost |
| user02 | *3620754A963ECB3D7296097F9DA00C1FA5476B03 | localhost |
+-----------+-------------------------------------------+-----------+
6 rows in set (0.00 sec)
2.查看权限
查看用户拥有的权限可以使用 SHOW GRANTS 命令。命令格式如下:SHOW GRANTS FOR ‘username’ @ ‘localhost’;
查看用户user03 的权限。mysql> show grants for'user03'@'localhost';
+---------------------------------------------------------+
| Grants for user03@localhost |
+---------------------------------------------------------+
| GRANT USAGE ON *.* TO 'user03'@'localhost' |
| GRANT SELECT ON "school"."info" TO 'user03'@'localhost' |
+---------------------------------------------------------+
2 rows in set (0.01 sec)
显示user03 对表school.info 拥有 select 权限,与之前设置相同。
3.撤销权限使用REVOKE 语句可以撤销指定用户的数据库权限。命令格式如下:REVOKE 权限列表 ON 数据库名.表名 FROM 用户@主机名。mysql> revoke select on school.info from 'user03'@'localhost';
Query OK, 0 rows affected (0.01 sec)
mysql> show grants for 'user03'@'localhost';
+--------------------------------------------+
| Grants for user03@localhost |
+--------------------------------------------+
| GRANT USAGE ON *.* TO 'user03'@'localhost' |
+--------------------------------------------+
1 row in set (0.00 sec)
mysql> show grants for 'user03'@'localhost';
+---------------------------------------------------------+
| Grants for user03@localhost |
+---------------------------------------------------------+
| GRANT USAGE ON *.* TO 'user03'@'localhost' |
| GRANT SELECT ON "school"."info" TO 'user03'@'localhost' |
+---------------------------------------------------------+
2 rows in set (0.00 sec)
4.权限列表说明ALL 设置GRANT OPTION 之外的所有权限
ALTER 允许使用 ALTER TABLE
CREATE 允许使用 CREATE TABLE
CREATE USER 允许使用 CREATE USER
DELETE 允许使用 TELETE
INDEX 允许使用INDEX
INSERT 允许使用INSERT
SELECT 允许使用SELECT
UPDATE 允许使用IPDATE
DROP 允许使用DROP TABLE
REPLICATION SLAVE 允许从主服务器中读取二进制文件
SHOW ADTABASES 允许显示所有数据