Error establish a database connection docker compose

只愿长相守 提交于 2020-05-17 06:54:08

问题


I am trying to setup wordpress with docker. I have included my yaml file below. Here I have set my mariadb_database to db_tyre.

When I hit docker-compose up -d, it is creating all the required files of wordpress. This is also creating db_tyre database but when I try localhost:8000, it gives me Error establishing a database connection.

I have checked the wp-config.php file, it has following lines.

define( 'DB_NAME', 'wordpress');

/** MySQL database username */
define( 'DB_USER', 'wordpress');

/** MySQL database password */
define( 'DB_PASSWORD', 'wordpress');

/** MySQL hostname */
define( 'DB_HOST', 'mariadb:3306');

yml file

version: '3'

services:
  # Database
  db:
    image: bitnami/mariadb:latest
    volumes:
      - db_data:/var/lib/mysql
    restart: always
    environment:
      MARIADB_ROOT_PASSWORD: password
      MARIADB_DATABASE: db_tyre
      MARIADB_USER: wordpress
      MARIADB_PASSWORD: wordpress
    networks:
      - wpsite
  # Wordpress
  wordpress:
    depends_on:
      - db
    image: wordpress:latest
    ports:
      - '8000:80'
    restart: always
    volumes: ['./:/var/www/html']
    environment:
      WORDPRESS_DB_HOST: mariadb:3306
      WORDPRESS_DB_USER: wordpress
      WORDPRESS_DB_PASSWORD: wordpress
    networks:
      - wpsite
  # phpmyadmin
  phpmyadmin:
    depends_on:
      - db
    image: phpmyadmin/phpmyadmin
    restart: always
    ports:
      - '8080:80'
    environment:
      PMA_HOST: db
      MYSQL_ROOT_PASSWORD: password 
    networks:
      - wpsite
networks:
  wpsite:
volumes:
  db_data:

回答1:


As mentioned in the comment, you should set update the HOST but still, it will not work, as the WordPress DB configuration does not seems correct.

ENV for DB is

      MARIADB_ROOT_PASSWORD: password
      MARIADB_DATABASE: db_tyre
      MARIADB_USER: wordpress
      MARIADB_PASSWORD: wordpress

so the WordPress DB configuration should be updated and it should be db_tyre

define( 'DB_NAME', 'db_tyre');

/** MySQL database username */
define( 'DB_USER', 'wordpress');

/** MySQL database password */
define( 'DB_PASSWORD', 'wordpress');

/** MySQL hostname */
define( 'DB_HOST', 'db:3306');

or can try with offical image

version: '3.1'

services:

  wordpress:
    image: wordpress
    restart: always
    ports:
      - 8080:80
    environment:
      WORDPRESS_DB_HOST: db
      WORDPRESS_DB_USER: exampleuser
      WORDPRESS_DB_PASSWORD: examplepass
      WORDPRESS_DB_NAME: exampledb
    volumes:
      - wordpress:/var/www/html

  db:
    image: mysql:5.7
    restart: always
    environment:
      MYSQL_DATABASE: exampledb
      MYSQL_USER: exampleuser
      MYSQL_PASSWORD: examplepass
      MYSQL_RANDOM_ROOT_PASSWORD: '1'
    volumes:
      - db:/var/lib/mysql

volumes:
  wordpress:
  db:


来源:https://stackoverflow.com/questions/60205516/error-establish-a-database-connection-docker-compose

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!