Docker MySQL无法被宿主机访问如何解决

来源:这里教程网 时间:2026-02-28 16:08:20 作者:

1 问题描述

docker
启动
mysql
容器后,创建一个
localhost
访问的用户:

create user test@localhost identified by 'test';

但是在宿主机中无法通过该用户登录:

mycli -u test

Docker MySQL无法被宿主机访问如何解决

2 原因

Docker
中的
MySQL
创建
localhost
的用户只能在
Docker
内部访问,而不能通过外部访问。

至于为什么能在宿主机访问

root
,是因为默认存在两个
root
,分别是:

root@localhost

root@%

Docker MySQL无法被宿主机访问如何解决

test
只有一个
localhost

Docker MySQL无法被宿主机访问如何解决

3 解决方案

创建

test@%
或者创建
test@172.17.0.1
即可:

create user test@% identified by 'test';
create user test@172.17.0.1 identified by 'test';

相关推荐