Docker中启动MySQL客户端遇用户访问拒绝错误求助
Hey there, let's work through this MySQL access denied error you're hitting in your Docker container. I’ve dealt with this exact issue a few times, so here are the most common fixes to try out:
It’s easy to mix up your local MySQL server and the one running inside Docker. When you run docker exec -it mysql mysql -u root -p, you’re trying to connect to the MySQL service inside the container—not your local Windows MySQL instance (which you said works fine).
- Quick test: Temporarily stop your local MySQL service by running
net stop mysqlin Command Prompt, then retry the Docker exec command. If it still fails, the problem is definitely with the container’s MySQL setup. - Confirm the container is running: Use
docker psto check if themysqlcontainer shows an "Up" status. If it’s stopped, start it withdocker start mysql.
When you first launched the MySQL container, you should’ve set the MYSQL_ROOT_PASSWORD environment variable. If you skipped this or used the wrong password, that’s almost certainly why you’re getting the 1045 error.
- If you haven’t already, recreate the container with a clear root password (replace
your_strong_passwordwith your desired password):docker run -d --name mysql -e MYSQL_ROOT_PASSWORD=your_strong_password -p 3306:3306 mysql:8.0 - If the container’s already running, you can’t update the password via environment variables anymore. Instead, reset it by bypassing authentication:
- Stop the container:
docker stop mysql - Restart it with grant tables skipped:
docker run -d --name mysql -p 3306:3306 mysql:8.0 --skip-grant-tables --skip-networking - Exec into the container without a password:
docker exec -it mysql mysql - Run these SQL commands to reset the root password (replace
new_secure_password):FLUSH PRIVILEGES; ALTER USER 'root'@'localhost' IDENTIFIED BY 'new_secure_password'; - Stop and restart the container normally:
docker stop mysql && docker start mysql
- Stop the container:
MySQL 8.0 has stricter default permissions. Sometimes the root user inside the container is configured to only allow connections from specific hosts, or uses an authentication plugin that’s causing issues for local connections.
- If you can exec into the container (using the skip-grant-tables method above if needed), check the root user’s host entries:
SELECT user, host FROM mysql.user WHERE user = 'root'; - If you don’t see
root@'localhost'listed, create it and grant full privileges:CREATE USER IF NOT EXISTS 'root'@'localhost' IDENTIFIED BY 'your_password'; GRANT ALL PRIVILEGES ON *.* TO 'root'@'localhost' WITH GRANT OPTION; FLUSH PRIVILEGES; - If you’re still having issues, try switching the authentication plugin to
mysql_native_password(this is less secure but can fix compatibility quirks with local client connections):ALTER USER 'root'@'localhost' IDENTIFIED WITH mysql_native_password BY 'your_password'; FLUSH PRIVILEGES;
Sometimes the MySQL client inside the container tries to use a Unix socket that isn’t properly configured, causing localhost connection issues. Try explicitly specifying the host as 127.0.0.1 when connecting:
docker exec -it mysql mysql -u root -p -h 127.0.0.1
This forces a TCP/IP connection, which often bypasses socket-related permission problems.
If none of the above works, dig into the container’s logs to see if there was an error during MySQL initialization:
docker logs mysql
Look for lines mentioning root user setup, password initialization, or permission failures—these can point you to the exact issue you’re facing.
内容的提问来源于stack exchange,提问作者Lauren Bolin

