『MySQL5.7』创建用户并授权
# 创建用户
CREATE USER 'username'@'hostname' IDENTIFIED BY 'password';
-- 比如:
CREATE USER 'kimen'@'localhost' IDENTIFIED BY '123456';
CREATE USER 'kimen'@'192.168.1.230' IDENTIFIED BY '123456';
CREATE USER 'kimen'@'%' IDENTIFIED BY '123456';
1
2
3
4
5
2
3
4
5
注意
创建完用户后,该用户只能连接数据库服务器,但是没有权限
# 授权
GRANT privilegesName ON databasename.tablename TO 'username'@'hostname'
-- 比如:
GRANT SELECT, INSERT ON test.tbL_user TO 'kimen'@'%';
GRANT ALL ON *.* TO 'kimen'@'%';
GRANT ALL ON test.* TO 'kimen'@'%';
1
2
3
4
5
2
3
4
5
用以上命令授权的用户不能给其它用户授权,加上WITH GRANT OPTION
可让该用户获得授权权利:
GRANT ALL ON test.tbL_user TO 'kimen'@'%' WITH GRANT OPTION;
1
Tips
当数据库名称带有中横线-
时,databasename需要增加反引号
GRANT ALL ON `test-a`.* TO 'kimen'@'%';
1
# 修改用户密码
SET PASSWORD FOR 'username'@'hostname' = PASSWORD('newpassword');
-- 如
SET PASSWORD FOR 'kimen'@'%' = PASSWORD("123456");
-- 如果是当前用户
SET PASSWORD = PASSWORD("newpassword");
1
2
3
4
5
2
3
4
5
# 撤销用户权限
REVOKE privilegesName ON databasename.tablename FROM 'username'@'hostname';
-- 比如
REVOKE SELECT ON *.* FROM 'kimen'@'%';
1
2
3
2
3
# 删除用户
DROP USER 'username'@'hostname';
-- 比如:
DROP USER 'kimen'@'%';
1
2
3
2
3
# 立即生效
flush privileges;
1
# 查看用户权限
show grants for username;
1
编辑 (opens new window)
上次更新: 2021/09/07, 16:52:40
- 01
- 当『Emby、Plex、Jellyfin』遇上『阿里云盘』🎬08-17
- 02
- 当『Infuse』遇上『阿里云盘』 🚀08-17
- 03
- 【NAS In Docker】重要数据备份并自动上传至阿里云盘05-21