【问题标题】:Wordpress Docker container can't connect to database containerWordpress Docker 容器无法连接到数据库容器
【发布时间】:2018-09-23 21:01:47
【问题描述】:

我正在尝试将 worpdress 站点迁移到 docker 容器以进行本地开发。

但是,每次我使用 docker compose 时,我都会收到这个:

MySQL 连接错误:(1045) 用户 'root'@'172.22.0.3' 的访问被拒绝

我已经仔细检查了密码并通过 db 容器上的 shell 对其进行了验证。

这是我的docker-compose 文件:

services: # configuring each container
db: # name of our mysql container
image: mysql:5.7 # which image to pull, in this case specifying v. 5.7
volumes: # data to map to the container
  - ./data:/docker-entrypoint-initdb.d # where to find our data - we'll talk more about this
restart: always # always restart the container after reboot
environment: # environment variables -- mysql options in this case
  MYSQL_ROOT_PASSWORD: *****
  MYSQL_DATABASE: **_***
  MYSQL_USER: *****
  MYSQL_PASSWORD: *****

....
wordpress:
    depends_on: # container dependencies that need to be running first
  - db
  image: wordpress:latest # image used by our container
   ports:
  - "8080:80" # setting our ports for networking
   restart: always
   environment:
     WORDPRESS_DB_HOST: db:3306 # default mysql port
     WORDPRESS_DB_PASSWORD: **** # matches $MYSQL_PASSWORD
   volumes: # this is where we tell Docker what to pay attention to
  - ./wp-content/themes/chronus:/var/www/html/wp-content/themes/chronus # mapping our custom theme to the container
  - ./wp-content/plugins:/var/www/html/wp-content/plugins # map our plugins to the container
  - ./wp-content/uploads:/var/www/html/wp-content/uploads # map our uploads to the container

【问题讨论】:

  • 您的撰写文件的某些部分似乎被遗漏了?您能否提供它们(例如,哪个图像用于 wordpress 服务)

标签: wordpress docker docker-compose


【解决方案1】:

我假设您使用的是来自 docker hub 的官方 Wordpress image。您已指定 WORDPRESS_DB_PASSWORD 标志,但未指定 WORDPRESS_DB_USER。这意味着,wordpress 插件默认为root

但是,根据您的 cmets,您在此处输入了某个任意用户(不是 root 用户)的密码。

在您的 compose 文件中按如下方式修改 wordpress 容器的环境变量,它应该可以工作:

   environment:
       WORDPRESS_DB_HOST: db:3306 # default mysql port
       WORDPRESS_DB_PASSWORD: **** # matches $MYSQL_PASSWORD
       WORDPRESS_DB_USER: **** # matches $MYSQL_USER

【讨论】:

  • 抱歉忘记了!
猜你喜欢
  • 2016-04-17
  • 2021-11-10
  • 2020-12-30
  • 2020-07-29
  • 2022-02-22
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-02-29
相关资源
最近更新 更多