我可以使用以下代码轻松授予对一个IP的访问权限:
$ mysql -u root -p
Enter password:
mysql> use mysql
mysql> GRANT ALL ON *.* to root@'192.168.1.4' IDENTIFIED BY 'your-root-password';
mysql> FLUSH PRIVILEGES;
Run Code Online (Sandbox Code Playgroud)
但我需要允许整个子网192.168.1.*远程访问数据库.
我怎样才能做到这一点?
Mal*_*ous 92
GRANT ... TO 'user'@'192.168.0.0/255.255.255.0' IDENTIFIED BY ...
Run Code Online (Sandbox Code Playgroud)
p0l*_*ear 91
编辑:考虑在这个页面上查看并提升Malvineous的答案.Netmasks是一个更优雅的解决方案.
只需在IP地址中使用百分号作为通配符即可.
来自http://dev.mysql.com/doc/refman/5.1/en/grant.html
您可以在主机名中指定通配符.例如,
user_name@'%.example.com'适用user_name于example.com域中的任何主机,并user_name@'192.168.1.%'适用于C类子网user_name中的任何主机192.168.1.
Mik*_*ant 28
您只需使用'%'作为通配符,如下所示:
GRANT ALL ON *.* to root@'192.168.1.%' IDENTIFIED BY 'your-root-password';
Run Code Online (Sandbox Code Playgroud)
Dav*_*len 12
mysql> GRANT ALL ON *.* to root@'192.168.1.%' IDENTIFIED BY 'your-root-password';
Run Code Online (Sandbox Code Playgroud)
通配符是"%"而不是"*"
只是我面临的一个特殊性的说明:
考虑:
db server: 192.168.0.101
web server: 192.168.0.102
Run Code Online (Sandbox Code Playgroud)
如果您在 mysql.user 中定义了一个用户为'user'@'192.168.0.102'password1,另一个用户'user'@'192.168.0.%'为 password2,
然后,
如果您尝试使用密码 2 以“用户”身份从 Web 服务器连接到数据库服务器,
这将导致“拒绝访问”错误,因为'user'@'192.168.0.102'在通配符'user'@'192.168.0.%'身份验证上使用了单个 IP 身份验证。