Can't connect to MySQL server error 111
Master System Design with Codemia
Enhance your system design skills with over 120 practice problems, detailed solutions, and hands-on exercises.
Introduction
Can't connect to MySQL server with error 111 usually means the client attempted a TCP connection and the target refused it. In Linux environments this often maps to "connection refused," which points to a server process that is down, listening on the wrong interface or port, or blocked by network rules.
Core Sections
Start With the Simplest Check
Before digging into grants or application code, verify that MySQL is actually running.
On some systems the service name is mysqld instead:
If the service is stopped, start it and retry the connection.
You can then test a direct client connection:
Using 127.0.0.1 forces TCP. That matters because 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 whether it is listening on the address and port you expect.
Typical outputs tell you a lot:
- '
127.0.0.1:3306means local TCP connections work, but remote hosts cannot connect.' - '
0.0.0.0:3306means the server is listening on all IPv4 interfaces.' - no output often means MySQL is not listening on TCP at all.
Then inspect the MySQL configuration, often in my.cnf, mysqld.cnf, or a file under conf.d.
If bind-address is 127.0.0.1, remote connections are intentionally blocked. For remote access, administrators often change it to 0.0.0.0 or to a specific private IP, then restart MySQL.
Check Firewall and Network Policy
Even a correctly running server can be unreachable if the host firewall blocks port 3306.
For ufw:
For firewalld:
In cloud environments, remember that security groups or VPC firewall rules can block the same port even if the machine-level firewall is open.
Verify User Grants for Remote Access
A refused connection and an authentication error are not the same thing, but grant configuration still matters once the port is reachable. If the user only exists as 'appuser'@'localhost', remote login from another host will fail even when the server is up.
You can inspect accounts from a privileged MySQL session:
To permit a user from a specific network:
Use the narrowest host pattern you can. Opening MySQL to every address is usually unnecessary and risky.
Application Connection String Problems
Sometimes MySQL is healthy and the real problem is the client configuration. Common mistakes include:
- wrong hostname
- wrong port
- using
localhostwhen the application expects TCP - containerized app trying to connect to itself instead of the database service
For example, inside Docker Compose, localhost usually points to the application container, not the MySQL container. The correct host is often the service name, such as db.
Common Pitfalls
- Treating error
111like an authentication failure instead of a TCP connectivity problem. - Testing with
localhostand forgetting that it may use a Unix socket rather than TCP. - Updating MySQL
bind-addresswithout also checking host firewalls or cloud security rules. - Exposing port
3306publicly when the database should really stay on a private network. - Blaming application code before confirming that the server is actually running and listening on the expected port.
Summary
- Error
111usually means the TCP connection was refused. - Check whether MySQL is running before changing application code.
- Verify the listening address and port with
ssor a similar tool. - Review firewalls, cloud security rules, and MySQL
bind-addresstogether. - Once connectivity works, confirm that the MySQL user is allowed from the connecting host.

