首页 / 数据库 / MySQL / MySQL之test数据库默认权限
默认情况下,mysql.db表中包含的行表示任意用户可以访问test数据库和test_开头的数据库。这些行的User字段的值为空,表示匹配任意用户。这意味着这些数据库(test数据库和test_开头的数据库)默认可以被任意用户使用(即使没有权限的用户)。表mysql.db的默认数据如下mysql> select * from mysql.dbG*************************** 1. row ***************************Host: %Db: testUser: Select_priv: YInsert_priv: YUpdate_priv: YDelete_priv: YCreate_priv: YDrop_priv: YGrant_priv: NReferences_priv: YIndex_priv: YAlter_priv: YCreate_tmp_table_priv: YLock_tables_priv: YCreate_view_priv: YShow_view_priv: YCreate_routine_priv: YAlter_routine_priv: NExecute_priv: NEvent_priv: YTrigger_priv: Y*************************** 2. row ***************************Host: %Db: test\_%User: Select_priv: YInsert_priv: YUpdate_priv: YDelete_priv: YCreate_priv: YDrop_priv: YGrant_priv: NReferences_priv: YIndex_priv: YAlter_priv: YCreate_tmp_table_priv: YLock_tables_priv: YCreate_view_priv: YShow_view_priv: YCreate_routine_priv: YAlter_routine_priv: NExecute_priv: NEvent_priv: YTrigger_priv: Y2 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)#使用只读用户连接mysqlmysql> 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)#操作test库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 databasemysql> 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)#同样适用于test_开头的库mysql> use test_aDatabase changedmysql> 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"#delete from mysql.db where db like "test%"如果你不想让拥有任意权限(哪怕仅仅只读权限)的用户能任意操作test数据库或者以test_开头命名的数据库,可以delete其mysql.db表中test相关的行,如下:
shell> mysql -u root -pEnter 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本文永久更新链接地址