To migrate MariaDB to a new server, you must export the data from the source server using a tool like mariadb-dump, transfer the resulting SQL file to the destination server, and then import that data into a freshly installed MariaDB instance. Depending on the database size and the allowed downtime, this can be achieved through a simple logical backup or a more complex physical replication process to minimize service interruption.
Choosing the Right Migration Method
The method you choose for migrating MariaDB depends primarily on the volume of data you are moving and whether your application can tolerate a window of downtime. There are two primary categories of migration: logical and physical.
Logical migration involves exporting the database structure and data into a set of SQL statements. This is the most compatible method, as it allows you to move data between different versions of MariaDB or even different operating systems. Because the process involves recreating the database from scripts, it is generally slower for multi-terabyte datasets but safer for smaller to medium-sized installations.
Physical migration involves copying the actual data files stored on the disk (typically located in /var/lib/mysql). This is significantly faster because it bypasses the need to execute SQL commands to rebuild indexes and tables. However, physical migration requires that the source and destination servers run the exact same version of MariaDB and often the same operating system architecture to avoid binary incompatibility.
Step-by-Step Logical Migration Process
For most users, the logical migration using mariadb-dump (formerly mysqldump) is the most reliable approach. This process ensures data integrity and allows for easy verification.
- Prepare the Destination: Install MariaDB on the new server. Ensure the version is equal to or newer than the source server to avoid syntax errors during import. Secure the installation using
mariadb-secure-installation. - Export Data from Source: Run the dump command on the old server. To ensure a consistent snapshot, use the
--single-transactionflag for InnoDB tables, which prevents the database from locking during the export. Example command:mariadb-dump -u root -p --all-databases --single-transaction --routines --triggers > full_backup.sql. - Transfer the File: Use a secure transfer protocol like SCP or Rsync to move the SQL file to the new server. For example:
scp full_backup.sql user@new-server-ip:/tmp/. - Import Data to Destination: Execute the SQL file on the new server. Use the command:
mariadb -u root -p < /tmp/full_backup.sql. - Verify Data Integrity: Check the row counts of your largest tables and run a few sample queries to ensure the data migrated correctly.
- Update Application Configuration: Change the database host IP or hostname in your application's configuration files to point to the new server.
Minimizing Downtime with Replication
If your application cannot afford hours of downtime, a "cut-over" migration using Master-Slave replication is the professional standard. This method allows the new server to stay synchronized with the old server in real-time.
First, you perform a logical backup of the source server, but you include the --master-data flag. This records the exact binary log position at the moment of the backup. After importing this backup into the new server, you configure the new server as a replica of the old one. The new server then connects to the old server and "catches up" by downloading all changes that occurred since the backup was taken.
Once the new server's status shows that it is fully synchronized (Seconds_Behind_Master: 0), you place the old server in read-only mode to prevent new writes. You then perform a final check and point your application to the new server. This reduces the downtime from the length of the entire data transfer to just the few seconds it takes to update a configuration file and restart the application.
Common Migration Problems and Fixes
Migration rarely happens without a few hurdles. Understanding the common failure points can save hours of troubleshooting.
- Max Allowed Packet Size: If you have large BLOB or TEXT columns, the import may fail with a "packet too large" error. To fix this, increase the
max_allowed_packetvalue in themy.cnforserver.cnffile on the destination server (e.g., set it to 64M or 128M) and restart the service. - User Permission Issues: A common misconception is that
--all-databaseshandles everything. While it dumps themysqlsystem database (which contains users), differences in MariaDB versions can cause permission errors. It is often cleaner to recreate application users manually on the new server and grant them specific privileges. - Character Set Mismatches: If you see strange symbols (mojibake) after migration, it is likely a mismatch between the source and destination character sets. Ensure both servers use
utf8mb4for full Unicode support, including emojis. - Disk Space Exhaustion: A logical dump creates a text file that can be very large. If the source server is nearly full, the dump will fail. In such cases, piping the dump directly to the new server over SSH is more efficient:
mariadb-dump ... | ssh user@new-server "mariadb -u root -p".
Security and Safety Considerations
Moving a database involves handling sensitive information in transit. Security should be prioritized over convenience.
Avoid transferring SQL dumps over unencrypted FTP or HTTP. Always use SSH-based tools like SCP or SFTP. If you are moving data between cloud providers, consider using a private VPC peering connection or an encrypted VPN tunnel to ensure the data does not traverse the public internet in a vulnerable state.
Additionally, never run the migration process as the root OS user if possible; instead, use a dedicated database user with the necessary privileges. Once the migration is complete, remember to delete the temporary .sql files from the /tmp or home directories of both servers. These files contain your entire database in plain text and are a massive security risk if left behind.
Comparing Migration Tools
While mariadb-dump is the standard, other tools exist for specific needs. Mariabackup is the official tool for physical backups. Unlike the dump tool, it does not lock your database, making it ideal for very large datasets where a logical dump would take too long or cause too much lag.
For those moving to managed cloud environments (like AWS RDS or Google Cloud SQL), cloud-native migration services are available. These tools often automate the replication process and handle the version compatibility checks, though they may lock you into a specific provider's ecosystem.
Frequently Asked Questions
Do I need to stop the database during a logical migration?
Not necessarily. If you use the --single-transaction flag with InnoDB tables, MariaDB can create a consistent snapshot of the data without locking the tables. However, any changes made to the data after the dump starts will not be included in the backup, which is why a brief maintenance window is still recommended during the final cut-over.
Can I migrate from an older version of MySQL to MariaDB?
Yes, MariaDB was designed as a binary drop-in replacement for MySQL for many years. In most cases, a logical dump from MySQL can be imported directly into MariaDB. However, if the versions are very far apart (e.g., MySQL 5.5 to MariaDB 10.11), you may encounter deprecated syntax that requires manual correction in the SQL file.
Why is my import process so slow?
Importing a large SQL file is slow because MariaDB must rebuild indexes and check constraints for every row inserted. You can speed this up by temporarily disabling foreign key checks and autocommit. Adding SET FOREIGN_KEY_CHECKS=0; and SET UNIQUE_CHECKS=0; to the top of your SQL file can significantly reduce the import time.
What happens if the migration fails halfway through?
If a logical import fails, the database will be in a "partial" state, meaning some tables are updated and others are not. The safest way to recover is to drop the partially imported databases, investigate the error log to fix the cause (such as disk space or packet size), and restart the import process from the beginning.
How do I handle very large databases that exceed available disk space?
When you cannot afford the disk space for a temporary .sql file, use a pipe. By piping the output of the dump command directly into the import command of the remote server via SSH, the data streams across the network without ever being saved as a file on either disk. This is the most space-efficient way to migrate large datasets.