一、用户管理概述
mysql 的用户管理也是按照表结构进行管理的,因为 mysql 需要记录哪些用户有什么权限。
在安装 mysql 时,即使没有创建数据库,默认情况下仍然会有系统自带的数据库。其中一个叫做 mysql 的数据库,里面有一张 user 表,这张表就是用来管理用户的。
可以通过以下命令查看:
select * from user\g;
user 表中的关键字段:
user:表示用户名,即谁在登录 mysql。host:表示允许该用户在哪些机器上登录 mysql。authentication_string:存储加密后的密码。- 其他字段:各种权限信息。
因此,在 mysql 下新建一个用户,实际上就是在这张表里面填写 user、host、authentication_string(密码)以及其他权限字段。
比较粗暴的方式可以通过 insert 插入,但过于麻烦,所以推荐使用专门的用户管理语句。
二、创建用户
语法:
create user '用户名'@'登陆主机/ip' identified by '密码';
注意:登陆主机/ip 必须写。
案例:
mysql> create user 'whb'@'localhost' identified by '12345678'; query ok, 0 rows affected (0.06 sec)
这个密码在进行保存的过程中,实际上会通过 hash 的方式加密后再填写到 authentication_string 字段。
查看用户:
mysql> select user,host,authentication_string from user; +---------------+-----------+-------------------------------------------+ | user | host | authentication_string | +---------------+-----------+-------------------------------------------+ | root | % | *a2f7c9d334175de9af4db4f5473e0bd0f5fa9e75 | | mysql.session | localhost | *thisisnotavalidpasswordthatcanbeusedhere | | mysql.sys | localhost | *thisisnotavalidpasswordthatcanbeusedhere | | whb | localhost | *84aac12f54ab666ecfc2a83c676908c8bbc381b1 | +---------------+-----------+-------------------------------------------+ 4 rows in set (0.00 sec)
此时便可以使用新账号新密码进行登录。
备注:
可能实际在设置密码的时候,因为 mysql 本身的认证等级比较高,一些简单的密码无法设置,会爆出如下报错:
error 1819 (hy000): your password does not satisfy the current policy requirements
解决方案:查看密码设置相关要求:
show variables like 'validate_password%';
关于新增用户这里,需要注意,不要轻易添加一个可以从任意地方登录的 user。
注意:使用新创建的用户是不可以远程进行登录的,因为该用户的 host 字段是 localhost,不是远程机器。
三、删除用户
语法:
drop user '用户名'@'主机名';
示例:
mysql> select user,host,authentication_string from user; +---------------+-----------+-------------------------------------------+ | user | host | authentication_string | +---------------+-----------+-------------------------------------------+ | root | % | *a2f7c9d334175de9af4db4f5473e0bd0f5fa9e75 | | mysql.session | localhost | *thisisnotavalidpasswordthatcanbeusedhere | | mysql.sys | localhost | *thisisnotavalidpasswordthatcanbeusedhere | | whb | localhost | *84aac12f54ab666ecfc2a83c676908c8bbc381b1 | +---------------+-----------+-------------------------------------------+ 4 rows in set (0.00 sec) mysql> drop user whb; -- 尝试删除 error 1396 (hy000): operation drop user failed for 'whb'@'%' -- 直接给个用户名,不能删除,它默认是%,表示所有地方可以登陆的用户 mysql> drop user 'whb'@'localhost'; -- 删除用户,所以要加上 ip 地址 query ok, 0 rows affected (0.00 sec) mysql> select user,host,authentication_string from user; +---------------+-----------+-------------------------------------------+ | user | host | authentication_string | +---------------+-----------+-------------------------------------------+ | root | % | *a2f7c9d334175de9af4db4f5473e0bd0f5fa9e75 | | mysql.session | localhost | *thisisnotavalidpasswordthatcanbeusedhere | | mysql.sys | localhost | *thisisnotavalidpasswordthatcanbeusedhere | +---------------+-----------+-------------------------------------------+ 3 rows in set (0.00 sec)
在删除老用户后,再新建用户的时候,不在明确写 ip 号。
即使写了明确的 ip,在绝大多数情况下也是没有什么实际用处的,因为 ip 是局域网 ip,没法在公网中找到;即使是公网 ip,其实也不建议这样写。
所以在 ip 那里写上 %,也就是:
create user 'whb'@'%' identified by '密码';
就代表任何机器都可以登录这个用户。
四、修改用户密码
语法:
自己改自己密码:
set password=password('新的密码');
其实就是 update。
root 用户修改指定用户的密码:
set password for '用户名'@'主机名'=password('新的密码');
示例:
mysql> select host,user, authentication_string from user;
+-----------+---------------+-------------------------------------------+
| host | user | authentication_string |
+-----------+---------------+-------------------------------------------+
| % | root | *a2f7c9d334175de9af4db4f5473e0bd0f5fa9e75 |
| localhost | mysql.session | *thisisnotavalidpasswordthatcanbeusedhere |
| localhost | mysql.sys | *thisisnotavalidpasswordthatcanbeusedhere |
| localhost | whb | *84aac12f54ab666ecfc2a83c676908c8bbc381b1 |
+-----------+---------------+-------------------------------------------+
4 rows in set (0.00 sec)
mysql> set password for 'whb'@'localhost'=password('87654321');
query ok, 0 rows affected, 1 warning (0.00 sec)
mysql> select host,user, authentication_string from user;
+-----------+---------------+-------------------------------------------+
| host | user | authentication_string |
+-----------+---------------+-------------------------------------------+
| % | root | *a2f7c9d334175de9af4db4f5473e0bd0f5fa9e75 |
| localhost | mysql.session | *thisisnotavalidpasswordthatcanbeusedhere |
| localhost | mysql.sys | *thisisnotavalidpasswordthatcanbeusedhere |
| localhost | whb | *5d24c4d94238e65a6407dfab95aa4ea97ca2b199 |
+-----------+---------------+-------------------------------------------+
4 rows in set (0.00 sec)五、数据库权限
mysql 数据库提供的权限列表:
| 权限 | 列 | 上下文 |
|---|---|---|
| create | create_priv | 数据库、表或索引 |
| drop | drop_priv | 数据库或表 |
| grant option | grant_priv | 数据库、表或保存的程序 |
| references | references_priv | 数据库或表 |
| alter | alter_priv | 表 |
| delete | delete_priv | 表 |
| index | index_priv | 表 |
| insert | insert_priv | 表 |
| select | select_priv | 表 |
| update | update_priv | 表 |
| create view | create_view_priv | 视图 |
| show view | show_view_priv | 视图 |
| alter routine | alter_routine_priv | 保存的程序 |
| create routine | create_routine_priv | 保存的程序 |
| execute | execute_priv | 保存的程序 |
| file | file_priv | 服务器主机上的文件访问 |
| create temporary tables | create_tmp_table_priv | 服务器管理 |
| lock tables | lock_tables_priv | 服务器管理 |
| create user | create_user_priv | 服务器管理 |
| process | process_priv | 服务器管理 |
| reload | reload_priv | 服务器管理 |
| replication client | repl_client_priv | 服务器管理 |
| replication slave | repl_slave_priv | 服务器管理 |
| show databases | show_db_priv | 服务器管理 |
| shutdown | shutdown_priv | 服务器管理 |
| super | super_priv | 服务器管理 |
5.1 给用户授权
刚创建的用户没有任何权限,需要给用户授权。
语法:
grant 权限列表 on 库.对象名 to '用户名'@'登陆位置' [identified by '密码']
说明:
- 权限列表,多个权限用逗号分开:
grant select on ... grant select, delete, create on ... grant all [privileges] on ... -- 表示赋予该用户在该对象上的所有权限
*.*:代表本系统中的所有数据库的所有对象(表、视图、存储过程等),即所有库的所有表。库.*:表示某个数据库中的所有数据对象(表、视图、存储过程等),即某个库的所有表。identified by可选。如果用户存在,赋予权限的同时修改密码;如果该用户不存在,就是创建用户。
案例:
使用 root 账号,终端 a:
mysql> show databases; +--------------------+ | database | +--------------------+ | information_schema | | 57test | | bit_index | | ccdata_pro | | innodb_test | | musicserver | | myisam_test | | mysql | | order_sys | | performance_schema | | scott | | sys | | test | | vod_system | +--------------------+ 14 rows in set (0.00 sec) mysql> use test; database changed mysql> show tables; +----------------+ | tables_in_test | +----------------+ | account | | student | | user | +----------------+ 3 rows in set (0.01 sec) -- 给用户 whb 赋予 test 数据库下所有文件的 select 权限 mysql> grant select on test.* to 'whb'@'localhost'; query ok, 0 rows affected (0.01 sec)
使用 whb 账号,终端 b:
mysql> show databases; +--------------------+ | database | +--------------------+ | information_schema | +--------------------+ 1 row in set (0.00 sec) -- 暂停等 root 用户给 whb 赋完权之后,再查看 mysql> show databases; +--------------------+ | database | +--------------------+ | information_schema | | test | -- 赋完权之后,就能看到新的表 +--------------------+ 2 rows in set (0.01 sec) mysql> use test; reading table information for completion of table and column names you can turn off this feature to get a quicker startup with -a database changed mysql> show tables; +----------------+ | tables_in_test | +----------------+ | account | | student | | user | +----------------+ 3 rows in set (0.00 sec) mysql> select * from account; +----+--------+---------+ | id | name | blance | +----+--------+---------+ | 2 | 李四 | 321.00 | | 3 | 王五 | 5432.00 | | 4 | 赵六 | 543.90 | | 5 | 赵六 | 543.90 | +----+--------+---------+ 4 rows in set (0.00 sec) -- 没有删除权限 mysql> delete from account; error 1142 (42000): delete command denied to user 'whb'@'localhost' for table 'account'
备注:查看特定用户现有权限
mysql> show grants for 'whb'@'%'; +-----------------------------------------------+ | grants for whb@% | +-----------------------------------------------+ | grant usage on *.* to 'whb'@'%' | | grant all privileges on `test`.* to 'whb'@'%' | +-----------------------------------------------+ 2 rows in set (0.00 sec) mysql> show grants for 'root'@'%'; +-------------------------------------------------------------+ | grants for root@% | +-------------------------------------------------------------+ | grant all privileges on *.* to 'root'@'%' with grant option | +-------------------------------------------------------------+ 1 row in set (0.00 sec)
注意:如果发现赋权限后没有生效,执行如下指令:
flush privileges;
5.2 回收权限
语法:
revoke 权限列表 on 库.对象名 from '用户名'@'登陆位置';
库.对象名 中的对象名就是具体的某个表。
示例:
回收 whb 对 test 数据库的所有权限。
root 身份,终端 a:
mysql> revoke all on test.* from 'whb'@'localhost'; query ok, 0 rows affected (0.00 sec)
whb 身份,终端 b:
mysql> show databases; +--------------------+ | database | +--------------------+ | information_schema | | test | +--------------------+ 2 rows in set (0.00 sec) mysql> show databases; +--------------------+ | database | +--------------------+ | information_schema | +--------------------+ 1 row in set (0.00 sec)
六、注意事项总结
- mysql 用户管理基于
mysql数据库中的user表。 - 创建用户必须指定
用户名和登录主机,密码会加密存储。 - 删除用户必须指定完整
'用户名'@'主机名',否则默认操作'用户名'@'%'可能失败。 - 修改密码可以使用
set password或set password for。 - 新用户默认没有任何权限,需要
grant授权。 - 权限可以针对所有库所有表(
*.*)、某个库所有表(库.*)或具体表。 - 授权后如未生效,可执行
flush privileges;。 - 回收权限使用
revoke。 - 主机名
%表示任意主机,localhost表示本机。 - 不要轻易创建可从任意地方登录的用户,注意安全。
- 密码策略可能导致简单密码无法设置,可通过
show variables like 'validate_password%';查看。
到此这篇关于mysql从创建、删除到权限控制全攻略( 用户管理)的文章就介绍到这了,更多相关mysql创建、删除到权限控制内容请搜索代码网以前的文章或继续浏览下面的相关文章希望大家以后多多支持代码网!
发表评论