1

I have issue that I cannot reach the 3306 port (mysql) even if I set it in iptables. How can I resolve this issue?

root@vps191532:# iptables-save
# Generated by iptables-save v1.4.21 on Thu Oct 22 20:42:38 2015
*filter
:INPUT ACCEPT [695:36753]
:FORWARD ACCEPT [0:0]
:OUTPUT ACCEPT [469:37083]
-A INPUT -p tcp -m tcp --dport 3306 -m state --state NEW,ESTABLISHED -j ACCEPT
-A OUTPUT -p tcp -m tcp --sport 3306 -m state --state ESTABLISHED -j ACCEPT
COMMIT
# Completed on Thu Oct 22 20:42:38 2015


root@vps191532:# netstat -lnpa | grep mysql
tcp        0      0 127.0.0.1:3306          0.0.0.0:*               LISTEN      8960/mysqld
unix  2      [ ACC ]     STREAM     LISTENING     42152    8960/mysqld         /var/run/mysqld/mysqld.sock


C:\Users>telnet 149.XXX.51.XXX 3306
Connecting To 149.XXX.51.XXX...Could not open connection to the host, on port 3306: Connect failed

root@vps191532:# iptables -L
Chain INPUT (policy ACCEPT)
target     prot opt source               destination
ACCEPT     tcp  --  anywhere             anywhere             tcp dpt:mysql state NEW,ESTABLISHED

Chain FORWARD (policy ACCEPT)
target     prot opt source               destination

Chain OUTPUT (policy ACCEPT)
target     prot opt source               destination
ACCEPT     tcp  --  anywhere             anywhere             tcp spt:mysql state ESTABLISHED
WinterTime
  • 13
  • 1
  • 3

1 Answers1

2

The problem is that your MySQL install is listening only for connections on the 127.0.0.1 address (also known as a very cozy place called localhost). Basically, just edit the /etc/mysql/my.cnf file and find the line:

bind-address = 127.0.0.1

Just change it to:

bind-address = 0.0.0.0

And restart your MySQL service with:

service mysql restart

Just remember that MySQL will start listening in all addresses, so just adjust it to suit your needs, and add firewall rules to block unwanted requests...

Cya!

Stefano Martins
  • 1,221
  • 8
  • 10
  • Thanks! Is it safe to open the MySQL for remote connection? Or it is better to use any MySQL web tool for example phpmyadmin etc.? – WinterTime Oct 22 '15 at 19:09
  • Not really. You have two possible approaches: 1) Open it to direct connections from everywhere or; 2) Install phpMyAdmin to manage it remotely. In the first scenario, you'll have the advantage of connect a application directly to the MySQL server, but you're gonna have to create firewall rules to control which host can connect to it. I guess the second one is more your case, right? – Stefano Martins Oct 22 '15 at 19:18