[英]Docker Compose: connect a WordPress container to a MySQL container?
I have an already running MySQL container that I use for several other containers.我有一个已经在运行的 MySQL 容器,我将它用于其他几个容器。
Now I'm trying to connect a wordpress container to it but I'm getting:现在我正在尝试将一个 wordpress 容器连接到它,但我得到:
Error establishing a database connection
建立数据库连接时出错
And the wp container log shows: wp 容器日志显示:
MySQL Connection Error: (1045) Access denied for user 'my_user'@'172.20.0.5' (using password: YES)
MySQL 连接错误:(1045) 拒绝用户 'my_user'@'172.20.0.5' 的访问(使用密码:YES)
The mysql container is already up and running and the container's name is mysql
, and I use a PHPMyAdmin container to manage it; mysql 容器已经启动并运行,容器名称为
mysql
,我使用 PHPMyAdmin 容器来管理它; I'm able to do so just fine (it's connected to the Mysql server container).我能够做到这一点(它连接到 Mysql 服务器容器)。
Here's my docker compose code for the WP container:这是我的 WP 容器的 docker compose 代码:
version: '2'
services:
wordpress:
image: wordpress:latest
restart: unless-stopped
ports:
- 80
environment:
WORDPRESS_DB_HOST: mysql
WORDPRESS_DB_USER: "my_user"
WORDPRESS_DB_PASSWORD: "12345"
WORDPRESS_DB_NAME: "my_user"
volumes:
- /f/Sites/mia:/var/www/html
networks:
- occms
- ocdb
networks:
occms:
external:
name: oly-cms
ocdb:
external:
name: oly-db
Notes:笔记:
my_user
my_user
my_user
, and gave it full permissions to the my_user
db.my_user
,并赋予它对my_user
数据库的完全权限。 (note that the actual user and db start with a "g"; I've changed them to "my_user" for privacy in this question.) WORDPRESS_DB_HOST:
variable and restart the wp container, i do see it's value reflected in the wp-config.php
file.WORDPRESS_DB_HOST:
变量并重新启动 wp 容器时,我确实看到它的值反映在wp-config.php
文件中。wp-config.php
I have made sure the credentials match:wp-config.php
中,我确保凭据匹配:define('DB_NAME', 'my_user');
/** MySQL database username */
define('DB_USER', 'my_user');
/** MySQL database password. Changed on: 3/20/2014 2:40 PM */
define('DB_PASSWORD', '12345');
/** MySQL hostname */
define('DB_HOST', 'mysql');
I was under the assumption that as long as I have both the mysql and wp containers in the same docker network, they should be able to connect to each other?我假设只要我在同一个 docker 网络中同时拥有 mysql 和 wp 容器,它们就应该能够相互连接吗? Am I missing something in the WP compose code?
我是否遗漏了 WP 撰写代码中的某些内容?
You set the variable for wordpress, not for MySQL, so this won't work.您为 wordpress 设置变量,而不是为 MySQL 设置变量,因此这将不起作用。
I wrote a docker-compose
to load a wordpress with my mysql:我写了一个
docker-compose
来用我的 mysql 加载一个 wordpress:
Download: https://github.com/aboutsam/wp-docker-startkit下载: https ://github.com/aboutsam/wp-docker-startkit
docker-compose up -d
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.