Dabbling with iptables to ensure remote MySQL connection works properly

iptables, mysql

I found in a cluster of machines that I couldn’t access MySQL remotely anymore. After some playing around with GRANT PRIVILEGES in MySQL, I realised that the issue wasn’t actually because of the MySQL settings at all.

So, was it the port number that I was connecting to that happened to be wrong?

netstat -tln | grep 3306

Showed that MySQL is running on port 3306 as intended.

telnet xx.xx.xx.xx 3306

From machine A to machine B refused to accept connections, so I knew that we had an issue somewhere else – probably on the firewall – stopping me from connecting remotely.

I didn’t really have much experience with iptables, so had to read a little bit more into it. Firstly, I needed to display all the rules on the firewall with this command:

/sbin/iptables -L -v -n

…under the Chain RH-Firewall-1-INPUT I saw no mention of the open port 3306 for MySQL.  We needed to add two rules, for tcp and udp connections.

/sbin/iptables -I RH-Firewall-1-INPUT -p tcp –dport 3306 -j ACCEPT

/sbin/iptables -I RH-Firewall-1-INPUT -p udp –dport 3306 -j ACCEPT

Afterwards, I was able to connect remotely to the MySQL machine again.