运行环境
- 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
$ 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 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 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)
bind-address=0.0.0.0
发现问题
我有一台其他机器,都是正常的,目前跑着web网站,所以对比一下发现问题如下:
/ # 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
/ # 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文件的问题?