MySQL
Database Error
Connection Issue
Error 111
Server Connection

Can't connect to MySQL server (error 111): connection refused fixes

Codemia Knowledge Hub ·

Can't connect to MySQL server with error 111 means the client tried to open a TCP connection and the target refused it ("connection refused" on Linux). The cause is almost always a MySQL server that is down, listening on the wrong interface or port, or blocked by network rules. It is a connectivity problem, not an authentication failure, so check the server before touching application code.

Check that MySQL is running

bash
sudo systemctl status mysql

On some systems the service is called mysqld:

bash
sudo systemctl status mysqld

If the service is stopped, start it and retry:

bash
sudo systemctl start mysql

Then test a direct client connection:

bash
mysql -h 127.0.0.1 -P 3306 -u root -p

Using 127.0.0.1 forces TCP. localhost may use a Unix socket instead, which can hide a TCP-specific problem.

Confirm the port and listening address

If MySQL is running but still refusing connections, check which address and port it listens on:

bash
sudo ss -ltnp | grep 3306
  • 127.0.0.1:3306 means local TCP connections work, but remote hosts cannot connect.
  • 0.0.0.0:3306 means the server is listening on all IPv4 interfaces.
  • no output often means MySQL is not listening on TCP at all.

Then look at the MySQL configuration, usually my.cnf, mysqld.cnf or a file under conf.d:

ini
[mysqld]
port = 3306
bind-address = 127.0.0.1

With bind-address = 127.0.0.1, remote connections are blocked on purpose. For remote access, administrators often change it to 0.0.0.0 or to a specific private IP, then restart MySQL:

bash
sudo systemctl restart mysql

Open the firewall and cloud network rules

A correctly running server is still unreachable if the host firewall blocks port 3306.

For ufw:

bash
sudo ufw status
sudo ufw allow 3306/tcp

For firewalld:

bash
sudo firewall-cmd --list-ports
sudo firewall-cmd --permanent --add-port=3306/tcp
sudo firewall-cmd --reload

In the cloud, security groups or VPC firewall rules can block the same port even when the machine firewall is open, so check them whenever you change bind-address. Avoid exposing 3306 publicly when the database should stay on a private network.

Allow the user from the connecting host

Once the port is reachable, grants decide who can log in. If the user only exists as 'appuser'@'localhost', logging in from another host fails even though the server is up.

List accounts from a privileged session:

sql
SELECT user, host
FROM mysql.user
ORDER BY user, host;

Allow a user from a specific network:

sql
CREATE USER 'appuser'@'10.0.0.%' IDENTIFIED BY 'strong-password';
GRANT ALL PRIVILEGES ON appdb.* TO 'appuser'@'10.0.0.%';
FLUSH PRIVILEGES;

Use the narrowest host pattern you can. Opening MySQL to every address is usually unnecessary and risky.

Fix the application's connection settings

Sometimes MySQL is healthy and the client configuration is wrong:

  • wrong hostname
  • wrong port
  • using localhost when the application expects TCP
  • containerized app trying to connect to itself instead of the database service

In Docker Compose, for example, localhost usually points to the application container, not the MySQL container. The correct host is often the service name, such as db.


Course
Beginner
27 lessons
10 hours
System Design Fundamentals

Build a strong foundation in designing scalable, reliable distributed systems.

View the course
System Design practice on Codemia

Work through 120+ system design problems with detailed solutions, from rate limiters to multi-region storage.

Practice system design