Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Moving Wordpress site to Docker: Error establishing DB connection

Ive been making new sites with Wordpress & Docker recently and have a reasonable grasp of how it all works and Im now looking to move some established sites into Docker.

Ive been following this guide:

https://stephenafamo.com/blog/moving-wordpress-docker-container/

I have everything setup as it should be but when I go to my domain.com:1234 I get the error message 'Error establishing a database connection'. I have changed 'DB HOST' to 'mysql' in wp-config.php as advised and all the DB details from the site Im bringing in are correct.

I have attached to the mysql container and checked that the db is there and with the right user and also made sure the pw is correct via mysql CLI too.

SELinux is set to permissive and I havent changed any dir/file ownership nor permissions and for the latter dirs are all 755 and files 644 as they should be.

Edit: I should mention that database/data and everything under that seem to be owned by user/group 'polkitd input' instead of root.

Docker logs aren't really telling me much either apart from the 500 error messages for the WP container when I browse the site on port 1234 (as expected though).

This is the docker-compose file:

version: '2'

services:
  example_db:
    image: mysql:latest
    container_name: example_db
    volumes:
      - ./database/data:/var/lib/mysql
      - ./database/initdb.d:/docker-entrypoint-initdb.d
    restart: always
    environment:
      MYSQL_ROOT_PASSWORD: password123 # any random string will do
      MYSQL_DATABASE: mydomin_db # the name of your mysql database
      MYSQL_USER: my domain_me # the name of the database user
      MYSQL_PASSWORD: password123 # the password of the mysql user

  example:
    depends_on:
      - example_db
    image: wordpress:php7.1 # we're using the image with php7.1
    container_name: example
    ports:
      - "1234:80"
    restart: always
    links:
      - example_db:mysql
    volumes:
      - ./src:/var/www/html

Suggestions most welcome as Im out of ideas!

like image 556
goblin_rocket Avatar asked Sep 05 '18 13:09

goblin_rocket


People also ask

What is error establishing database connection in WordPress?

It is one of the most common WordPress errors. Apart from incorrect credentials, this error can also appear if the database server is down, or the database files are corrupt. Let’s take a look at how to fix error establishing database connection issue in WordPress with step by step troubleshooting. 1. Check Your WordPress Database Credentials

How do I connect to the database in WordPress?

To connect to the database, WordPress needs the database name, username, password, and server. These credentials are stored in wp-config.php, which is your site’s configuration file. If any of this information is incorrect, then WordPress will be unable to connect to the database.

Why is my Wordpress site not connecting to the MySQL server?

WordPress retrieves all the information it needs from the MySQL server constantly. If there is an error in your database, WordPress will not be able to communicate with the MySQL server and will throw up a database connection error that we will troubleshoot below. 1. Check your database credentials

Why is my WordPress database not responding?

Unresponsive database server – The server where your database lives could be as dead as a dodo; caput, unresponsive or still recovering from a traffic upsurge. A corrupted database – Bad plugins, addons, themes and data-transfer interruptions might leave your database short of tables or completely corrupted at worst.


2 Answers

With the new version of docker-compose it will look like this (if you don't want to use PhpMyAdmin you can leave it out):

version: '3.7'

volumes:
  wp-data:
networks:
  wp-back:

services:

  db:
    image: mysql:5.7
    volumes:
      - wp-data:/var/lib/mysql
    environment:
       MYSQL_ROOT_PASSWORD: rootPassword
       MYSQL_DATABASE: wordpress
       MYSQL_USER: wp-user
       MYSQL_PASSWORD: wp-pass
    ports:
      - 8889:3306
    networks:
      - wp-back

  phpmyadmin:
    depends_on:
      - db
    image: phpmyadmin/phpmyadmin
    environment:
      PMA_HOST: db
      MYSQL_USER: wp-user
      MYSQL_PASSWORD: wp-pass
      MYSQL_ROOT_PASSWORD: rootPassword
    ports:
      - 3001:80
    networks:
      - wp-back

  wordpress:
    depends_on:
      - db
    image: wordpress:latest
    ports:
      - 8888:80
      - 443:443
    environment:
       WORDPRESS_DB_HOST: db
       WORDPRESS_DB_USER: wp-user
       WORDPRESS_DB_PASSWORD: wp-pass
    volumes:
      - ./wordpress-files:/var/www/html
    container_name: wordpress-site
    networks:
      - wp-back

The database volume is a named volume wp-data, while the wordpress html is a bind-mount to your current directory ./wordpress-files .

like image 77
xtra Avatar answered Nov 15 '22 19:11

xtra


Please take a look at the following compose script. I tried and tested. It works fine.

version: '2'

services:
  db:
    image: mysql:latest
    container_name: db_server
    volumes:
      - ./database/data:/var/lib/mysql
      - ./database/initdb.d:/docker-entrypoint-initdb.d
    restart: always
    environment:
      MYSQL_ROOT_PASSWORD: password123 # any random string will do
      MYSQL_DATABASE: udb_test # the name of your mysql database
      MYSQL_USER: me_prname # the name of the database user
      MYSQL_PASSWORD: password123 # the password of the mysql user

  example:
    depends_on:
      - db
    image: wordpress:php7.1 # we're using the image with php7.1
    container_name: wp-web
    environment:
      WORDPRESS_DB_HOST: db:3306
      WORDPRESS_DB_USER: me_prname
      WORDPRESS_DB_PASSWORD: password123
      WORDPRESS_DB_NAME: udb_test
    ports:
      - "1234:80"
    restart: always
    volumes:
      - ./src:/var/www/html

Let me know if you encounter further issues.

like image 27
cooshal Avatar answered Nov 15 '22 19:11

cooshal