You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何通过Docker Compose自动完成MediaWiki部署配置?

解决MediaWiki Docker化自动初始化数据库问题

问题背景

使用MediaWiki 1.39.3搭配MariaDB,运行在Docker 24.0.2和Docker Compose 2.18.1环境下,默认部署正常。希望实现团队协作:同事克隆仓库后执行docker compose up即可完成全部配置,根目录已挂载手动配置后下载的LocalSettings.php作为MediaWiki容器卷。

当前问题:挂载LocalSettings.php后,MediaWiki跳过网页安装流程,但数据库未初始化,访问Wiki时触发报错。

错误信息

Deprecated: Caller from LCStoreDB::get ignored an error originally raised from LCStoreDB::get: [1146] Table 'my_wiki.l10n_cache' doesn't exist in /var/www/html/includes/debug/MWDebug.php on line 381
[3c48d5308eca6dc9224e9901] 2023-05-31 06:58:04: Fatal exception of type Wikimedia\Rdbms\DBQueryError

尝试用update.php初始化数据库时,收到错误:Can not upgrade from versions older than 1.31, please upgrade to that version or later first.

当前配置文件

docker-compose.yml

# MediaWiki with MariaDB
#
# Access via "http://localhost:8080"
#   (or "http://$(docker-machine ip):8080" if using docker-machine)
version: '3'
services:
  mediawiki:
    image: mediawiki:1.39.3
    restart: always
    ports:
      - 8080:80
    links:
      - database
    volumes:
      - images:/var/www/html/images
      # After initial setup, download LocalSettings.php to the same directory as
      # this yaml and uncomment the following line and use compose to restart
      # the mediawiki service
      - ./LocalSettings.php:/var/www/html/LocalSettings.php
    command: sh -c "php maintenance/update.php"
  # This key also defines the name of the database host used during setup instead of the default "localhost"
  database:
    image: mariadb
    restart: always
    environment:
      # @see https://phabricator.wikimedia.org/source/mediawiki/browse/master/includes/DefaultSettings.php
      MYSQL_DATABASE: my_wiki
      MYSQL_USER: wikiuser
      MYSQL_PASSWORD: example
      MYSQL_RANDOM_ROOT_PASSWORD: 'yes'
    volumes:
      - db:/var/lib/mysql

volumes:
  images:
  db:

LocalSettings.php

<?php
# This file was automatically generated by the MediaWiki 1.39.3
# installer. If you make manual changes, please keep track in case you
# need to recreate them later.
#
# See docs/Configuration.md for all configurable settings
# and their default values, but don't forget to make changes in _this_
# file, not there.
#
# Further documentation for configuration settings may be found at:
# https://www.mediawiki.org/wiki/Manual:Configuration_settings

# Protect against web entry
if ( !defined( 'MEDIAWIKI' ) ) {
    exit;
}


## Uncomment this to disable output compression
# $wgDisableOutputCompression = true;

$wgSitename = "HI-LONa";

## The URL base path to the directory containing the wiki;
## defaults for all runtime URL paths are based off of this.
## For more information on customizing the URLs
## (like /w/index.php/Page_title to /wiki/Page_title) please see:
## https://www.mediawiki.org/wiki/Manual:Short_URL
$wgScriptPath = "";

## The protocol and server name to use in fully-qualified URLs
$wgServer = "http://localhost:8080";

## The URL path to static resources (images, scripts, etc.)
$wgResourceBasePath = $wgScriptPath;

## The URL paths to the logo.  Make sure you change this from the default,
## or else you'll overwrite your logo when you upgrade!
$wgLogos = [
    '1x' => "$wgResourceBasePath/resources/assets/change-your-logo.svg",
    'icon' => "$wgResourceBasePath/resources/assets/change-your-logo-icon.svg",
];

## UPO means: this is also a user preference option

$wgEnableEmail = true;
$wgEnableUserEmail = true; # UPO

$wgEmergencyContact = "";
$wgPasswordSender = "";

$wgEnotifUserTalk = false; # UPO
$wgEnotifWatchlist = false; # UPO
$wgEmailAuthentication = true;

## Database settings
$wgDBtype = "mysql";
$wgDBserver = "database";
$wgDBname = "my_wiki";
$wgDBuser = "wikiuser";
$wgDBpassword = "example";

# MySQL specific settings
$wgDBprefix = "";

# MySQL table options to use during installation or update
$wgDBTableOptions = "ENGINE=InnoDB, DEFAULT CHARSET=binary";

# Shared database table
# This has no effect unless $wgSharedDB is also set.
$wgSharedTables[] = "actor";

## Shared memory settings
$wgMainCacheType = CACHE_NONE;
$wgMemCachedServers = [];

