How to Fix MySQL Database Error 1045: Access Denied for User
MySQL error 1045, Access denied for user, fixed for beginners on a real MySQL server: how to read the user, the host and using password YES or NO, why the same password works on the server and fails from another machine (a MySQL account is a user name plus a host), the shell eating a $ in double quotes, a misspelled user, Ubuntu's root and error 1698, and a Docker volume keeping the old root password.
The password is right, still denied
Your app says: Access denied for user, using password: YES. Most people's first guess is that the password is wrong. This morning, Trish's dispatch board at a small plumbing company shows exactly that. She takes the password from the board's settings file, types it on the office server, and she's in. MySQL isn't confused. It's being very particular about who's asking. So if the password is right, what is MySQL refusing? The message itself tells you. Then five more causes, including a new password that never reached the database at all.
python3 board.pyBoard can't load today's jobs: (1045, "Access denied for user 'dispatch'@'192.168.71.20' (using password: YES)")
cat board.ini[database] host = dispatch-db name = dispatch user = dispatch password = Pipe$Wrench9
mysql -u dispatch -p -e 'SELECT CURRENT_USER()'Enter password: +--------------------+ | CURRENT_USER() | +--------------------+ | dispatch@localhost | +--------------------+
Read the error: user, host, password
Read the message in three parts. First, the user name: dispatch. Second, the part after the at sign is the host. That's the address the connection came from. Here it's the board, at 192.168.71.20. Third, using password: YES means a password was sent. It doesn't say the password was right, only that one arrived.

Same user, another host
Here's the fix first, then why it works. Last week Trish gave dispatch a new password, and put it in the board's settings file. On the server, she lists every account named dispatch. Two rows: dispatch at localhost, meaning on this server itself, and dispatch at percent. Percent means any other address; more on that in a moment. She only changed the localhost one. She opens MySQL as the server's admin with sudo mysql, and sets the new password on the percent account too, with ALTER USER. The board loads again: today's jobs, three. Why did one name need its password set twice?
sudo mysql -e "SELECT user, host FROM mysql.user WHERE user = 'dispatch'"+----------+-----------+ | user | host | +----------+-----------+ | dispatch | % | | dispatch | localhost | +----------+-----------+
sudo mysqlWelcome to the MySQL monitor. Commands end with ; or \g. Your MySQL connection id is 14 Server version: 8.0.46-0ubuntu0.24.04.5 (Ubuntu) Copyright (c) 2000, 2026, Oracle and/or its affiliates. Oracle is a registered trademark of Oracle Corporation and/or its affiliates. Other names may be trademarks of their respective owners. Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.
mysql> ALTER USER 'dispatch'@'%' IDENTIFIED BY 'Pipe$Wrench9';Query OK, 0 rows affected (0.00 sec)
mysql> exitBye
python3 board.pyToday's jobs: 3 8:00 Burst pipe, Elm Street Mike 10:30 Water heater swap, Oak Avenue Mike 14:00 Blocked drain, Bay Road Trish
Think of one gym membership at two branches. Your name is the same at both, but each branch keeps its own card on file. Change your PIN at one branch, and the other still has the old one.

MySQL works the same way. An account is a user name plus a host: where the connection comes from. dispatch at localhost is dispatch connecting on the server itself. dispatch at percent is dispatch from anywhere else: percent is MySQL's wildcard. They're two separate accounts, each with its own password. Trish's change only reached the localhost card. The board connects from another address, so it gets the percent account, the branch with the old card.

MySQL's manual says it in one line: your identity is based on two pieces of information, your user name, and the host you connect from.

using password: NO
A week later the board fails again, but the end of the message changed: using password: NO. That means no password was sent at all. The server isn't the problem. Look at the app's settings. Her coworker Mike tidied the file. Now a hash sign sits at the start of the password line, and a hash tells the app to ignore that line.
python3 board.pyBoard can't load today's jobs: (1045, "Access denied for user 'dispatch'@'192.168.71.20' (using password: NO)")
grep -n password board.ini5:# password = Pipe$Wrench9
One character fixes it: remove the hash at the start of the line.

