mysql8创建用户、删除用户、授权、取消授权

无论在linux主机还是在docker容器中,打开bash窗口

一、创建用户

CREATE USER 'username'@'host' IDENTIFIED WITH mysql_native_password BY 'password';

说明
host需要替换成对应的字符串,
如果允许本地访问, 使用localhost
如果允许远程访问, 使用%

例子

CREATE USER 'test'@'192.168.3.41' IDENTIFIED WITH mysql_native_password BY '123456';

FLUSH PRIVILEGES;

更新用户密码

ALTER USER 'username'@'host' IDENTIFIED WITH mysql_native_password BY 'password';

例子

ALTER USER 'test'@'192.168.3.41' IDENTIFIED WITH mysql_native_password BY '123456';    
FLUSH PRIVILEGES;

二、删除用户

DROP USER 'username'@'host';

例子

DROP USER 'test'@'192.168.3.41';

三、授权

GRANT privileges ON databasename.tablename TO 'username'@'host' [WITH GRANT OPTION];

说明

  • privileges:用户的操作权限,如SELECT,INSERT,UPDATE等,如果要授予所的权限则使用ALL
  • databasename:数据库名
  • tablename:表名,如果要授予该用户对所有数据库和表的相应操作权限则可用表示,如.*
  • WITH GRANT OPTION 该用户是否可授权

例子

GRANT select,insert,update,delete,create,drop ON mysql.user TO 'test'@'192.168.3.41';
GRANT ALL ON *.* TO 'test'@'192.168.3.41';

查看授权信息

SHOW GRANTS FOR 'test'@'192.168.3.41';

撤销权限

REVOKE privilege ON databasename.tablename FROM 'username'@'host';

注意用什么的语句授权就用什么样的语句收回权限,否则可能无法撤销权限

# mysql 

评论

Your browser is out-of-date!

Update your browser to view this website correctly. Update my browser now

×