文章目录
一、用户管理
1. 进一步认识DCL
- 我们知道,SQL语言按功能用途分为4类,分别是
DDL
、DML
、DQL
和DCL
- 其中,
DCL
是数据控制语言,主要用于管理用户和权限。在企业中,这部分工作通常是由DBA完成,一般开发人员很少接触、 - DCL的功能
- 创建用户
- 删除用户
- 修改密码
- 给用户赋予权限
- 撤销用权限
2. MySQL权限体系
-
MySQL的权限体系大致分为5个层级,全局层级、数据库层级、表层级、列层级和子程序层级
-
MySQL的权限信息主要存储在以下表中,当用户连接数据库时,MySQL会根据这些表对用户进行权限验证
-
root登录后,输入指令
show databases
显示所有mysql数据库 ,use mysql
切换到mysql数据库,再show tables
,就能看到上面这些表mysql> use mysql; Database changed mysql> show tables; +---------------------------+ | Tables_in_mysql | +---------------------------+ | columns_priv | | db | | event | | func | | general_log | | help_category | | help_keyword | | help_relation | | help_topic | | host | | ndb_binlog_index | | plugin | | proc | | procs_priv | | proxies_priv | | servers | | slow_log | | tables_priv | | time_zone | | time_zone_leap_second | | time_zone_name | | time_zone_transition | | time_zone_transition_type | | user | +---------------------------+ 24 rows in set (0.22 sec)
-
查看所有用户信息
mysql> select * from user \G; *************************** 1. row *************************** Host: localhost User: root Password: *818FB0F46F4B325191D8B9401EC2BBDF19568CFF 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: *************************** 2. row *************************** Host: % User: root Password: *818FB0F46F4B325191D8B9401EC2BBDF19568CFF 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: 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 2 rows in set (0.10 sec)
- 可以看到两个用户
3. 用户管理
(1)创建用户
-
在MySQL中,使用
CREATE USER
来创建用户 -
用户创建后没有任何权限,只能登录,创建数据库什么的都不行
-
语法:
CREATE USER '用户名' [@'主机名'] [IDENTIFIED BY '密码'];
-
注意:MySQL的用户账号由两部分组成:用户名和主机名,即用户名@主机名
- 主机名可以是IP或机器名称,主机名为
%
表示允许任何地址的主机远程登录MySQL数据库 - 之前的学习中一直用的root账户,只要输个密码就行了,实际上需要主机名和用户名两部分
- 由于记录了主机,相同的用户名和密码在别的主机上也不能登录。除非设置为任意主机都允许登录(主机名
%
)
- 主机名可以是IP或机器名称,主机名为
-
示例
-
创建用户
#创建用户,用户名`wxc97123`,主机任意,密码`123456` create user 'wxc971231'@'%' identified by '123456'; #创建用户,用户名`wxc97123`,主机必须是192.168.101.103,密码`123456` create user 'wxc971231'@'192.168.10.103' identified by '123456';
-
用wxc97123用户登录
C:\Users\Administrator>mysql -h localhost -u wxc971231 -p Enter password: ****** Welcome to the MySQL monitor. Commands end with ; or \g. Your MySQL connection id is 2 Server version: 5.5.15 MySQL Community Server (GPL) Copyright (c) 2000, 2010, 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.
-
(2)删除用户
- 语法:
DROP USER '用户名' [@'主机名'];
(3)修改用户密码
- 语法:
ALTER USER '用户名'@'主机名' IDENTIFIED BY '新密码';
二、权限管理
1. 授权和撤销授权
//授权(所有权限)
grant all privileges on databaseName.tableName to '用户名' [@'主机名'];
//撤销授权(所有权限)
revoke all privileges on databaseName.tableName from '用户名' [@'主机名'];
//刷新权限(刷新后授权才生效)
FLUSH PRIVILEGES;
//查看权限
show grants for '用户名' [@'主机名'] ;
2. MySQL权限列表
- 使用grant和revoke进行授权、撤销授权时,需要指定具体是哪些权限,这些权限大体可以分为3类,数据类、结构类和管理类,上面语法中的
all privileges
可以换成这些
3. 示例
-
在root用户下创建
'wxc971231'@'%'
这个用户 -
新开一个cmd,登录新用户,查看权限
# 查看权限(刚创建的用户只有USAGE,只能用来登录) mysql> show grants for 'wxc971231'@'%'; +---------------------------------------------------------------------+ | Grants for wxc971231@% | +---------------------------------------------------------------------+ | GRANT USAGE ON *.* TO 'wxc971231'@'%' IDENTIFIED BY PASSWORD '*6BB4837EB74329105EE4568DDA7DC67ED2CA2AD9' | +----------------------------------------------------------------------+ 1 row in set (0.00 sec)
-
在root用户下进行授权
grant select on test.* to 'wxc971231'@'%'; #test是root用户已经创建一个数据库
-
再到新用户的那个cmd,查看数据库,此时可以看到test库(之前是看不到的),也可对test库进行select
mysql> show databases; +--------------------+ | Database | +--------------------+ | information_schema | | test | +--------------------+ 2 rows in set (0.00 sec) mysql> use test; Database changed mysql> select * from employee; +----+--------+------+--------+------+ | id | name | sex | salary | dept | +----+--------+------+--------+------+ | 1 | 张三 | 男 | 5500 | A | | 2 | 李小梅 | 女 | 4200 | A | | 3 | 欧阳辉 | 男 | 7500 | C | | 4 | 李芳 | 女 | 8500 | A | | 5 | 张江 | 男 | 6800 | A | | 6 | 李四 | 男 | 12000 | B | | 7 | 王五 | 男 | 3600 | B | | 8 | 马小龙 | 男 | 6000 | A | | 9 | 龙五 | 男 | 8000 | C | | 10 | 冯小芳 | 女 | 10000 | C | | 11 | 马小龙 | 女 | 4000 | A | +----+--------+------+--------+------+ 11 rows in set (0.12 sec)
三、未授权的操作被禁止
-
进行未授权的操作时,报错
mysql> DELETE FROM table_name; ERROR 1142 (42000): DELETE command denied to user 'wxc971231'@'localhost' for table 'table_name'
-
在root用户登录,收回wxc971231用户的权限
revoke select on test.* from 'wxc971231'@'%'; #test是root用户已经创建一个数据库
-
撤销新用户权限后,再登录新用户账户查看
# 先退出 mysql> exit Bye # 重新登录 C:\Users\Administrator>mysql -h localhost -u wxc971231 -p Enter password: ****** Welcome to the MySQL monitor. Commands end with ; or \g. Your MySQL connection id is 4 Server version: 5.5.15 MySQL Community Server (GPL) Copyright (c) 2000, 2010, 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. # 因为没有test库上的select权限了,现在test库看不到了 mysql> show databases; +--------------------+ | Database | +--------------------+ | information_schema | +--------------------+ 1 row in set (0.00 sec)