Docker 服务未链接 - WordPress 未连接到 MySQL



这是我第一次使用 Docker。我有一个WordPress网站,我用 https://phpdocker.io/生成一些文件,包括docker-compose.yml

我将包内容放在项目的根目录中,然后尝试运行docker-compose up -d,我可以连接到我的MySQL,在那里我加载SQL转储,然后我转到 http://localhost,但它说"无法建立连接",启用调试时我得到:

警告:mysqli_real_connect():(HY000/2002):第 1531 行的/application/wp-include/wp-db.php 中没有此类文件或目录 建立数据库连接时出错

我添加了links,但这没有帮助。这是我的docker-compose.yml的样子:

    ###############################################################################
#                          Generated on local                                 #
###############################################################################
version: "3.1"
services:
  redis:
    image: redis:alpine
    container_name: local-wordpress-redis
  mysql:
    image: mysql:5.7
    container_name: local-wordpress-mysql
    working_dir: /application
    volumes:
      - .:/application
    command: --max_allowed_packet=32505856
    environment:
      - MYSQL_ROOT_PASSWORD=mypass
      - MYSQL_DATABASE=local
      - MYSQL_USER=orbuser
      - MYSQL_PASSWORD=mypass
    ports:
      - "3306:3306"
  webserver:
    image: nginx:alpine
    container_name: local-wordpress-webserver
    working_dir: /application
    volumes:
      - .:/application
      - ./local/nginx/nginx.conf:/etc/nginx/conf.d/default.conf
    ports:
      - "80:80"
    links:
      - mysql
      - redis
  php-fpm:
    build: local/php-fpm
    container_name: local-wordpress-php-fpm
    working_dir: /application
    volumes:
      - .:/application
      - ./local/php-fpm/php-ini-overrides.ini:/etc/php/7.2/fpm/conf.d/99-overrides.ini
    links:
      - mysql
      - redis

wp-config.php

<?php
define('WP_CACHE', true); // Added by W3 Total Cache
define('DB_NAME', 'local');
define('DB_USER', 'orbuser');
define('DB_PASSWORD', 'mypass');
define('DB_HOST', 'localhost');
define('DB_CHARSET', 'utf8mb4');
define('DB_COLLATE', '');
define('AUTH_KEY',         'xx');
define('SECURE_AUTH_KEY',  'xx');
define('LOGGED_IN_KEY',    'xx');
define('AUTH_SALT',        'xx');
define('SECURE_AUTH_SALT', 'xx');
define('LOGGED_IN_SALT',   'xx');
define('NONCE_SALT',       'xx');
$table_prefix  = 'ftom_';
define('WP_DEBUG', true);
define('WP_DEBUG_LOG', true);
if ( !defined('ABSPATH') )
    define('ABSPATH', '/');
require_once(ABSPATH . 'wp-settings.php');

DB_HOST应设置为容器名称:mysql。目前,您的wordpress正在自己的循环中寻找数据库。

最新更新