When migrating database servers, many DBAs consider copying the entire /var/lib/mysql
directory as a potential shortcut. While this approach can work, it comes with significant version compatibility considerations, especially when moving between MySQL and MariaDB.
Here's what you need to verify before attempting the migration:
# Check your current MySQL/MariaDB version
SHOW VARIABLES LIKE 'version%';
# Verify storage engine configuration
SHOW VARIABLES LIKE 'innodb_file_per_table';
Follow this process for the safest migration:
- Stop both database servers
- Backup your existing data directory
- Copy the files with proper permissions
- Run the upgrade process
# On source server
sudo systemctl stop mysql
sudo rsync -avz /var/lib/mysql/ user@newserver:/var/lib/mysql/
# On destination server
sudo chown -R mysql:mysql /var/lib/mysql
sudo mysql_upgrade -u root -p
sudo systemctl start mariadb
When dealing with innodb_file_per_table
databases:
- Verify all .ibd files are intact
- Check for orphaned tablespace files
- Ensure consistent table definitions
# Recover orphaned InnoDB tables
ALTER TABLE database.table IMPORT TABLESPACE;
For more reliable results, consider these alternatives:
# Using mysqldump with compression
mysqldump -u root -p --all-databases | gzip > alldbs.sql.gz
# Using Percona XtraBackup (for large databases)
innobackupex --user=root --password=password /backup/dir
innobackupex --apply-log /backup/dir
After migration, always verify data integrity:
# Check table status
CHECK TABLE tablename EXTENDED;
# Verify foreign key constraints
SET foreign_key_checks = 0;
-- Perform operations
SET foreign_key_checks = 1;
When migrating between MySQL and MariaDB by copying the entire /var/lib/mysql
directory, several technical considerations come into play:
- Storage Engine Compatibility: InnoDB file-per-table configuration (innodb_file_per_table=ON) makes migration easier as each table has its own .ibd file
- Version Differences: MariaDB 10.0 maintains good compatibility with MySQL 5.6, but newer versions may introduce changes
- System Table Structures: The mysql system database schema may differ between versions
Here's the safest way to perform the migration:
# On the source server (MySQL):
sudo systemctl stop mysql
sudo rsync -avz /var/lib/mysql/ user@new-server:/tmp/mysql-data/
# On the destination server (MariaDB):
sudo systemctl stop mariadb
sudo mv /var/lib/mysql /var/lib/mysql-backup
sudo mv /tmp/mysql-data /var/lib/mysql
sudo chown -R mysql:mysql /var/lib/mysql
After moving the files, run these checks:
# Check for any errors in the log
sudo tail -n 100 /var/log/mysql/error.log
# Run mysql_upgrade
sudo mysql_upgrade -u root -p
# Verify table status
mysqlcheck -u root -p --all-databases --check-upgrade --auto-repair
Common problems and their solutions:
- Incompatible .ibd files: If tables won't open, try exporting/importing with mysqlpump:
mysqlpump --user=root --password --all-databases > full_backup.sql
- Authentication plugin conflicts: For mysql_native_password issues:
ALTER USER 'user'@'localhost' IDENTIFIED WITH mysql_native_password BY 'password';
After migration, consider these optimizations:
# For InnoDB tables in MariaDB
SET GLOBAL innodb_defragment=1;
SET GLOBAL innodb_optimize_fulltext_only=1;
OPTIMIZE TABLE important_table;