Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
3.3k views
in Technique[技术] by (71.8m points)

docker run mysql成功,但是3306端口不通?

运行环境

  • Centos7
  • Docker 18.06.3-ce
  • docker-compose 1.27.4

问题描述

Docker 运行 mysql 容器成功后,无法连接到容器内的mysql服务,telnet 也不通。

容器状态

  • 启动容器
docker run --name mysql -p 33306:3306 -e MYSQL_ROOT_PASSWORD=123456 -d wangyongdong/docker-mysql
  • 容器状态&端口映射
$ docker ps -l
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
77e1401c35c8 wangyongdong/docker-mysql "/scripts/startup.sh" 6 minutes ago Up 6 minutes 0.0.0.0:3306->3306/tcp docker-mysql
  • 容器ip
$ docker inspect --format '{{ .NetworkSettings.IPAddress }}' docker-mysql
172.17.0.2
  • 宿主机端口
$ sudo netstat -lnp | grep 3306
tcp6 0 0 :::3306 :::* LISTEN 9895/docker-proxy
$ ps -ef | grep 9895
root 9895 13947 0 00:35 ? 00:00:00 /usr/bin/docker-proxy -proto tcp -host-ip 0.0.0.0 -host-port 3306 -container-ip 172.17.0.2 -container-port 3306
root 10231 8567 0 00:46 pts/0 00:00:00 grep -i --color=auto 9895

Debug 过程

  • ping
$ ping 172.17.0.2
PING 172.17.0.2 (172.17.0.2) 56(84) bytes of data.
64 bytes from 172.17.0.2: icmp_seq=1 ttl=64 time=0.077 ms
64 bytes from 172.17.0.2: icmp_seq=2 ttl=64 time=0.091 ms
64 bytes from 172.17.0.2: icmp_seq=3 ttl=64 time=0.070 ms
64 bytes from 172.17.0.2: icmp_seq=4 ttl=64 time=0.421 ms
  • Telnet
$ telnet 172.17.0.2 3306
Trying 172.17.0.2...
telnet: connect to address 172.17.0.2: Connection refused
  • 容器内登陆
$ docker exec -it mysql /bin/sh
/ # mysql -u root -p
Enter password:
Welcome to the MariaDB monitor. Commands end with ; or g.
Your MariaDB connection id is 9
Server version: 10.3.25-MariaDB-log MariaDB Server

Copyright (c) 2000, 2018, Oracle, MariaDB Corporation Ab and others.

Type 'help;' or 'h' for help. Type 'c' to clear the current input statement.

MariaDB [(none)]>
  • 查询用户权限
MariaDB [mysql]> select user,host,password from user;
+------+-----------+-------------------------------------------+
| user | host | password |
+------+-----------+-------------------------------------------+
| root | localhost | *6BB4837EB74329105EE4568DDA7DC67ED2CA2AD9 |
| root | % | *6BB4837EB74329105EE4568DDA7DC67ED2CA2AD9 |
+------+-----------+-------------------------------------------+
2 rows in set (0.000 sec)
  • /etc/mysql/my.cnf 配置文件
bind-address=0.0.0.0

发现问题

我有一台其他机器,都是正常的,目前跑着web网站,所以对比一下发现问题如下:

  • 正常的 mysql 容器内网络
/ # netstat -a
Active Internet connections (servers and established)
Proto Recv-Q Send-Q Local Address Foreign Address State
tcp 0 0 127.0.0.11:40741 0.0.0.0:* LISTEN
tcp 0 0 :::mysql :::* LISTEN
udp 0 0 127.0.0.11:55135 0.0.0.0:*
Active UNIX domain sockets (servers and established)
Proto RefCnt Flags Type State I-Node Path
unix 2 [ ACC ] STREAM LISTENING 1506857 /run/mysqld/mysqld.sock
  • 有问题的 mysql 容器内部查看网络
/ # netstat -a
Active Internet connections (servers and established)
Proto Recv-Q Send-Q Local Address Foreign Address State
tcp 0 0 127.0.0.11:42118 0.0.0.0:* LISTEN
udp 0 0 127.0.0.11:58271 0.0.0.0:*
Active UNIX domain sockets (servers and established)
Proto RefCnt Flags Type State I-Node Path
unix 2 [ ACC ] STREAM LISTENING 216478 /run/mysqld/mysqld.sock

对比发现正常的有 mysql tcp 的网络

当我重新执行上述操作在我的另一台机器上,一切都正常。

由于我属于docker新手,并不确定产生问题的原因,会不会是Dockerfile文件的问题?


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

-p 参数是 host:guest, 你启动的是

docker run --name mysql -p 33306:3306  。。。

所以在宿主机上要连 33306, 而不是 3306


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...