Skip to main content

Fixed: "ERROR 1044 (42000): Access denied for user to database" in MySQL

· 4 min read
Jagdish Kumawat
Founder @ Dewiride

ERROR 1044 (42000): Access denied for user 'lock_viewer'@'%' to database 'inventory' looks like a login failure, and it isn't one: your password was right. This account just has no rights on the database you named. It's different from ERROR 1045, and has a different fix.

What I Was Doing​

I'd made an account just for looking at locks, on MySQL 9.7.4 in Docker, and connected with the same command I always use, with the database at the end:

Terminal
docker compose exec mysql mysql --user=lock_viewer --password inventory

What I Expected​

A mysql> prompt.

What I Got​

Output
ERROR 1044 (42000): Access denied for user 'lock_viewer'@'%' to database 'inventory'

The mysql client failing to connect with ERROR 1044 (42000): Access denied for user 'lock_viewer'@'%' to database 'inventory'.

What I Checked​

Whether the password was right. The same account, with no database:

Terminal
docker compose exec mysql mysql --user=lock_viewer --password --execute="SELECT CURRENT_USER()"
Output
+----------------+
| CURRENT_USER() |
+----------------+
| lock_viewer@% |
+----------------+

It logged in.

What the account can use. As root, its grants cover performance_schema and sys, and nothing on inventory:

Output (SHOW GRANTS FOR 'lock_viewer'@'%', from the how-to)
+-------------------------------------------------------------+
| Grants for lock_viewer@% |
+-------------------------------------------------------------+
| GRANT PROCESS ON *.* TO `lock_viewer`@`%` |
| GRANT SELECT, EXECUTE ON `sys`.* TO `lock_viewer`@`%` |
| GRANT SELECT ON `performance_schema`.* TO `lock_viewer`@`%` |
+-------------------------------------------------------------+
3 rows in set (0.000 sec)

Why It Happens​

The login happens in two steps: check the account and password, then switch to the database you asked for. The first one worked, and the second one was refused. If the password were wrong, you'd get ERROR 1045 instead, and the words using password would be in the message.

The Fix​

In my case, the account isn't supposed to touch that database, so I left it off the command:

Terminal
docker compose exec mysql mysql --user=lock_viewer --password

If the account should have access, grant it, as root:

mysql: root session
GRANT SELECT ON inventory.* TO 'lock_viewer'@'%';

After the Fix​

Output
+----------------+
| CURRENT_USER() |
+----------------+
| lock_viewer@% |
+----------------+

The same account connecting with no database named, and SELECT CURRENT_USER() returning lock_viewer@%.

Common Mistakes​

MistakeWhat happensDo this instead
Changing the passwordIt was right all alongRead the error number
Granting rights the account shouldn't haveA narrow account becomes a wide oneLeave the database off instead
Confusing it with ERROR 1045That's a failed login1044 is rights, 1045 is the login

Troubleshooting​

SymptomLikely causeFix
The same error on USE inventory;The same missing rightGrant it, or use another database
The same error on CREATE DATABASEThe account can't create databasesGrant CREATE, or ask an administrator
The database name has a typoNo rights on a database that doesn't existCheck the name, with SHOW DATABASES

Conclusion​

ERROR 1044 means the login worked, and the account has no rights on that database. Leave it off the command, connect to a database it can use, or grant the rights if it really needs them.

Additional Resources​

Stay Updated

Subscribe to our newsletter for the latest tutorials, tech insights, and developer news.

By subscribing, you agree to our privacy policy. Unsubscribe at any time.