Maaz Zindani

Loading the index…

↑↓ move↵ openesc close4072 pages
Databases and Redis

ERROR 1044: Access denied for user to database

ErrorERROR 1044 (42000): Access denied for user ''@'localhost' to database 'db'

Short answer

Error 1044 is different from 1045: the login worked, but the account MySQL matched has no privileges on the database you named. When the user in the message is empty (''@'localhost'), you were matched to an anonymous account. Check CURRENT_USER(), then connect as the right user or grant rights.

  • Applies to MySQL 8.4
  • Updated
  • By Maaz Zindani, Senior Software Engineer

Fix it fast

  1. 1Find out which cause you have

    Ask the server which account your connection was matched to. It can differ from the user name you typed.

    SELECT USER(), CURRENT_USER();
    SHOW GRANTS;
  2. 2Match what you see, then jump to its fix

    1. 1The message shows an empty user name: ''@'localhost'. CURRENT_USER() returns @localhost.An anonymous account matched your connectionFix
    2. 2CURRENT_USER() shows the right user, and SHOW GRANTS has no line for the database.The account exists but has no privileges on that databaseFix
    3. 3A user was granted on a database such as my_app, but another similar name (myXapp) is the one denied or exposed.The database name contains an underscore that acted as a wildcardFix

What does the error look like?

  • ERROR 1044 (42000): Access denied for user ''@'localhost' to database 'db'
  • ERROR 1044 (42000): Access denied for user 'app'@'localhost' to database 'shop'

Cause 1 of 3

An anonymous account matched your connection

1Check

A blank user name in an account matches any user name. The server sorts accounts so that more specific hosts come first, and it uses the first match, so an anonymous row for localhost can win over a 'user'@'%' row even though you gave a user name.

SELECT Host, User FROM mysql.user WHERE User = '' OR User = 'app' ORDER BY Host;

2Fix

Remove the anonymous account (mysql_secure_installation offers this), or create the real account with the exact host you connect from.

DROP USER ''@'localhost';
CREATE USER 'app'@'localhost' IDENTIFIED BY 'choose-a-strong-password';
GRANT ALL ON shop.* TO 'app'@'localhost';

3Verify

Log in again and check that the matched account now carries your user name.

mysql -u app -p shop -e "SELECT CURRENT_USER();"

Confirmed by dev.mysql.com

Did this fix it?

Cause 2 of 3

The account exists but has no privileges on that database

1Check

Privileges are granted per database. A user with rights on db1.* gets error 1044 on db2.

SHOW GRANTS FOR 'app'@'localhost';

2Fix

Grant the privileges at database level with ON db_name.*. GRANT ALL at this level gives every database-level privilege but not global ones such as FILE.

GRANT SELECT, INSERT, UPDATE, DELETE ON shop.* TO 'app'@'localhost';

3Verify

Reconnect and list the grants.

SHOW GRANTS FOR 'app'@'localhost';

Confirmed by dev.mysql.com

Did this fix it?

Cause 3 of 3

The database name contains an underscore that acted as a wildcard

1Check

In database-level GRANT, _ and % are wildcards (deprecated). A grant on my_app.* also matches names like myXapp. To grant on the literal name, escape the underscore.

SHOW GRANTS FOR 'app'@'localhost';

2Fix

Grant with the underscore escaped by a backslash, inside backticks.

GRANT ALL ON `my\_app`.* TO 'app'@'localhost';

3Verify

SHOW GRANTS lists the escaped name and the connection to the database works.

SHOW GRANTS FOR 'app'@'localhost';

Confirmed by dev.mysql.com

Did this fix it?

How do I stop it coming back?

  • Run mysql_secure_installation, or drop anonymous accounts, on every new server.
  • Create one account per application with grants on its own database only.
  • Always check CURRENT_USER() when a connection gets different rights than expected.

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.