百分百源码网-让建站变得如此简单! 登录 注册 签到领金币!

主页 | 如何升级VIP | TAG标签

当前位置: 主页>网站教程>数据库> CentOS系统下mysql绑定多个ip地址访问
分享文章到:

CentOS系统下mysql绑定多个ip地址访问

发布时间:01/15 来源: 浏览: 关键词:
为了安全起见,我们将centos服务器上的mysql绑定允许访问的ip,这样就实现了mysql只允许指定ip访问,具体实现请看本文。

msyql配置文件my.cnf中有选项bind-address=127.0.0.1,就是说mysql server监听的是本地发来的请求,如果开放任意主机都可以请求,则写为0.0.0.0,但是这样又不太安全。监听某ip,指定此ip地址即可,但是要保证mysql的user中有允许此ip访问,否则不能对数据库操作。那么是否可以在配置里只规定几个ip呢?

简单直接回答:不可能

请参考:#option_mysqld_bind-address

The MySQL server listens on a single network socket for TCP/IP connections. This socket is bound to a single address, but it is possible for an address to map onto multiple network interfaces. The default address is 0.0.0.0. To specify an address explicitly, use the ?bind-address=addr option at server startup, where addr is an IPv4 address or a host name. If addr is a host name, the server resolves the name to an IPv4 address and binds to that address. The server treats different types of addresses as follows:

If the address is 0.0.0.0, the server accepts TCP/IP connections on all server host IPv4 interfaces.
If the address is a “regular” IPv4 address (such as 127.0.0.1), the server accepts TCP/IP connections only for that particular IPv4 address.

但是有此需求,就会到访问控制,那么使用防火墙iptables可实现此效果

mysql-server为192.168.1.3,只允许192.168.1.4,  192.168.1.5,  192.168.1.6来访问3306端口

在my.cnf中

bind-address = 0.0.0.0

在访问3306端口的主机中,只允许192.168.1.4-6,其他ip一律DROP掉
/sbin/iptables -A INPUT -p tcp -s 192.168.1.4 --dport 3306 -j ACCEPT /sbin/iptables -A INPUT -p tcp -s 192.168.1.5 --dport 3306 -j ACCEPT /sbin/iptables -A INPUT -p tcp -s 192.168.1.6 --dport 3306 -j ACCEPT /sbin/iptables -A INPUT -p tcp --dport 3306 -j DROP


/sbin/iptables -A INPUT -p tcp --dport 3306 ! -s 192.168.1.4 -j DROP /sbin/iptables -A INPUT -p tcp --dport 3306 ! -s 192.168.1.5 -j DROP /sbin/iptables -A INPUT -p tcp --dport 3306 ! -s 192.168.1.6 -j DROP

保存防火墙规则
service iptables save

查看INPUT链包含3306的规则
echo -e "target prot opt source destination\n$(iptables -L INPUT -n | grep 3306)"

这样就实现了mysql只允许指定ip访问。

总结

虽然mysql没有直接绑定多个ip访问的,但是我们可以通过防火墙iptables可实现,也是一个不错的办法。

打赏

打赏

取消

感谢您的支持,我会继续努力的!

扫码支持
扫码打赏,你说多少就多少

打开支付宝扫一扫,即可进行扫码打赏哦

百分百源码网 建议打赏1~10元,土豪随意,感谢您的阅读!

共有3人阅读,期待你的评论!发表评论
昵称: 网址: 验证码: 点击我更换图片
最新评论

本文标签

广告赞助

能出一分力是一分吧!

订阅获得更多模板

本文标签

广告赞助

订阅获得更多模板