在Mysql 中,权限设置很是重要,分配权限能够清晰的划分责任,管理人员只须要关注本身的任务便可,最重要的是保证系统数据安全。mysql
1.授予权限sql
(1)权限控制主要是出于安全因素,所以须要遵循如下几个原则:数据库
a. 只授予能知足须要的最小权限,为了防止用户误操做和干坏事。好比用户只须要查询,只需赋予 serlect 权限就能够了,不用给用户 uodate 、insert 、delete 权限。安全
b. 建立用户的时候限制用户的登陆主机,通常是限制成指定IP 或者内网 IP.服务器
c. 初始化数据库时删除没有密码的用户。安装完数据库时会自动建立一些用户,这些用户没有密码。ide
e. 为每一个用户设置知足密码复杂度的密码。ui
f. 按期清理不须要的用户。收回权限或者删除用户。this
(2)授予权限使用 GRANT 命令,命令格式以下:spa
GRANT 权限列表 ON 库名.表名 TO 用户名@主机地址 [IDENTIFIED BY ‘密码‘].orm
命令格式很明确,是指定用户容许它操做某些表,对于这些表拥有相应的操做权限。
[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 -ADatabase 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 容许显示全部数据