Can't connect to MySQL server

Can’t connect to MySQL server remotely on Ubuntu

Last updated on

To test a remote MySQL connection in Linux terminal:

mysql -u username -h 192.16.8.1.22 -p

Or via Telnet 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 Telnet

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

1. Check Firewall

Firstly, if ufw firewall is enabled, ensure you have a rule for MySQL

sudo ufw allow mysql

Restart the service

sudo service ufw restart

2. Check MySQL config

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

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

Find the line bind-address  (CTRL + W to search)

/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. Comment out this line by adding # before it.

Restart the MySQL service for changes to take effect.

sudo service mysql restart

Let me know in the comments if this helped. Follow me on Twitter, Facebook and YouTube.

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

1 Star2 Stars3 Stars4 Stars5 Stars 4.67 (9 votes)

Leave a Reply

Your email address will not be published. Required fields are marked *

We use Markdown to style comments, like on Github and Reddit.
To do a line break, type two spaces after the sentence.
You can add inline code by wrapping it in backticks: `code here`

    To do an entire block of code  
    type four spaces before the line
    and it will appear in a block like this.
    <-- four empty spaces

7 replies

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