Can't connect to MySQL server

Can’t Connect to MySQL Server Remotely on Ubuntu

Last updated on

MySQL by default will not allow incoming remote connections for security reasons. In this article we will edit the MySQL config file mysqld.cnf to allow remote connections.

Testing a Remote MySQL Connection

If you are seeing a different error “Access denied for user (using password: YES)”, please see: MySQL Rejecting Correct Password

To test a remote MySQL connection in Linux terminal, simply replace username and hostname_or_ip with your own.

mysql -u username -h hostname_or_ip -p

Or via Command Line in Windows:

telnet 192.16.8.1.22 3306

If you are getting an error when trying to connect to your remote MySQL server

ERROR 2003 (HY000): Can't connect to MySQL server on '192.16.8.1.22' (111)

or in Windows Telnet

Connecting To 192.16.8.1.22...Could not open connection to the host, on port 3306: Connect failed

1. Edit MySQL config

You may need to comment out bind-address in the MySQL config file mysqld.cnf.

Open mysqld.cnf with nano editor.

sudo nano /etc/mysql/mysql.conf.d/mysqld.cnf

Press CTRL + W and search for bind-address

/etc/mysql/mysql.conf.d/mysqld.cnf
# Instead of skip-networking the default is now to listen only on
# localhost which is more compatible and is not less secure.
bind-address = 127.0.0.1

The above line is telling MySQL to only accept local connections (127.0.0.1 / localhost).

If you know the IP that you are trying to remotely connect from, you should enter it here to restrict remote connections to that IP.

If you have to allow all IP addresses to connect, you can comment out the line by adding # before it so it looks like:

/etc/mysql/mysql.conf.d/mysqld.cnf
# Instead of skip-networking the default is now to listen only on
# localhost which is more compatible and is not less secure.
# bind-address = 127.0.0.1

Note: It is not good security practice to leave your MySQL server accessible to any IP, so consider this a temporary measure. You can also set the host value for your MySQL user to only accept connections from a specific IP. Please see: Understanding MySQL Users and Hosts

Restart the MySQL service for changes to take effect.

sudo service mysql restart

2. Check Firewall

If you still can’t connect, check if there is a firewall configured on your server. The most common firewall for Ubuntu server is ufw.

Check if ufw is enabled:

sudo ufw status

If it’s enabled, you should see some rules:

To                         Action      From
--                         ------      ----
OpenSSH                    ALLOW       Anywhere
Apache Full                ALLOW       Anywhere
OpenSSH (v6)               ALLOW       Anywhere (v6)
Apache Full (v6)           ALLOW       Anywhere (v6)

If MySQL is not listed, add a rule for it.

sudo ufw allow mysql

There should be a rule for MySQL:

To                         Action      From
--                         ------      ----
OpenSSH                    ALLOW       Anywhere
Apache Full                ALLOW       Anywhere
3306                       ALLOW       Anywhere
OpenSSH (v6)               ALLOW       Anywhere (v6)
Apache Full (v6)           ALLOW       Anywhere (v6)
3306 (v6)                  ALLOW       Anywhere (v6)

Restart the ufw service:

sudo service ufw restart
1 Star2 Stars3 Stars4 Stars5 Stars 4.57 (14 votes)

Let me know if this helped. Follow me on Twitter, Facebook and YouTube, or 🍊 buy me a smoothie.

p.s. I increased my AdSense revenue by 68% using AI 🤖. Read my Ezoic review to find out how.

8 replies

avatar
  Subscribe  
newest oldest
Notify of
Arhakim
Guest
Arhakim
Arhakim
1 year ago

Awesome! It’s work.

Thank you!

Lakshya Singhal
Guest
Lakshya Singhal
Lakshya Singhal
1 year ago

Implemented the above steps and the problem was resolved immediately. Thank you so much for the guidance.

Alexander Suvorov
Guest
Alexander Suvorov
Alexander Suvorov
1 year ago

Thanks. Many “How-To’s” on the web don’t mention the default IP binding thing in MySQL.

MANOEL ALVES BASTOS
Guest
MANOEL ALVES BASTOS
MANOEL ALVES BASTOS
1 year ago

Thanks for all…
Works Fine!!!

artem
Guest
artem
artem
1 year ago

Still get this
Can’t connect to MySQLServer on ip adress(10600)

anan
Guest
anan
anan
1 year ago

it’s working now ,thanks your share

pablo
Guest
pablo
pablo
2 years ago

muy bueno gracias