时间:2021-07-01 10:21:17 帮助过:2人阅读
| 表名 | user | db | host |
| 用户列 | User | Host | Host |
| Password | Db | Db | |
| 权限列 | Select_priv | User | Select_priv |
| Insert_priv | Select_priv | Insert_priv | |
| Update_priv | Insert_priv | Update_priv | |
| Delete_priv | Update_priv | Delete_priv | |
| Create_priv | Delete_priv | Create_priv | |
| Drop_priv | Create_priv | Drop_priv | |
| Reload_priv | Drop_priv | Grant_priv | |
| Shutdown_priv | Grant_priv | References_priv | |
| Process_priv | References_priv | Index_priv | |
| File_priv | Index_priv | Alter_priv | |
| Grant_priv | Alter_priv | Create_tmp_table_priv | |
| References_priv | Create_tmp_table_priv | Lock_tables_priv | |
| Index_priv | Lock_tables_priv | Create_view_priv | |
| Alter_priv | Create_view_priv | Show_view_priv | |
| Show_db_priv | Show_view_priv | Create_routine_priv | |
| Super_priv | Create_routine_priv | Alter_routine_priv | |
| Create_tmp_table_priv | Alter_routine_priv | Execute_priv | |
| Lock_tables_priv | Execute_priv | Trigger_priv | |
| Execute_priv | Event_priv | ||
| Repl_slave_priv | Trigger_priv | ||
| Repl_client_priv | |||
| Create_view_priv | |||
| Show_view_priv | |||
| Create_routine_priv | |||
| Alter_routine_priv | |||
| Create_user_priv | |||
| Event_priv | |||
| Trigger_priv | |||
| Create_tablespace_priv | |||
| 安全列 | ssl_type | ||
| ssl_cipher | |||
| x509_issuer | |||
| x509_subject | |||
| max_questions | |||
| max_updates | |||
| max_connections | |||
| max_user_connections |
mysql> grant select on *.* to cqh@localhost;
Query OK, 0 rows affected (0.05 sec)
mysql> select * from user where user=‘cqh‘ and host=‘localhost‘ \G
*************************** 1. row ***************************
Host: localhost
User: cqh
Password:
Select_priv: Y
Insert_priv: N
Update_priv: N
Delete_priv: N
Create_priv: N
Drop_priv: N
...
(2)再来看db表:
mysql> select * from db where user=‘cqh‘; Empty set (0.00 sec)可以发现,user表的select_priv列是“Y”,而db表中并没有记录,也就是说,对所有数据库都具有相同的权限的用户记录并不需要记入db表,而仅仅需要将user表中的select_priv改为“Y”即可。换句话,user表中的每个权限都代表了对所有数据库都有的权限。 (3)将cqh@localhost上的权限改为只对test数据库上所有表的select权限。
mysql> revoke select on *.* from cqh@localhost;
Query OK, 0 rows affected (0.00 sec)
mysql> grant select on test.* to cqh@localhost;
Query OK, 0 rows affected (0.00 sec)
mysql> select * from user where user=‘cqh‘ and host=‘localhost‘ \G
*************************** 1. row ***************************
Host: localhost
User: cqh
Password:
Select_priv: N
Insert_priv: N
Update_priv: N
Delete_priv: N
Create_priv: N
Drop_priv: N
Reload_priv: N
Shutdown_priv: N
Process_priv: N
File_priv: N
Grant_priv: N
References_priv: N
Index_priv: N
Alter_priv: N
Show_db_priv: N
Super_priv: N
Create_tmp_table_priv: N
Lock_tables_priv: N
Execute_priv: N
Repl_slave_priv: N
Repl_client_priv: N
Create_view_priv: N
Show_view_priv: N
Create_routine_priv: N
Alter_routine_priv: N
Create_user_priv: N
Event_priv: N
Trigger_priv: N
Create_tablespace_priv: N
ssl_type:
ssl_cipher:
x509_issuer:
x509_subject:
max_questions: 0
max_updates: 0
max_connections: 0
max_user_connections: 0
plugin:
authentication_string: NULL
1 row in set (0.00 sec)
mysql> select * from db where user=‘cqh‘\G
*************************** 1. row ***************************
Host: localhost
Db: test
User: cqh
Select_priv: Y
Insert_priv: N
Update_priv: N
Delete_priv: N
Create_priv: N
Drop_priv: N
Grant_priv: N
References_priv: N
Index_priv: N
Alter_priv: N
Create_tmp_table_priv: N
Lock_tables_priv: N
Create_view_priv: N
Show_view_priv: N
Create_routine_priv: N
Alter_routine_priv: N
Execute_priv: N
Event_priv: N
Trigger_priv: N
1 row in set (0.00 sec)
这个时候发现,user表中的select_priv变为“N”,而db表中则增加了db为test的一条记录,也就是说,当只授予数据库某些权限时,user表中的相应权限时,user表中的相应权限列保持“N”,而将具体的数据库权限写入db表。
table和column的权限机制和db类似,这里就不再赘述了。GRANT
priv_type [(column_list)]
[, priv_type [(column_list)]] ...
ON [object_type] priv_level
TO user_specification [, user_specification] ...
[REQUIRE {NONE | ssl_option [[AND] ssl_option] ...}]
[WITH with_option ...]
GRANT PROXY ON user_specification
TO user_specification [, user_specification] ...
[WITH GRANT OPTION]
object_type:
TABLE
| FUNCTION
| PROCEDURE
来看下面的几个例子。
例1:创建用户cqh,权限为可以在所有数据库上执行所有权限,只能从本地进行连接。mysql> grant all privileges on *.* to cqh@localhost;
Query OK, 0 rows affected (0.00 sec)
mysql> select * from user where user=‘cqh‘ and host=‘localhost‘ \G
*************************** 1. row ***************************
Host: localhost
User: cqh
Password:
Select_priv: Y
Insert_priv: Y
Update_priv: Y
Delete_priv: Y
Create_priv: Y
Drop_priv: Y
Reload_priv: Y
Shutdown_priv: Y
Process_priv: Y
File_priv: Y
Grant_priv: N
References_priv: Y
Index_priv: Y
Alter_priv: Y
Show_db_priv: Y
Super_priv: Y
Create_tmp_table_priv: Y
Lock_tables_priv: Y
Execute_priv: Y
Repl_slave_priv: Y
Repl_client_priv: Y
Create_view_priv: Y
Show_view_priv: Y
Create_routine_priv: Y
Alter_routine_priv: Y
Create_user_priv: Y
Event_priv: Y
Trigger_priv: Y
Create_tablespace_priv: Y
ssl_type:
ssl_cipher:
x509_issuer:
x509_subject:
max_questions: 0
max_updates: 0
max_connections: 0
max_user_connections: 0
plugin:
authentication_string: NULL
1 row in set (0.00 sec)
可以发现,除了Grant_priv权限外,所有权限在user表里都是“Y”。
例2:在例1基础上,增加对cqh的grant权限mysql> grant all privileges on *.* to cqh@localhost with grant option;
Query OK, 0 rows affected (0.00 sec)
mysql> select * from user where user=‘cqh‘ and host=‘localhost‘ \G
*************************** 1. row ***************************
Host: localhost
User: cqh
Password:
Select_priv: Y
Insert_priv: Y
Update_priv: Y
Delete_priv: Y
Create_priv: Y
Drop_priv: Y
Reload_priv: Y
Shutdown_priv: Y
Process_priv: Y
File_priv: Y
Grant_priv: Y
References_priv: Y
Index_priv: Y
Alter_priv: Y
Show_db_priv: Y
Super_priv: Y
Create_tmp_table_priv: Y
Lock_tables_priv: Y
Execute_priv: Y
Repl_slave_priv: Y
Repl_client_priv: Y
Create_view_priv: Y
Show_view_priv: Y
Create_routine_priv: Y
Alter_routine_priv: Y
Create_user_priv: Y
Event_priv: Y
Trigger_priv: Y
Create_tablespace_priv: Y
ssl_type:
ssl_cipher:
x509_issuer:
x509_subject:
max_questions: 0
max_updates: 0
max_connections: 0
max_user_connections: 0
plugin:
authentication_string: NULL
1 row in set (0.00 sec)
例3:在例2基础上,设置密码为“123”。
mysql> grant all privileges on *.* to cqh@localhost identified by ‘123‘ with grant option; Query OK, 0 rows affected (0.00 sec)从user表中查看修改的密码:
mysql> select * from user where user=‘cqh‘ and host=‘localhost‘ \G
*************************** 1. row ***************************
Host: localhost
User: cqh
Password: *23AE809DDACAF96AF0FD78ED04B6A265E05AA257
Select_priv: Y
Insert_priv: Y
Update_priv: Y
Delete_priv: Y
Create_priv: Y
Drop_priv: Y
Reload_priv: Y
Shutdown_priv: Y
Process_priv: Y
File_priv: Y
Grant_priv: Y
References_priv: Y
Index_priv: Y
Alter_priv: Y
Show_db_priv: Y
Super_priv: Y
Create_tmp_table_priv: Y
Lock_tables_priv: Y
Execute_priv: Y
Repl_slave_priv: Y
Repl_client_priv: Y
Create_view_priv: Y
Show_view_priv: Y
Create_routine_priv: Y
Alter_routine_priv: Y
Create_user_priv: Y
Event_priv: Y
Trigger_priv: Y
Create_tablespace_priv: Y
ssl_type:
ssl_cipher:
x509_issuer:
x509_subject:
max_questions: 0
max_updates: 0
max_connections: 0
max_user_connections: 0
plugin:
authentication_string: NULL
1 row in set (0.00 sec)
可以发现,密码变成了一堆加密后的字符串。在MySQL5.0里面,密码的算法是生成一个以*开始的41位的字符串,而MySQL4.0之前是16位,因此安全性大大提高。
例4:创建新用户chenqionghe,可以从任何IP进行连接,权限为test数据库里的所有表进行SELECT、UPDATE、INSERT和DELETE操作,初始密码为“123”。mysql> grant select,insert,update,delete on test.* to ‘chenqionghe‘@‘%‘ identified by ‘123‘;
Query OK, 0 rows affected (0.00 sec)
mysql> select * from user where user=‘chenqionghe‘ and host=‘%‘ \G
*************************** 1. row ***************************
Host: %
User: chenqionghe
Password: *23AE809DDACAF96AF0FD78ED04B6A265E05AA257
Select_priv: N
Insert_priv: N
Update_priv: N
Delete_priv: N
Create_priv: N
Drop_priv: N
Reload_priv: N
Shutdown_priv: N
Process_priv: N
File_priv: N
Grant_priv: N
References_priv: N
Index_priv: N
Alter_priv: N
Show_db_priv: N
Super_priv: N
Create_tmp_table_priv: N
Lock_tables_priv: N
Execute_priv: N
Repl_slave_priv: N
Repl_client_priv: N
Create_view_priv: N
Show_view_priv: N
Create_routine_priv: N
Alter_routine_priv: N
Create_user_priv: N
Event_priv: N
Trigger_priv: N
Create_tablespace_priv: N
ssl_type:
ssl_cipher:
x509_issuer:
x509_subject:
max_questions: 0
max_updates: 0
max_connections: 0
max_user_connections: 0
plugin:
authentication_string: NULL
1 row in set (0.00 sec)
mysql> select * from db where user=‘chenqionghe‘ and host=‘%‘ \G
*************************** 1. row ***************************
Host: %
Db: test
User: chenqionghe
Select_priv: Y
Insert_priv: Y
Update_priv: Y
Delete_priv: Y
Create_priv: N
Drop_priv: N
Grant_priv: N
References_priv: N
Index_priv: N
Alter_priv: N
Create_tmp_table_priv: N
Lock_tables_priv: N
Create_view_priv: N
Show_view_priv: N
Create_routine_priv: N
Alter_routine_priv: N
Execute_priv: N
Event_priv: N
Trigger_priv: N
1 row in set (0.00 sec)
如上文所述,user表中的权限都是“N”,db表中增加的记录权限则都是“Y”。一般地,我们只授予用户适当的权限,而一般不会授予过多的权限,本例中的权限适合大多数应用账号。
本例中的IP限制为所有IP都可以连接,因此设置为“*”,mysql数据库中是通过user表的host字段来进行控制,host可以是以下类型的值。
| Host值 | User值 | 被条目匹配的连接 |
| cqh.loc.gov | cqh | cqh,从cqh.loc.gov连接 |
| cqh.loc.gov | 任何用户,从cqh.loc.gov连接 | |
| % | cqh | cqh,从任何主机连接 |
| % | 任何用户,从任何主机连接 | |
| %.loc.gov | cqh | cqh,从在loc.gov域的任何主机连接 |
| x.y.% | cqh | cqh,从x.y.net、x.y.com、x.y.edu等连接 |
| 114.115.166.177 | cqh | cqh,从有114.115.166.177IP地址的主机连接 |
| 114.115.166.% | cqh | cqh,从144.155.166C类子网的任何主机连接 |
mysql> grant super,process,file on *.* to ‘cqh2‘@‘%‘; Query OK, 0 rows affected (0.00 sec)因为这几个权限都属于管理权限,因此不能够指定某个数据库,on后面必须跟“*.*”,下面的语法将提示错误:
mysql> grant super,process,file on test.* to ‘cqh2‘@‘%‘; ERROR 1221 (HY000): Incorrect usage of DB GRANT and GLOBAL PRIVILEGES例6:只授予登录权限给cqh3@localhost
mysql> grant usage on *.* to ‘cqh3‘@‘localhost‘; Query OK, 0 rows affected (0.00 sec) mysql> exit Bye [root@iZ28dr6w0qvZ ~]# mysql -ucqh3 Welcome to the MySQL monitor. Commands end with ; or \g. Your MySQL connection id is 1640 Server version: 5.5.37-log MySQL Community Server (GPL) Copyright (c) 2000, 2014, Oracle and/or its affiliates. All rights reserved. Oracle is a registered trademark of Oracle Corporation and/or its affiliates. Other names may be trademarks of their respective owners. Type ‘help;‘ or ‘\h‘ for help. Type ‘\c‘ to clear the current input statement. mysql> show databases; +--------------------+ | Database | +--------------------+ | information_schema | +--------------------+ 1 row in set (0.00 sec)usage权限只能用于数据库登录,不能执行任何操作。 直接操作权限表也可以进行权限的创建,其实GRANT操作的本质就是修改权限后进行权限的刷新,因此,GRANT比操作权限表更简单,下面继续以上文的例子来说明一下更新权限的用法。
mysql> grant select,insert,update,delete on test.* to ‘chenqionghe‘@‘%‘ identified by ‘123‘;
[root@iZ28dr6w0qvZ ~]# mysql -uroot -p Enter password: Welcome to the MySQL monitor. Commands end with ; or \g. Your MySQL connection id is 1560 Server version: 5.5.37-log MySQL Community Server (GPL) Copyright (c) 2000, 2014, Oracle and/or its affiliates. All rights reserved. Oracle is a registered trademark of Oracle Corporation and/or its affiliates. Other names may be trademarks of their respective owners. Type ‘help;‘ or ‘\h‘ for help. Type ‘\c‘ to clear the current input statement. mysql> use mysql; Database changed mysql> insert into db (host,db,user,select_priv,insert_priv,update_priv,delete_priv) values (‘%‘,‘test‘,‘chenqionghe‘,‘Y‘,‘Y‘,‘Y‘,‘Y‘); Query OK, 1 row affected (0.00 sec) mysql> flush privileges; mysql> exit; Bye [root@iZ28dr6w0qvZ ~]# mysql -ucqh3 ERROR 1045 (28000): Access denied for user ‘cqh3‘@‘localhost‘ (using password: NO) [root@iZ28dr6w0qvZ ~]# mysql -ucqh3 -p Enter password: Welcome to the MySQL monitor. Commands end with ; or \g. Your MySQL connection id is 1643 Server version: 5.5.37-log MySQL Community Server (GPL) Copyright (c) 2000, 2014, Oracle and/or its affiliates. All rights reserved. Oracle is a registered trademark of Oracle Corporation and/or its affiliates. Other names may be trademarks of their respective owners. Type ‘help;‘ or ‘\h‘ for help. Type ‘\c‘ to clear the current input statement. mysql> show databases; +--------------------+ | Database | +--------------------+ | information_schema | | test | +--------------------+ 2 rows in set (0.00 sec)
show grants for user@host;如以下示例
mysql> show grants for cqh@localhost; +---------------------------------------------------------------------------------------------------------------------------------------+ | Grants for cqh@localhost | +---------------------------------------------------------------------------------------------------------------------------------------+ | GRANT ALL PRIVILEGES ON *.* TO ‘cqh‘@‘localhost‘ IDENTIFIED BY PASSWORD ‘*23AE809DDACAF96AF0FD78ED04B6A265E05AA257‘ WITH GRANT OPTION | | GRANT SELECT ON `test`.* TO ‘cqh‘@‘localhost‘ | +---------------------------------------------------------------------------------------------------------------------------------------+ 2 rows in set (0.00 sec)host可以不写,默认是“%”,如下所示
mysql> show grants for chenqionghe; +------------------------------------------------------------------------------------------------------------+ | Grants for chenqionghe@% | +------------------------------------------------------------------------------------------------------------+ | GRANT USAGE ON *.* TO ‘chenqionghe‘@‘%‘ IDENTIFIED BY PASSWORD ‘*23AE809DDACAF96AF0FD78ED04B6A265E05AA257‘ | +------------------------------------------------------------------------------------------------------------+ 1 row in set (0.00 sec)对于MySQL5.0以后的版本,也可以利用新新增的information_schema数据库进行权限的查看;
mysql> select * from SCHEMA_PRIVILEGES where grantee="‘cqh‘@‘localhost‘"; +-------------------+---------------+--------------+----------------+--------------+ | GRANTEE | TABLE_CATALOG | TABLE_SCHEMA | PRIVILEGE_TYPE | IS_GRANTABLE | +-------------------+---------------+--------------+----------------+--------------+ | ‘cqh‘@‘localhost‘ | def | test | SELECT | NO | +-------------------+---------------+--------------+----------------+--------------+ 1 row in set (0.00 sec)
mysql> show grants for cqh3@localhost; +------------------------------------------+ | Grants for cqh3@localhost | +------------------------------------------+ | GRANT USAGE ON *.* TO ‘cqh3‘@‘localhost‘ | +------------------------------------------+ 1 row in set (0.00 sec)(2)赋予cqh3@localhost所有数据库上的所有表的SELECT权限。
mysql> grant select on *.* to ‘cqh3‘@‘localhost‘; Query OK, 0 rows affected (0.00 sec) mysql> show grants for cqh3@localhost; +-------------------------------------------+ | Grants for cqh3@localhost | +-------------------------------------------+ | GRANT SELECT ON *.* TO ‘cqh3‘@‘localhost‘ | +-------------------------------------------+ 1 row in set (0.00 sec)(3)继续给cqh3@localhost赋予SELECT和INSERT权限,和已胡的SELECT权限进行合并。
mysql> show grants for cqh3@localhost; +-------------------------------------------+ | Grants for cqh3@localhost | +-------------------------------------------+ | GRANT SELECT ON *.* TO ‘cqh3‘@‘localhost‘ | +-------------------------------------------+ 1 row in set (0.00 sec) mysql> grant select,insert on *.* to ‘cqh3‘@‘localhost‘; Query OK, 0 rows affected (0.00 sec) mysql> show grants for cqh3@localhost; +---------------------------------------------------+ | Grants for cqh3@localhost | +---------------------------------------------------+ | GRANT SELECT, INSERT ON *.* TO ‘cqh3‘@‘localhost‘ | +---------------------------------------------------+ 1 row in set (0.00 sec)(4)REVOKE语句可以回收已经赋予的权限,语法如下:
REVOKE
priv_type [(column_list)]
[, priv_type [(column_list)]] ...
ON [object_type] priv_level
FROM user [, user] ...
REVOKE ALL PRIVILEGES, GRANT OPTION
FROM user [, user] ...
REVOKE PROXY ON user
FROM user [, user] ...
对于上面的例子,这里决定要收回cqh3@localhost上的INSERT和SELECT权限:
mysql> revoke select,insert on *.* from cqh3@localhost; Query OK, 0 rows affected (0.00 sec) mysql> show grants for cqh3@localhost; +------------------------------------------+ | Grants for cqh3@localhost | +------------------------------------------+ | GRANT USAGE ON *.* TO ‘cqh3‘@‘localhost‘ | +------------------------------------------+ 1 row in set (0.00 sec)usage权限不能被回收,也就是说,REVOKE用户并不能删除用户。
mysql> show grants for cqh3@localhost; +------------------------------------------+ | Grants for cqh3@localhost | +------------------------------------------+ | GRANT USAGE ON *.* TO ‘cqh3‘@‘localhost‘ | +------------------------------------------+ 1 row in set (0.00 sec) mysql> revoke usage on *.* from cqh@localhost; Query OK, 0 rows affected (0.00 sec) mysql> show grants for cqh3@localhost; +------------------------------------------+ | Grants for cqh3@localhost | +------------------------------------------+ | GRANT USAGE ON *.* TO ‘cqh3‘@‘localhost‘ | +------------------------------------------+ 1 row in set (0.00 sec)
shell> mysqladmin -u user_name -h host_name password "newpwd"方法2:执行SET PASSWORD语句。下例中将账号‘chenqionghe‘@‘%‘的密码改为“cqh123”
SET PASSWORD FOR ‘chenqionghe‘@‘%‘ = PASSWORD(‘cqh123‘);如果是更改自己的密码,可以省略for语句:
SET PASSWORD = PASSWORD(‘cqh123‘);方法3:还可以在全局级别使用GRANT USAGE语句(在*.*)来指定某个账户的密码而不影响账户当前的权限。
GRANT USAGE ON *.* TO ‘chenqionghe‘@‘%‘ IDENTIFIED BY ‘cqh123‘;方法4:直接更改数据库的user表。
mysql> INSERT INTO user (Host,User,Password) VALUES(‘%‘,‘chenqionghe‘,PASSWORD(‘333333‘)); mysql> FLUSH PRIVILEGES; mysql> UPDATE user SET Password = PASSWORD(‘333333‘) WHERE Host=‘%‘ AND User=‘chenqionghe‘; mysql> FLUSH PRIVILEGES;注意:更改密码的时候一定要使用PASSWORD函数(mysqladmin和GRANT两种方式不用写,会自动加上)。
DROP USER user [, user] ...举一个单的例子,将cqh3@localhost用户删除
mysql> show grants for cqh3@localhost; +------------------------------------------+ | Grants for cqh3@localhost | +------------------------------------------+ | GRANT USAGE ON *.* TO ‘cqh3‘@‘localhost‘ | +------------------------------------------+ 1 row in set (0.00 sec) mysql> drop user cqh3@localhost; Query OK, 0 rows affected (0.00 sec) mysql> show grants for cqh3@localhost; ERROR 1141 (42000): There is no such grant defined for user ‘cqh3‘ on host ‘localhost‘修改权限表方法只要把user用户中的用户记录删除即可,这里不再演示 到这里,就把衣钵都传给你们了,如果觉得文章不错可以关注下,或者点个赞哦~
MySQL中的账号与权限管理
标签: