Skip to content
Advertisement

docker-compose, WordPress and MariaDB, PHP Fatal error: Uncaught mysqli_sql_exception: Connection refused

I have a WP project with following docker-compose configuration. When I try to connect my http://localhost to access the installation, I get 500 and I read this in the log:

PHP Fatal error: Uncaught mysqli_sql_exception: Connection refused in /var/www/html/web/wp/wp-includes/wp-db.php

I assume there is something wrong with the ip or the host but the weird thing is that phpmyadmin is working fine, and it connects with mysql without issues.

version: '3.9'
services:
  nginx:
    image: nginx:latest
    container_name: ${APP_NAME}-nginx
    ports:
      - '80:80'
    volumes:
      - "./nginx/:/etc/nginx/templates/"
      - ./src:/var/www/html:rw,cached
      - ./certs:/etc/certs
    environment:
      - "NGINX_ENVSUBST_TEMPLATE_SUFFIX=.conf"
      - "DOMAIN=${DOMAIN}"
    depends_on:
      - wordpress
    restart: always

  mysql:
    image: mariadb:latest
    container_name: ${APP_NAME}-mysql
    command: --lower_case_table_names=2
    volumes:
       - './data/db:/var/lib/mysql:delegated'
    environment:
      - MYSQL_ROOT_PASSWORD=${DB_ROOT_PASSWORD}
      - MYSQL_DATABASE=${DB_NAME}
    restart: always
    ports:
      - '3307:3306'

  wordpress:
    build:
      context: .
      dockerfile: Dockerfile
    container_name: ${APP_NAME}-wordpress
    volumes:
      - ./src:/var/www/html:rw,cached
      - ./config/php.ini:/usr/local/etc/php/conf.d/php.ini
    environment:
      XDEBUG_ENABLED: 1
      XDEBUG_CONFIG: remote_host=host.docker.internal
      PHP_IDE_CONFIG: serverName=localhost
    env_file:
      - src/.env
    depends_on:
      - mysql
    restart: always

  phpmyadmin:
    image: phpmyadmin/phpmyadmin
    container_name: ${APP_NAME}-phpmyadmin
    volumes:
      - ./config/phpmyadmin.ini:/usr/local/etc/php/conf.d/phpmyadmin.ini
    environment:
      PMA_HOST: "${DB_HOST}"
      PMA_PORT: 3306
      MYSQL_ROOT_PASSWORD: "${DB_ROOT_PASSWORD}"
    ports:
      - '8083:80'
    links:
      - mysql:mysql

  mailhog:
    container_name: ${APP_NAME}-mailhog
    image: mailhog/mailhog
    ports:
      - "8025:8025"
      - "1025:1025"

  composer:
    image: composer
    container_name: ${APP_NAME}-composer
    working_dir: /var/www/html
    restart: 'no'
    volumes:
      - ./src:/var/www/html:rw,cached

My wordpress .env, among the other settings, set the host like the following:

DB_HOST="mysql:3307"

But I also tried

DB_HOST="mysql:3306"

or simply

DB_HOST="mysql"

Do you have any suggestions?

Thanks.

Advertisement

Answer

your WordPress Instance and MySQL Instance isn’t connected. Try adding networks on the docker-compose.yml

nginx:
 ...
 networks:
  - your-network-name

mysql:
 ...
 networks:
  - your-network-name

wordpress:
 ...
 networks:
  - your-network-name

and on the bottom of the file add:

networks:
 your-network-name:
  driver: bridge

this is the way to configure docker to be connected each other

User contributions licensed under: CC BY-SA
1 People found this is helpful
Advertisement