【问题标题】:Access denied for user 'root'@'localhost' in the mysql dockermysql docker中用户'root'@'localhost'的访问被拒绝
【发布时间】:2025-12-12 18:40:01
【问题描述】:

在以下命令中运行 docker mysql 容器

docker run -it --name mysql  -e MYSQL_ROOT_PASSWORD=123456 mysql

然后docker创建一个正在运行的mysql容器,日志为

2020-07-23 09:39:19+00:00 [Note] [Entrypoint]: Entrypoint script for MySQL Server 8.0.21-1debian10 started.
2020-07-23 09:39:20+00:00 [Note] [Entrypoint]: Switching to dedicated user 'mysql'
2020-07-23 09:39:20+00:00 [Note] [Entrypoint]: Entrypoint script for MySQL Server 8.0.21-1debian10 started.
2020-07-23 09:39:20+00:00 [Note] [Entrypoint]: Initializing database files
2020-07-23T09:39:20.408751Z 0 [Warning] [MY-011070] [Server] 'Disabling symbolic links using --skip-symbolic-links (or equivalent) is the default. Consider not using this option as it' is deprecated and will be removed in a future release.
2020-07-23T09:39:20.408909Z 0 [System] [MY-013169] [Server] /usr/sbin/mysqld (mysqld 8.0.21) initializing of server in progress as process 42
2020-07-23T09:39:20.424771Z 1 [System] [MY-013576] [InnoDB] InnoDB initialization has started.
2020-07-23T09:39:22.342488Z 1 [System] [MY-013577] [InnoDB] InnoDB initialization has ended.
2020-07-23T09:39:26.473394Z 6 [Warning] [MY-010453] [Server] root@localhost is created with an empty password ! Please consider switching off the --initialize-insecure option.
2020-07-23 09:39:35+00:00 [Note] [Entrypoint]: Database files initialized
2020-07-23 09:39:35+00:00 [Note] [Entrypoint]: Starting temporary server
mysqld will log errors to /var/lib/mysql/da5f3f1ae045.err
mysqld is running as pid 91
2020-07-23 09:39:37+00:00 [Note] [Entrypoint]: Temporary server started.
Warning: Unable to load '/usr/share/zoneinfo/iso3166.tab' as time zone. Skipping it.
Warning: Unable to load '/usr/share/zoneinfo/leap-seconds.list' as time zone. Skipping it.
Warning: Unable to load '/usr/share/zoneinfo/zone.tab' as time zone. Skipping it.
Warning: Unable to load '/usr/share/zoneinfo/zone1970.tab' as time zone. Skipping it.

2020-07-23 09:39:56+00:00 [Note] [Entrypoint]: Stopping temporary server
2020-07-23 09:39:59+00:00 [Note] [Entrypoint]: Temporary server stopped

2020-07-23 09:39:59+00:00 [Note] [Entrypoint]: MySQL init process done. Ready for start up.

2020-07-23T09:39:59.908211Z 0 [Warning] [MY-011070] [Server] 'Disabling symbolic links using --skip-symbolic-links (or equivalent) is the default. Consider not using this option as it' is deprecated and will be removed in a future release.
2020-07-23T09:39:59.910343Z 0 [System] [MY-010116] [Server] /usr/sbin/mysqld (mysqld 8.0.21) starting as process 1
2020-07-23T09:39:59.945124Z 1 [System] [MY-013576] [InnoDB] InnoDB initialization has started.
2020-07-23T09:40:00.579927Z 1 [System] [MY-013577] [InnoDB] InnoDB initialization has ended.
2020-07-23T09:40:00.969050Z 0 [System] [MY-011323] [Server] X Plugin ready for connections. Bind-address: '::' port: 33060, socket: /var/run/mysqld/mysqlx.sock
2020-07-23T09:40:01.137873Z 0 [Warning] [MY-010068] [Server] CA certificate ca.pem is self signed.
2020-07-23T09:40:01.138424Z 0 [System] [MY-013602] [Server] Channel mysql_main configured to support TLS. Encrypted connections are now supported for this channel.
2020-07-23T09:40:01.145079Z 0 [Warning] [MY-011810] [Server] Insecure configuration for --pid-file: Location '/var/run/mysqld' in the path is accessible to all OS users. Consider choosing a different directory.
2020-07-23T09:40:01.233430Z 0 [System] [MY-010931] [Server] /usr/sbin/mysqld: ready for connections. Version: '8.0.21'  socket: '/var/run/mysqld/mysqld.sock'  port: 3306  MySQL Community Server - GPL.

尝试使用

访问容器
mysql -h localhost -u root -p

它要求输入密码,使用 123456 作为密码失败;使用空密码也会失败。

ERROR 1045 (28000): Access denied for user 'root'@'localhost' (using password: YES)

【问题讨论】:

  • 是否传递了正确的密码? mysql -h localhost -u root -p123456?
  • 是的,我输入了正确的密码。我也用tableplus用root和密码123456访问mysql,也失败了
  • 你从哪里执行mysql -h localhost -u root -p
  • 您是否在容器内运行了该命令?还是您尝试连接表单主机?
  • 通过“docker exec -it mysql mysql -uroot -p”,提示输入密码123456,就可以成功访问mysql命令行了。但是为什么我不能使用 root@localhost 和 123456 作为密码的 tableplus。

标签: mysql docker


【解决方案1】:

docker exec -it mysql mysql -uroot -p,然后提示输入密码123456,就可以成功访问mysql命令行了。但是为什么我不能使用 tableplus

根据上面的评论,验证您可以访问容器内的mysql 服务器,所以tableplus 的问题不是容器根密码。

只需发布端口,它应该可以工作,

docker run -it -p 3306:3306 --name mysql  -e MYSQL_ROOT_PASSWORD=123456 mysql

此外,似乎tableplus 连接主机MySQL 而不是容器一,因为在您的情况下,容器没有发布端口,因此它试图连接到其他地方。

【讨论】:

  • 我试过 docker run -it -p 3306:3306 --name mysql -e MYSQL_ROOT_PASSWORD=123456 mysql,它告诉我 docker: Error response from daemon: Ports are not available: listen tcp 0.0.0.0 :3306: 绑定:地址已在使用中。然后我用了 lsof -n -i:3306,发现没有运行端口 3306 的进程,有什么想法吗?
  • 似乎端口已被占用,您应该使用 sudo 运行我认为sudo lsof -n -i:3306 可能是用户问题。或者只是暴露另一个端口来测试-p 3305:3306 和测试连接
【解决方案2】:

可能是这个 MySQL 安装使用了操作系统的身份验证机制。而不是root密码。

要测试是否是这种情况,请执行以下命令:

sudo mysql -u root

它会询问您的操作系统特权密码,而不是 MySQL 根密码(可能未设置)。

进入后,您可以像往常一样添加新用户,如 MySQL 手册和教程中所述。

另外,您可能想尝试这里提到的说明:

https://dev.mysql.com/doc/refman/8.0/en/resetting-permissions.html

请参阅“B.4.3.2.3 重置 Root 密码:通用说明”部分。

在下面引用它(并稍微清理一下),以防网站无法访问:

必要时停止 MySQL 服务器,然后使用 --skip-grant-tables 选项(请参阅How to start MySQL with --skip-grant-tables? 了解一些方法)

使用mysql客户端连接MySQL服务器;没有密码是 必要的,因为服务器是用 --skip-grant-tables 启动的:

外壳> mysql

在mysql客户端,告诉服务器重新加载授权 表格,以便帐户管理报表起作用:

mysql> 刷新权限;

然后更改'root'@'localhost'帐户 密码。将密码替换为您要使用的密码。 更改具有不同主机名的 root 帐户的密码 部分,修改说明以使用该主机名。

mysql> ALTER USER 'root'@'localhost' IDENTIFIED BY 'MyNewPass';

你 现在应该能够使用 root 连接到 MySQL 服务器 新密码。停止服务器并正常重新启动它(没有 --skip-grant-tables 选项)。

【讨论】:

  • 输入操作系统密码后直接显示“Access denied for user 'root'@'localhost' (using password: NO)”
  • @user824624 所以,root 密码似乎以某种奇怪的方式被破解了。我添加了一个链接,指向我对修复损坏的 root 帐户的官方说明的回答。
  • @user824624 用更简单的说明和额外的有用链接更新了答案。