*.*代表所有数据库的权限
mysql> grant all on *.* to "test"@"%";
Query OK, 0 rows affected (0.00 sec)
mysql> grant select, insert on *.* to "test"@"%";
Query OK, 0 rows affected (0.00 sec)
(2)数据库级别权限
作用于某个指定的数据库上或所有的数据库上
mysql> grant all on test.* to "test"@"%";
Query OK, 0 rows affected (0.00 sec)
mysql> grant select, insert on test.* to "test"@"%";
Query OK, 0 rows affected (0.00 sec)
(3)数据库对象级别权限
作用于指定的数据库对象上(表、视图等)或所有数据库对象上
mysql> grant select, insert on test.orders to "test"@"localhost";
Query OK, 0 rows affected (0.07 sec)
mysql> grant select(order_date), insert(order_id,customer_name) on test.orders_1 to "test"@"localhost";
Query OK, 0 rows affected (0.01 sec)
权限存储在mysql库的user、db、tables_priv、columns_priv、procs_priv这几个系统表中,待MySQL实例启动后加载到内存中。
2.查看权限
(1)查看所有用户
mysql> SELECT DISTINCT CONCAT("User: """,user,"""@""",host,""";") AS query FROM mysql.user;
+------------------------------------+
| query |
+------------------------------------+
| User: "mysql.session"@"localhost"; |
| User: "mysql.sys"@"localhost"; |
| User: "root"@"localhost"; |
+------------------------------------+
3 rows in set (0.01 sec)
(2)查看用户权限
mysql> show grants for "root"@"localhost";
+---------------------------------------------------------------------+
| Grants for root@localhost |
+---------------------------------------------------------------------+
| GRANT ALL PRIVILEGES ON *.* TO "root"@"localhost" WITH GRANT OPTION |
| GRANT PROXY ON ""@"" TO "root"@"localhost" WITH GRANT OPTION |
+---------------------------------------------------------------------+
2 rows in set (0.00 sec)
(3)对比root用户在几个权限系统表中的数据
mysql> select * from mysql.user where user="root" and host="localhost";
+-----------+------+-------------+-------------+-------------+-------------+-------------+-----------+-------------+---------------+--------------+-----------+------------+-----------------+------------+------------+--------------+------------+-----------------------+------------------+--------------+-----------------+------------------+------------------+----------------+---------------------+--------------------+------------------+------------+--------------+------------------------+----------+------------+-------------+--------------+---------------+-------------+-----------------+----------------------+-----------------------+-------------------------------------------+------------------+-----------------------+-------------------+----------------+
| Host | User | Select_priv | Insert_priv | Update_priv | Delete_priv | Create_priv | Drop_priv | Reload_priv | Shutdown_priv | Process_priv | File_priv | Grant_priv | References_priv | Index_priv | Alter_priv | Show_db_priv | Super_priv | Create_tmp_table_priv | Lock_tables_priv | Execute_priv | Repl_slave_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 | plugin | authentication_string | password_expired | password_last_changed | password_lifetime | account_locked |
+-----------+------+-------------+-------------+-------------+-------------+-------------+-----------+-------------+---------------+--------------+-----------+------------+-----------------+------------+------------+--------------+------------+-----------------------+------------------+--------------+-----------------+------------------+------------------+----------------+---------------------+--------------------+------------------+------------+--------------+------------------------+----------+------------+-------------+--------------+---------------+-------------+-----------------+----------------------+-----------------------+-------------------------------------------+------------------+-----------------------+-------------------+----------------+
| localhost | root | Y | Y | Y | Y | Y | Y | Y | Y | Y | Y | Y | Y | Y | Y | Y | Y | Y | Y | Y | Y | Y | Y | Y | Y | Y | Y | Y | Y | Y | | | | | 0 | 0 | 0 | 0 | mysql_native_password | *E74858DB86EBA20BC33D0AECAE8A8108C56B17FA | N | 2017-11-18 18:21:57 | NULL | N |
+-----------+------+-------------+-------------+-------------+-------------+-------------+-----------+-------------+---------------+--------------+-----------+------------+-----------------+------------+------------+--------------+------------+-----------------------+------------------+--------------+-----------------+------------------+------------------+----------------+---------------------+--------------------+------------------+------------+--------------+------------------------+----------+------------+-------------+--------------+---------------+-------------+-----------------+----------------------+-----------------------+-------------------------------------------+------------------+-----------------------+-------------------+----------------+
mysql> select * from mysql.db where user="root" and host="localhost";
Empty set (0.01 sec)
mysql> select * from mysql.tables_priv where user="root" and host="localhost";
Empty set (0.00 sec)
mysql> select * from mysql.columns_priv where user="root" and host="localhost";
Empty set (0.00 sec)
mysql> select * from mysql.procs_priv where user="root" and host="localhost";
Empty set (0.00 sec)
(4)权限认证中的大小敏感
- 字段user,password,authencation_string,db,table_name大小写敏感
- 字段host,column_name,routine_name大小写不敏感
3.MySQL支持的权限
ALL或ALL PRIVILEGES 代表指定权限等级的所有权限。
ALTER 允许使用ALTER TABLE来改变表的结构,ALTER TABLE同时也需要CREATE和INSERT权限。重命名一个表需要对旧表具有ALTER和DROP权限,对新表具有CREATE和INSERT权限。
ALTER ROUTINE 允许改变和删除存储过程和函数
CREATE 允许创建新的数据库和表
CREATE ROUTINE 允许创建存储过程和包
CREATE TABLESPACE 允许创建、更改和删除表空间和日志文件组
CREATE TEMPORARY TABLES 允许创建临时表
CREATE USER 允许更改、创建、删除、重命名用户和收回所有权限
CREATE VIEW 允许创建视图
DELETE 允许从数据库的表中删除行
DROP 允许删除数据库、表和视图
EVENT 允许在事件调度里面创建、更改、删除和查看事件
EXECUETE 允许执行存储过程和包
FILE 允许在服务器的主机上通过LOAD DATA INFILE、SELECT ... INTO OUTFILE和LOAD_FILE()函数读写文件
GRANT OPTION 允许向其他用户授予或移除权限
INDEX 允许创建和删除索引
INSERT 允许向数据库的表中插入行
LOCK TABLE 允许执行LOCK TABLES语句来锁定表
PROCESS 允许显示在服务器上执行的线程信息,即被会话所执行的语句信息。这个权限允许你执行SHOW PROCESSLIST和mysqladmin processlist命令来查看线程,同时这个权限也允许你执行SHOW ENGINE命令
PROXY 允许用户冒充成为另外一个用户
REFERENCES 允许创建外键
RELOAD 允许使用FLUSH语句
REPLICATION CLIENT 允许执行SHOW MASTER STATUS,SHOW SLAVE STATUS和SHOW BINARY LOGS命令
REPLICATION SLAVE 允许SLAVE服务器连接到当前服务器来作为他们的主服务器
SELECT 允许从数据库中查询表
SHOW DATABASES 允许账户执行SHOW DATABASE语句来查看数据库。没有这个权限的账户只能看到他们具有权限的数据库。
SHOW VIEW 允许执行SHOW CREATE VIEW语句
SHUTDOWN 允许执行SHUTDOWN语句和mysqladmin shutdown已经mysql_shutdown() C API函数
SUPER 允许用户执行CHANGE MASTER TO,KILL或mysqladmin kill命令来杀掉其他用户的线程,允许执行PURGE BINARY LOGS命令,通过SET GLOBAL来设置系统参数,执行mysqladmin debug命令,开启和关闭日志,即使read_only参数开启也可以执行update语句,打开和关闭从服务器上面的复制,允许在连接数达到max_connections的情况下连接到服务器。
TRIGGER 允许操作触发器
UPDATE 允许更新数据库中的表
USAGE 代表没有任何权限,只能登陆
4.系统权限表
User表:存放用户账户信息以及全局级别(所有数据库)权限,决定了来自哪些主机的哪些用户可以访问数据库实例,如果有全局权限则意味着对所有数据库都有此权限;
DB表:存放数据库级别的权限,决定了来自哪些主机的哪些用户可以访问此数据库;
Tables_priv表:存放表级别的权限,决定了来自哪些主机的哪些用户可以访问此数据库的这个表;
Columns_priv表:存放列级别的权限,决定了来自哪些主机的哪些用户可以访问此数据库的这个表的这个字段;
Procs_priv表:存放存储过程和函数级别的权限。
5.授权方式详解
GRANT命令用来建立新用户,指定用户口令并增加用户权限
mysql> GRANT ON TO [IDENTIFIED BY "" ] [WITH GRANT OPTION];
参数说明:
1.privileges是一个用逗号分隔的你想要赋予的MySQL用户权限的列表。
你可以指定的权限可以分为三种类型:
数据库/数据表/数据列权限(Alter、Create、Delete....)
全局管理MySQL用户权限(file、PROCESS、reload、shutdown)
特别的权限(all、usage)
user表中host列的值的意义
% 匹配所有主机
localhost localhost不会被解析成IP地址,直接通过UNIXsocket连接
127.0.0.1 会通过TCP/IP协议连接,并且只能在本机访问;
::1 ::1就是兼容支持ipv6的,表示同ipv4的127.0.0.1
WITH GRANT OPTION 权限传递
A.如果带了 with grant option ,那么用户testuser1可以将select ,update权限传递给其他用户( 如testuser2)
grant select,update on bd_corp to testuser2
B.如果没带with grant option,那么用户testuser1不能给testuser2授权
6.权限的生效
- 执行grant、revoke、set password、rename user命令修改权限后,MySQL会自动将修改后的权限信息同步加载到系统内存中;
- 如果执行insert、update、delete操作上述的系统权限表后,则必须在执行刷新命令才能同步到内存
- 如果是修改tables和colunms级别的权限,则客户端的下次操作时新权限会生效;
- 如果是修改database级别的权限,则新权限在客户端执行use database命令后生效
- --skip-grant-tables可以跳过所有系统权限表而允许所有用户登录
7.创建用户
mysql> create user "test"@"localhost" identified by "mysql";
Query OK, 0 rows affected (0.00 sec)
mysql> grant select on *.* to "test"@"localhost" with grant option;
Query OK, 0 rows affected (0.00 sec)
mysql> show create user "test"@"localhost"G
*************************** 1. row ***************************
CREATE USER for test@localhost: CREATE USER "test"@"localhost" IDENTIFIED WITH "mysql_native_password" AS "*E74858DB86EBA20BC33D0AECAE8A8108C56B17FA" REQUIRE NONE PASSWORD EXPIRE DEFAULT ACCOUNT UNLOCK
1 row in set (0.00 sec)
8.回收权限
revoke跟grant的语法差不多,只需要把关键字 “to” 换成 “from” 即可;
mysql> show grants for "test"@"localhost";
+---------------------------------------------------------------------+
| Grants for test@localhost |
+---------------------------------------------------------------------+
| GRANT SELECT, DELETE ON *.* TO "test"@"localhost" WITH GRANT OPTION |
+---------------------------------------------------------------------+
1 row in set (0.00 sec)
mysql> revoke delete on *.* from "test"@"localhost";
Query OK, 0 rows affected (0.00 sec)
mysql> show grants for "test"@"localhost";
+-------------------------------------------------------------+
| Grants for test@localhost |
+-------------------------------------------------------------+
| GRANT SELECT ON *.* TO "test"@"localhost" WITH GRANT OPTION |
+-------------------------------------------------------------+
1 row in set (0.00 sec)
9.删除用户
mysql> drop user "test"@"localhost";
Query OK, 0 rows affected (0.00 sec)
分类:
Mysql