How do I fix an "Access Denied" error when connecting to MySQL?

An Access Denied error occurs when:

  • You input the incorrect username, password, or database name in the connection string.
  • The user does not have the proper privileges to connect to the database.

Furthermore, access denied errors can look different depending on the root cause.

For instance, this error means that you’re using the wrong password:

ERROR 1045 (28000): Access denied for user 'sammy'@'203.0.113.0' (using password: YES)

This access denied error means the connection string didn’t contain the -p flag but a password is required:

ERROR 1045 (28000): Access denied for user 'sammy'@'203.0.113.0'' (using password: NO)

This access denied error means the user does not have privileges on the database specified in the connection string:

ERROR 1044 (42000): Access denied for user 'sammy'@'%' to database 'defaultdb'

If you are getting this error:

  • Ensure you have the correct username, password, and database name in your connection string.
  • Ensure the user has the correct privileges on the database you want to connect to.
Update backup user permissions, remove the –single-transaction flag, or downgrade mysqldump.
Verify the format and values in the connection string for typos or formatting errors.
Prepend your database client command to the connection string.