Maaz Zindani

Loading the index…

↑↓ move↵ openesc close4072 pages
Databases and Redis

EF Core logs 'An error occurred using the connection to database' with MySQL

ErrorAn 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.

  • Applies to EF Core 8, MySQL 8.4, .NET 8
  • Updated
  • By Maaz Zindani, Senior Software Engineer

Fix it fast

  1. 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 -p
  2. 2Match what you see, then jump to its fix

    1. 1Can't connect to MySQL server on '...' (111) or (10061) in the inner exceptionThe MySQL server cannot be reachedFix
    2. 2Access denied for user '...'@'...' (using password: YES) in the inner exceptionThe user or password is rejectedFix
    3. 3Login works without a database name but fails when the Database value is addedThe account has no privileges on the databaseFix

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 version

2Fix

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 version

Confirmed by dev.mysql.com

Did this fix it?

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 -p

Confirmed by dev.mysql.com

Did this fix it?

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

Did this fix it?

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:

Related fixes

Next step

Want this handled for good?

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