How to fix ERROR 1045: Access denied for user in MySQL

MySQLBEGINNERHIGHUpdated Dec 22, 2025· 168 views

MySQL Error 1045 occurs when connection credentials are incorrect, the user lacks privileges from the specified host, or authentication settings are misconfigured. Fix it by verifying credentials, checking user permissions, and adjusting authentication plugins on MySQL 8.0+.

Quick fix

Fixes ERROR 1045: Access denied for user

mysql -u root -p'S0me$pecial@Pass' -h localhost
  1. Verify the username and password are correct
  2. Verify the user exists and check its host permissions
  3. Check and fix authentication plugin compatibility (MySQL 8.0+)
  4. Reset root password using skip-grant-tables (if locked out)
mysql — error
mysql -u root -p'S0me$pecial@Pass' -h localhost
ERROR 1045: Access denied for user

What this error means

ERROR 1045 (ER_ACCESS_DENIED_ERROR) is a MySQL authentication failure that prevents users from connecting to the database server. This error occurs during the initial connection handshake when MySQL verifies the username, password, and host combination against its internal user privileges table (mysql.user). The error message includes three key pieces of information: the username, the hostname the connection is coming from, and whether a password was provided during the connection attempt. This error does not mean the database is broken—it's a security mechanism working as intended.

Typical symptoms

  • Cannot connect to MySQL server with mysql command or any database client
  • Connection fails immediately with authentication error message
  • Applications unable to connect despite having correct host and database name
  • Error message shows "using password: NO" even though a password was provided
  • Error persists after reinstalling or upgrading MySQL
  • Only root or specific users affected, others can connect normally

Common causes

  1. Incorrect password provided during connection (most common)
  2. User account does not exist on the MySQL server
  3. User exists but is not permitted to connect from the current hostname or IP address
  4. Bash shell escaping or special characters in password causing incorrect transmission
  5. Wrong authentication plugin (MySQL 8.0+ uses caching_sha2_password instead of mysql_native_password)
  6. User account locked after excessive failed login attempts
  7. Empty password expected but one was provided, or vice versa
  8. Anonymous users in mysql.user table conflicting with named user accounts

How to fix "ERROR 1045: Access denied for user"

1Verify the username and password are correct

Double-check the credentials you are using. MySQL is case-sensitive for usernames and passwords are case-sensitive by default. If your password contains special characters like @, $, or &, wrap it in single quotes when passing via command line:

bash
mysql -u root -p'S0me$pecial@Pass' -h localhost

If you are unsure of the password, you may need to reset it using safe recovery mode (see Step 3).

2Verify the user exists and check its host permissions

Connect to MySQL with an account that has admin privileges (or using recovery mode) and check if the user exists and from which hosts they can connect:

sql
SELECT user, host FROM mysql.user WHERE user = 'username';

MySQL requires an exact match between the hostname in the connection string and the host listed in the user table. For example, user'@'localhost' cannot connect from 127.0.0.1, and vice versa. If the user does not exist, create it:

sql
CREATE USER 'username'@'localhost' IDENTIFIED BY 'password';
GRANT ALL PRIVILEGES ON database.* TO 'username'@'localhost';
FLUSH PRIVILEGES;

To allow connection from any host, use '%' as the host:

sql
CREATE USER 'username'@'%' IDENTIFIED BY 'password';
3Check and fix authentication plugin compatibility (MySQL 8.0+)

MySQL 8.0 changed the default authentication plugin from mysql_native_password to caching_sha2_password. If you are connecting with a client that does not support the newer plugin, you will see Error 1045. Check the current plugin for your user:

sql
SELECT user, host, plugin FROM mysql.user WHERE user = 'username';

If the plugin is caching_sha2_password and your client does not support it, change it to mysql_native_password:

sql
ALTER USER 'username'@'hostname' IDENTIFIED WITH mysql_native_password BY 'password';
FLUSH PRIVILEGES;
4Reset root password using skip-grant-tables (if locked out)

If you are completely locked out, you can restart MySQL in safe mode to bypass authentication:

1. Stop the MySQL server:

bash
sudo systemctl stop mysql
# or on macOS:
sudo /usr/local/mysql/support-files/mysql.server stop

2. Edit /etc/mysql/my.cnf or /etc/my.cnf and add the following under [mysqld]:

ini
skip-grant-tables
skip-networking

3. Start MySQL:

bash
sudo systemctl start mysql
# or on macOS:
sudo /usr/local/mysql/support-files/mysql.server start

4. Connect without password:

bash
mysql -u root

5. Flush privileges and set a new password:

sql
FLUSH PRIVILEGES;
ALTER USER 'root'@'localhost' IDENTIFIED BY 'new_secure_password';

6. Remove the skip-grant-tables and skip-networking lines from my.cnf and restart MySQL normally.

5Verify GRANT privileges are properly set and flushed

After creating a user or modifying permissions, you must flush the in-memory privilege cache:

sql
FLUSH PRIVILEGES;

Verify the user has the necessary privileges:

sql
SHOW GRANTS FOR 'username'@'hostname';

If no privileges are shown, grant them:

sql
GRANT ALL PRIVILEGES ON *.* TO 'username'@'localhost';
FLUSH PRIVILEGES;

For application-specific users, grant only the minimum required privileges:

sql
GRANT SELECT, INSERT, UPDATE, DELETE ON myapp_database.* TO 'app_user'@'localhost';
FLUSH PRIVILEGES;

Advanced notes

When troubleshooting Error 1045, always check the full error message which includes the username, hostname, and whether a password was provided. This tells you exactly which user record MySQL was trying to match. Pay special attention to hostname: localhost, 127.0.0.1, and the server's actual IP address are treated as different hosts. If connecting via TCP (remote host), use the actual IP or hostname; MySQL cannot use localhost for remote connections. For application connection strings, check that the password is not being URL-encoded or escaped by your connection library. Some libraries require percent-encoding special characters (e.g., @ becomes %40). Finally, remember that Error 1045 happens at the network protocol level before the database selection step, so even if the database name is wrong, you would not get Error 1045—you would successfully authenticate and then get an "unknown database" error.

Frequently asked questions

What does "ERROR 1045: Access denied for user" mean?
ERROR 1045 (ER_ACCESS_DENIED_ERROR) is a MySQL authentication failure that prevents users from connecting to the database server. user). The error message includes three key pieces of information: the username, the hostname the connection is coming from, and whether a password was provided during the connection attempt.
How do I fix "ERROR 1045: Access denied for user"?
MySQL Error 1045 occurs when connection credentials are incorrect, the user lacks privileges from the specified host, or authentication settings are misconfigured. Fix it by verifying credentials, checking user permissions, and adjusting authentication plugins on MySQL 8.0+.
What causes "ERROR 1045: Access denied for user"?
Incorrect password provided during connection (most common) User account does not exist on the MySQL server User exists but is not permitted to connect from the current hostname or IP address
Is the "ERROR 1045: Access denied for user" error serious?
Yes. This is a high-severity error that usually blocks work until it is fixed, but the steps above resolve it.

Official resources & further reading