Mysql 授权控制
??????? 在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????????????????????????? 允許顯示所有數據
轉載于:https://blog.51cto.com/13706703/2165630
總結
以上是生活随笔為你收集整理的Mysql 授权控制的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 【worker】js中的多线程
- 下一篇: CentOS下MySQL的彻底卸载