The shell changed the password
This one fools people at the terminal. Trish puts the password right on the command line, in double quotes. Guess first: does it log in? No. Access denied, using password: YES. The shell is the program that reads what you type in the terminal, and echo prints back what it passes along. Echo shows why: inside double quotes, the shell reads dollar Wrench9 as a variable, which is empty, so MySQL received just Pipe. In single quotes the shell leaves the dollar sign alone, and she's in. Better still, type dash p with nothing after it. MySQL asks for the password, and the shell never sees it. It's safer too: MySQL warns that a password on the command line can be insecure.
mysql -u dispatch -p"Pipe$Wrench9" -e 'SELECT CURRENT_USER()'mysql: [Warning] Using a password on the command line interface can be insecure. ERROR 1045 (28000): Access denied for user 'dispatch'@'localhost' (using password: YES)
echo "Pipe$Wrench9"Pipe
mysql -u dispatch -p'Pipe$Wrench9' -e 'SELECT CURRENT_USER()'mysql: [Warning] Using a password on the command line interface can be insecure. +--------------------+ | CURRENT_USER() | +--------------------+ | dispatch@localhost | +--------------------+
Typos, and root on Ubuntu
A misspelled user name gets the very same message. Here the user name is D, I, S, P, A, C, H, with no T, and the password is right: access denied. MySQL won't say whether a user exists, so check the spelling in the message before you change any password.
mysql -u dispach -p -e 'SELECT CURRENT_USER()'Enter password: ERROR 1045 (28000): Access denied for user 'dispach'@'localhost' (using password: YES)
On Ubuntu, logging in to MySQL as root with a password can fail even when the password is right. Check the error code: it's 1698, not 1045. That's because root, the built-in admin account, uses a login method called auth_socket. It ignores passwords and checks which Linux user is asking. sudo runs the command as the Linux root user. So sudo mysql gets in with no password at all. That's how Trish ran her fix earlier.
mysql -u root -pEnter password: ERROR 1698 (28000): Access denied for user 'root'@'localhost'
sudo mysql -e "SELECT user, host, plugin FROM mysql.user WHERE user = 'root'"+------+-----------+-------------+ | user | host | plugin | +------+-----------+-------------+ | root | localhost | auth_socket | +------+-----------+-------------+
Docker kept the old password
Here's one that catches Docker users. Mike keeps a test copy of the database in a Docker container. He changes the root password setting in his compose file and starts it again, so the container is rebuilt. The new password is refused. The old one, from the first start, still works.
grep ROOT compose.yamlMYSQL_ROOT_PASSWORD: Tap-Washer-12
docker compose up -dContainer ce-p125-testdb-db-1 Recreate Container ce-p125-testdb-db-1 Recreated Container ce-p125-testdb-db-1 Starting Container ce-p125-testdb-db-1 Started
docker compose exec db mysql -uroot -pTap-Washer-12 -e 'SELECT 1'mysql: [Warning] Using a password on the command line interface can be insecure. ERROR 1045 (28000): Access denied for user 'root'@'localhost' (using password: YES)
docker compose exec db mysql -uroot -pfirst-test-pw -e 'SELECT CURRENT_USER()'mysql: [Warning] Using a password on the command line interface can be insecure. +----------------+ | CURRENT_USER() | +----------------+ | root@localhost | +----------------+
The image's own documentation says why: settings like that root password have no effect if the data folder already holds a database. The root password was set once, on the first start, and saved in the volume: the folder Docker keeps even when the container is rebuilt. The new password never reached it.

So change it the way you'd change any MySQL password: log in with the old one, and run ALTER USER for both root accounts, at percent and at localhost. Same lesson as Trish's board: one name, two accounts. Now the new password works. Deleting the volume also resets it, but it erases every row in the database.
docker compose exec db mysql -uroot -pfirst-test-pw -e "ALTER USER 'root'@'%' IDENTIFIED BY 'Tap-Washer-12'; ALTER USER 'root'@'localhost' IDENTIFIED BY 'Tap-Washer-12'"mysql: [Warning] Using a password on the command line interface can be insecure.
docker compose exec db mysql -uroot -pTap-Washer-12 -e 'SELECT CURRENT_USER()'mysql: [Warning] Using a password on the command line interface can be insecure. +----------------+ | CURRENT_USER() | +----------------+ | root@localhost | +----------------+
Prevent it
Four habits turn error 1045 into a two-minute fix. Read the user, the host, and YES or NO in the message. List the accounts with that user name before you change a password. Type passwords at the prompt, or in single quotes. And in Docker, change passwords with ALTER USER, not the compose file.

Recap
So: the board's password was right, but it reached dispatch at percent, which still had the old password. In MySQL, who you are includes where you connect from.

If this got your app back into its database, subscribing helps a small channel reach more people stuck on the same error.
All the lab files and commands are linked in the description. Thanks for watching Cloud Explainers.
Sources
- MySQL 8.0 Reference Manual, Access Control, Stage 1: Connection Verification
- MySQL 8.0 Reference Manual, Specifying Account Names (the % wildcard, localhost)
- MySQL 8.0 Reference Manual, Socket Peer-Credential Pluggable Authentication (auth_socket)
- MySQL 8.0 Reference Manual, ALTER USER
- GNU Bash manual, Quoting (double and single quotes)
- The official mysql Docker image, Environment Variables
Demos: containers on our own Mac standing in for an office server, a board PC and a laptop; the plumbing company is made up. Every command, error and number was real. Watch the full video on YouTube.