0

I have docker compose app running wordpress, phpmyadmin, mysql on my development system. I successfully created the database and was able to initialize wordpress. When I click on the login link after initializing wordpress I see "Error establishing a database connection".

It seems like all I can find when searching are actual problems connecting to the database. I believe I'm connecting to the database successfully, but something happens a bit downstream which is causing the error.

The wordpress container can indeed connect to the database, but throws the error after DESCRIBE wp_users is executed. Or at least that's the last item in the mysql log. Also note phpmyadmin can connect to the database with the credentials wordpress and the password in /run/secrets/appdb-password (see docker-compose.yml).

I set up the wordpress username password as lou.king password, but it seems that would be irrelevant.

It might be relevant that I'm accessing wordpress using a port, i.e., via http://dev.localhost:8010/wp-login.php

Below are the mysql log after SET GLOBAL general_log = 'ON';, and relevant queries. This is what happens when accessing the wp-login.php view (or any other view, of course).

2025-02-17T12:18:55.580518Z 82 Connect [email protected] on using TCP/IP 2025-02-17T12:18:55.580861Z 82 Query SET NAMES utf8mb4 2025-02-17T12:18:55.581354Z 82 Query SET NAMES 'utf8mb4' COLLATE 'utf8mb4_unicode_520_ci' 2025-02-17T12:18:55.581499Z 82 Query SELECT @@SESSION.sql_mode 2025-02-17T12:18:55.581706Z 82 Query SET SESSION sql_mode='NO_ZERO_IN_DATE,ERROR_FOR_DIVISION_BY_ZERO,NO_ENGINE_SUBSTITUTION' 2025-02-17T12:18:55.582140Z 82 Init DB wordpress 2025-02-17T12:18:55.583342Z 82 Query SELECT option_name, option_value FROM wp_options WHERE autoload IN ( 'yes', 'on', 'auto-on', 'auto' ) 2025-02-17T12:18:55.584121Z 82 Query DESCRIBE wp_users 2025-02-17T12:18:55.585367Z 82 Quit ^C sh-5.1# mysql -u wordpress -p************ wordpress mysql: [Warning] Using a password on the command line interface can be insecure. Reading table information for completion of table and column names You can turn off this feature to get a quicker startup with -A Welcome to the MySQL monitor. Commands end with ; or \g. Your MySQL connection id is 85 Server version: 8.0.40 MySQL Community Server - GPL Copyright (c) 2000, 2024, Oracle and/or its affiliates. Oracle is a registered trademark of Oracle Corporation and/or its affiliates. Other names may be trademarks of their respective owners. Type 'help;' or '\h' for help. Type '\c' to clear the current input statement. mysql> describe wp_users; +---------------------+-----------------+------+-----+---------------------+----------------+ | Field | Type | Null | Key | Default | Extra | +---------------------+-----------------+------+-----+---------------------+----------------+ | ID | bigint unsigned | NO | PRI | NULL | auto_increment | | user_login | varchar(60) | NO | MUL | | | | user_pass | varchar(255) | NO | | | | | user_nicename | varchar(50) | NO | MUL | | | | user_email | varchar(100) | NO | MUL | | | | user_url | varchar(100) | NO | | | | | user_registered | datetime | NO | | 0000-00-00 00:00:00 | | | user_activation_key | varchar(255) | NO | | | | | user_status | int | NO | | 0 | | | display_name | varchar(250) | NO | | | | +---------------------+-----------------+------+-----+---------------------+----------------+ 10 rows in set (0.00 sec) mysql> select * from wp_users; +----+------------+------------------------------------+---------------+-----------------------------+----------+---------------------+---------------------+-------------+--------------+ | ID | user_login | user_pass | user_nicename | user_email | user_url | user_registered | user_activation_key | user_status | display_name | +----+------------+------------------------------------+---------------+-----------------------------+----------+---------------------+---------------------+-------------+--------------+ | 1 | lou.king | $P$BkSsVsE9aF0ZmWfPT8YYy4jKGh5Co41 | lou-king | [email protected] | http: | 2025-02-16 18:43:16 | | 0 | lou.king | +----+------------+------------------------------------+---------------+-----------------------------+----------+---------------------+---------------------+-------------+--------------+ 1 row in set (0.00 sec) 

I'm not sure this is relevant, but here's the docker-compose.yml file

# see .env for image version env variables # see repo/settings/environments for image version env variables for github actions services: wordpress: image: wordpress:${WORDPRESS_VER}-php${PHP_VER}-fpm-alpine restart: always depends_on: - db networks: - backend-network - frontend-network secrets: - appdb-password volumes: - wordpress:/var/www/html - ${VAR_LOG_HOST}:/var/log environment: TZ: ${TZ} WORDPRESS_DB_HOST: db WORDPRESS_DB_USER: wordpress WORDPRESS_DB_NAME: wordpress WORDPRESS_DB_PASSWORD_FILE: /run/secrets/appdb-password WORDPRESS_DEBUG: ${WORDPRESS_DEBUG} db: # https://github.com/docker-library/mysql/issues/275#issuecomment-636831964 image: mysql:${MYSQL_VER} # # changed in mysql 8.4 # command: --mysql-native-password=ON # command: '--default-authentication-plugin=mysql_native_password' command: '--default-authentication-plugin=mysql_native_password --log_error_verbosity=3' # mysql restart: always secrets: - root-password - appdb-password volumes: - db:/var/lib/mysql - ${VAR_LOG_HOST}:/var/log networks: - backend-network environment: TZ: ${TZ} MYSQL_ROOT_PASSWORD_FILE: /run/secrets/root-password MYSQL_DATABASE: wordpress MYSQL_USER: wordpress MYSQL_PASSWORD_FILE: /run/secrets/appdb-password web: image: louking/${APP_NAME}-web:${APP_VER} build: context: web args: - NGINX_VER=${NGINX_VER} - PHPMYADMIN_VER=${PHPMYADMIN_VER} restart: always networks: - frontend-network volumes: - ${VAR_LOG_HOST}:/var/log environment: TZ: ${TZ} ports: - ${APP_PORT}:80 # uncomment to debug # command: [nginx-debug, '-g', 'daemon off;'] phpmyadmin: image: phpmyadmin:${PHPMYADMIN_VER}-fpm restart: always depends_on: - db networks: - backend-network - frontend-network volumes: - ${VAR_LOG_HOST}:/var/log environment: TZ: ${TZ} PMA_ABSOLUTE_URI: http://phpmyadmin/phpmyadmin/ volumes: db: wordpress: secrets: root-password: file: config/db/root-password.txt appdb-password: file: config/db/appdb-password.txt networks: backend-network: frontend-network: 

