gpt4 book ai didi

wordpress - docker-compose wordpress mysql连接被拒绝

转载 作者:IT老高 更新时间:2023-10-28 12:44:59 26 4
gpt4 key购买 nike

我创建了一个小型 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

但是当我现在启动它时(可能是因为 docker 更新到 Docker 版本 1.9.1,构建 a34a1d5),它失败了

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

当我 cat wordpress_1/etc/hosts 时,有 MySQL 条目:

172.17.0.10 mysql 12a564fdbc56 mariadb

我可以 ping MariaDB 服务器。

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

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

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

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

最佳答案

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

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

restart: unless-stopped

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

sudo restart docker

或(对于 ubuntu 15+)

sudo service docker restart 

这里是对我有用的完整配置,用 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"

关于wordpress - docker-compose wordpress mysql连接被拒绝,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34068671/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com