EF Core logs 'An error occurred using the connection to database' with MySQL
An error occurred using the connection to database '' on server 'localhost'.Short answer
This EF Core message is only a wrapper. The cause is the inner MySqlException below it. In practice it is one of three things: the MySQL server cannot be reached on that host and port, the user or password is rejected, or the account has no privileges on the database named in the connection string.
Fix it fast
1Find out which cause you have
Test the same host, port and user outside the app. If the mysql client fails the same way, the problem is not EF Core.
mysql -h 127.0.0.1 -P 3306 -u app_user -p2Match what you see, then jump to its fix
- 1
Can't connect to MySQL server on '...' (111) or (10061) in the inner exceptionThe MySQL server cannot be reachedFix - 2
Access denied for user '...'@'...' (using password: YES) in the inner exceptionThe user or password is rejectedFix - 3
Login works without a database name but fails when the Database value is addedThe account has no privileges on the databaseFix
- 1
What does the error look like?
An error occurred using the connection to database '' on server 'localhost'.Can't connect to MySQL server on 'host_name' (111)Access denied for user 'root'@'localhost' (using password: YES)
Cause 1 of 3
The MySQL server cannot be reached
1Check
The manual says this normally means no MySQL server is running, or the client uses the wrong port or socket. Also check bind_address (127.0.0.1 accepts only local connections), skip_networking and firewalls.
ps xa | grep mysqld
mysqladmin -h 127.0.0.1 --port=3306 version2Fix
Start the server, make the Server and Port in the connection string match it, and let the server listen on an address the app host can reach. Open the port in the firewall if the database is on another machine.
# appsettings.json
"ConnectionStrings": { "Default": "Server=127.0.0.1;Port=3306;Database=app;User=app_user;Password=..." }3Verify
mysqladmin should print the server version.
mysqladmin -h 127.0.0.1 --port=3306 versionConfirmed by dev.mysql.com
Cause 2 of 3
The user or password is rejected
1Check
Compare the user and password in the connection string with the ones that work in the mysql client. An old password in an option file, or an account defined for a different host, gives the same error.
mysql --no-defaults -u app_user -p -e "SELECT USER();"2Fix
Create or update the account for the host the app connects from, then correct the connection string. If the server says Host ... is not allowed to connect, there is no account row for the client host.
CREATE USER 'app_user'@'%' IDENTIFIED BY 'a-strong-password';3Verify
Log in with the new account from the app host.
mysql -h db.example.com -u app_user -pConfirmed by dev.mysql.com
Cause 3 of 3
The account has no privileges on the database
1Check
Connect without naming a database, then check what the account may do.
mysql -u app_user -p -e "SHOW GRANTS;"2Fix
Grant the privileges the app needs on that one database. EF Core migrations also need rights to create and alter tables.
GRANT ALL PRIVILEGES ON app.* TO 'app_user'@'%';3Verify
Connect with the database name included.
mysql -u app_user -p app -e "SELECT 1;"Confirmed by dev.mysql.com
How do I stop it coming back?
- Keep one connection string per environment in configuration, not in code.
- Give the app its own MySQL account with rights on a single database.
- Add a health check that opens a connection, so a bad address shows on deploy.
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
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 fixDatabases
MySqlException: unable to connect to any of the specified MySQL hosts
Can't connect to MySQL server on 'host' (10061)
The connector could not open a TCP connection to the host and port in your connection string.
4 causes, each with a fix