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
On some systems the service is called mysqld:
If the service is stopped, start it and retry:
Then test a direct client connection:
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:
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 look at the MySQL configuration, usually my.cnf, mysqld.cnf or a file under conf.d:
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:
Open the firewall and cloud network rules
A correctly running server is still unreachable if the host firewall blocks port 3306.
For ufw:
For firewalld:
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:
Allow a user from a specific network:
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
localhostwhen 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.
Related reading
- Can't connect to RDS instance from EC2 instance
- Can't connect to RDS PostgreSQL DB instance through an RDS proxy
- Can't create backup mongodump with --db. Authentication failed
- Can't delete from table after switch from logical to streaming replication
- Can't find documents searching by ObjectId using Mongoose
- Can't find the 'libpq-fe.h header when trying to install pg gem
- Can't launch ClickHouse. Exit code 203
- Can't set JPA naming strategy after configuring multiple data sources Spring 1.4.1 / Hibernate 5.x
System design interview problems on this topic
- Design a Distributed OLTP Database: solution and walkthrough
- Design a Wide Column Database: solution and walkthrough
- Design a Key Value Store: solution and walkthrough
- Design a Web Cache: solution and walkthrough

System Design Fundamentals
Build a strong foundation in designing scalable, reliable distributed systems.
View the courseSystem Design practice on Codemia
Work through 120+ system design problems with detailed solutions, from rate limiters to multi-region storage.