Klaxx
Klaxx

Reputation: 400

How to establish database connection from wordpress docker

I try running a docker compose wordpress by using this guide: https://docs.docker.com/compose/wordpress/

This is the yaml file as described in the guide:

version: '3.3'
services:
   db:
     image: mysql:5.7
     volumes:
       - db_data:/var/lib/mysql
     restart: always
     environment:
       MYSQL_ROOT_PASSWORD: wordpress
       MYSQL_DATABASE: wordpress
       MYSQL_USER: wordpress
       MYSQL_PASSWORD: wordpress

   wordpress:
     depends_on:
       - db
     image: wordpress:latest
     ports:
       - "8000:80"
     restart: always
     environment:
       WORDPRESS_DB_HOST: db:3306
       WORDPRESS_DB_USER: wordpress
       WORDPRESS_DB_PASSWORD: wordpress
       WORDPRESS_DB_NAME: wordpress
       WORDPRESS_DEBUG: "true"
volumes:
    db_data: {}

After I run my

"docker-compose up -d" 

command, I go to "http://localhost:8000/" in my browser and get the white page with "Error establishing a database connection". According to the guide, wordpress should show me the 5 minute Installation already at this point. When I run the container with wordpress debug true, this error message is shown then:

Warning: mysqli_real_connect(): (HY000/2002): Connection refused in /var/www/html/wp-includes/wp-db.php on line 1612

Connection refused

I now use

docker exec it container_id /bin/bash 

and type "mysql -p". Now I use the MYSQL_ROOT_PASSWORD from the docker compose file but I get access denied ("Access denied for user 'root'@'localhost' (using password: YES)")

I am not sure what I did earlier, but at some point it worked and I listed the databases and the mysql.users and the db and user were there.

So I dont even know, what the problem here is...

And why can I not access as root anymore? Does anyone know what to do?

EDIT: changed port back to 3306, I tried 3308 just to see if that may be a port issue

Upvotes: 4

Views: 4782

Answers (2)

go je jo
go je jo

Reputation: 391

Check the logs of a service. To check the services, do:

$ docker-compose logs
...
db_1 | 2022-10-21  2:08:08 0 [Note] InnoDB: Buffer pool(s) load completed at 221021  2:08:08
db_1 | 2022-10-21  2:08:08 0 [Note] mysqld: ready for connections.
db_1 | Version: '10.6.4-MariaDB-1:10.6.4+maria~focal'  socket: '/run/mysqld/mysqld.sock'  port: 3306  mariadb.org binary distribution

For example, If you have a mariadb service named db and it doesn't print that way, you might have to wait for it to print.

Upvotes: 0

Klaxx
Klaxx

Reputation: 400

I found another post and they used this yaml. Still not sure why this works, but it does.

version: '3.3'
services:
   db:
     image: mysql:5.7
     volumes:
       - db_data:/var/lib/mysql
     restart: always
     environment:
       MYSQL_ROOT_PASSWORD: wordpress
       MYSQL_DATABASE: wordpress
       MYSQL_USER: wordpress
       MYSQL_PASSWORD: wordpress

   wordpress:
     depends_on:
       - db
     image: wordpress:latest
     ports:
       - 8000:80
     restart: always
     environment:
       WORDPRESS_DB_HOST: db:3306
       WORDPRESS_DB_USER: wordpress
       WORDPRESS_DB_PASSWORD: wordpress
volumes:
    db_data: {}

Upvotes: 2

Related Questions