MediaWiki-Docker/Configuration recipes/Alternative databases
Alternative databases
editThe default configuration uses SQLite for the database backend, but you can use MySQL (with replication or without) or Postgres instead.
MariaDB (single database server)
editBetter support for this will be built in to the existing dev-image and its tooling. |
These instruction assume a new installation (no LocalSettings.php).
docker-compose.override.yml
services:
mariadb:
image: 'bitnami/mariadb:latest'
volumes:
- mariadbdata:/bitnami/mariadb
environment:
- MARIADB_ROOT_PASSWORD=root_password
- MARIADB_USER=my_user
- MARIADB_PASSWORD=my_password
- MARIADB_DATABASE=my_database
ports:
- 3306:3306
volumes:
mariadbdata:
driver: local
To install MediaWiki, open a bash shell in the container:
docker compose exec mediawiki /bin/bash
In this bash shell, install MediaWiki with the following command:
php maintenance/run.php install --dbname=my_database --dbuser=my_user --dbpass=my_password --dbserver=mariadb --server="${MW_SERVER}" --scriptpath="${MW_SCRIPT_PATH}" --lang en --pass ${MEDIAWIKI_PASSWORD} mediawiki ${MEDIAWIKI_USER}
MariaDB (database replication)
editThis is the configuration recipe that is most like Wikimedia production.
These instruction assume a new installation (no LocalSettings.php).
docker-compose.override.yml
services:
mariadb-main:
image: 'bitnami/mariadb:latest'
volumes:
- mariadbdata:/bitnami/mariadb
environment:
- MARIADB_REPLICATION_MODE=master
- MARIADB_REPLICATION_USER=repl_user
- MARIADB_REPLICATION_PASSWORD=repl_password
- MARIADB_ROOT_PASSWORD=main_root_password
- MARIADB_USER=my_user
- MARIADB_PASSWORD=my_password
- MARIADB_DATABASE=my_database
mariadb-replica:
image: 'bitnami/mariadb:latest'
depends_on:
- mariadb-main
environment:
- MARIADB_REPLICATION_MODE=slave
- MARIADB_REPLICATION_USER=repl_user
- MARIADB_REPLICATION_PASSWORD=repl_password
- MARIADB_MASTER_HOST=mariadb-main
- MARIADB_MASTER_PORT_NUMBER=3306
- MARIADB_MASTER_ROOT_PASSWORD=main_root_password
volumes:
mariadbdata:
driver: local
To install MediaWiki, open a bash shell in the container:
docker compose exec mediawiki /bin/bash
In this bash shell, install MediaWiki with the following command:
php maintenance/install.php --dbname=my_database --dbuser=my_user --dbpass=my_password --dbserver=mariadb-main \
--server="${MW_SERVER}" --scriptpath="${MW_SCRIPT_PATH}" --lang en \
--pass ${MEDIAWIKI_PASSWORD} mediawiki ${MEDIAWIKI_USER}
After installing, add these snippets so that MediaWiki knows to read from the replica but write to the main database.
LocalSettings.php
$wgDBname = 'my_database';
$dockerMainDb = [
'host' => "mariadb-main",
'dbname' => 'my_database',
'user' => 'root',
'password' => 'main_root_password',
'type' => "mysql",
'flags' => DBO_DEFAULT,
'load' => 0,
];
$dockerReplicaDb = [
'host' => "mariadb-replica",
'dbname' => 'my_database',
'user' => 'root',
'password' => 'main_root_password',
'type' => "mysql",
'flags' => DBO_DEFAULT,
'max lag' => 60,
'load' => 1,
];
// Integration tests fail when run with replication, due to not having the temporary tables.
if ( !defined( 'MW_PHPUNIT_TEST' ) ) {
$wgDBservers = [ $dockerMainDb, $dockerReplicaDb ];
} else {
$wgDBserver = $dockerMainDb['host'];
$wgDBuser = $dockerMainDb['user'];
$wgDBpassword = $dockerMainDb['password'];
$wgDBtype = $dockerMainDb['type'];
}
Simulating replication lag
editOn the replica database (e.g. docker compose exec mariadb-replica mariadb -u root -p
):
STOP REPLICA;
CHANGE MASTER TO MASTER_DELAY=$n;
START REPLICA;
where $n
is the number of seconds to delay. Set this to 0
to remove the lag.
You can check the amount of lag by running SHOW REPLICA STATUS\G;
and looking for the row SQL_Delay
.
Postgres (single database server)
editThese instruction assume a new installation (no LocalSettings.php).
docker-compose.override.yml
services:
database:
image: postgres
environment:
POSTGRES_PASSWORD: example
volumes:
- dbdata:/var/lib/postgresql/data
volumes:
dbdata:
driver: local
To install the MediaWiki database tables, use:
docker compose exec mediawiki php maintenance/install.php --dbuser postgres --dbpass example --dbserver database --dbtype postgres --lang en --pass dockerpass mediawiki admin