Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

WP-CLI: Error establishing a database connection #69

Open
joelsmith80 opened this issue Apr 20, 2021 · 5 comments
Open

WP-CLI: Error establishing a database connection #69

joelsmith80 opened this issue Apr 20, 2021 · 5 comments

Comments

@joelsmith80
Copy link

Not sure what I'm doing wrong here. I'm following all of the instructions, not changing any default variables, and while WordPress and PHPMyAdmin work perfectly, I can't get wp-cli to work. Any time I run the command in the instructions, I get Error establishing a database connection. Any time I try to restart it via the Docker dashboard, I get the same error. What gives? What database credentials is wp-cli using? What else can I do to configure this?

If it makes a difference, I'm on a new MacBook Pro with the M1 chip. (This has caused problems with other programs; don't know if it's an issue here.)

@Snk74
Copy link

Snk74 commented Apr 29, 2021

Hey,
Same here, just wait few minutes and it works fine for me

@satcar77
Copy link
Contributor

satcar77 commented May 7, 2021

Since March 2021, WordPress images use a customized wp-config.php that pulls the values directly from the environment variables defined above (see wp-config-docker.php in docker-library/wordpress#572 and docker-library/wordpress#577). As a result of reading environment variables directly, the cli container also needs the same set of environment variables to properly evaluate wp-config.php.

Pass in the same environment variables from wordpress service to the wpcli and it should work.

@nguyenlamlll
Copy link

nguyenlamlll commented Jun 7, 2021

@satcar77 May you please explain a little bit more? I don't get the part "pass in the same environment variables". I think I have cloned the branch master, simply docker-compose up -d and then docker-compose run --rm wpcli plugin list as the readme file says, but it does not work. Got the same database connection error.

@nguyenlamlll
Copy link

nguyenlamlll commented Jun 7, 2021

Ok, silly me. I'm sorry. But I think I got what is required. The wpcli section in my docker-compose.yml does not have the matching environment section as wp's. So, I ensure everything is matching.

services:
  wp:
    image: wordpress:5
    ports:
      - ${IP}:80:80
    volumes:
      - ./config/php.conf.ini:/usr/local/etc/php/conf.d/conf.ini
      - ./wp-app:/var/www/html # Full wordpress project
    environment:
      WORDPRESS_DB_HOST: db
      WORDPRESS_DB_NAME: "${DB_NAME}"
      WORDPRESS_DB_USER: root
      WORDPRESS_DB_PASSWORD: "${DB_ROOT_PASSWORD}"
    depends_on:
      - db
    links:
      - db

  wpcli:
    image: wordpress:cli
    environment:
      WORDPRESS_DB_HOST: db
      WORDPRESS_DB_NAME: "${DB_NAME}"
      WORDPRESS_DB_USER: root
      WORDPRESS_DB_PASSWORD: "${DB_ROOT_PASSWORD}"
    volumes:
      - ./config/php.conf.ini:/usr/local/etc/php/conf.d/conf.ini
      - ./wp-app:/var/www/html
    depends_on:
      - db
      - wp

Then, docker-compose run --rm wpcli user list. It works!

By the way, I've just seen your PR #70. I see it now.
Thank you a lot!

@bondansebastian
Copy link

bondansebastian commented Sep 27, 2023

Hi there,
I'm experiencing the same issue on Windows WSL 2, using Ubuntu-20.4 distro.
Here's my .docker-compose.yml:

version: '3'

services:
  wp:
    image: wordpress:latest # https://hub.docker.com/_/wordpress/
    ports:
      - ${IP}:${PORT}:80 # change ip if required
    volumes:
      - ./config/php.conf.ini:/usr/local/etc/php/conf.d/conf.ini
      - ./wp-app:/var/www/html # Full wordpress project
      - ./joy-booking-plugin/:/var/www/html/wp-content/plugins/joy_booking # Plugin development
      #- ./theme-name/trunk/:/var/www/html/wp-content/themes/theme-name # Theme development
    environment:
      WORDPRESS_DB_HOST: db
      WORDPRESS_DB_NAME: "${DB_NAME}"
      WORDPRESS_DB_USER: root
      WORDPRESS_DB_PASSWORD: "${DB_ROOT_PASSWORD}"
      WORDPRESS_DEBUG: "${WORDPRESS_DEBUG}"
    depends_on:
      - db
    links:
      - db

  wpcli:
    image: wordpress:cli
    volumes:
      - ./config/php.conf.ini:/usr/local/etc/php/conf.d/conf.ini
      - ./wp-app:/var/www/html
    environment:
      WORDPRESS_DB_HOST: db
      WORDPRESS_DB_NAME: "${DB_NAME}"
      WORDPRESS_DB_USER: root
      WORDPRESS_DB_PASSWORD: "${DB_ROOT_PASSWORD}"
    depends_on:
      - db
      - wp

  pma:
    image: phpmyadmin/phpmyadmin
    environment:
      # https://docs.phpmyadmin.net/en/latest/setup.html#docker-environment-variables
      PMA_HOST: db
      PMA_PORT: 3306
      MYSQL_ROOT_PASSWORD: "${DB_ROOT_PASSWORD}"
      UPLOAD_LIMIT: 50M
    ports:
      - ${IP}:8080:80
    links:
      - db:db

  db:
    image: mysql:latest # https://hub.docker.com/_/mysql/ - or mariadb https://hub.docker.com/_/mariadb
    ports:
      - ${IP}:3306:3306 # change ip if required
    command: [
        '--default_authentication_plugin=mysql_native_password',
        '--character-set-server=utf8mb4',
        '--collation-server=utf8mb4_unicode_ci'
    ]
    volumes:
      - ./wp-data:/docker-entrypoint-initdb.d
      - db_data:/var/lib/mysql
    environment:
      MYSQL_DATABASE: "${DB_NAME}"
      MYSQL_ROOT_PASSWORD: "${DB_ROOT_PASSWORD}"

volumes:
  db_data:

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

5 participants