默認情況下,MySQL.db表中包含的行表示任意用戶可以訪問test數(shù)據(jù)庫和test_開頭的數(shù)據(jù)庫。這些行的User字段的值為空,表示匹配任意用戶。這意味著這些數(shù)據(jù)庫(test數(shù)據(jù)庫和test_開頭的數(shù)據(jù)庫)默認可以被任意用戶使用(即使沒有權(quán)限的用戶)。
成都創(chuàng)新互聯(lián)公司2013年至今,先為劍閣等服務(wù)建站,劍閣等地企業(yè),進行企業(yè)商務(wù)咨詢服務(wù)。為劍閣企業(yè)網(wǎng)站制作PC+手機+微官網(wǎng)三網(wǎng)同步一站式服務(wù)解決您的所有建站問題。
mysql> select * from mysql.db\G
*************************** 1. row ***************************
Host: %
Db: test
User:
Select_priv: Y
Insert_priv: Y
Update_priv: Y
Delete_priv: Y
Create_priv: Y
Drop_priv: Y
Grant_priv: N
References_priv: Y
Index_priv: Y
Alter_priv: Y
Create_tmp_table_priv: Y
Lock_tables_priv: Y
Create_view_priv: Y
Show_view_priv: Y
Create_routine_priv: Y
Alter_routine_priv: N
Execute_priv: N
Event_priv: Y
Trigger_priv: Y
*************************** 2. row ***************************
Host: %
Db: test\_%
User:
Select_priv: Y
Insert_priv: Y
Update_priv: Y
Delete_priv: Y
Create_priv: Y
Drop_priv: Y
Grant_priv: N
References_priv: Y
Index_priv: Y
Alter_priv: Y
Create_tmp_table_priv: Y
Lock_tables_priv: Y
Create_view_priv: Y
Show_view_priv: Y
Create_routine_priv: Y
Alter_routine_priv: N
Execute_priv: N
Event_priv: Y
Trigger_priv: Y
2 rows in set (0.00 sec)
可以看到,任意用戶對test數(shù)據(jù)庫和test_開頭的數(shù)據(jù)庫都擁有很大的權(quán)限(上述為Y的權(quán)限)
#創(chuàng)建一個只讀賬號
mysql> grant select on yujx.t to 'select'@'localhost' identified by 'select';
Query OK, 0 rows affected (0.00 sec)
mysql> flush privileges;
Query OK, 0 rows affected (0.00 sec)
#使用只讀用戶連接mysql
mysql> select user();
+------------------+
| user() |
+------------------+
| select@localhost |
+------------------+
1 row in set (0.00 sec)
mysql> show grants for 'select'@'localhost';
+---------------------------------------------------------------------------------------------------------
| Grants for select@localhost |
+---------------------------------------------------------------------------------------------------------
| GRANT USAGE ON *.* TO 'select'@'localhost' IDENTIFIED BY PASSWORD '*852200EDF18814F8BFC1F1DC816AAC4152D8262E'
| GRANT SELECT ON `yujx`.`t` TO 'select'@'localhost' |
+-------------------------------------------------------------------------------------------------
2 rows in set (0.00 sec)
mysql> show databases;
+--------------------+
| Database |
+--------------------+
| information_schema |
| test |
| test_a |
| yujx |
+--------------------+
4 rows in set (0.00 sec)
mysql> use test;
Database changed
#可以創(chuàng)建表
mysql> create table t(x int);
Query OK, 0 rows affected (0.01 sec)
#可以insert表
mysql> insert into t select 1;
Query OK, 1 row affected (0.00 sec)
Records: 1 Duplicates: 0 Warnings: 0
#可以drop database
mysql> drop database test;
Query OK, 1 row affected (0.01 sec)
mysql> show databases;
+--------------------+
| Database |
+--------------------+
| information_schema |
| test_a |
| yujx |
+--------------------+
3 rows in set (0.00 sec)
mysql> use test_a
Database changed
mysql> create table a ( x int);
Query OK, 0 rows affected (0.01 sec)
mysql> show tables;
+------------------+
| Tables_in_test_a |
+------------------+
| a |
+------------------+
1 row in set (0.00 sec)
mysql> drop table a;
Query OK, 0 rows affected (0.01 sec)
mysql> drop database test_a;
Query OK, 0 rows affected (0.00 sec)
#只要是dbname以test開頭的都能創(chuàng)建成功
mysql> create database test;
Query OK, 1 row affected (0.00 sec)
mysql> create database test_a;
Query OK, 1 row affected (0.00 sec)
mysql> create database test_b;
Query OK, 1 row affected (0.00 sec)
mysql> create database a;
ERROR 1044 (42000): Access denied for user 'select'@'localhost' to database 'a'
如果你不想讓擁有任意權(quán)限(哪怕僅僅只讀權(quán)限)的用戶能任意操作test數(shù)據(jù)庫或者以test_開頭命名的數(shù)據(jù)庫,可以delete其mysql.db表中test相關(guān)的行,如下:
shell>mysql -u root -p
Enter password:(enter root password here)
mysql>DELETE FROM mysql.db WHERE Db LIKE 'test%';
mysql>FLUSH PRIVILEGES;
#如下,已經(jīng)無法任意操作test相關(guān)數(shù)據(jù)庫
mysql> select user();
+------------------+
| user() |
+------------------+
| select@localhost |
+------------------+
mysql> show databases;
+--------------------+
| Database |
+--------------------+
| information_schema |
| yujx |
+--------------------+
2 rows in set (0.00 sec)
mysql> create database test;
ERROR 1044 (42000): Access denied for user 'select'@'localhost' to database 'test'
mysql> create database test_a;
ERROR 1044 (42000): Access denied for user 'select'@'localhost' to database 'test_a'
至此,可以看到默認情況下,初始化的mysql環(huán)境中mysql.db表默認包含的2行test數(shù)據(jù)庫相關(guān)的配置,導(dǎo)致任意用戶可以隨意操作test或者test_開頭的數(shù)據(jù)庫,如果你想避免此問題,可以直接drop test數(shù)據(jù)庫。
關(guān)于此現(xiàn)象,大家可能需要注意的問題:
1、正式環(huán)境千萬別使用test數(shù)據(jù)庫或者創(chuàng)建test_開頭的數(shù)據(jù)庫來存儲業(yè)務(wù)數(shù)據(jù)
2、對用戶的權(quán)限進行測試、驗證的時候,千萬別去test數(shù)據(jù)庫,這可能誤導(dǎo)你
3、如果想徹底避免以上問題,可以將mysql.db中test相關(guān)的數(shù)據(jù)delete掉,參考上文
參考鏈接:
https://dev.mysql.com/doc/refman/5.6/en/default-privileges.html
分享名稱:MySQL之test數(shù)據(jù)庫默認權(quán)限
當(dāng)前地址:http://www.chinadenli.net/article30/jcogso.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供建站公司、、網(wǎng)站設(shè)計公司、關(guān)鍵詞優(yōu)化、微信小程序、電子商務(wù)
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請盡快告知,我們將會在第一時間刪除。文章觀點不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時需注明來源: 創(chuàng)新互聯(lián)