Databases

How to fix MySQL "Access denied for user" (error 1045)

MySQL error 1045 "Access denied for user" means the username, password or host was rejected. Learn how to fix database users and privileges in cPanel and on a VPS.

IntermediatecPanel hosting and VPS2 min read

MySQL error 1045 "Access denied for user 'name'@'host' (using password: YES)" means the database server rejected the login. Either the username does not exist for that host, the password is wrong, or the user has no rights on the database. On cPanel it often appears after moving a site, because names and passwords in the config file no longer match.

Before you start

  • Open the application's config file (for WordPress, wp-config.php) and note DB_NAME, DB_USER, DB_PASSWORD and DB_HOST.
  • Open cPanel → Databases → MySQL Databases.

Read the error carefully

  • using password: NO – the app sent no password; the config value is empty or not read.
  • 'user'@'localhost' – the user and host MySQL saw. Make sure that is the user you expected.

Fix it in cPanel

  1. Check the full names. cPanel prefixes databases and users with your account name, e.g. cpuser_wp. The config must use the full prefixed names.
  2. Reset the password. Under Current Users, click Change Password for the user, set a new one and copy it exactly into the config file.
  3. Add the user to the database. Under Add User to Database, choose the user and database, then grant ALL PRIVILEGES.
  4. Use localhost as host for apps on the same account.
  5. For remote connections, add your IP in Databases → Remote MySQL and connect to the server hostname.

Fix it on a VPS

  1. Log in as root: mysql -u root -p (or sudo mysql).
  2. Check users: SELECT user, host FROM mysql.user;
  3. Reset the password and grant rights:
    ALTER USER 'appuser'@'localhost' IDENTIFIED BY 'NewStrongPass!';
    GRANT ALL PRIVILEGES ON appdb.* TO 'appuser'@'localhost';
    FLUSH PRIVILEGES;
  4. If root itself is denied, follow reset the MySQL root password.

Tip: Special characters such as $ or quotes in a password can break config files. If you set one, check it is quoted correctly, or pick a long password without them.

FAQ

Why did it work before the migration?

The new account has a different prefix or password. Update the config file to match.

Is 'localhost' different from 127.0.0.1?

To MySQL, yes: localhost uses a socket, 127.0.0.1 uses TCP, and users can be defined for one but not the other.

Does this cause "Error establishing a database connection"?

Yes, on WordPress that message often hides a 1045 error.

Next steps

Was this guide helpful?

Still stuck?

Ask MdmHost AI for a quick answer, or open a ticket and our team will take a look. We are here 24/7.