MySQL & MariaDB error 2003: Can't connect to MySQL server on host
Can't connect to MySQL server on '127.0.0.1:3307' (111) (8.0.46) A TCP connection to the server failed. Nothing is listening on that host and port, or a firewall, bind-address or wrong port is in the way.
| MySQL | MariaDB | |
|---|---|---|
| Symbol | CR_CONN_HOST_ERROR | CR_CONN_HOST_ERROR |
| SQLSTATE | HY000 | HY000 |
| Type | Client library error | Client library error |
| Versions | every release since 5.0 active | since 10.2.44 active |
What it means
The client opened a TCP connection to host:port and the operating system refused it or it timed out. Like 2002, it is raised by the client before the server is involved. MariaDB’s client never reports 2003: its client library (Connector/C) returns 2002 for refused TCP connections too, so on MariaDB a “2002” can be a TCP problem, not a socket problem. Our tests confirm this on MariaDB 10.6 to 11.8. The trailing number is the OS error: (111) / (10061) connection refused (nothing listening), (110) / (10060) timed out (usually a firewall), (113) no route to host.
Why it happens
- The server is stopped, or listens on a different port (
SELECT @@port). - The server only listens on localhost:
bind-address = 127.0.0.1(the default in many packages) orskip-networking, so remote clients are refused. - A firewall or cloud security group blocks port 3306.
- Docker: the port isn’t published, or the app uses
localhostinstead of the database container’s service name.
How to fix it
- On the server: is it running, and on which address and port?
ss -ltnp | grep mysqldandSELECT @@port, @@bind_address;. - To accept remote connections, set
bind-address = 0.0.0.0(or the server’s IP) inmy.cnfand restart, then make sure an account exists for the remote host (otherwise you will see 1045 or 1130). - Open the port in the firewall or security group, ideally only for the application servers.
- Test from the client machine:
nc -vz dbhost 3306. If that fails, the problem is the network, not MySQL.
Reproduce it yourself
These steps are the exact test our harness runs. Use a scratch server, never production.
Connect with
mysql -u root -pand create a scratch database:CREATE DATABASE demo; USE demo;In a shell on the database host:
mysql -uroot -p -h127.0.0.1 -P3307 -e 'SELECT 1'
You get: ERROR 2003 (HY000): Can't connect to MySQL server on '127.0.0.1:3307' (111)(9.7.4)
Verified fixes
Check the port the server really listens on, then connect to it
On the server, check the port and address it listens on:
SELECT @@port, @@bind_address;Then connect to that port (and check a firewall isn't in the way):
mysql -u root -p -h 127.0.0.1 -P 3306 nc -vz dbhost 3306
Tested on
| Step | MySQL | MariaDB | |||||||
|---|---|---|---|---|---|---|---|---|---|
| 5.6 | 5.7 | 8.0 | 8.4 | 9 | 10.6 | 10.11 | 11.4 | 11.8 | |
| Connect over TCP to a port where no server is listening (MariaDB's client reports this as 2002) repro | ✓ 2003 | ✓ 2003 | ✓ 2003 | ✓ 2003 | ✓ 2003 | ✓ 2002 | ✓ 2002 | ✓ 2002 | ✓ 2002 |
| Check the port the server really listens on, then connect to it fix | ✓ | ✓ | ✓ | ✓ | ✓ | ✓ | ✓ | ✓ | ✓ |
Related errors
How the message changed between versions
| MySQL from | Symbol | Message |
|---|---|---|
| 5.0.96 | CR_CONN_HOST_ERROR | Keine Verbindung zu MySQL Server auf %-.100s (%d) |
| 5.5.63 | CR_CONN_HOST_ERROR | Can't connect to MySQL server on '%-.100s' (%d) |
| 8.0.26 | CR_CONN_HOST_ERROR | Can't connect to MySQL server on '%-.100s:%u' (%d) |