Cloud Explainers

How to Fix MySQL Database Error 1045: Access Denied for User

Databases & SQL7 min video

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.

Trish on the dispatch board
python3 board.pyBoard can't load today's jobs: (1045, "Access denied for user 'dispatch'@'192.168.71.20' (using password: YES)")
Trish on the dispatch board
cat board.ini[database]
host = dispatch-db
name = dispatch
user = dispatch
password = Pipe$Wrench9
Trish on the dispatch board
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.

Error 1045, taken apart: diagram of 'dispatch'@'192.168.71.20', 'dispatch' = the user name, '192.168.71.20' = the host, using password: YES = one was sent, not that it was right

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?

Trish on dispatch-db, the office server
sudo mysql -e "SELECT user, host FROM mysql.user WHERE user = 'dispatch'"+----------+-----------+
| user     | host      |
+----------+-----------+
| dispatch | %         |
| dispatch | localhost |
+----------+-----------+
Trish on dispatch-db, the office server
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.
Trish on dispatch-db, the office server
mysql> ALTER USER 'dispatch'@'%' IDENTIFIED BY 'Pipe$Wrench9';Query OK, 0 rows affected (0.00 sec)
Trish on dispatch-db, the office server
mysql> exitBye
Trish on dispatch-db, the office server
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.

One name, two branches, two cards on file: diagram of Trish, Downtown branch, Harbor branch

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.

A MySQL account is a user name plus a host: diagram of dispatch board, Trish on the server, 'dispatch'@'%', 'dispatch'@'localhost', mysql.user

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.

Same user, another host: browser screenshot from the video

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.

Trish on the dispatch board
python3 board.pyBoard can't load today's jobs: (1045, "Access denied for user 'dispatch'@'192.168.71.20' (using password: NO)")
Trish on the dispatch board
grep -n password board.ini5:# password = Pipe$Wrench9

One character fixes it: remove the hash at the start of the line.

Put the password line back: code change of ~/board/board.ini, password = Pipe$Wrench9 (sent again)

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.

Trish on dispatch-db, the office server
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)
Trish on dispatch-db, the office server
echo "Pipe$Wrench9"Pipe
Trish on dispatch-db, the office server
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.

Trish on dispatch-db, the office server
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.

Trish on dispatch-db, the office server
mysql -u root -pEnter password:
ERROR 1698 (28000): Access denied for user 'root'@'localhost'
Trish on dispatch-db, the office server
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.

Mike's laptop: ~/testdb
grep ROOT compose.yaml      MYSQL_ROOT_PASSWORD: Tap-Washer-12
Mike's laptop: ~/testdb
docker compose up -d Container 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
Mike's laptop: ~/testdb
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)
Mike's laptop: ~/testdb
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.

Docker kept the old password: browser screenshot from the video

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.

Mike's laptop: ~/testdb
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.
Mike's laptop: ~/testdb
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.

Four habits for error 1045: cards of Read user, host, YES/NO, List the accounts, Prompt or single quotes, Docker: ALTER USER

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.

Who you are includes where you connect from: diagram of dispatch board, Trish on the server, 'dispatch'@'%', 'dispatch'@'localhost'

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

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.

More videos

PostgreSQL9 min

Every PostgreSQL Setting to Change After You Install It

Docker9 min

What Happens When You Install Docker on Ubuntu

Python8 min

How to Fix Python 'Address Already in Use' Errors

Go (Golang)5 min

Golang Interfaces and Mocks in 5 Minutes

Git7 min

How to Fix Git 'Detached HEAD' State

Linux21 min

Linux Firewall with ufw: Full Tutorial for Beginners

Go (Golang)5 min

Golang defer Statements in 5 Minutes

Auth (OAuth, JWT)9 min

How to Fix OAuth 'invalid_grant' Errors

Databases & SQL11 min

The SQL Roadmap for Beginners

Go (Golang)5 min

Golang make() for Slices and Maps in 5 Minutes

Networking7 min

How to Fix Network 'No Route to Host' Errors on Linux

Linux6 min

How to Fix Linux 'Too Many Open Files' Errors

Django (Python web framework)11 min

Every Django Concept a Beginner Needs to Know

Linux24 min

SSH on Linux: Full Tutorial for Beginners

Go (Golang)5 min

Golang for range Loops in 5 Minutes

Databases & SQL10 min

SQL Databases: SQLite vs PostgreSQL

Django (Python web framework)10 min

The Django Roadmap for Beginners

Go (Golang)5 min

Golang Structs in 5 Minutes

Caching & Redis22 min

Redis: Full Tutorial for Beginners

Go (Golang)5 min

Golang context.Context in 5 Minutes

Go (Golang)5 min

Golang Method Receivers in 5 Minutes

Python8 min

How to Fix Python SSL: CERTIFICATE_VERIFY_FAILED Errors

Django (Python web framework)9 min

How to Fix Django Static Files Not Loading in Production

OpenRouter (one API for many AI models)8 min

OpenRouter vs Direct API Keys: Which One Should You Use?

Go (Golang)5 min

