返回博客

Linux 配置数据库远程连接:Iptables 开启 3306 端口

本文介绍如何在 Linux 系统上配置数据库远程连接,并使用 Iptables 开启 3306 端口,解决 Oneinstack 环境下无法远程连接数据库的问题。

Mt.r
|

我服务器用的 Oneinstack 配置的环境,但是无法远程连接数据库,排查了一下是由于防火墙开启,3306 端口不通导致的

防火墙开启 3306 端口

Centos

iptables -I INPUT 4 -p tcp -m state --state NEW -m tcp --dport 3306 -j ACCEPT
service iptables save #保存 iptables 规则

Ubuntu/Debian

iptables -I INPUT 4 -p tcp -m state --state NEW -m tcp --dport 3306 -j ACCEPT
iptables-save > /etc/iptables.up.rules  #保存 iptables 规则

数据库授权

# mysql -uroot -p
MySQL [(none)]> grant all privileges on db_name.* to db_user@'%' identified by 'db_pass'; #授权语句,特别注意有分号
MySQL [(none)]> flush privileges;
MySQL [(none)]> exit; #退出数据库控制台,特别注意有分号

后记

参考文章 - 如何配置数据库远程连接