MySQL with Docker

Skip to main content
Du bist hier:
Drucken

MySQL with Docker

With Docker, you can start the database files of another server (the folder /var/lib/mysql) in a MariaDB container. This lets you rescue or test data from a broken server or from a backup and finally export it with mariadb-dump.

Step 1: Install Docker

You need a Linux server with Docker. On Ubuntu or Debian, install it as root with:

apt install docker.io

Step 2: Find out the version of the source

The container must use the same version as the source server, for example MariaDB 11.4. With a different version, the database often does not start or converts the files. If the source server is still running, this command shows the version there:

mysqld --version

On newer MariaDB versions, mariadbd --version also works. If you only have the files left, the version is usually stored in the file mysql_upgrade_info in the data folder (from MariaDB 11: mariadb_upgrade_info).

Step 3: Copy the database files

Copy the files before you start the container for the first time. If the container starts with an empty folder, it creates a new, empty database. The files must come from a stopped database server (on the source systemctl stop mariadb or systemctl stop mysql) or from a backup. Files of a running server are usually incomplete.

mkdir -p /docker/data
rsync -a root@server.musterdomain.ch:/var/lib/mysql/ /docker/data/

If the files are in a backup, copy the contents of the backed-up folder /var/lib/mysql to /docker/data/ instead.

Step 4: Start the container

docker run --detach --name mariadb \
  --publish 127.0.0.1:6603:3306 \
  --volume /docker/data:/var/lib/mysql \
  mariadb:11.4

Replace 11.4 with the version from step 2. With 127.0.0.1:6603, the database can only be reached on this server itself via port 6603. Never publish the port without 127.0.0.1: Docker bypasses the server’s firewall in that case.

You do not set a root password. The users and passwords of the source server apply, and a setting such as MARIADB_ROOT_PASSWORD is ignored when data already exists.

Step 5: Check the start and log in

docker logs mariadb

If the output ends with «ready for connections», the database is running. Otherwise, the log shows you the reason. Then log in and check with SHOW DATABASES; whether your databases are there:

docker exec -it mariadb mariadb -u root -p

Use the root password of the source server. If root had no password there (default on Ubuntu and Debian), leave out -p. If the data comes from a Plesk server, log in with -u admin. The password of this MySQL user is stored on the Plesk server in the file /etc/psa/.psa.shadow.

Step 6: Export the data

docker exec -it mariadb mariadb-dump -u root -p --routines --events --result-file=/tmp/musterdb.sql musterdb
docker cp mariadb:/tmp/musterdb.sql /root/musterdb.sql

Replace musterdb with the name of your database. To export all databases at once, use the option --all-databases instead of musterdb. The same user and password apply as in step 5. You can then import the file /root/musterdb.sql on another MySQL or MariaDB server.

Step 7: Clean up

When you no longer need the container, remove it:

docker rm -f mariadb

The files in /docker/data are kept. Only delete them once you have checked your export.

Notes

  • You can restart the container at any time with docker restart mariadb.
  • If you accidentally started the container with an empty folder, remove it with docker rm -f mariadb, empty /docker/data and start again at step 3.
  • If the data comes from MySQL instead of MariaDB, use the image mysql in the same version (for example mysql:8.0) and the commands mysql and mysqldump instead of mariadb and mariadb-dump.
Related Post