Docker部署的WordPress无法连接本地MariaDB数据库求助
WordPress容器无法连接本地MariaDB数据库
我使用Docker官方WordPress镜像部署服务,尝试连接本地主机的MariaDB数据库,但访问http://192.168.18.79:8080时始终显示Error establishing a database connection。
容器启动命令
sudo docker run --name wordpress -p 8080:80 -d -e WORDPRESS_DB_HOST=192.168.18.79 -e WORDPRESS_DB_USER='wordpressuser' -e WORDPRESS_DB_PASSWORD='password' -e WORDPRESS_DB_NAME='wordpress_container' -e WORDPRESS_AUTH_KEY='[KEY]' -e WORDPRESS_SECURE_AUTH_KEY='[KEY]' -e WORDPRESS_LOGGED_IN_KEY='[KEY]' -e WORDPRESS_NONCE_KEY='[KEY]' -e WORDPRESS_AUTH_SALT='[KEY]' -e WORDPRESS_SECURE_AUTH_SALT='[KEY]' -e WORDPRESS_LOGGED_IN_SALT='[KEY]' -e WORDPRESS_NONCE_SALT='[KEY]' wordpress
注:[KEY]来自WordPress官方密钥生成工具
已完成的配置操作
- 修改MariaDB配置文件
/etc/mysql/mariadb.conf.d/50-server.cnf,将bind-address设为0.0.0.0,端口监听状态显示3306已对外开放:
Active Internet connections (only servers) Proto Recv-Q Send-Q Local Address Foreign Address State PID/Program name tcp 0 0 0.0.0.0:8080 0.0.0.0:* LISTEN 1742/docker-proxy tcp 0 0 0.0.0.0:22 0.0.0.0:* LISTEN 601/sshd: /usr/sbin tcp 0 0 0.0.0.0:80 0.0.0.0:* LISTEN 603/nginx: master p tcp 0 0 0.0.0.0:3306 0.0.0.0:* LISTEN 667/mariadbd tcp 0 0 0.0.0.0:443 0.0.0.0:* LISTEN 603/nginx: master p
- 在MariaDB中创建数据库并授权用户:
MariaDB [(none)]> CREATE DATABASE wordpress_container DEFAULT CHARACTER SET utf8 COLLATE utf8_unicode_ci; MariaDB [(none)]> CREATE USER 'wordpressuser' IDENTIFIED BY 'password'; MariaDB [(none)]> GRANT ALL ON wordpress_container.* TO 'wordpressuser';
- 容器日志显示WordPress初始化正常,但访问时返回500错误:
justin@debian:~$ sudo docker logs wordpress WordPress not found in /var/www/html - copying now... Complete! WordPress has been successfully copied to /var/www/html No 'wp-config.php' found in /var/www/html, but 'WORDPRESS_...' variables supplied; copying 'wp-config-docker.php' (WORDPRESS_AUTH_KEY WORDPRESS_AUTH_SALT WORDPRESS_DB_HOST WORDPRESS_DB_NAME WORDPRESS_DB_PASSWORD WORDPRESS_DB_USER WORDPRESS_LOGGED_IN_KEY WORDPRESS_LOGGED_IN_SALT WORDPRESS_NONCE_KEY WORDPRESS_NONCE_SALT WORDPRESS_SECURE_AUTH_KEY WORDPRESS_SECURE_AUTH_SALT) AH00558: apache2: Could not reliably determine the server's fully qualified domain name, using 172.17.0.2. Set the 'ServerName' directive globally to suppress this message AH00558: apache2: Could not reliably determine the server's fully qualified domain name, using 172.17.0.2. Set the 'ServerName' directive globally to suppress this message [Sun May 26 10:03:28.196354 2024] [mpm_prefork:notice] [pid 1] AH00163: Apache/2.4.59 (Debian) PHP/8.2.19 configured -- resuming normal operations [Sun May 26 10:03:28.196464 2024] [core:notice] [pid 1] AH00094: Command line: 'apache2 -D FOREGROUND' 172.17.0.1 - - [26/May/2024:10:03:54 +0000] "HEAD / HTTP/1.1" 500 285 "-" "curl/7.88.1"
- 进入容器测试网络连通性:telnet主机3306端口超时,但22端口可正常连接:
justin@debian:~$ sudo docker exec -it wordpress /bin/bash root@85fef2d89d2f:/var/www/html# telnet 192.168.18.79 3306 Trying 192.168.18.79... telnet: Unable to connect to remote host: Connection timed out root@85fef2d89d2f:/var/www/html# telnet 192.168.18.79 22 Trying 192.168.18.79... Connected to 192.168.18.79. Escape character is '^]'. SSH-2.0-OpenSSH_9.2p1 Debian-2+deb12u2
排查与解决方案
1. 检查主机防火墙规则
Debian常用ufw防火墙,先查看当前规则:
sudo ufw status
如果3306端口未允许Docker子网(默认是172.17.0.0/16)访问,添加规则:
sudo ufw allow from 172.17.0.0/16 to any port 3306
也可以临时关闭防火墙测试是否是拦截问题:
sudo ufw disable
2. 修正MariaDB用户权限
之前的授权语句默认仅允许localhost登录,需要修改为允许从Docker子网或任意地址登录:
-- 允许任意地址登录(测试用,生产建议限制子网) GRANT ALL ON wordpress_container.* TO 'wordpressuser'@'%' IDENTIFIED BY 'password'; -- 仅允许Docker子网登录(更安全) GRANT ALL ON wordpress_container.* TO 'wordpressuser'@'172.17.0.0/16' IDENTIFIED BY 'password'; -- 刷新权限使配置生效 FLUSH PRIVILEGES;
3. 测试数据库连通性
在容器内安装mysql客户端直接测试连接:
sudo docker exec -it wordpress apt update && apt install -y mysql-client mysql -h 192.168.18.79 -u wordpressuser -p
输入密码后若能成功进入数据库,说明权限和网络正常;若仍失败,再检查防火墙或网络配置。
4. 尝试主机网络模式(测试用)
如果默认桥接网络有问题,可尝试用主机网络模式启动容器(生产环境不推荐,会暴露容器端口到主机):
sudo docker run --name wordpress --net=host -d -e WORDPRESS_DB_HOST=127.0.0.1 -e WORDPRESS_DB_USER='wordpressuser' -e WORDPRESS_DB_PASSWORD='password' -e WORDPRESS_DB_NAME='wordpress_container' -e WORDPRESS_AUTH_KEY='[KEY]' -e WORDPRESS_SECURE_AUTH_KEY='[KEY]' -e WORDPRESS_LOGGED_IN_KEY='[KEY]' -e WORDPRESS_NONCE_KEY='[KEY]' -e WORDPRESS_AUTH_SALT='[KEY]' -e WORDPRESS_SECURE_AUTH_SALT='[KEY]' -e WORDPRESS_LOGGED_IN_SALT='[KEY]' -e WORDPRESS_NONCE_SALT='[KEY]' wordpress
此时直接访问http://192.168.18.79即可(容器用主机80端口)。
内容的提问来源于stack exchange,提问作者Justin Christian
相关产品推荐
相关产品推荐

