简体   繁体   中英

Docker Compose: connect a WordPress container to a MySQL container?

I have an already running MySQL container that I use for several other containers.

Now I'm trying to connect a wordpress container to it but I'm getting:

Error establishing a database connection

And the wp container log shows:

MySQL Connection Error: (1045) Access denied for user 'my_user'@'172.20.0.5' (using password: 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; I'm able to do so just fine (it's connected to the Mysql server container).

Here's my docker compose code for the WP container:

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
  • I already pre-created the mysql user my_user , and gave it full permissions to the my_user db. (note that the actual user and db start with a "g"; I've changed them to "my_user" for privacy in this question.)

在此处输入图像描述

  • I have pre-imported a bunch of data into the db (it's for debugging a currently in-production website, using a sql backup).
  • 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.
  • 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.
  • In the wp-config.php I have made sure the credentials match:
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? Am I missing something in the WP compose code?

You set the variable for wordpress, not for MySQL, so this won't work.

I wrote a docker-compose to load a wordpress with my mysql:

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

  2. docker-compose up -d

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM