Migrating from MySQL to MariaDB
For those of you who may not be familiar, MariaDB - https://mariadb.org/ - is a completely open source fork of the MySQL database managed by the MariaDB Foundation. From the MariaDB Foundation's About page:
MariaDB Server is one of the most popular database servers in the world. It’s made by the original developers of MySQL and guaranteed to stay open source. Notable users include Wikipedia, WordPress.com and Google.
MariaDB turns data into structured information in a wide array of applications, ranging from banking to websites. Originally designed as enhanced, drop-in replacement for MySQL, MariaDB is used because it is fast, scalable, and robust, with a rich ecosystem of storage engines, plugins and many other tools make it very versatile for a wide variety of use cases.
MariaDB is developed as open source software and as a relational database it provides an SQL interface for accessing data. The latest versions of MariaDB also include GIS and JSON features.
The MariaDB fork of MySQL occurred in 2009 after MySQL AB was purchased, first by Sun Microsystems in 2008 and then by Oracle (purchasing Sun) in 2009. The MariaDB Foundation was created by Monty Widenius, the original founder of MySQL, in order to preserve a fully open source version of the software. It's named after Monty's daughter Maria.
So - with the background info out of the way, my situation is this: I run a Nextcloud instance for storing software config backups, cheat sheets for various editors and programs, and various types of personal files that I want to ensure don't disappear. I've been running Nextcloud pretty much since their fork from ownCloud, using MySQL as the backend for both. However, Nextcloud has announced that version 21 will no longer support MySQL version 5.7 and will require MySQL 8.0. Since this is a major revision update requiring me to migrate all the databases on my server (Debian 10.8), I figure now is as good a time as any to migrate to MariaDB. What follows are the basic steps to perform this migration.
- Step 1 - If your system is virtualized, take a snapshot. This will ensure that you can easily roll back to the current configuration if needed.
- Step 2 - Backup All Existing Databases
First off, we need to identify all the existing databases and back them up. Identification is easy enough, simply execute:
mysql -u root -p -e "show databases;"
This will give us output similar to the following:
+--------------------+
| Database |
+--------------------+
| information_schema |
| nextcloud |
| sys |
| ttrss |
| etc... |
+--------------------+
Since I have a sizable number of databases on my server, I wrote the following script to automate the process:
#!/bin/bash
# Read user account
echo -n "MySQL User Name: "
read mysql_user
echo -n -e "\n"
# Read password
echo -n "MySQL User Password: "
read -s mysql_password
echo -n -e "\n\n"
# Assign variables
directory="./mysql_database_backups"
database_count=13
current_date=`date +%F`
databases=`mysql -u ${mysql_user} --password=${mysql_password} -e "show databases;" --batch | tail -${database_count}`
# Backup the databases
for database in ${databases}
do
mysqldump -u ${mysql_user} --password=${mysql_password} ${database} >> ${directory}/${database}_${current_date}.sql
echo "Database ${database} backed up successfully."
done
# Exit successfully
exit 0
The script requires the following in order to execute successfully:
- I'm assuming you're using the MySQL user
root
or another account that has access to all databases. - The
database_count
variable contains the number of databases obtained using themysql
command from above. - The
directory
variable exists on your server.
It's not required that you have to use my script; in fact, if you only have 1 or 2 databases, it may be simpler to do the following:
mysqldump -u [MySQL User] -p [Database Name] >> [Backup File Name]
However, if you want to use the script, it can be downloaded directly from here.
Now that the databases are backed up, we want to shut down the existing MySQL instance so there are no port conflicts, etc. It's also a good idea to stop your webserver if you have sites that rely on the database server:
systemctl stop mysql [nginx|apache2]
Now we can proceed to install the MariaDB server. Please note - during the install, all installed MySQL packages will be removed:
After the install is finished, we want to execute mysql_secure_installation
. Since we already have a root password set, we'll need to enter it when prompted; you'll then be asked a series of questions (my answers are listed below, modify for your environment):
root@server [~]# mysql_secure_installation
NOTE: RUNNING ALL PARTS OF THIS SCRIPT IS RECOMMENDED FOR ALL MariaDB
SERVERS IN PRODUCTION USE! PLEASE READ EACH STEP CAREFULLY!
In order to log into MariaDB to secure it, we'll need the current
password for the root user. If you've just installed MariaDB, and
you haven't set the root password yet, the password will be blank,
so you should just press enter here.
Enter current password for root (enter for none):
OK, successfully used password, moving on...
Setting the root password ensures that nobody can log into the MariaDB
root user without the proper authorisation.
You already have a root password set, so you can safely answer 'n'.
Change the root password? [Y/n] n
... skipping.
By default, a MariaDB installation has an anonymous user, allowing anyone
to log into MariaDB without having to have a user account created for
them. This is intended only for testing, and to make the installation
go a bit smoother. You should remove them before moving into a
production environment.
Remove anonymous users? [Y/n] Y
... Success!
Normally, root should only be allowed to connect from 'localhost'. This
ensures that someone cannot guess at the root password from the network.
Disallow root login remotely? [Y/n] Y
... Success!
By default, MariaDB comes with a database named 'test' that anyone can
access. This is also intended only for testing, and should be removed
before moving into a production environment.
Remove test database and access to it? [Y/n] Y
- Dropping test database...
... Success!
- Removing privileges on test database...
... Success!
Reloading the privilege tables will ensure that all changes made so far
will take effect immediately.
Reload privilege tables now? [Y/n] Y
... Success!
Cleaning up...
All done! If you've completed all of the above steps, your MariaDB
installation should now be secure.
Thanks for using MariaDB!
After completing the configuration script, you should now be able to log in to MariaDB as root, using your existing password:
root@server [~]# mysql -u root -p
Enter password:
Welcome to the MariaDB monitor. Commands end with ; or \g.
Your MariaDB connection id is 21
Server version: 10.3.27-MariaDB-0+deb10u1 Debian 10
Copyright (c) 2000, 2018, Oracle, MariaDB Corporation Ab and others.
Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.
MariaDB [(none)]>
Once successfully logged in, you should be able to query your databases as normal. You may also start your webserver and test the sites using MySQL as the backend; everything should be working as expected.
You've now completed the migration to MariaDB, a fully open source (and very well supported) database server. In addition, you now have a script that can be used to easily back up your databases and could be fully automated with a few minor changes (I'll leave that as an exercise for the reader).