我试图在一台主机服务器上运行多个mysql docker容器。设置一个第一个mysql容器后,它工作正常。我按照相同的步骤设置第二个并将运行端口从3306更改为3307并使用不同的dump.sql路径。容器启动后,我无法使用端口容器ip telnet容器,但我可以ping ip。它显示连接被拒绝。想要将绑定地址= 0.0.0.0的行添加到/etc/my.cnf并重启容器,问题仍然是一样的。我可以看到第二个mysql容器启动,我能够在容器上执行一些命令但是大约1分钟后,容器自动停止
usr@ip-172-0-1-199:~/$ docker run --name=test-db -p 3310:3306 -e MYSQL_ROOT_PASSWORD=pass -e MYSQL_USER=user -e MYSQL_DATABASE=db -e MYSQL_PASSWORD=pass -v dump.sql:/var/lib/mysql -d mysql/mysql-server:5.7
0e3fbcbd9126a726584549a13248eb07eb7d97fbeba4795fb5a924037a66b205
usr@ip-172-0-1-199:~/$ docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
0e3fbcbd9126 mysql/mysql-server:5.7 "/entrypoint.sh mysql" 29 seconds ago Up 28 seconds 33060/tcp, 0.0.0.0:3310->3306/tcp test-db
usr@ip-172-0-1-199:~/$ docker inspect --format '{{ .NetworkSettings.IPAddress }}' test-db
172.17.0.4
usr@ip-172-0-1-199:~/$ telnet 172.17.0.4 3310
Trying 172.17.0.4...
telnet: Unable to connect to remote host: Connection refused
usr@ip-172-0-1-199:~/$ ping 172.17.0.4
PING 172.17.0.4 (172.17.0.4) 56(84) bytes of data.
64 bytes from 172.17.0.4: icmp_seq=1 ttl=64 time=0.037 ms
^C
--- 172.17.0.4 ping statistics ---
3 packets transmitted, 3 received, 0% packet loss, time 1999ms
rtt min/avg/max/mdev = 0.037/0.042/0.053/0.009 ms
usr@ip-172-0-1-199:~/$ docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
0e3fbcbd9126 mysql/mysql-server:5.7 "/entrypoint.sh mysql" About a minute ago Up About a minute 33060/tcp, 0.0.0.0:3310->3306/tcp test-db
usr@ip-172-0-1-199:~/$ docker ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
0e3fbcbd9126 mysql/mysql-server:5.7 "/entrypoint.sh mysql" About a minute ago Up About a minute 33060/tcp, 0.0.0.0:3310->3306/tcp test-db
usr@ip-172-0-1-199:~/$ docker exec test-db "exec mysql -h 172.17.0.4 -u user -p"
Error response from daemon: Container 0e3fbcbd9126a726584549a13248eb07eb7d97fbeba4795fb5a924037a66b205 is not running
usr@ip-172-0-1-199:~/$ docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
usr@ip-172-0-1-199:~/$ docker ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
0e3fbcbd9126 mysql/mysql-server:5.7 "/entrypoint.sh mysql" 2 minutes ago Exited (1) 37 seconds ago test-db
答案 0 :(得分:0)
设置MYSQL_ROOT_HOST=%
以允许从任何IP进行root登录。
docker run --name mysql -p 3306:3306 \
-e MYSQL_RANDOM_ROOT_PASSWORD=yes \
-e MYSQL_ROOT_HOST=% -d mysql/mysql-server:latest