centos7+MariaDB 针对特定ip开启远程数据库连接

前端之家收集整理的这篇文章主要介绍了centos7+MariaDB 针对特定ip开启远程数据库连接前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。

前提

公司数据库没有版本控制,这个就相当坑爹了,问以前的技术,就是在本地建一个表,然后导入到线上,WTF。目前就只能临时开启远程连接数据了,后面在使用版本控制

防火墙

centos7 之前的防火墙是不一样的,比如你要添加3306端口:

  1. ## 全部
  2. iptables -A INPUT -p tcp -m tcp --dport 3306 -j ACCEPT
  3.  
  4. ## 部分ipiptables
  5. iptables -A INPUT -p tcp -s 138.111.21.11 -dport 3306 -j ACCEP
  6.  
  7. service iptables save
  8. service iptables restart
  9.  
  10. ## 查看 iptables
  11. iptables -L -n

但这个在centos7 就不好使,查看文档才知道centos7 使用了增强版firewall

  1. firewall-cmd --zone=public --permanent --add-port=3306/tcp
  2. 1firwall-cmd:是Linux提供的操作firewall的一个工具;
  3. 2、--permanent:表示设置为持久;
  4. 3、--add-port:标识添加的端口;
  5. 4、--zone=public:指定的zonepublic

当然如果不太习惯使用命令,我们可以直接改配置文件

进入etc/firewalld/zone中,修改public.xml

  1. <?xml version="1.0" encoding="utf-8"?>
  2. <zone>
  3. <short>Public</short>
  4. <description>For use in public areas.</description>
  5. <rule family="ipv4">
  6. <source address="122.10.70.234"/>
  7. <port protocol="udp" port="514"/>
  8. <accept/>
  9. </rule>
  10. <rule family="ipv4">
  11. <source address="123.60.255.14"/>
  12. <port protocol="tcp" port="10050-10051"/>
  13. <accept/>
  14. </rule>
  15. <rule family="ipv4">
  16. <source address="192.249.87.114"/> 放通指定ip,指定端口、协议
  17. <port protocol="tcp" port="80"/>
  18. <accept/>
  19. </rule>
  20. <rule family="ipv4"> 放通任意ip访问服务器的9527端口
  21. <port protocol="tcp" port="9527"/>
  22. <accept/>
  23. </rule>
  24. </zone>

上述配置文件可以看出:

  1. 1添加需要的规则,开放通源ip122.10.70.234,端口514,协议tcp
  2. 2、开放通源ip123.60.255.14,端口10050-10051,协议tcp;/3、开放通源ip为任意,端口9527,协议

firewall 常用命令

  1. # 重启
  2. service firewalld restart
  3.  
  4. # 开启
  5. service firewalld start
  6.  
  7. # 关闭
  8. service firewalld stop
  9.  
  10. # 查看firewall 服务状态
  11. systemctl status firewall
  12.  
  13. # 查看防火墙
  14. firewall-cmd --list-all

开启服务器3306对外开放后,还需要设置数据库用户授权

MariaDB 开启远程连接

数据库MysqL 中的user表中可以看到默认是只能本地连接的,所有可以添加一个用户

  1. # 针对ip
  2. create user 'root'@'192.168.10.10' identified by 'password';
  3.  
  4. #全部
  5. create user 'root'@'%' identified by 'password';

建议还是针对于ip开放吧,不要全部开放

授权用户

  1. # 给用户最大权限
  2. grant all privileges on *.* to 'root'@'%' identified by 'password';
  3.  
  4. # 给部分权限(test 数据库)
  5.  
  6. grant all privileges on test.* to 'root'@'%' identified by 'password' with grant option;
  7.  
  8. # 刷新权限表
  9. flush privileges;
  10.  
  11. # show grants for 'root'@'localhost';

接下来就是可以本地连接了

参考文章

  • 防火墙:

    1. http://blog.csdn.net/xlgen157387/article/details/52672988
    2.  
    3. http://blog.sina.com.cn/s/blog_4c197d4201017rgl.html
  • 用户授权:

    1. http://www.cnblogs.com/xujishou/p/6306765.html

猜你在找的CentOS相关文章