docker-compose wordpress mysql 连接被拒绝

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/34068671/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-08 11:10:52  来源:igfitidea点击:

docker-compose wordpress mysql connection refused

wordpressdockermariadbdocker-compose

提问by Harry

I've created a small docker-compose.ymlwhich used to work like a charm to deploy small WordPress instances. It looks like this:

我创建了一个小型的docker-compose.yml,它曾经像一个魅力一样工作来部署小型 WordPress 实例。它看起来像这样:

wordpress:
  image: wordpress:latest
  links:
   - mysql
  ports:
   - "1234:80"
  environment:
    WORDPRESS_DB_USER: wordpress
    WORDPRESS_DB_NAME: wordpress
    WORDPRESS_DB_PASSWORD: "password"
    WORDPRESS_DB_HOST: mariadb
    MYSQL_PORT_3306_TCP: 3306
  volumes:
    - /srv/wordpress/:/var/www/html/
mysql:
  image: mariadb:latest
  mem_limit: 256m
  container_name: mariadb
  environment:
    MYSQL_ROOT_PASSWORD: "password"
    MYSQL_DATABASE: wordpress
    MYSQL_USER: wordpress
    MYSQL_PASSWORD: "password"
  volumes:
    - /srv/mariadb:/var/lib/mysql

But when I start it now (maybe since docker update to Docker version 1.9.1, build a34a1d5), it fails

但是当我现在启动它时(也许因为 docker update 到 Docker version 1.9.1, build a34a1d5),它失败了

wordpress_1 | Warning: mysqli::mysqli(): (HY000/2002): Connection    refused in - on line 10
wordpress_1 | 
wordpress_1 | MySQL Connection Error: (2002) Connection refused

When I cat /etc/hostsof the wordpress_1there are entries for MySQL:

当我的猫/etc/hostswordpress_1有项针对MySQL:

172.17.0.10 mysql 12a564fdbc56 mariadb

and I am able to ping the MariaDB server.

我能够 ping MariaDB 服务器。

When I docker-compose up, WordPress gets installed and after several restarts the MariaDB container prints:

当我docker-compose up安装 WordPress 并在多次重新启动后,MariaDB 容器打印:

Version: '10.0.22-MariaDB-1~jessie'  socket: '/var/run/mysqld/mysqld.sock'  port: 3306  mariadb.org binary distribution

Which schould indicate it to be running, isn't it?

哪个应该表明它正在运行,不是吗?

How do I get the WordPress to be able to connect to the MariaDB container?

如何让 WordPress 能够连接到 MariaDB 容器?

采纳答案by Harry

The reason for this behaviour probably was related to a recent kernel and docker update. I recognized several other connection issues in other docker-compose setups. Therefore I restarted the server (not just the docker service) and didn't have had any issues like this ever since.

这种行为的原因可能与最近的内核和 docker 更新有关。我在其他 docker-compose 设置中发现了其他几个连接问题。因此,我重新启动了服务器(不仅仅是 docker 服务)并且从那时起就没有出现过任何这样的问题。

回答by Mohamed Salem Lamiri

To fix this issue the first thing to do is:

要解决这个问题,首先要做的是:

Add the following code to wordpress & database containers (in the docker-compose file):

将以下代码添加到 wordpress 和数据库容器(在 docker-compose 文件中):

restart: unless-stopped

This will make sure you Database is started and intialized before wordpress container trying to connect to it. Then restart docker engine

这将确保您的数据库在 wordpress 容器尝试连接到它之前启动并初始化。然后重启docker引擎

sudo restart docker

or (for ubuntu 15+)

或(适用于 ubuntu 15+)

sudo service docker restart 

Here the full configuration that worked for me, to setup wordpress with MariaDB:

这是对我有用的完整配置,用于使用 MariaDB 设置 wordpress:

version: '2'

services:
  wordpress:
    image: wordpress:latest
    links:
      - database:mariadb
    environment:
      - WORDPRESS_DB_USER=wordpress
      - WORDPRESS_DB_NAME=mydbname
      - WORDPRESS_TABLE_PREFIX=ab_
      - WORDPRESS_DB_PASSWORD=password
      - WORDPRESS_DB_HOST=mariadb
      - MYSQL_PORT_3306_TCP=3306
    restart: unless-stopped
    ports:
      - "test.dev:80:80"
    working_dir: /var/www/html
    volumes:
     - ./wordpress/:/var/www/html/
  database:
   image: mariadb:latest
   environment:
     - MYSQL_ROOT_PASSWORD=password
     - MYSQL_DATABASE=mydbname
     - MYSQL_USER=wordpress
     - MYSQL_PASSWORD=password
   restart: unless-stopped
   ports:
     - "3306:3306"

回答by Awakening Byte

I was using your docker-compose.yml, had the same problem. Just restarting didn't fix. After nearly an hour of researching the logs, I found the problem was: wordpressservice started connecting mysqlservice before it had fully started. Simply adding depends_on won't help.Docker Compose wait for container X before starting Y

我正在使用您的 docker-compose.yml,遇到了同样的问题。只是重新启动并没有解决。经过将近一个小时的日志研究,我发现问题是: wordpress服务mysql在完全启动之前就开始连接服务。简单地添加depends_on 无济于事。Docker Compose 在启动 Y 之前等待容器 X

the work around could be start the dbserver before Up. When it has fully started, run docker-compose up. Or just use external service.

解决方法可能是db在 Up 之前启动服务器。完全启动后,运行docker-compose up. 或者只是使用外部服务。

回答by zipzit

I too had troubles here. I was using docker-compose to set up multiple wordpress websites on a single (micro) Virtual Private Server, including phpmyadminand jwilder/nginx-proxyas a controller.

我在这里也遇到了麻烦。我使用 docker-compose 在单个(微)虚拟专用服务器上设置多个 wordpress 网站,包括phpmyadminjwilder/nginx-proxy作为控制器。

$ docker logs XXXXwill help indicate areas of concern. In my case, the MariaDB databases would keep restarting all the time.

$ docker logs XXXX将有助于指出关注的领域。就我而言,MariaDB 数据库会一直重启。

It turns out that all that stuff just wouldn't fit on a micro 512M Single CPU service. I never received error messages that told me directly that size was an issue, but after adding things up, I realized that when all the databases were starting up, I was running out of memory. An upgrade to 1Gb, 1 CPU service worked just fine.

事实证明,所有这些东西都不适合微型 512M 单 CPU 服务。我从来没有收到过直接告诉我大小是一个问题的错误消息,但是在加起来之后,我意识到当所有数据库都启动时,我的内存不足。升级到 1Gb、1 CPU 服务运行良好。

回答by gold-kou

I had almost same problem, but just restarting the Wordpress container saved me:

我遇到了几乎相同的问题,但只是重新启动 Wordpress 容器就救了我:

$ docker restart wordpress

I hope this help many people.

我希望这能帮助很多人。

回答by Dimitri

In my case, I'm using Mysql (not MariaDb) but I had the same problem. After upgrading the MySQL version, it's works fine.

就我而言,我使用的是 Mysql(不是 MariaDb),但我遇到了同样的问题。升级 MySQL 版本后,它工作正常。

You can see my open source docker-compose configuration: https://github.com/rimiti/wordpress-dockerized-environment

可以看到我的开源docker-compose配置:https: //github.com/rimiti/wordpress-dockerized-environment