ERROR 1044: Access denied for user to database
ERROR 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.
Fix it fast
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;2Match what you see, then jump to its fix
- 1
The message shows an empty user name: ''@'localhost'. CURRENT_USER() returns @localhost.An anonymous account matched your connectionFix - 2
CURRENT_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
A 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
- 1
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
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
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
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:
- 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
MySQL says Access denied for user 'root'@'localhost' and Django cannot connect
ERROR 1045 (28000): Access denied for user 'root'@'localhost' (using password: NO)
ERROR 1045 means MySQL found no account that matches the user, host and password your client sent.
3 causes, each with a fixDatabases
Installing the Airflow MySQL provider fails on pkg-config or mysqlclient
Building the mysqlclient wheel fails during the install
The Airflow MySQL provider depends on mysqlclient, which pip compiles against the MySQL C client library.
2 causes, each with a fixDatabases
A query on Aurora MySQL dies with 'table /rdsdbdata/tmp/#sql... is full'
ERROR 1114 (HY000): The table '/rdsdbdata/tmp/#sqlfd_1586_2' is full
In Aurora MySQL 3, internal temporary tables share one pool, and when that pool and the memory-mapped overflow file are used up, a reader instance ends the query with error 1114.
3 causes, each with a fixDatabases
Azure Database for MySQL refuses a connection that does not use SSL
ERROR 3159 (HY000): Connections using insecure transport are prohibited while --require_secure_transport=ON.
Azure Database for MySQL Flexible Server requires encrypted connections by default (require_secure_transport = ON, TLS 1.2).
4 causes, each with a fixDatabases
Compiling a C or C++ program that includes mysql.h fails
undefined reference to `mysql_init'
The compiler only finds mysql.h when the MySQL client development files are installed and their folder is on the include path.
3 causes, each with a fixDatabases
DBeaver fails to resolve Maven dependencies when downloading a database driver
A driver library does not download automatically
DBeaver downloads driver jars from a Maven repository.
3 causes, each with a fix