MySqlException: unable to connect to any of the specified MySQL hosts
Unable to connect to any of the specified MySQL hostsShort 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.
Fix it fast
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 version2Match what you see, then jump to its fix
- 1
mysqladmin reports a refused connection, and no mysqld process existsThe MySQL server is not runningFix - 2
The server runs, but only a different port answersThe connection string uses another host or port than the serverFix - 3
Connecting from the same machine works, connecting from another machine is refusedNetworking is off or the server listens on loopback onlyFix - 4
The server runs and listens on the right address, but remote connections time out or are refusedA firewall blocks the MySQL portFix
- 1
What does the error look like?
Unable to connect to any of the specified MySQL hostsCan'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 mysqld2Fix
Start the server, then repeat the first step.
# start the MySQL service with your operating system's service manager,
# then run the check above again3Verify
mysqladmin prints the server version.
mysqladmin -h 127.0.0.1 versionConfirmed by dev.mysql.com
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=test3Verify
The app opens the connection.
conn.Open(); Console.WriteLine(conn.State);Confirmed by dev.mysql.com
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 versionConfirmed by dev.mysql.com
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 33062Fix
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 only3Verify
telnet shows the server's greeting with its version number.
telnet db.internal 3306Confirmed by dev.mysql.com
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:
- TaxiDexMy own multi-tenant taxi dispatch SaaS: API, dispatcher portal, driver and passenger apps, and the servers underneath.
- QistLockA NestJS and React SaaS driving an Android Device Owner agent that locks financed phones when payments lapse.
- ShopGoAn e-commerce aggregator with a Python scraping engine behind its catalog, past 10K Google Play downloads.
Related fixes
Databases
EF Core logs 'An error occurred using the connection to database' with MySQL
Can't connect to MySQL server on 'host_name' (111)
This EF Core message is only a wrapper.
3 causes, each with a fixDatabases
DbContextOptionsBuilder has no UseMySql method
Cannot find the UseMysql method on DbContextOptions
UseMySql comes from a provider package, not from EF Core itself.
3 causes, each with a fixDatabases
There is already an open DataReader on the MySQL connection
The error appears inside a while (reader.Read()) loop
One MySQL connection runs one operation at a time.
3 causes, each with a fixDatabases
C# INSERT fails with 'Fatal error encountered during command execution'
MySql.Data.MySqlClient.MySqlException: Fatal error encountered during command execution
A fatal error during command execution means the connection broke while the INSERT was in flight.
3 causes, each with a fixDatabases
MySqlException: Timeout expired in a .NET app
MySql.Data.MySqlClient.MySqlException: Timeout expired
Timeout expired means a wait ended before it finished.
3 causes, each with a fixDatabases
C# cannot reach MySQL: connection attempt failed, no response in time
System.Net.Sockets.SocketException (10060)
SocketError 10060 means the connection attempt timed out, or the host did not respond.
3 causes, each with a fix