Question

MySQL remote connection iptables issue

Hello,

I’m trying top setup following droplet configuration

droplet1 - webserver, public access droplet2 - db server, no apache, no php, no public access

I have following iptables settings on my db server droplet:

sudo iptables -F
sudo iptables -A INPUT -i lo -j ACCEPT
sudo iptables -I INPUT -m state --state ESTABLISHED,RELATED -j ACCEPT
sudo iptables -A INPUT -p tcp -s [home_ip_address] -m tcp --dport 22 -j ACCEPT
sudo iptables -A INPUT -p tcp -s [droplet_1_private_IP] --sport 3306 -j ACCEPT
sudo iptables -A OUTPUT -p tcp -d [droplet_1_private_IP] --dport 3306 -j ACCEPT
sudo iptables -P OUTPUT ACCEPT
sudo iptables -P INPUT DROP
sudo iptables -P FORWARD DROP

When I try to connect from droplet1 with following command

mysql -h [droplet2_private_IP] -u [dbuser] -p

I confirm the password and nothing happens, connection freezes.

If I stop **iptables ** service on DB server everything is ok, but when iptables works I’m not allowed to connect to DB server.

What’s wrong with iptables on db server droplet?

Thank you in advance


Submit an answer


This textbox defaults to using Markdown to format your answer.

You can type !ref in this text area to quickly search our full set of tutorials, documentation & marketplace offerings and insert the link!

Sign In or Sign Up to Answer

These answers are provided by our Community. If you find them useful, show some love by clicking the heart. If you run into issues leave a comment, or add your own answer to help others.

Ryan Quinn
DigitalOcean Employee
DigitalOcean Employee badge
June 23, 2016
Accepted Answer

By default, MySQL does not listen for remote connections. In order to enable this you need to modify your /etc/mysql/my.cnf file so that bind_address reads

bind-address        =  private_ip

I met the same issue here, after run command:

iptables -A INPUT -i eth0 -p tcp -m tcp --dport 3306 -j ACCEPT service iptables save

still get response: ERROR 2003 (HY000): Can’t connect to MySQL server on ‘...’ (113)

After the stop the iptabels, issue disappeared.

Try DigitalOcean for free

Click below to sign up and get $200 of credit to try our products over 60 days!

Sign up

Become a contributor for community

Get paid to write technical tutorials and select a tech-focused charity to receive a matching donation.

DigitalOcean Documentation

Full documentation for every DigitalOcean product.

Resources for startups and SMBs

The Wave has everything you need to know about building a business, from raising funding to marketing your product.

Get our newsletter

Stay up to date by signing up for DigitalOcean’s Infrastructure as a Newsletter.

New accounts only. By submitting your email you agree to our Privacy Policy

The developer cloud

Scale up as you grow — whether you're running one virtual machine or ten thousand.

Get started for free

Sign up and get $200 in credit for your first 60 days with DigitalOcean.*

*This promotional offer applies to new accounts only.