MySQL & MariaDB error 1044: Access denied for user to database
Access denied for user 'appuser'@'%' to database 't_0' (5.6.51) The login worked, but the account has no privileges on the database it tried to use or create. Different from 1045, which is a failed login.
| MySQL | MariaDB | |
|---|---|---|
| Symbol | ER_DBACCESS_DENIED_ERROR | ER_DBACCESS_DENIED_ERROR |
| SQLSTATE | 42000 | 42000 |
| Type | Server error | Server error |
| Versions | every release since 5.0 active | every release since 5.5 active |
What it means
Access denied for user 'appuser'@'%' to database 'shop'. Authentication succeeded, so the user name and password are fine. The account can’t USE, CREATE or otherwise access that database.
Why it happens
- The account was created but never granted privileges on that database.
- Grants exist for a different database name. Wildcards in grants (
shop_%) treat_as a single-character wildcard, so they may not match what you expect. - Trying to
CREATE DATABASEwithout the global CREATE privilege (common on shared hosting). - The grant is on a different account (host) from the one you logged in as.
How to fix it
SHOW GRANTS;(as that user) shows exactly what you have;SELECT CURRENT_USER();shows which account you are.- As an admin:
GRANT SELECT, INSERT, UPDATE, DELETE ON shop.* TO 'appuser'@'%'; - On shared hosting, create databases through the control panel; the account usually can’t.
Reproduce it yourself
These steps are the exact test our harness runs. Use a scratch server, never production.
Connect with
mysql -u root -pand create a scratch database:CREATE DATABASE demo; USE demo;Run:
CREATE USER 'appuser'@'%' IDENTIFIED BY 'Test#1234';In a shell on the database host:
mysql -uappuser -p'Test#1234' -h127.0.0.1 -e 'USE demo'
You get: ERROR 1044 (42000) at line 1: Access denied for user 'appuser'@'%' to database 't_0'(11.8.9)
Verified fixes
Grant privileges on the database, then check with SHOW GRANTS
CREATE USER 'appuser'@'%' IDENTIFIED BY 'Test#1234'; GRANT SELECT ON demo.* TO 'appuser'@'%';mysql -uappuser -p'Test#1234' -h127.0.0.1 -N -e 'USE demo; SELECT DATABASE()'
Tested on
| Step | MySQL | MariaDB | |||||||
|---|---|---|---|---|---|---|---|---|---|
| 5.6 | 5.7 | 8.0 | 8.4 | 9 | 10.6 | 10.11 | 11.4 | 11.8 | |
| Use a database the account has no privileges on repro | ✓ 1044 | ✓ 1044 | ✓ 1044 | ✓ 1044 | ✓ 1044 | ✓ 1044 | ✓ 1044 | ✓ 1044 | ✓ 1044 |
| Grant privileges on the database, then check with SHOW GRANTS fix | ✓ | ✓ | ✓ | ✓ | ✓ | ✓ | ✓ | ✓ | ✓ |