测试系统 centos7.7
mysql权限赋予命令,最新版本与早期版本有些不同,如5.5及8.0版本。
mysql5.5只需要使用grant语句,即使用户没有也会主机自动创建,mysql8.0需要使用create创建新用户,之后使用grant赋予指定用户权限; mysql5.5指定用户密码时直接指定,mysql8.0指定用户密码时要添加关键词mysql_native_password (加密模式,不指定默认为caching_sha2_password ,支持本地连接但不能远程连接,所以要修改模式)。
1、mysql5.5(或5.6,5.7)设置远程访问权限
# 允许所有客户端访问数据库
grant all privileges on *.* to 'root'@'%' identified by 'root' with grant option;
# 只允许客户端192.168.1.1
GRANT ALL PRIVILEGES ON *.* TO root@192.168.1.1 identified by "root" WITH GRANT OPTION;
# 立刻生效
flush privileges;
2、mysql8.0设置远程访问权限
# 方式1
# 创建用户,加密模式为mysql_native_password
create user 'service'@'%' identified with mysql_native_password by 'service';
# 赋予用户权限
grant all privileges on *.* to 'service'@'%' with grant option;
# 立刻生效
flush privileges;
#方式2
# 创建用户,默认加密模式
create user 'service'@'%' identified by 'service';
# 修改加密模式为mysql_native_password
alter user 'service'@'%' identified with mysql_native_password by 'service';
# 赋予用户权限
grant all privileges on *.* to 'service'@'%' with grant option;
# 立刻生效
flush privileges;
其他相关语句如
# 指定权限
grant select,insert,update,delete on test.* to service@192.168.1.1 identified by "service";
grant create,drop,alter on test.* to service@192.168.1.1 identified by "service";
#收回权限
revoke all on *.* from dba@localhost;
revoke all on test.* from service@192.168.1.1 ;