Mysql服务添加 iptables防火墙策略的方案

作者:寰宇001 时间:2024-01-21 14:44:34 

如果你的Mysql数据库安装在centos7的系统上,并且你的操作系统启用了防火墙。应用要访问mysql数据库,你有2个解决方案。

方案一:停止防火墙服务
方案二:在防火墙中添加策略,让应用能正常访问mysql服务端口

停止Centos7防火墙

查看防火墙运行状态


[root@mysql ~]# firewall-cmd --state
running

停止防火墙服务


[root@mysql ~]# systemctl stop firewalld.service

启动Centos7防火墙

查看防火墙运行状态


[root@mysql ~]# firewall-cmd --state
not running

启动防火墙服务


[root@mysql ~]# systemctl start firewalld.service

配置防火墙开机启动


[root@mysql ~]# systemctl enable firewalld.service

访问Mysql服务测试
连接Mysql服务


[mysql@mysql ~]$ mysql -utony -ptony -h 192.168.112.131 -P 3306
mysql: [Warning] Using a password on the command line interface can be insecure.
ERROR 2003 (HY000): Can't connect to MySQL server on '192.168.112.131' (113)

主从复制连接测试[root@localhost] 15:23:46 [(none)]>show slave status\G;
*************************** 1. row ***************************
              Slave_IO_State: Connecting to master
                 Master_Host: 192.168.112.131
                 Master_User: repl
                 Master_Port: 3306
               Connect_Retry: 60
             Master_Log_File: binlog.000034
         Read_Master_Log_Pos: 194
              Relay_Log_File: mysql-relay-bin.000007
               Relay_Log_Pos: 401
       Relay_Master_Log_File: binlog.000034
            Slave_IO_Running: Connecting
           Slave_SQL_Running: Yes
          .....
Master_SSL_Verify_Server_Cert: No
               Last_IO_Errno: 2003
               Last_IO_Error: error connecting to master 'repl@192.168.112.131:3306' - retry-time: 60  retries: 1
              Last_SQL_Errno: 0

主从的IO线程已断开,并且报2003错误,这里就确认是网络不通,访问不了主库的服务。

在防火墙中添加Mysql服务访问策略

查看防火墙策略


[root@mysql ~]# iptables -L -n --line-number|grep 3306

由于在防火墙中没有添加3306端口的访问策略,外部应用是无法mysql服务的。


[mysql@mysql ~]$ mysql -utony -ptony -h 192.168.112.131 -P 3306
mysql: [Warning] Using a password on the command line interface can be insecure.
ERROR 2003 (HY000): Can't connect to MySQL server on '192.168.112.131' (113)

添加3306端口访问策略


# iptables -I INPUT -p tcp -m state --state NEW -m tcp --dport 3306 -j ACCEPT
[root@mysql ~]# iptables -L -n --line-number|grep 3306
1    ACCEPT     tcp  --  0.0.0.0/0            0.0.0.0/0            state NEW tcp dpt:3306

可以看到已经添加了3306端口的访问策略,外部应用可以通过TCP协议访问3306端口。

删除防火墙策略


[root@mysql ~]# iptables -D INPUT 1
[root@mysql ~]# iptables -L -n --line-number|grep 3306

来源:https://blog.csdn.net/qq_40907977/article/details/114671598

标签:Mysql,iptables,防火墙
0
投稿

猜你喜欢

  • py中的目录与文件判别代码

    2023-06-01 03:32:06
  • 如何解决django配置settings时遇到Could not import settings 'conf.local'

    2021-07-23 13:31:57
  • Mysql使用Describe命令判断字段是否存在

    2011-04-25 18:27:00
  • 用色彩打造专业的视觉效果

    2010-09-25 19:04:00
  • 全面理解JavaScript中的闭包

    2024-04-17 10:09:24
  • 用python编写一个图片拼接工具

    2023-09-01 18:31:52
  • 详解Python下ftp上传文件linux服务器

    2023-12-31 19:02:37
  • dedecms 批量提取第一张图片最为缩略图的代码(文章+软件)

    2024-04-28 09:45:40
  • Python通过Manager方式实现多个无关联进程共享数据的实现

    2021-12-27 04:03:17
  • 如何利用Python写猜数字和字母的游戏

    2021-01-28 00:00:09
  • Python简单实现的代理服务器端口映射功能示例

    2022-09-10 07:52:54
  • python3调用c语言代码的全过程记录

    2022-12-06 05:00:24
  • python中format的用法实例详解

    2023-08-03 12:27:28
  • 详解JavaScript中的作用域链与闭包

    2024-04-22 13:07:00
  • Go打包二进制文件的实现

    2024-05-08 10:15:19
  • 基于Php mysql存储过程的详解

    2024-06-05 09:22:00
  • python-json校验-jsonpath解析

    2023-11-20 11:40:01
  • nodejs利用ajax实现网页无刷新上传图片实例代码

    2024-05-13 10:05:11
  • ASP.NET中URL Routing和IIS上URL Rewriting的区别

    2024-06-05 09:25:27
  • javascript 常用功能总结

    2023-07-02 03:50:17
  • asp之家 网络编程 m.aspxhome.com