简体   繁体   English

Docker Compose:将 WordPress 容器连接到 MySQL 容器?

[英]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 db name and it's user name are the same: my_user我的数据库名称和它的用户名相同: my_user
  • I already pre-created the mysql user my_user , and gave it full permissions to the my_user db.我已经预先创建了 mysql 用户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.) (请注意,实际用户和数据库以“g”开头;为了保护隐私,我已将它们更改为“my_user”。)

在此处输入图像描述

  • I have pre-imported a bunch of data into the db (it's for debugging a currently in-production website, using a sql backup).我已经将一堆数据预先导入到数据库中(它用于调试当前正在生产的网站,使用 sql 备份)。
  • After starting up the wp container, I manually copied all of my in-production's wp files into the wp container's folder that has the same respective files, overwriting them.启动 wp 容器后,我手动将所有生产中的 wp 文件复制到 wp 容器的文件夹中,该文件夹具有相同的相应文件,覆盖它们。
  • When I edit the 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文件中。
  • In the 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:

  1. Download: https://github.com/aboutsam/wp-docker-startkit下载: https ://github.com/aboutsam/wp-docker-startkit

  2. docker-compose up -d

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

相关问题 WordPress Docker 容器无法连接到 MySQL 容器 - WordPress Docker container cannot connect to MySQL container docker-compose mysql 容器拒绝访问 wordpress 容器 - docker-compose mysql container denies access to wordpress container 通过 docker 将 laravel 容器连接到现有的 mysql 容器 - Connect laravel container to existing mysql container via docker compose docker-compose:无法将应用程序容器与 mysql 容器连接 - docker-compose: Can't connect application container with mysql container Java 容器无法使用 docker-compose 连接到 MYSQL 容器 - Java container cant connect to MYSQL container with docker-compose docker wordpress 容器无法连接到 mysql 容器 - docker wordpress container can't connect to mysql container 使用docker-compose的Rails应用不会连接到mysql容器 - Rails app using docker-compose wont connect to the mysql container 使用Docker Compose连接到另一个容器中运行的mysql - Using Docker Compose to connect to mysql running in another container 容器初始化后如何连接到docker-compose mysql? - How to connect to docker-compose mysql after container has initilized? Docker MySQL - cant connect SpringBoot app to MySQL database in docker container using docker-compose - Docker MySQL - cant connect SpringBoot app to MySQL database in docker container using docker-compose
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM