You are currently viewing Migrate MySQL database

Migrate MySQL database

Move or Migrate MySQL database to another drive or another or new server – or PC. That’s how it works, but it’s not easy.

You can also use it to migrate volumes to bind mounts and vice versa. This is how it works.

Introduction:

An SQL database is gone if it is lost or damaged, and this can happen quickly. This usually has massive consequences. Or suddenly the smileys are missing and only ugly special characters are displayed – a classic case of an incorrect character set.

I have mainly executed the command lines in PowerShell. If you use “CMD” or the command prompt, some commands will not work as expected – but we will still need “CMD” later.

We are going the way of Docker Desktop for Windows and WSL (Windows Subsystem for Linux). This is not very different from using it under Linux. You just have to adapt a few commands. The Docker commands are the same.

If you only want to process a small, simple data volume or a mount point, this article is more suitable for you:

If your project is a PostgreSQL database, please switch to this article – the handling differs from that of a MySQL database.

Personal note:

Personally, I only touch a MySQL database when I really have to. A lot can go wrong – or simply not work as expected. Especially when restoring a mysqldump file, there can be problems if you use PowerShell.

Especially with the utf8mb4 character set, which is mandatory for European languages (including umlauts such as ö, ä, ü, Ö, Ä, Ü, ß), the restore should be done via the classic CMD prompt (DOS window).

You can find all the details in the article and in the video.

But as I said, things can quickly go wrong – always have several backups ready!


Notes on data backup

When processing storage media: hard disk, virtual hard disk, USB stick, volume etc. and databases, data may be lost for several reasons. Liability is generally excluded.

If you follow the topics and examples discussed in this article and use them, I strongly recommend that you make a data backup beforehand!


Video: Migrate MySQL database

Language: 🇩🇪
☝️ Use YouTube subtitles for all languages.

The way: Copy/export raw database

The quickest way is to copy the raw database – i.e. directly at file level.

As I use this procedure regularly, I can say the following from experience:
Copying usually only works on the first attempt.

My guess: The mysql.sock file is no longer correctly integrated or connected after the first access. However, I have never researched this – so it remains an assumption.

Procedure

  1. Stop the database, for example with the command “docker stop db”.
  2. Extract the database with the commands listed below.
  3. Then adjust the configuration, for example in “docker-compose.yml”, in “Portainer” or another tool that you use. This will point the database to the new directory. Follow the video for the detailed procedure.
  4. Apply the new configuration by deploying to Portainer or using the commands docker-compose down and docker-compose up -d, depending on your configuration.
  5. Finally, start the database, for example with “docker start db”.

Command lines

Sample command:

docker run --rm --volumes-from db -v d:/backup:/backup busybox cp -a -r /var/lib/mysql/. /backup/

PowerShell

My command, for example, then reads correctly:

docker run --rm --volumes-from db -v d:\"Visual Edit"\wpress-app\dbdata:/backup busybox cp -a -r /var/lib/mysql/. /backup/

PowerShell
  • “-rm” = Removes the container after it has been terminated
  • “–volumes-from” = Define the volume to be used
  • “db” = Name of the database / container or the ID. in this case the database name is “db”
  • “-v” = mount volume
  • “d:\”Visual Edit”\wpress-app\dbdata:/backup” Specify the paths for the volume. Save the database under “d:\”Visual Edit”\wpress-app\dbdata”. Data transfer in the container is “:/backup”
  • “busybox” = Use the “busybox” program to transfer the data
  • “cp” = Copy the database with “cp” (Linux command copy)
  • “-a” = Transfer archives, owner and rights
  • “-r” = Copy the directory structure
  • “/var/lib/mysql/. /backup/” = “/var/lib/mysql/.” Praha of the database in the container. Use “/backup/ to transfer the data.

For long file names or spaces, you must enclose the path in single or double quotation marks ” ” for the transfer to work.

I use the path d: “Visual Edit”\wpress-app\dbdata for the output, which corresponds to the future mount point of the database. Since I copy the database directly to the target (the future mount point), I don’t have to copy the database there separately and thus save myself the fuss with the MySQL.sock file.

The way MySQLdump

Alternatively, or if the method described above does not work, you can use “mysqldump” to transfer the database.

The “mysqldump” command is used with the running database.

Here you should make sure that nobody is working in the database anymore. Changes to the database after the backup are no longer applied. You should therefore ensure a clean cut and transition.

Command lines

Sample command:

docker exec -I (SQL Container Name) mysqldump -v --default-character-set=utf8mb4 -u root -p(SQL root Passwort) --all-databases > (Ziel Datei).sql

PowerShell

My command, for example, then reads correctly:

docker exec -i db mysqldump -v --default-character-set=utf8mb4 -u root -pPassWort --all-databases > mysql-all-data.sql

PowerShell

Procedure

  1. Back up the database with the command listed above
  2. Customize the configuration e.g. “docker-compose.yml”, “Portainer” or whatever you are using. So that the database uses the new directory. Follow the video here for a detailed procedure.
  3. Apply the new configuration with deploy in “Portainer” or with “docker-compose down” and “docker-compose up -d”, or depending on your configuration.
  4. If Docker does not initiate a new database in the target directory, create one with a short “PowerShell” script
    docker run -d –name mysql
    -e MYSQL_ROOT_PASSWORD=rootpass -p 3306:3306
    -v mysql_data:/var/lib/mysql `
    mysql:8.0
  5. Restore the database with the command listed below
  6. If you have created a database under point 4, remove the container again.
    docker stop mysql
    docker rm mysql

Restoration

Sample command

docker exec -i (SQL Container Name) mysql --default-character-set=utf8mb4 -u root -p(SQL root Passwort) < .\mysql-all-data_2025-06-14.sql

CMD-Terminal

The password for the root user is the one you entered during creation/initialization. For the one under point 4, it is “rootpass”, otherwise you can find it in your database configuration under docker-compose.yml or Portainer. Note that you may have stored this in a “secret file”.

“CMD” – command prompt, e.g. my command is then correct:

docker exec -i db mysql --default-character-set=utf8mb4 -u root -pPassWort   < .\mysql-all-data.sql.sql

CMD-Trminal
  • “docker exec” = Execute the following command in the container
  • “-i” = Do not execute as a prioritized task
  • “db” = Database name
  • “mysql” = Command for recovery
  • “–default-character-set=utf8mb4″ =32”-MB character set with öäüÖÄÜß + smileys
  • -u root” = Execute as root user
  • -pPassWort” = password for the root user
  • “.\mysql-all-data.sql” = Backup file that is to be restored

It is possible that an error containing “(Password: YES)” is displayed in the command prompt. Then simply add two spaces between “-pPassWord < .\mysql-all-data.sql.sql”. Then it should work.

… that was it. I hope you were able to implement everything and it helped you.

Link to support / donation for the channel
PayPal Link
Bank transfer, Bitcoin and Lightning

Docker #DockerVolume #MountPoint #Migration #DockerMountPoint #Volume #DockerData #DockerDataTransfer #DockerDataCopy #DockerDataMove #MySQL #MySQLMigrate #MySQLMove #MySQLDatabase

This Post Has One Comment

Leave a Reply