Setting up a Replication Slave with Mariabackup

The terms master and slave have historically been used in replication, but the terms terms primary and replica are now preferred. The old terms are used throughout the documentation, and in MariaDB commands, although MariaDB 10.5 has begun the process of renaming. The documentation will follow over time. See MDEV-18777 to follow progress on this effort.

Mariabackup makes it very easy to set up a replication slave using a full backup. This page documents how to set up a replication slave from a backup.

If you are using MariaDB Galera Cluster, then you may want to try one of the following pages instead:

Backup the Database and Prepare It

The first step is to simply take and prepare a fresh full backup of a database server in the replication topology. If the source database server is the desired replication master, then we do not need to add any additional options when taking the full backup. For example:

$ mariabackup --backup \
   --target-dir=/var/mariadb/backup/ \
   --user=mariabackup --password=mypassword

If the source database server is a replication slave of the desired replication master, then we should add the --slave-info option, and possibly the --safe-slave-backup option. For example:

$ mariabackup --backup \
   --slave-info --safe-slave-backup \
   --target-dir=/var/mariadb/backup/ \
   --user=mariabackup --password=mypassword

And then we would prepare the backup as you normally would. For example:

$ mariabackup --prepare \
   --target-dir=/var/mariadb/backup/

Copy the Backup to the New Slave

Once the backup is done and prepared, we can copy it to the new slave. For example:

$ rsync -avP /var/mariadb/backup dbserver2:/var/mariadb/backup

Restore the Backup on the New Slave

At this point, we can restore the backup to the datadir, as you normally would. For example:

$ mariabackup --copy-back \
   --target-dir=/var/mariadb/backup/

And adjusting file permissions, if necessary:

$ chown -R mysql:mysql /var/lib/mysql/

Create a Replication User on the Master

Before the new slave can begin replicating from the master, we need to create a user account on the master that the slave can use to connect, and we need to grant the user account the REPLICATION SLAVE privilege. For example:

CREATE USER 'repl'@'dbserver2' IDENTIFIED BY 'password';
GRANT REPLICATION SLAVE ON *.*  TO 'repl'@'dbserver2';

Configure the New Slave

Before we start the server on the new slave, we need to configure it. At the very least, we need to ensure that it has a unique server_id value. We also need to make sure other replication settings are what we want them to be, such as the various GTID system variables, if those apply in the specific environment.

Once configuration is done, we can start the MariaDB Server process on the new slave.

Start Replication on the New Slave

At this point, we need to get the replication coordinates of the master from the original backup directory.

If we took the backup on the master, then the coordinates will be in the xtrabackup_binlog_info file. If we took the backup on another slave and if we provided the --slave-info option, then the coordinates will be in the file xtrabackup_slave_info file.

Mariabackup dumps replication coordinates in two forms: GTID coordinates and binary log file and position coordinates, like the ones you would normally see from SHOW MASTER STATUS output. We can choose which set of coordinates we would like to use to set up replication.

For example:

mariadb-bin.000096 568 0-1-2

Regardless of the coordinates we use, we will have to set up the master connection using CHANGE MASTER TO and then start the replication threads with START SLAVE.

GTIDs

If we want to use GTIDs, then we will have to first set gtid_slave_pos to the GTID coordinates that we pulled from either the xtrabackup_binlog_info file or the xtrabackup_slave_info file in the backup directory. For example:

$ cat xtrabackup_binlog_info
mariadb-bin.000096 568 0-1-2

And then we would set MASTER_USE_GTID=slave_pos in the CHANGE MASTER TO command. For example:

SET GLOBAL gtid_slave_pos = "0-1-2";
CHANGE MASTER TO 
   MASTER_HOST="dbserver1", 
   MASTER_PORT=3306, 
   MASTER_USER="repl",  
   MASTER_PASSWORD="password", 
   MASTER_USE_GTID=slave_pos;
START SLAVE;

File and Position

If we want to use the binary log file and position coordinates, then we would set MASTER_LOG_FILE and MASTER_LOG_POS in the CHANGE MASTER TO command to the file and position coordinates that we pulled; either the xtrabackup_binlog_info file or the xtrabackup_slave_info file in the backup directory, depending on whether the backup was taken from the master or from a slave of the master. For example:

CHANGE MASTER TO 
   MASTER_HOST="dbserver1", 
   MASTER_PORT=3306, 
   MASTER_USER="repl",  
   MASTER_PASSWORD="password", 
   MASTER_LOG_FILE='mariadb-bin.000096',
   MASTER_LOG_POS=568;
START SLAVE;

Check the Status of the New Slave

We should be done setting up the slave now, so we should check its status with SHOW SLAVE STATUS. For example:

SHOW SLAVE STATUS\G
Content reproduced on this site is the property of its respective owners, and this content is not reviewed in advance by MariaDB. The views, information and opinions expressed by this content do not necessarily represent those of MariaDB or any other party.

© 2021 MariaDB
Licensed under the Creative Commons Attribution 3.0 Unported License and the GNU Free Documentation License.
https://mariadb.com/kb/en/setting-up-a-replication-slave-with-mariabackup/