当前位置:数据库 > > 正文

mysql并发控制的重要方式(Mysql服务添加 iptables防火墙策略的方案)

时间:2021-10-11 00:18:18类别:数据库

mysql并发控制的重要方式

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

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

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

停止Centos7防火墙

查看防火墙运行状态

  • ?
  • 1
  • 2
  • [root@mysql ~]# firewall-cmd --state
  • running
  • 停止防火墙服务

  • ?
  • 1
  • [root@mysql ~]# systemctl stop firewalld.service
  • 启动Centos7防火墙

    查看防火墙运行状态

  • ?
  • 1
  • 2
  • [root@mysql ~]# firewall-cmd --state
  • not running
  • 启动防火墙服务

  • ?
  • 1
  • [root@mysql ~]# systemctl start firewalld.service
  • 配置防火墙开机启动

  • ?
  • 1
  • [root@mysql ~]# systemctl enable firewalld.service
  • 访问Mysql服务测试
    连接Mysql服务

  • ?
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • [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服务访问策略

    查看防火墙策略

  • ?
  • 1
  • [root@mysql ~]# iptables -L -n --line-number|grep 3306
  • 由于在防火墙中没有添加3306端口的访问策略,外部应用是无法mysql服务的。

  • ?
  • 1
  • 2
  • 3
  • [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端口访问策略

  • ?
  • 1
  • 2
  • 3
  • # 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端口。

    删除防火墙策略

  • ?
  • 1
  • 2
  • [root@mysql ~]# iptables -D INPUT 1
  • [root@mysql ~]# iptables -L -n --line-number|grep 3306
  • 到此这篇关于给Mysql服务添加 iptables防火墙策略的文章就介绍到这了,更多相关Mysql服务添加 iptables防火墙内容请搜索开心学习网以前的文章或继续浏览下面的相关文章希望大家以后多多支持开心学习网!

    原文链接:https://blog.csdn.net/qq_40907977/article/details/114671598

    上一篇下一篇

    猜您喜欢

    热门推荐