最近有个故障整改,需要限制IP访问MySQL数据库,查了一下资料。 做做笔记,多多指教
select Host,User from mysql.user;
SELECT DISTINCT CONCAT('User: ''',user,'''@''',host,''';') AS query FROM mysql.user;
查看root用户可以在哪台机器登录
select user,host from mysql.user where user='root';
修改mysql库里边的user表,限制root用户只能从哪个ip登录
update mysql.user set host='localhost' where user='root';
刷新权限
flush privileges;
mysql>grant select,insert,update,create on test.* to test@'192.168.8.%' identified by '123456';
mysql>grant select,insert,update,create on test.* to test@'192.168.9.%' identified by '123456';
mysql>grant select,insert,update,create on test.* to test@'192.168.20.%' identified by '123456';
上面的语句表示将test数据库的所有权限授权给test这个用户,只允许test用户在192.168.8/9/20这三个网段的IP范围进行远程登陆,并设置test用户的密码为123456
刷新权限表使其设置生效: mysql> flush privileges;
发现rename也可以用
rename user test@'%' to test@'172.22.137.%';
Copyright: 采用 知识共享署名4.0 国际许可协议进行许可 Links: https://lixj.fun/archives/mysql限制ip访问的方法