Maaz Zindani

Loading the index…

↑↓ move↵ openesc close4072 pages
Databases and Redis

MySqlException: unable to connect to any of the specified MySQL hosts

ErrorUnable to connect to any of the specified MySQL hosts

Short answer

The connector could not open a TCP connection to the host and port in your connection string. Check, in order: the server is running, the port is right, networking is not disabled, the server is not bound to loopback only, and no firewall blocks the port. Test with the mysql tools first.

  • Applies to MySQL 8.4, Connector/NET 8.0, MySqlConnector 2.x, .NET 8
  • Updated
  • By Maaz Zindani, Senior Software Engineer

Fix it fast

  1. 1Find out which cause you have

    Take the .NET app out of the picture. Test the same host and port with the MySQL command-line tool.

    mysqladmin -h 127.0.0.1 --port=3306 version
  2. 2Match what you see, then jump to its fix

    1. 1mysqladmin reports a refused connection, and no mysqld process existsThe MySQL server is not runningFix
    2. 2The server runs, but only a different port answersThe connection string uses another host or port than the serverFix
    3. 3Connecting from the same machine works, connecting from another machine is refusedNetworking is off or the server listens on loopback onlyFix
    4. 4The server runs and listens on the right address, but remote connections time out or are refusedA firewall blocks the MySQL portFix

What does the error look like?

  • Unable to connect to any of the specified MySQL hosts
  • Can't connect to MySQL server on 'host' (10061)

Cause 1 of 4

The MySQL server is not running

1Check

The MySQL manual says to look for a mysqld process on the server host (ps on Unix, Task Manager on Windows).

ps xa | grep mysqld

2Fix

Start the server, then repeat the first step.

# start the MySQL service with your operating system's service manager,
# then run the check above again

3Verify

mysqladmin prints the server version.

mysqladmin -h 127.0.0.1 version

Confirmed by dev.mysql.com

Did this fix it?

Cause 2 of 4

The connection string uses another host or port than the server

1Check

Connector/NET defaults to localhost and 3306. Ask the server which port it listens on.

SHOW VARIABLES LIKE 'port';

2Fix

Put the real host and port in the connection string. Several hosts can be listed separated by commas.

server=db.internal;port=3307;user id=app;password=...;database=test

3Verify

The app opens the connection.

conn.Open(); Console.WriteLine(conn.State);

Confirmed by dev.mysql.com

Did this fix it?

Cause 3 of 4

Networking is off or the server listens on loopback only

1Check

The manual says skip_networking stops all TCP/IP connections, and a bind_address of 127.0.0.1 accepts local connections only.

SHOW VARIABLES LIKE 'skip_networking';
SHOW VARIABLES LIKE 'bind_address';

2Fix

Turn skip_networking off and set bind_address to an address the other machine can reach, then restart the server.

# my.cnf, [mysqld] section
# remove the skip_networking line, and set bind_address to an address
# the other machine can reach, then restart the server
bind_address = <server address reachable from the app host>

3Verify

From the other machine, the first-step command succeeds.

mysqladmin -h db.internal --port=3306 version

Confirmed by dev.mysql.com

Did this fix it?

Cause 4 of 4

A firewall blocks the MySQL port

1Check

The manual tells you to check IP tables on Linux, and Windows Firewall or similar tools on Windows.

telnet db.internal 3306

2Fix

Allow the MySQL port for the app host in the firewall, and open it only to the hosts that need it.

# add a rule in your firewall (IP tables on Linux, Windows Firewall on Windows)
# that allows TCP port 3306 from the app host only

3Verify

telnet shows the server's greeting with its version number.

telnet db.internal 3306

Confirmed by dev.mysql.com

Did this fix it?

How do I stop it coming back?

  • Keep host and port in one configuration value instead of copying them into several connection strings.
  • Check the server with mysqladmin before you debug application code.
  • Allow the database port only for the hosts that need it.

Sources

How was this page made?

I drafted this page with AI assistance from the official documents listed under Sources. Every cause above names the document that confirms it. I have not reproduced this fix on a machine of my own yet, so read it as documented guidance, not a test report. Found a mistake? Tell me.

Which stack is behind this page?

Projects of mine built on the same technologies:

Related fixes

Next step

Want this handled for good?

I look after servers, apps and releases end to end. Tell me what keeps breaking.