## To enable image uploads, make sure the 'images' directory
## is writable, then set this to true:
$wgEnableUploads = false;
$wgUseImageMagick = true;
$wgImageMagickConvertCommand = "/usr/bin/convert";

# InstantCommons allows wiki to use images from https://commons.wikimedia.org
$wgUseInstantCommons = false;

# Periodically send a pingback to https://www.mediawiki.org/ with basic data
# about this MediaWiki instance. The Wikimedia Foundation shares this data
# with MediaWiki developers to help guide future development efforts.
$wgPingback = true;

# Site language code, should be one of the list in ./includes/languages/data/Names.php
$wgLanguageCode = "en-gb";

# Time zone
$wgLocaltimezone = "UTC";

## Set $wgCacheDirectory to a writable directory on the web server
## to make your wiki go slightly faster. The directory should not
## be publicly accessible from the web.
#$wgCacheDirectory = "$IP/cache";

$wgSecretKey = "7b136b2940a0158c40955529c9f5e6a7b28c777ef359397a82da99729f48e8c3";

# Changing this will log out all existing sessions.
$wgAuthenticationTokenVersion = "1";

# Site upgrade key. Must be set to a string (default provided) to turn on the
# web installer while LocalSettings.php is in place
$wgUpgradeKey = "3bb56c36c2f9ad8b";

## For attaching licensing metadata to pages, and displaying an
## appropriate copyright notice / icon. GNU Free Documentation
## License and Creative Commons licenses are supported so far.
$wgRightsPage = ""; # Set to the title of a wiki page that describes your license/copyright
$wgRightsUrl = "";
$wgRightsText = "";
$wgRightsIcon = "";

# Path to the GNU diff3 utility. Used for conflict resolution.
$wgDiff3 = "/usr/bin/diff3";

## Default skin: you can change the default skin. Use the internal symbolic
## names, e.g. 'vector' or 'monobook':
$wgDefaultSkin = "vector";

# Enabled skins.
# The following skins were automatically enabled:
wfLoadSkin( 'MinervaNeue' );
wfLoadSkin( 'MonoBook' );
wfLoadSkin( 'Timeless' );
wfLoadSkin( 'Vector' );


# End of automatically generated settings.
# Add more configuration options below.

解决方案

核心问题分析

  • update.php仅用于已有MediaWiki数据库的版本升级,无法初始化空数据库,这是触发版本错误提示的根本原因(空库无版本记录,脚本误判为极旧版本)。
  • 挂载LocalSettings.php后,MediaWiki跳过网页安装流程,但未自动执行数据库初始化,导致表缺失报错。

具体修复步骤

  1. 修改docker-compose.yml的mediawiki服务命令:
    • 增加数据库等待逻辑,确保MariaDB完全启动后再执行操作
    • 仅当数据库未初始化时,执行install.php完成建表
    • 最后启动Apache服务保持容器运行

修改后的mediawiki服务配置:

services:
  mediawiki:
    image: mediawiki:1.39.3
    restart: always
    ports:
      - 8080:80
    links:
      - database
    volumes:
      - images:/var/www/html/images
      - ./LocalSettings.php:/var/www/html/LocalSettings.php
    command: >
      sh -c "
        # 等待数据库服务可用
        until mysql -h database -u wikiuser -pexample -e 'SELECT 1'; do
          sleep 2;
        done;
        # 检查数据库是否已初始化(判断user表是否存在)
        if ! mysql -h database -u wikiuser -pexample my_wiki -e 'DESCRIBE user'; then
          # 非交互模式执行安装脚本,参数与LocalSettings.php保持一致
          php maintenance/install.php \
            --conf LocalSettings.php \
            --dbuser wikiuser \
            --dbpass example \
            --dbname my_wiki \
            --dbserver database \
            --lang en-gb \
            --site-name 'HI-LONa' \
            --pass 'admin123'; # 可自行修改初始管理员密码
        fi;
        # 启动Apache服务
        apache2-foreground
      "
  1. 配置一致性验证:

    • 确保install.php的参数(如--site-name、--lang)与LocalSettings.php中的$wgSitename、$wgLanguageCode完全匹配
    • 初始管理员密码可根据团队需求修改,需同步告知同事
  2. 测试运行:

    • 清理现有数据库卷(若有残留数据):docker compose down -v
    • 执行docker compose up,容器会自动完成数据库初始化,无需手动操作

额外说明

  • 该配置支持重复启动:第一次启动时初始化数据库,后续启动会跳过初始化步骤,直接启动MediaWiki服务
  • 若需修改初始配置,只需同步更新LocalSettings.php和install.php对应参数即可

内容的提问来源于stack exchange,提问作者artem

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.20 03:15:07