Docker mysql不能连接到容器 [英] Docker mysql cant connect to container

查看:728
本文介绍了Docker mysql不能连接到容器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个docker-compose文件,用于创建mysql映像并将端口公开到3306,但是当我尝试安装CMS时,会给我错误的连接到数据库。我尝试扫描3306端口,并显示我已经打开,所以mysql正在运行。

I've got docker-compose file for creating mysql image and expose port to 3306, but when I try to install CMS, it gives me error that it can't connect to Database. I try to scan port 3306 and it's showing me that it's open so mysql is running.

为什么两个docker容器看不到对方?

Why the two of docker containers can't see each other ?

这是我的docker-compose文件:

Here is my docker-compose file:

phpfpm:
  restart: always
  extends:
    file: php-fpm-5.6.yml
    service: phpfpm
  links:
    - db:db

nginx:
  restart: always
  image: nginx
  ports:
    - "8000:80"
  links:
    - phpfpm:phpfpm
  volumes:
    - ./nginx/vhost.conf:/etc/nginx/conf.d/default.conf
    - ./app:/var/www/html
    - ./log/nginx:/var/log/nginx

db:
  restart: always
  image: mysql
  ports:
    - "3306:3306"
  environment:
    MYSQL_ROOT_PASSWORD: 123456
    MYSQL_USER: user
    MYSQL_PASSWORD: password
    MYSQL_DATABASE: database


推荐答案

要连接到数据库,请使用th您提供的e链接/别名作为主机名。所以,CMS可以使用 db 作为主机名和3306端口连接到MySQL。

To connect to the database, use the link/alias you provided as a hostname. So, you CMS can connect to MySQL using db as hostname, and port 3306.

你不会能够连接到localhost或127.0.0.1,因为localhost是每个容器 中的本地主机,所以在phpfpm容器中使用localhost将尝试连接到phpfpm容器内的一个MySQL数据库,但是没有服务器在那里运行。

You won't be able to connect to localhost or 127.0.0.1, because "localhost" is the localhost inside each container, so, using "localhost" in the phpfpm container will try to connect to a MySQL database inside the phpfpm container, but there's no server running there.


请注意,您不必发布(3306:3306)MySQL端口,如果您只从连接的容器内部连接到数据库。发布端口在公共网络接口上公开MySQL,这可能是互联网

Note that you don't have to publish ("3306":"3306") the MySQL ports if you only connect to the database from inside the linked containers. Publishing the ports exposes MySQL on the public network interface, which may be "the Internet"

这篇关于Docker mysql不能连接到容器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