首页 > 解决方案 > WordPress Docker 容器无法连接到 MySQL 容器

问题描述

我有一个使用 Traefik 处理前端的服务器,并且我已经设置了一个 WordPress/MySQL 设置来尝试运行一个博客。但是,Wordpress 容器抛出以下错误:

[12-Sep-2020 04:17:14 UTC] PHP Warning:  mysqli::__construct(): (HY000/1045): Access denied for user 'my_name'@'172.19.0.5' (using password: YES) in Standard input code on line 22
MySQL Connection Error: (1045) Access denied for user 'my_name'@'172.19.0.5' (using password: YES)
(this error message repeats several times)


WARNING: unable to establish a database connection to 'db'
  continuing anyways (which might have unexpected results)

[Sat Sep 12 04:17:41.602702 2020] [mpm_prefork:notice] [pid 1] AH00163: Apache/2.4.38 (Debian) PHP/7.4.10 configured -- resuming normal operations
[Sat Sep 12 04:17:41.602774 2020] [core:notice] [pid 1] AH00094: Command line: 'apache2 -D FOREGROUND'

这是用于 Wordpress/MySQL 设置的 docker-compose.yml 文件。两个容器都在proxy创建 Traefik 容器时指定的网络中运行。

version: '3.1'

services:

  wordpress:
    image: wordpress
    restart: always
    environment:
      WORDPRESS_DB_HOST: db
      WORDPRESS_DB_USER: user
      WORDPRESS_DB_PASSWORD: password
      WORDPRESS_DB_NAME: database
    volumes:
      - wordpress:/var/www/html
    networks:
      - proxy
    labels:
      - "traefik.enable=true"
      - "traefik.http.routers.wordpress.entrypoints=http"
      - "traefik.http.routers.wordpress.rule=Host(`catblog.cf`)"
      - "traefik.http.routers.wordpress.middlewares=https-redirect@file"
      - "traefik.http.routers.wordpress-secure.entrypoints=https"
      - "traefik.http.routers.wordpress-secure.rule=Host(`catblog.cf`)"
      - "traefik.http.routers.wordpress-secure.tls=true"
      - "traefik.http.routers.wordpress-secure.tls.certresolver=http"
      - "traefik.http.routers.wordpress-secure.service=wordpress"
      - "traefik.http.services.wordpress.loadbalancer.server.port=9000"
      - "traefik.docker.network=proxy"

  db:
    image: mysql:5.7
    restart: always
    environment:
      MYSQL_DATABASE: exampledb
      MYSQL_USER: db_username
      MYSQL_PASSWORD: db_password
      MYSQL_RANDOM_ROOT_PASSWORD: '1'
    volumes:
      - db:/var/lib/mysql
    networks:
      - proxy
    labels:
      - "traefik.enable=true"
      - "traefik.http.routers.mysql.entrypoints=http"
      - "traefik.http.routers.mysql.middlewares=https-redirect@file"
      - "traefik.http.routers.mysql-secure.entrypoints=https"
      - "traefik.http.routers.mysql-secure.tls=true"
      - "traefik.http.routers.mysql-secure.tls.certresolver=http"
      - "traefik.http.routers.mysql-secure.service=mysql"
      - "traefik.http.services.mysql.loadbalancer.server.port=9000"
      - "traefik.docker.network=proxy"

volumes:
  wordpress:
  db:

networks:
  proxy:
    external: true

标签: mysqlwordpressdockertraefik

解决方案


您在同一个网络上拥有它,对于单个节点,您需要拥有它“取决于:db”。


推荐阅读