MySQL之test数据库默认权限

默认情况下,MySQL.db表中包含的行表示任意用户可以访问test数据库和test_开头的数据库。这些行的User字段的值为空,表示匹配任意用户。这意味着这些数据库(test数据库和test_开头的数据库)默认可以被任意用户使用(即使没有权限的用户)。

表mysql.db的默认数据如下

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)

#操作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 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)

#同样适用于test_开头的库

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'

#delete from mysql.db where db like 'test%'

内容版权声明:除非注明,否则皆为本站原创文章。

转载注明出处:https://www.heiqu.com/50673e08998ee8ad57d1aa359c44c78d.html