您好,登錄后才能下訂單哦!
默認情況下,mysql.db表中包含的行表示任意用戶可以訪問test數據庫和test_開頭的數據庫。這些行的User字段的值為空,表示匹配任意用戶。這意味著這些數據庫(test數據庫和test_開頭的數據庫)默認可以被任意用戶使用(即使沒有權限的用戶)。
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數據庫和test_開頭的數據庫都擁有很大的權限(上述為Y的權限)
#創建一個只讀賬號
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
#可以創建表
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開頭的都能創建成功
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'
如果你不想讓擁有任意權限(哪怕僅僅只讀權限)的用戶能任意操作test數據庫或者以test_開頭命名的數據庫,可以delete其mysql.db表中test相關的行,如下:
shell> mysql -u root -p
Enter password: (enter root password here)
mysql> DELETE FROM mysql.db WHERE Db LIKE 'test%';
mysql> FLUSH PRIVILEGES;
#如下,已經無法任意操作test相關數據庫
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環境中mysql.db表默認包含的2行test數據庫相關的配置,導致任意用戶可以隨意操作test或者test_開頭的數據庫,如果你想避免此問題,可以直接drop test數據庫。
關于此現象,大家可能需要注意的問題:
1、正式環境千萬別使用test數據庫或者創建test_開頭的數據庫來存儲業務數據
2、對用戶的權限進行測試、驗證的時候,千萬別去test數據庫,這可能誤導你
3、如果想徹底避免以上問題,可以將mysql.db中test相關的數據delete掉,參考上文
參考鏈接:
https://dev.mysql.com/doc/refman/5.6/en/default-privileges.html
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。