Docker:无法连接容器中的mysql

 本多天舞 发布于 2022-10-25 08:40
  • 环境

    • Macbook pro 10.10

    • DockerToolbox 1.8 & Docker 1.8.2

  • 先看一下错误输出吧

$ mysql -uroot -p -h192.168.99.100 -P3306        
Enter password: 
ERROR 2003 (HY000): Can't connect to MySQL server on '192.168.99.100' (61)

$ mysqladmin -h192.168.99.100 --port=3306 version                                                                                                                                                      127 ↵
mysqladmin: connect to server at '192.168.99.100' failed
error: 'Can't connect to MySQL server on '192.168.99.100' (61)'
Check that mysqld is running on 192.168.99.100 and that the port is 3306.
You can check this by doing 'telnet 192.168.99.100 3306'

DEBUG的过程

  • 容器状态&端口映射

$ docker ps -l 
CONTAINER ID        IMAGE               COMMAND                  CREATED             STATUS              PORTS                    NAMES
b20584bc086e        sunzy/mysql         "/usr/bin/mysqld_safe"   21 minutes ago      Up 21 minutes       0.0.0.0:3306->3306/tcp   mysql
  • 宿主机的IP

$ docker-machine ls
NAME      ACTIVE   DRIVER       STATE     URL                         SWARM
default   *        virtualbox   Running   tcp://192.168.99.100:2376   
$ docker-machine ls
NAME      ACTIVE   DRIVER       STATE     URL                         SWARM
default   *        virtualbox   Running   tcp://192.168.99.100:2376   
  • PING

$ ping 192.168.99.100                                                                                                                                                                                    1 ↵
PING 192.168.99.100 (192.168.99.100): 56 data bytes
64 bytes from 192.168.99.100: icmp_seq=0 ttl=64 time=0.347 ms
64 bytes from 192.168.99.100: icmp_seq=1 ttl=64 time=0.388 ms
64 bytes from 192.168.99.100: icmp_seq=2 ttl=64 time=0.272 ms
  • Telnet

$ telnet 192.168.99.100 3306                                                                                                                                                                             1 ↵
Trying 192.168.99.100...
telnet: connect to address 192.168.99.100: Connection refused
telnet: Unable to connect to remote host
  • 在容器内连接

# mysql -uroot -p
Enter password: 
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 4
Server version: 5.5.44-0ubuntu0.14.04.1 (Ubuntu)

Copyright (c) 2000, 2015, Oracle and/or its affiliates. All rights reserved.

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> 
mysql> 
  • mysql允许任意IP连接

mysql> select user,host,password from mysql.user;
+------------------+--------------+-------------------------------------------+
| user             | host         | password                                  |
+------------------+--------------+-------------------------------------------+
| root             | localhost    | *6BB4837EB74329105EE4568DDA7DC67ED2CA2AD9 |
| root             | e611e15f9c9d | *6BB4837EB74329105EE4568DDA7DC67ED2CA2AD9 |
| root             | 127.0.0.1    | *6BB4837EB74329105EE4568DDA7DC67ED2CA2AD9 |
| root             | ::1          |                                           |
| debian-sys-maint | localhost    | *1BD91232C0D2D2B5500129217CEF09B7EE71ABEC |
| root             | %            | *6BB4837EB74329105EE4568DDA7DC67ED2CA2AD9 |
+------------------+--------------+-------------------------------------------+
3 个回答
  • docker run --name djistore_mysql -p 3306:3306 -e MYSQL_ROOT_PASSWORD=root -d mysql:5.6.27 --character-set-server=utf8mb4 --collation-server=utf8mb4_unicode_ci

    连接到该 MySQL 服务

    mysql -h 192.168.99.100 -u root -p

    相关的日志

    ➜  ~ mysql -h 192.168.99.100 -u root -p
    Enter password:
    Welcome to the MySQL monitor.  Commands end with ; or \g.
    Your MySQL connection id is 1
    Server version: 5.6.27 MySQL Community Server (GPL)
    
    Copyright (c) 2000, 2016, Oracle and/or its affiliates. All rights reserved.
    
    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> show databases;
    +--------------------+
    | Database           |
    +--------------------+
    | information_schema |
    | mysql              |
    | performance_schema |
    +--------------------+
    3 rows in set (0.01 sec)
    2022-11-12 01:39 回答
  • 宿主机的防火墙 开着么? sudo service iptables status 看看。

    在宿主机上 执行 sudo netstat -lnp | grep 3306 看看端口3306 被哪个进程使用中。

    2022-11-12 01:39 回答
  • 突然想起来以前用vagrant也遇到这种情况,不过那个报的是2013-Lost connection to MySQL server at ‘reading initial communication packet错误=>围观

    1. 打开mysql配置文件 sudo vim /etc/mysql/my.cnf

    2. 找到bind-address,把127.0.0.1改成0.0.0.0或者注释掉该行

    3. 重启mysql

    试了一下,正常了

    2022-11-12 01:39 回答
撰写答案
今天,你开发时遇到什么问题呢?
立即提问
热门标签
PHP1.CN | 中国最专业的PHP中文社区 | PNG素材下载 | DevBox开发工具箱 | json解析格式化 |PHP资讯 | PHP教程 | 数据库技术 | 服务器技术 | 前端开发技术 | PHP框架 | 开发工具 | 在线工具
Copyright © 1998 - 2020 PHP1.CN. All Rights Reserved 京公网安备 11010802041100号 | 京ICP备19059560号-4 | PHP1.CN 第一PHP社区 版权所有