Golang if err != nil Checks in 5 Minutes

Go (Golang)8 min

How to Fix Golang 'go.mod File Not Found' Errors

AI agents & MCP22 min

MCP Servers: Full Tutorial for Beginners

Go (Golang)5 min

Golang Error Return Values in 5 Minutes

Python8 min

How to Fix Python ModuleNotFoundError: No Module Named

Go (Golang)5 min

Golang Pointers in 5 Minutes

Kubernetes8 min

How to Fix a Kubernetes Pod Stuck in CrashLoopBackOff

Homelab & self-hosting9 min

How to Fix a Homelab ESXi Host Not Responding

PostgreSQL24 min

PostgreSQL: Full Tutorial for Beginners

Load balancing & Nginx9 min

How a Load Balancer Works with HAProxy

Python9 min

How Python Virtual Environments Work

Homelab & self-hosting9 min

How to Fix Cloudflare Tunnel Errors in a Homelab

APIs9 min

How to Fix API CORS Errors: No 'Access-Control-Allow-Origin'

Auth (OAuth, JWT)9 min

How Authenticator App Login Codes Work (TOTP)

Git22 min

Git and GitHub: Full Tutorial for Beginners

Homelab & self-hosting10 min

How a Homelab Hypervisor Works (VMware ESXi)

Kubernetes9 min

How to Fix Kubernetes ImagePullBackOff Errors

DNS8 min

How to Fix DNS 'Temporary Failure in Name Resolution' on Linux

AWS8 min

How to Fix AWS CLI 'Unable to Locate Credentials'

Homelab & self-hosting10 min

Proxmox vs ESXi for Your Homelab: When Each One Wins

PostgreSQL10 min

PostgreSQL UUID vs Auto Increment: Which Primary Key Should You Use?

Kubernetes21 min

Kubernetes: Full Tutorial for Beginners

Networking9 min

How TCP Sockets Work

PostgreSQL7 min

How to Fix PostgreSQL 'Could Not Connect to Server'

Claude & the Claude Developer Platform9 min

Claude Code Hooks Will Finally Make Sense After This

DevOps & CI/CD9 min

DevOps with Ansible Will Finally Make Sense After This

Networking9 min

How SNMP Network Monitoring Works

Git8 min

How to Fix GitHub 'Permission Denied (publickey)' Errors

Docker21 min

Docker: Full Tutorial for Beginners

PostgreSQL7 min

How to Fix PostgreSQL 'Password Authentication Failed'

Linux8 min

Why One Linux Server Handles More Users Than You Think

Networking7 min

How to Fix Network 'Connection Refused' Errors

DevOps & CI/CD9 min

DevOps Pipelines: Jenkins vs GitHub Actions

Linux11 min

Linux Nginx Reverse Proxy: Full Tutorial for Beginners

Networking11 min

Every Networking Term a Beginner Needs in 12 Minutes

Docker6 min

How to Fix Docker 'Permission Denied' on docker.sock

Git7 min

How to Fix Git 'Updates Were Rejected' When You Push

Linux7 min

How to Fix nginx 502 Bad Gateway Errors on Linux

Linux8 min

How to Fix Linux apt 'Could Not Get Lock' Errors (Ubuntu)

Networking9 min

How Email Works on the Internet (SMTP) in 10 Minutes

Python8 min

The Python Roadmap for Beginners

Go (Golang)21 min

Golang REST API with Gin: Full Tutorial for Beginners

Networking10 min

How a WireGuard VPN Network Works

Linux9 min

Linux Makefiles Will Finally Make Sense After This

Auth (OAuth, JWT)9 min

SAML vs OAuth vs OpenID Connect: What's the Difference?

Python7 min

How to Fix Python cron Jobs That Never Run

Linux22 min

Linux Bash Scripting: Full Tutorial for Beginners

Networking10 min

How PXE Network Boot Works

Active Directory9 min

How Active Directory Works

Linux10 min

How to Fix a Linux systemd Service That Won't Start

Linux9 min

Linux Logs: syslog vs journald

Auth (OAuth, JWT)9 min

JWT vs Session Cookies: Which One Should You Use?

Docker10 min

Docker vs Docker Compose: Which One Should You Use?

Docker9 min

How to Fix Docker Compose Containers That Can't Talk to Each Other

Django (Python web framework)22 min

Django Full Tutorial for Beginners

Linux9 min

How Linux systemd Works

Networking8 min

How to Fix 'Windows Cannot Access' Network Share Errors (SMB)

APIs10 min

REST vs gRPC: Which One Should You Use?

Certificates & PKI (ADCS)9 min

How to Fix SSL Certificate Chain Errors (Missing Intermediate)

Python9 min

Every Python Concept for Beginners in 10 Minutes

Databases & SQL9 min

Every SQL Join Type in 10 Minutes

OpenRouter (one API for many AI models)9 min

Why OpenRouter Free Models Hit Their Limit So Fast

Gemini API & Google AI Studio9 min

Why Your Gemini API Free Tier Runs Out So Fast

OpenRouter (one API for many AI models)10 min

OpenRouter Will Finally Make Sense After This

Caching & Redis10 min

How Redis Caching Works

Gemini API & Google AI Studio10 min

Gemini API vs Vertex AI: Which One Should You Use?

Gemini API & Google AI Studio8 min

How to Fix a Leaked Gemini API Key

Go (Golang)9 min

Why Golang Uses Goroutines Instead of Threads

Gemini API & Google AI Studio9 min

How to Fix Gemini API Error 429: Resource Exhausted

Claude & the Claude Developer Platform10 min

Claude Token Pricing Will Finally Make Sense After This

Python11 min

Why Python Web Scraping Returns Nothing (and the Fix)

Python10 min

Python uv vs pip: Which One Should You Use?

Python22 min

Python Automation: Full Tutorial for Beginners

Auth (OAuth, JWT)9 min

How OAuth Login Works

Gemini API & Google AI Studio10 min

Google AI Studio Will Finally Make Sense After This

Docker10 min

What Happens When You Run a Docker Container

Claude & the Claude Developer Platform8 min

How to Fix Claude Code Billing Your API Key Instead of Your Plan

Auth (OAuth, JWT)9 min

Why Passkeys Can't Be Phished Like Passwords

PowerShell10 min

PowerShell vs Bash: Which One Should You Learn?

Linux10 min

The Linux Roadmap for Beginners

Django (Python web framework)9 min

Django vs FastAPI: Which One Should You Use?

Databases & SQL10 min

Why SQLite Is the Most Used Database in the World

Databases & SQL11 min

How a NoSQL Database Like MongoDB Works

Python11 min

How to Fix Python pip error: externally-managed-environment

Databases & SQL9 min

How to Fix MySQL Too Many Connections Database Errors

Go (Golang)9 min

Golang Concurrency Will Finally Make Sense After This

AWS10 min

How Amazon S3 Works

Kubernetes10 min

How Kubernetes Works

Git10 min

How Git Works

Docker10 min

How to Fix Docker Port Already Allocated Errors

PostgreSQL10 min

How PostgreSQL Works

Git8 min

How to Fix Git Merge Conflicts

Linux9 min

How to Fix No Space Left on Device in Linux

Networking10 min

How the Internet Works

Databases & SQL9 min

SQL vs NoSQL: When Each One Wins

Auth (OAuth, JWT)8 min

How to Fix JWT Token Expired Errors (401 Unauthorized)

Claude & the Claude Developer Platform9 min

Claude API vs Claude Pro: Why Your Subscription Doesn't Include It

AI agent skills9 min

Why AI Agent Skills Beat Giant Prompts

Linux10 min

Linux Cron Jobs Will Finally Make Sense After This

Cybersecurity10 min

Security Logs Will Finally Make Sense After This

Cybersecurity8 min

How to Fix a Security Leak: An API Key Pushed to GitHub

DNS9 min

DNS Will Finally Make Sense After This

Cybersecurity9 min

Never Ignore a Security Update

Auth (OAuth, JWT)9 min

Two-Factor Authentication Will Finally Make Sense

Networking8 min

Never Expose a Database to the Internet

Cybersecurity10 min

SSH Keys vs Passwords: Which Is Safer for Security?

Linux10 min

The 5 Levels of Linux Boot: From Power On to Login

Networking12 min

The 5 Levels of Network Firewalls

Cybersecurity10 min

Password Security: Why Hackers Crack Weak Passwords

Linux9 min

Why Your Linux Server Runs Out of Memory

Networking10 min

HTTP vs HTTPS: What Actually Changes on Your Network

Linux12 min

Every Linux File Permission You Need to Know

Linux10 min

Linux Processes vs Threads: When Each One Wins

Networking11 min

The OSI Model Will Finally Make Sense After This

Git11 min

The 7 Levels of Git

Networking10 min

Every Network Troubleshooting Command You Need

Cybersecurity10 min

Hashing vs Encryption in Cybersecurity: When Each One Wins

Networking11 min

Why the Internet Ran Out of IP Addresses

Linux11 min

Linux Package Managers: apt vs Snap vs Flatpak

Python9 min

Why Python Is Slow (and When It Doesn't Matter)

Networking11 min

TCP vs UDP: When Each One Wins

Linux8 min

Stop Running Linux as Root (Do This Instead)

Linux11 min

Linux Users and Groups Will Finally Make Sense

Cybersecurity9 min

Why Phishing Beats Good Security (and How to Spot It)

Docker12 min

The 7 Levels of Docker

AI agents & MCP12 min

AI Agents Will Finally Make Sense After This

Git8 min

Stop Using git push --force (Use This Instead)

AI agent skills10 min

Agent Skills vs MCP: When Each One Wins

Networking12 min

Networking Will Finally Make Sense After This

Linux12 min

Every Linux Command You Actually Need

Cybersecurity12 min

The 7 Levels of Cybersecurity

System design12 min

Every System Design Concept You Need, in One Diagram

PostgreSQL10 min

Why PostgreSQL Ignores Your Index