2 Answers 2

0

Ensure database connection details in wp-config.php match the MySQL container setting:

define( 'DB_NAME', 'database_name_here' );

/** Database username */ define( 'DB_USER', 'username_here' );

/** Database password */ define( 'DB_PASSWORD', 'password_here' );

/** Database hostname */ define( 'DB_HOST', 'localhost' )

DB_HOST should match the service name in docker-compose.yml.

1
  • thanks. but as mentioned the connection is getting established but fails trying to DESCRIBE wp_users. actually I think this is a problem with my nginx configuration which I didn't show. I'll answer the question with how I corrected the issue. Commented Feb 18 at 19:57
0

It looks like there was a problem with my nginx.conf which I hadn't posted. I am able to connect just fine using the wordpress apache image.

# see .env for image version env variables # see repo/settings/environments for image version env variables for github actions services: wordpress: image: wordpress:${WORDPRESS_VER}-php${PHP_VER}-apache restart: always depends_on: - db ports: - ${APP_PORT}:80 networks: - backend-network - frontend-network secrets: - appdb-password volumes: - wordpress:/var/www/html - ${VAR_LOG_HOST}:/var/log environment: TZ: ${TZ} WORDPRESS_DB_HOST: db WORDPRESS_DB_USER: wordpress WORDPRESS_DB_NAME: wordpress WORDPRESS_DB_PASSWORD_FILE: /run/secrets/appdb-password WORDPRESS_DEBUG: ${WORDPRESS_DEBUG} db: # https://github.com/docker-library/mysql/issues/275#issuecomment-636831964 image: mysql:${MYSQL_VER} # # changed in mysql 8.4 # command: --mysql-native-password=ON # command: '--default-authentication-plugin=mysql_native_password' command: '--default-authentication-plugin=mysql_native_password --log_error_verbosity=3' # mysql restart: always secrets: - root-password - appdb-password volumes: - db:/var/lib/mysql - ${VAR_LOG_HOST}:/var/log networks: - backend-network environment: TZ: ${TZ} MYSQL_ROOT_PASSWORD_FILE: /run/secrets/root-password MYSQL_DATABASE: wordpress MYSQL_USER: wordpress MYSQL_PASSWORD_FILE: /run/secrets/appdb-password volumes: db: wordpress: secrets: root-password: file: config/db/root-password.txt appdb-password: file: config/db/appdb-password.txt networks: backend-network: frontend-network: 

for completeness, here is the nginx.conf file which didn't seem to work

events { } # uncomment to debug # error_log /var/log/nginx/error.log debug; http { include mime.types; # see https://github.com/sjmf/reverse-proxy-minimal-example map $http_x_forwarded_proto $real_scheme { default $scheme; https "https"; } map $http_host $port { default $server_port; "~^[^\:]+:(?<p>\d+)$" $p; } server { listen 80; root /usr/share/nginx/html; index index.php index.html index.htm; # use web server for static files, requires copy from phpadmin in web/Dockerfile location ~* ^/phpmyadmin/.+\.php$ { # try_files $uri $uri/ =404; root /var/www/html; rewrite ^/phpmyadmin(/.*)$ $1 break; # https://stackoverflow.com/a/47447211/799921 fastcgi_pass phpmyadmin:9000; include fastcgi.conf; # replace these with forwarded versions fastcgi_param REMOTE_ADDR $http_x_forwarded_for; fastcgi_param REMOTE_PORT $http_x_forwarded_port; fastcgi_param SERVER_ADDR $http_x_forwarded_for; fastcgi_param SERVER_PORT $http_x_forwarded_port; fastcgi_param HTTP_HOST $http_x_forwarded_host; fastcgi_param HTTPS $http_x_forwarded_scheme; } location ~ \.php$ { # try_files $uri $uri/ =404; # fpm listens to port 9000 per https://linux.die.net/man/8/php-fpm # fpm uses fastcgi per https://www.nginx.com/resources/wiki/start/topics/examples/fastcgiexample/ root /var/www/html; fastcgi_pass wordpress:9000; include fastcgi.conf; # replace these with forwarded versions fastcgi_param REMOTE_ADDR $http_x_forwarded_for; fastcgi_param REMOTE_PORT $http_x_forwarded_port; fastcgi_param SERVER_ADDR $http_x_forwarded_for; fastcgi_param SERVER_PORT $http_x_forwarded_port; fastcgi_param HTTP_HOST $http_x_forwarded_host; fastcgi_param HTTPS $http_x_forwarded_scheme; } } include /etc/nginx/conf.d/*.conf; } 

You must log in to answer this question.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.