当前位置: 首页 > 工具软件 > docker-lnp > 使用案例 >

docker run -i -t mysql_docker run mysql成功,但是3306端口不通?

储国发
2023-12-01

运行环境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 msTelnet$ 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文件的问题?

 类似资料: