Many Linux distributions now come pre-installed with Systemd. MariaDB added support for Systemd in version 10.1. This support mostly came as a contribution from MariaDB community. In this post, I will discuss how to manage multiple instances of MariaDB server using Systemd. Once MariaDB packages are installed, the Systemd unit files can be located under /lib/systemd/system/.
As we are going to deal with multiple instances, we are interested in the last unit file. With the current implementation, there are two ways to handle multiple instances.
- Individual configuration files for each instance
This is the default method for which you do not have to make any changes to the unit file. All you need is create separate configuration files for each MariaDB instance under the standard MariaDB configuration directory /etc/mysql/conf.d/ named with distinct suffixes (aka instance name) but common prefix “my”.
For instance, in my test setup, I have the following two option files:
That’s it. You can now start MariaDB instances using the following command:
So, why should configuration files have common prefix “my” but different suffixes? If you look into the unit file, the command used to start a MariaDB instance uses “%I” in –defaults-file option. This gets replaced by the instance name – the suffix used in the above command. Thus, MariaDB gets started with the corresponding configuration file.
- Using separate group suffixes in single configuration file
This method makes use of MariaDB’s –defaults-group-suffix option. So, you can add options for all the instances in a single configuration file, such that options with values unique to a particular instance are placed in a group with a different suffix. The instructions related to this method are commented by default in the multi-instance unit file. So in order to use this method, the multi-instance unit file (/lib/systemd/system/mariadb@.service) needs to be modified.
- Comment – ConditionPathExists=/etc/mysql/conf.d/my%I.cnf
- Comment all ExecStartPre and ExecStart commands with –defaults-file
- Uncomment all ExecStartPre and ExecStart commands with –defaults-group-suffix
- Reload the systemd daemon:
As you might have guessed by now, in this method the instance name “%I” is used in the –defaults-group-suffix (unlike the first method).
Thus, each instance is started with options from the group with specified suffix along with the option listed under common group(s) like [mariadb], [mysqld], [server] or [galera].
For example, the configuration file in my test setup has the following groups :
That’s all. Now, the same set of commands can be used to start/stop/restart/status MariaDB instances.
A word of caution: DO NOT use period in group suffix, as that will cause set-environment to fail.
Recent bug fixes:
- MDEV-10004: Galera’s pc.recovery process fails in 10.1 with systemd
- MDEV-10145: Systemd fails to start mysqld in multi-instance mode
Some useful links: