MySQL to MariaDB Migration: Safe Step-by-Step Guide
In this article
Migrating from MySQL to MariaDB is a straightforward process because MariaDB is a drop-in replacement, meaning it uses the same commands, APIs, and file formats. This guide walks you through a safe migration that minimizes downtime and data loss, whether you're moving a small application or a production database. Before you start, back up everything and test the migration on a staging environment.
What You'll Need
- Root or sudo access to the server(s) running MySQL and the target MariaDB server.
- A recent backup of your MySQL databases (use
mysqldumpor a file-level backup). - Enough disk space for the backup and the new MariaDB data directory (usually double your current data size).
- A maintenance window if you have live traffic, because the migration involves downtime.
- Access to the internet or a local repository to install MariaDB packages.
Step-by-Step
- Back up all databases – Run
mysqldump --all-databases --single-transaction --routines --triggers --events > backup.sqlon the MySQL server. For InnoDB tables,--single-transactiongives a consistent snapshot without locking. If you use MyISAM, add--lock-tables. - Verify the backup – Check the backup file size and grep for table names to ensure it's not empty. Restore it on a test MariaDB instance to confirm it works before touching production.
- Note your MySQL version and settings – Run
SELECT VERSION();and check yourmy.cnf(ormy.inion Windows) for custom settings likesql_mode,max_allowed_packet, and storage engines. MariaDB is compatible with MySQL 5.x and 8.x, but some old MySQL 5.1 features might need adjustment. - Stop the MySQL service – On most Linux systems, run
sudo systemctl stop mysqlorsudo service mysql stop. Ensure no processes are accessing the data directory. - Install MariaDB – Use your package manager. On Ubuntu/Debian:
sudo apt install mariadb-server. On CentOS/RHEL:sudo yum install mariadb-server. The MariaDB package will replace the MySQL server binaries, but your data directory remains. - Preserve your data directory (optional but recommended) – MariaDB can read MySQL's data files directly, but it's safer to keep your backup. Move the MySQL data directory (usually
/var/lib/mysql) to a backup location:sudo mv /var/lib/mysql /var/lib/mysql.bak. Then create a fresh MariaDB data directory:sudo mkdir /var/lib/mysql && sudo chown mysql:mysql /var/lib/mysql. - Start MariaDB and initialize it – Run
sudo systemctl start mariadb. MariaDB will create the system tables automatically. If you moved the data directory, you'll need to restore from your dump. - Restore your databases – If you used a fresh data directory, import the backup:
sudo mysql < backup.sql. If you kept the old data directory and MariaDB starts successfully, you can skip this step, but verify data integrity. - Run the upgrade script – MariaDB includes
mysql_upgrade(ormariadb-upgradein newer versions). Runsudo mysql_upgrade -u root -pto update system tables and check for incompatibilities. - Test your applications – Connect your application to MariaDB using the same host, port (3306), and credentials. Run a few queries, check logs, and monitor for errors.
- Switch over and monitor – Once testing passes, point all production traffic to MariaDB. Monitor error logs and performance for at least 24 hours.
Common Mistakes
- Skipping the backup – Even though MariaDB is compatible, a failed migration can corrupt data. Always have a verified backup.
- Ignoring MySQL-specific features – If you use MySQL's JSON data type, it works in MariaDB, but some functions like
JSON_TABLEmay differ. Check your queries. - Forgetting to run
mysql_upgrade– This step updates system tables and prevents weird errors later. - Not testing on a staging server – Production is not the place to discover a compatibility issue. Always test first.
- Changing the port or socket – Keep the same port (3306) and socket path to avoid breaking applications.
Tips
- Use
mysqldumpwith--opt(enabled by default) for optimized dump output. - If you have large databases, consider using Percona XtraBackup or MariaDB's
mariabackupfor a hot backup with minimal downtime. - Check your application's database driver – most MySQL drivers work with MariaDB, but update to the latest version for best compatibility.
- After migration, run
ANALYZE TABLEon your tables to update statistics for the query optimizer. - Keep the MySQL backup for at least a week after migration, in case you need to roll back.
Troubleshooting
MariaDB won't start after installation – Check the error log (/var/log/mysql/error.log or /var/log/mariadb/mariadb.log). If it says "Can't open the mysql.plugin table", you likely need to run mysql_upgrade or restore from a fresh data directory.
Access denied for user 'root' – MariaDB uses unix_socket authentication by default for root. Run sudo mysql to connect as root, then create a new user or set a password with ALTER USER 'root'@'localhost' IDENTIFIED BY 'newpassword';
Queries fail with syntax errors – Compare your MySQL version to MariaDB's compatibility. For example, MySQL 8.0's WITH clauses are supported, but some window functions might differ. Check the MariaDB documentation for the specific function.
Data appears missing – If you kept the old data directory, ensure the MySQL files are not corrupt. Run CHECK TABLE on your tables. If issues persist, restore from your dump.
Performance degradation – Run mysql_upgrade to update table statistics. Also, review your my.cnf – MariaDB has different defaults for some variables like innodb_buffer_pool_size; adjust them to match your previous settings.
Frequently Asked Questions
Is MariaDB 100% compatible with MySQL?
No, but it's highly compatible. Most applications work without changes, but some MySQL-specific features (like the JSON data type's internal storage) differ slightly. Test your application thoroughly.
Can I migrate without downtime?
Yes, using replication. Set up MariaDB as a replica of MySQL, let it catch up, then switch over. This requires more setup but minimizes downtime.
Will my existing MySQL databases work without changes?
Yes, MariaDB reads MySQL's data files directly. However, you should run mysql_upgrade to update system tables and ensure compatibility.
Do I need to change my application's connection string?
No, if you keep the same host, port, and credentials. If you change the port, update the connection string.
What about MySQL users and privileges?
They are preserved in the mysql database. After migration, verify that users can log in and have the correct privileges.
Can I roll back to MySQL after migrating?
Yes, restore from your backup. It's a good reason to keep the backup for a while.