一、允许用户所有IP访问

更改 mysql 数据库里的 user 表里的 host 项,从localhost"改成%即可。有两种方式可以实现。

第一种:直接修改user 表

1. mysql -u root -p  

2. select host,user from user where user='root';

3. update user set host = '%' where user='root' and host='localhost';  

4. select host, user from user where user='root';

第二种:授权的方式

grant all privileges on *.* to root@'%' identified by '123' with grant option;

flush privileges;


# *.* 表示所有的数据库 ,可以针对某个数据进行设置,如 on 库名.表名
# grant all privileges on 库名.表名 to '用户名'@'IP地址' identified by '密码' with grant option;

二、限制某个IP或者IP段访问

建议通过授权的方式实现。

#单个IP
grant all privileges on *.* to root@'192.168.1.3' identified by '123' with grant option;

flush privileges;

#IP段,使用%代替IP
grant all privileges on *.* to root@'192.168.1.%' identified by '123' with grant option;
or
grant all privileges on *.* to root@'192.168.%.%' identified by '123' with grant option;

flush privileges;

注意:

1、注意授权后必须FLUSH PRIVILEGES;否则无法立即生效。

2、高版本无法使用grant all privileges on *.* to "root"@"%" identified by "xxxx";去修改用户权限。

如:

mysql> SELECT @@VERSION;
+-----------+
| @@VERSION |
+-----------+
| 8.0.14    |
+-----------+

# 先创建远程用户,再授权
create user 'root'@'%' identified by  'password';

grant all privileges on *.* to 'root'@'%' with grant option;

flush privileges;

#查看
select User,Host from user;

Logo

本社区面向用户介绍CSDN开发云部门内部产品使用和产品迭代功能,产品功能迭代和产品建议更透明和便捷

更多推荐