简体   繁体   English

无法连接到 Mysql 5.7 运行为 docker 容器从 docker 主机使用 Z81C3BE090DAD54BF527E1

[英]Unable to connect to Mysql 5.7 running as docker container from docker host using mysql client

After going through the following links from SOF- I still am facing the issue and there is no reasoning or solution available.通过 SOF 的以下链接后,我仍然面临这个问题,并且没有可用的推理或解决方案。

References:参考:

Mysql 5.7 is running as container in my local machine. Mysql 5.7 在我的本地机器中作为容器运行。 Once the server is up, connecting to the server using root or the new user created fails over localhost/0.0.0.0/127.0.0.1.服务器启动后,使用 root 连接到服务器或创建的新用户将故障转移到 localhost/0.0.0.0/127.0.0.1。 Even after disabling the firewald, the connectivity failed with the same error.即使禁用防火墙后,连接也失败并出现相同的错误。 I might have done something terribly wrong about this connectivity.我可能在这种连接上做错了什么。 Any advice would be helpful.任何意见将是有益的。

version: '3.5'
services:
  service:
    image: mysql:5.7
    #build:
    #  context: ./
    #  dockerfile: Dockerfile
    environment:
      MYSQL_USER: user
      MYSQL_PASSWORD: manage
      MYSQL_ROOT_PASSWORD: manage
      MYSQL_DATABASE: testapp
    ports:
      - published: 3306
        target: 3306

Following is the output after docker-compose up -d以下是docker-compose up -d之后的 output

docker ps
CONTAINER ID        IMAGE               COMMAND                  CREATED             STATUS              PORTS                               NAMES
45ba02e6e457        mysql:5.7           "docker-entrypoint.s…"   52 seconds ago      Up 50 seconds       0.0.0.0:3306->3306/tcp, 33060/tcp   

Below are the attempts to connect to the mysql container.以下是连接到 mysql 容器的尝试。

mysql -u root -h localhost -pmanage
mysql: [Warning] Using a password on the command line interface can be insecure.
ERROR 2002 (HY000): Can't connect to local MySQL server through socket '/var/run/mysqld/mysqld.sock' (2)

mysql -u root -h 0.0.0.0 -pmanage
mysql: [Warning] Using a password on the command line interface can be insecure.
ERROR 2002 (HY000): Can't connect to local MySQL server through socket '/var/run/mysqld/mysqld.sock' (2)

mysql -u root -h 127.0.0.1 -pmanage
mysql: [Warning] Using a password on the command line interface can be insecure.
ERROR 2002 (HY000): Can't connect to local MySQL server through socket '/var/run/mysqld/mysqld.sock' (2)

IP of the running container works fine.正在运行的容器的 IP 工作正常。 But this is not a solution as the IP can change with time.但这不是解决方案,因为 IP 会随时间而变化。

mysql -u root -h 172.31.0.2 -p
Enter password: 
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 2
Server version: 5.7.30 MySQL Community Server (GPL)

Copyright (c) 2000, 2020, 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.

I am not looking for solutions with another docker command.我不是在寻找另一个 docker 命令的解决方案。 How to connect to mysql container from docker host using mysql client.如何使用 mysql 客户端从 docker 主机连接到 mysql 容器。

You need to use the --protocol=tcp in your connection url.您需要在连接 url 中使用--protocol=tcp

mysql -u root -h localhost --protocol=tcp

Your mysql is running inside the docker container.您的 mysql 在 docker 容器内运行。 So, you have to connect through TCP and local socket is not available for that.因此,您必须通过 TCP 进行连接,并且本地套接字不可用。 This is basically duplicate of this question.这基本上是这个问题的重复

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM