Skip to content

Import and export MySQL dumps

Releasev0.35.0 · cfeceb67

Watch it in action

Export and restore a MySQL database

The bundled mysql service runs MariaDB and supplies mariadb and mariadb-dump. These examples use its development credentials, root / toor. A custom MySQL image may instead supply mysql and mysqldump.

Start the service and use an existing database, such as an installed wiki named default:

Terminal window
mw dev mysql create

No host database port needs to be exposed. Add --context NAME after dev when using another context.

Back up before destroying or changing the image

Section titled “Back up before destroying or changing the image”

Export a dump before any destructive database operation — the data is not otherwise preserved:

  • mw dev mysql destroy removes the service’s data volume outright.
  • Changing MYSQL_IMAGE across a MariaDB major version (for example 10.11 to 11.4) upgrades the on-disk data format one way, and cannot be downgraded afterward.

Both the primary and replica enable MARIADB_AUTO_UPGRADE=1 so the image runs any required system-table upgrade on startup. The image backs up the system database before upgrading; this is not a backup of your wiki databases.

Take a dump with Export a dump first, recreate the service on the new image, then reload it with Import a dump. If you do not need the existing data, skip the dump and reinstall with mw dev mediawiki install --dbtype=mysql.

Inspect the dump and back up existing data first: SQL dumps can replace tables or contain DROP, CREATE DATABASE and USE statements. For a single-database dump without its own database-selection statements:

Terminal window
mw --no-interaction dev compose exec -- -T mysql mariadb -uroot -ptoor default < ./default.sql

The target database must already exist. -T disables the container terminal so SQL reaches stdin unchanged; --no-interaction prevents CLI setup questions consuming that input. Omit the final default argument if the dump selects its own databases. Changing that argument does not rename databases embedded in the SQL.

A MediaWiki restore does not include uploaded files or LocalSettings.php. Configure the intended wiki before restoring, restore its uploads separately, then run its schema updates if required by the MediaWiki version:

Terminal window
mw dev mediawiki mwscript MW_DB=default update

Write the dump inside the container, then copy it to the host:

Terminal window
mw --no-interaction dev compose exec -- -T mysql sh -c 'mariadb-dump -uroot -ptoor --single-transaction default > /tmp/wm-default.sql'
mw --no-interaction dev compose cp mysql:/tmp/wm-default.sql ./default.sql
mw --no-interaction dev compose exec -- -T mysql rm /tmp/wm-default.sql

This avoids mixing CLI startup notices into SQL redirected from the CLI’s stdout. Replace default with your database name. The dump contains that database’s tables without CREATE DATABASE or USE, so it can be imported into another existing database. --single-transaction gives a consistent snapshot of transactional tables; stop application writes if non-transactional tables also need a consistent backup.

See database services for web interfaces and replication.


Read this guide offline with mw help guides dev mysql-dumps.

Report an issue with this page

Report a documentation issue

The report includes this page’s link, section and documentation version.

Review and submit on Phabricator, or run the command using your saved CLI profile. The command asks before creating the task.