docker安装msyql并使用

问题

如标题

背景

  • windows机器中安装CentOS 7.x虚拟机,windows中通过ssh连接CentOS机器
  • centOS中安装docker
  • docker中安装mysql镜像,并启动
  • 试图从windows客户端中连接此镜像mysql

问题

  1. docker pull mysql
  2. 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中直接连接了,我也莫名其妙)
这是一个问题。

  1. 巧妙方法:docker exec -it 792047fe0054 /bin/bash
    即直接进入mysql的容器的 /bin/bash中。

  2. 然后即可成功进入此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> 

上个示意图
在这里插入图片描述
剩下的就可以自由发挥了。


上图留念:
在这里插入图片描述
在这里插入图片描述

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值