文章目录 一、用户管理二、用户(一)用户信息(二)创建用户1.语法:2.案例: (三) 删除用户1.语法:2.示例: (四)修改用户密码1.语法: 三、数据库的权限(一)
如果我们只能使用root用户,这样存在安全隐患。这时,就需要使用Mysql的用户管理。
mysql中的用户,都存储在系统数据库mysql的user表中。
mysql> use mysql;Database changedmysql> select host,user,authentication_string from user;
字段解释:
create user '用户名'@'登陆主机/ip' identified by '密码';
mysql> create user 'whb'@'localhost' identified by '12345678';
Query OK, 0 rows affected (0.06 sec)mysql> select user,host,authentication_string from user;+---------------+-----------+-------------------------------------------+| user | host | authentication_string |+---------------+-----------+-------------------------------------------+| root | % | *A2F7C9D334175DE9AF4DB4F5473E0BD0F5FA9E75|| mysql.session | localhost | *THISISNOTAVALIDPASSWORDTHATCANBEUSEDHERE|| mysql.sys | localhost | *THISISNOTAVALIDPASSWORDTHATCANBEUSEDHERE|| mt | localhost | *84AAC12F54AB666ECFC2A83C676908C8BBC381B1| --新增用户+---------------+-----------+-------------------------------------------+4 rows in set (0.00 sec)-- 此时便可以使用新账号新密码进行登陆啦--备注:可能实际在设置密码的时候,因为mysql本身的认证等级比较高,一些简单的密码无法设置,会爆出如下报错:-- ERROR 1819 (HY000): Your password does not satisfy the current policyrequirements-- 解决方案:https://blog.csdn.net/zhanaolu4821/article/details/93622812--查看密码设置相关要求:SHOW VARIABLES LIKE 'validate_password%';
drop user '用户名'@'主机名'
mysql> select user,host,authentication_string from user;mysql> select user,host,authentication_string from user;+---------------+-----------+-------------------------------------------+| user | host | authentication_string |+---------------+-----------+-------------------------------------------+| root | % | *A2F7C9D334175DE9AF4DB4F5473E0BD0F5FA9E75|| mysql.session | localhost | *THISISNOTAVALIDPASSWORDTHATCANBEUSEDHERE|| mysql.sys | localhost | *THISISNOTAVALIDPASSWORDTHATCANBEUSEDHERE|| mt | localhost | *84AAC12F54AB666ECFC2A83C676908C8BBC381B1| --新增用户+---------------+-----------+-------------------------------------------+4 rows in set (0.00 sec)mysql> drop user whb; --尝试删除ERROR 1396 (HY000): Operation DROP USER failed for 'mt'@'%' -- <= 直接给个用户名,不能删除,它默认是%,表示所有地方可以登陆的用户mysql> drop user 'mt'@'localhost'; --删除用户Query OK, 0 rows affected (0.00 sec)+---------------+-----------+-------------------------------------------+| user | host | authentication_string |+---------------+-----------+-------------------------------------------+| root | % | *A2F7C9D334175DE9AF4DB4F5473E0BD0F5FA9E75|| mysql.session | localhost | *THISISNOTAVALIDPASSWORDTHATCANBEUSEDHERE|| mysql.sys | localhost | *THISISNOTAVALIDPASSWORDTHATCANBEUSEDHERE|| mt | localhost | *84AAC12F54AB666ECFC2A83C676908C8BBC381B1| --新增用户+---------------+-----------+-------------------------------------------+4 rows in set (0.00 sec)
set password=password('新的密码');
set password for '用户名'@'主机名'=password('新的密码');
mysql> select host,user, authentication_string from user;
+---------------+-----------+-------------------------------------------+| user | host | authentication_string |+---------------+-----------+-------------------------------------------+| root | % | *A2F7C9D334175DE9AF4DB4F5473E0BD0F5FA9E75|| mysql.session | localhost | *THISISNOTAVALIDPASSWORDTHATCANBEUSEDHERE|| mysql.sys | localhost | *THISISNOTAVALIDPASSWORDTHATCANBEUSEDHERE|| mt | localhost | *84AAC12F54AB666ECFC2A83C676908C8BBC381B1| --新增用户+---------------+-----------+-------------------------------------------+4 rows in set (0.00 sec)
mysql> set password for 'mt'@'localhost'=password('87654321');Query OK, 0 rows affected, 1 warning (0.00 sec)
mysql> select host,user, authentication_string from user;`+---------------+-----------+-------------------------------------------+| user | host | authentication_string |+---------------+-----------+-------------------------------------------+| root | % | *A2F7C9D334175DE9AF4DB4F5473E0BD0F5FA9E75|| mysql.session | localhost | *THISISNOTAVALIDPASSWORDTHATCANBEUSEDHERE|| mysql.sys | localhost | *THISISNOTAVALIDPASSWORDTHATCANBEUSEDHERE|| mt | localhost | *5D24C4D94238E65A6407DFAB95AA4EA97CA2B199| --新增用户+---------------+-----------+-------------------------------------------+4 rows in set (0.00 sec)`
MySQL数据库提供的权限列表:
刚创建的用户没有任何权限。需要给用户授权。
grant 权限列表 on 库.对象名 to '用户名'@'登陆位置' [identified by '密码']
grant select on ...grant select, delete, create on ....grant all [privileges] on ... -- 表示赋予该用户在该对象上的所有权限
--使用root账号--终端Amysql> show databases;
--给用户whb赋予test数据库下所有文件的select权限mysql> grant select on test.* to 'whb'@'localhost';Query OK, 0 rows affected (0.01 sec)
--使用whb账号--终端B
revoke 权限列表 on 库.对象名 from '用户名'@'登陆位置';
-- 回收mt对test数据库的所有权限--root身份,终端Amysql> revoke all on test.* from 'mt'@'localhost';Query OK, 0 rows affected (0.00 sec)
来源地址:https://blog.csdn.net/weixin_54447296/article/details/133146837
--结束END--
本文标题: 十四、MySql的用户管理
本文链接: https://lsjlt.com/news/427115.html(转载时请注明来源链接)
有问题或投稿请发送至: 邮箱/279061341@qq.com QQ/279061341
2024-10-23
2024-10-22
2024-10-22
2024-10-22
2024-10-22
2024-10-22
2024-10-22
2024-10-22
2024-10-22
2024-10-22
回答
回答
回答
回答
回答
回答
回答
回答
回答
回答
0