You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

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:

1. Make sure you’re targeting the right MySQL instance

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 mysql in 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 ps to check if the mysql container shows an "Up" status. If it’s stopped, start it with docker start mysql.
2. Validate your container’s root password setup

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_password with 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:
    1. Stop the container: docker stop mysql
    2. Restart it with grant tables skipped:
      docker run -d --name mysql -p 3306:3306 mysql:8.0 --skip-grant-tables --skip-networking
      
    3. Exec into the container without a password: docker exec -it mysql mysql
    4. Run these SQL commands to reset the root password (replace new_secure_password):
      FLUSH PRIVILEGES;
      ALTER USER 'root'@'localhost' IDENTIFIED BY 'new_secure_password';
      
    5. Stop and restart the container normally: docker stop mysql && docker start mysql
3. Fix the root user’s host permissions in 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;
    
4. Force a TCP/IP connection instead of using the socket

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.

5. Check the container logs for hidden clues

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

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.08 23:22:35