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+.
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.
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:
mysql -u root -p'S0me$pecial@Pass' -h localhostIf you are unsure of the password, you may need to reset it using safe recovery mode (see Step 3).
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:
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:
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:
CREATE USER 'username'@'%' IDENTIFIED BY 'password';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:
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:
ALTER USER 'username'@'hostname' IDENTIFIED WITH mysql_native_password BY 'password';
FLUSH PRIVILEGES;If you are completely locked out, you can restart MySQL in safe mode to bypass authentication:
1. Stop the MySQL server:
sudo systemctl stop mysql
# or on macOS:
sudo /usr/local/mysql/support-files/mysql.server stop2. Edit /etc/mysql/my.cnf or /etc/my.cnf and add the following under [mysqld]:
skip-grant-tables
skip-networking3. Start MySQL:
sudo systemctl start mysql
# or on macOS:
sudo /usr/local/mysql/support-files/mysql.server start4. Connect without password:
mysql -u root5. Flush privileges and set a new password:
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.
After creating a user or modifying permissions, you must flush the in-memory privilege cache:
FLUSH PRIVILEGES;Verify the user has the necessary privileges:
SHOW GRANTS FOR 'username'@'hostname';If no privileges are shown, grant them:
GRANT ALL PRIVILEGES ON *.* TO 'username'@'localhost';
FLUSH PRIVILEGES;For application-specific users, grant only the minimum required privileges:
GRANT SELECT, INSERT, UPDATE, DELETE ON myapp_database.* TO 'app_user'@'localhost';
FLUSH PRIVILEGES;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.
EE_WRITE (3): Error writing file
How to fix "EE_WRITE (3): Error writing file" in MySQL
CR_PARAMS_NOT_BOUND (2031): No data supplied for parameters
How to fix "CR_PARAMS_NOT_BOUND (2031): No data supplied for parameters" in MySQL
CR_DNS_SRV_LOOKUP_FAILED (2070): DNS SRV lookup failed
How to fix "CR_DNS_SRV_LOOKUP_FAILED (2070): DNS SRV lookup failed" in MySQL
ERROR 1146: Table 'database.table' doesn't exist
How to fix "ERROR 1146: Table doesn't exist" in MySQL
ERROR 1040: Too many connections
How to fix "ERROR 1040: Too many connections" in MySQL