wordpress - Docker 中的 wp-cli 从 wp-config 获取错误的数据库登录凭据
问题描述
这是我的docker-compose.yml
文件:
version: '3.9'
services:
db:
image: mysql:5.7
restart: always
command: [
'--disable-partition-engine-check',
'--default_authentication_plugin=mysql_native_password',
'--character-set-server=utf8mb4',
'--collation-server=utf8mb4_unicode_ci',
'--max_allowed_packet=100M'
]
volumes:
- ./db_data:/var/lib/mysql
environment:
MYSQL_DATABASE: wordpress
MYSQL_ROOT_PASSWORD: pswd4!
pma:
image: phpmyadmin/phpmyadmin
environment:
PMA_HOST: db
PMA_PORT: 3306
MYSQL_ROOT_PASSWORD: pswd4!
UPLOAD_LIMIT: 1G
ports:
- 8080:80
depends_on:
- db
wp:
image: wordpress:php8.0
ports:
- 80:80
volumes:
- ./config/php.conf.ini:/usr/local/etc/php/conf.d/conf.ini
- ./:/var/www/html
environment:
WORDPRESS_DB_HOST: db
WORDPRESS_DB_NAME: wordpress
WORDPRESS_DB_USER: root
WORDPRESS_DB_PASSWORD: pswd4!
WORDPRESS_DEBUG: 1
depends_on:
- db
links:
- db:mysql
hostname: test.localhost
wpcli:
image: wordpress:cli-php8.0
volumes_from:
- wp
depends_on:
- db
- wp
links:
- db:mysql
entrypoint: wp
command: "--info"
volumes:
db_data:
当我尝试wp-cli
在 Docker(例如docker-compose run --rm wpcli plugin list
)中使用时,它会收到无法连接到数据库的错误:
Error: `Access denied for user 'username_here'@'192.168.32.5' (using password: YES)`
Error establishing a database connection
This either means that the username and password information in your `wp-config.php` file is incorrect or we can’t contact the database server at `mysql`. This could mean your host’s database server is down.
Are you sure you have the correct username and password?
Are you sure you have typed the correct hostname?
Are you sure the database server is running?
If you’re unsure what these terms mean you should probably contact your host. If you still need help you can always visit the WordPress Support Forums. `Access denied for user 'username_here'@'192.168.32.5' (using password: YES)
`
看起来 wp-cli 看到错误的数据库凭据(username_here
而不是root
)
命令执行结果docker-compose run --rm wpcli config list
:
有什么问题?我已经在互联网上搜索了几个小时,但仍然没有找到问题的原因。
解决方案
您应该为 wpcli 容器指定与 wp 容器中相同的一组环境变量。如果没有,则使用 wordpress 中 php 文件中的默认变量。
请注意:volumes_from
并且link
选项在 compose v3 中已弃用。对于链接选项,docker-compose 会自动创建一个网络(或者您可以根据需要指定一个),并且嵌入的 docker dns 会根据容器名称(或组成服务名称)自动为您的容器赋予别名。更多信息在这里
对于卷,您可以在此处找到更多信息
推荐阅读
- python - 能否获取 EC2 实例的公网 IP 地址
- python - Tensorflow 对象检测中的输入张量与 Python 签名不兼容
- node.js - 缓冲到图像节点js
- python - 如何阻止python字典忽略列表中的输入
- ios - 处理具有多个捆绑 ID 的应用内购买
- python - Ipywidgets (Vbox) 没有出现在 Jupyter 笔记本上
- javascript - 使用 vee-validate 在 vue js 中验证时出错
- c# - 如何使 ac# 程序在后台和启动时运行
- amazon-web-services - 如何通过aws API网关从aws lambda中的简单curl post请求中获取文件名
- jquery - jquery Ajax Post成功消息不起作用