问题
如标题
背景
- windows机器中安装CentOS 7.x虚拟机,windows中通过ssh连接CentOS机器
- centOS中安装docker
- docker中安装mysql镜像,并启动
- 试图从windows客户端中连接此镜像mysql
问题
docker pull mysql
docker run -e MYSQL_ROOT_PASSWORD=my-secret-pw -d -P mysql:latest
得到
[root@localhost /]# docker run -e MYSQL_ROOT_PASSWORD=my-secret-pw -d -P mysql:latest
792047fe0054d51cd4eb0ada74e5e2f14085b17402772a3f31aa34f430998488
[root@localhost /]# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
792047fe0054 mysql:latest "docker-entrypoint.s…" 2 seconds ago Up 1 second 0.0.0.0:49154->3306/tcp, 0.0.0.0:49153->33060/tcp some-mysql2
[root@localhost /]# netstat -tunlp
Active Internet connections (only servers)
Proto Recv-Q Send-Q Local Address Foreign Address State PID/Program name
tcp 0 0 0.0.0.0:49154 0.0.0.0:* LISTEN 33027/docker-proxy
tcp 0 0 0.0.0.0:22 0.0.0.0:* LISTEN 993/sshd
tcp 0 0 0.0.0.0:49153 0.0.0.0:* LISTEN 33016/docker-proxy
tcp6 0 0 :::22 :::* LISTEN 993/sshd
udp 0 0 0.0.0.0:68 0.0.0.0:* 5243/dhclient
可见mysql镜像成功启动。
现在试图从windows客户端中访问。
上个示意图:
此后有一个问题出现
mysql默认是只允许本机连接的,即外界的,windows等机器是无法连接的。
(后来我又启动的mysql 容器就可以从windows中直接连接了,我也莫名其妙)
这是一个问题。
-
巧妙方法:
docker exec -it 792047fe0054 /bin/bash
即直接进入mysql的容器的 /bin/bash中。 -
然后即可成功进入此mysql。
如下图:
[root@localhost ~]# docker exec -it 792047fe0054 /bin/bash
root@792047fe0054:/# mysql -uroot -p
Enter password:
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 10
Server version: 8.0.27 MySQL Community Server - GPL
Copyright (c) 2000, 2021, Oracle and/or its affiliates.
Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.
Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.
mysql>
上个示意图
剩下的就可以自由发挥了。
上图留念: