Sling Academy
Home/MySQL/MySQL 8: How to create a replica using Percona XtraBackup

MySQL 8: How to create a replica using Percona XtraBackup

Last updated: January 26, 2024

Introduction

Creating a replica (or slave) of a MySQL database is a foundational practice for ensuring data redundancy, maintaining backup, and facilitating load distribution across databases. With the release of MySQL 8, creating a replica has never been more robust and flexible, particularly when coupled with Percona’s XtraBackup tool, which allows you to perform hot backups of MySQL, MariaDB, and Percona Server databases. In this tutorial, you’ll learn step-by-step how to create a replica of a MySQL 8 database using Percona XtraBackup.

Prerequisites

Before starting this tutorial, you should have:

  • A running instance of MySQL 8 on the master server.
  • Sudo or root access on the master and replica servers.
  • Percona XtraBackup installed on the master server. Instructions for installation can be found on Percona’s official website.

Step 1: Configure the Master Server

Begin by configuring the master MySQL server to accept replication connections and to generate binary logs:

sudo nano /etc/mysql/mysql.conf.d/mysqld.cnf

Add or uncomment the following configurations:

server-id               = 1
log_bin                 = /var/log/mysql/mysql-bin.log
binlog_do_db            = my_database
binlog_format           = row

Replace my_database with your actual database name. Then, restart MySQL to apply the changes:

sudo systemctl restart mysql

Step 2: Creating a Database User for Replication

Create a dedicated user that will be used by the slave to connect to the master:

mysql -u root -p
CREATE USER 'replica_user'@'%' IDENTIFIED BY 'replica_password';
GRANT REPLICATION SLAVE ON *.* TO 'replica_user'@'%';
FLUSH PRIVILEGES;
exit

Step 3: Taking a Backup with XtraBackup

Now, let’s use Percona XtraBackup to create a full backup of the master server:

sudo xtrabackup --backup --user=root --password=password --target-dir=/data/backup

Step 4: Preparing the Backup

Before the backup can be used on the replica, it must be prepared:

sudo xtrabackup --prepare --target-dir=/data/backup

Step 5: Copying the Backup to the Replica

Use a tool like rsync to copy the backup to the replica server:

rsync -avP /data/backup replica_user@replica_ip:/data/backup

Step 6: Setting Up the Replica Server

On the replica server, stop MySQL and configure it for replication:

sudo systemctl stop mysql
sudo nano /etc/mysql/mysql.conf.d/mysqld.cnf

Add or update the following lines:

server-id               = 2
relay-log               = /var/log/mysql/mysql-relay-bin.log

Then start MySQL with the –skip-networking option to prevent any remote connections during preparation:

sudo mysqld --skip-networking &

Step 7: Restoring the Backup

With the replica MySQL server still running without networking, restore the backup:

sudo xtrabackup --copy-back --target-dir=/data/backup
chown -R mysql:mysql /var/lib/mysql

Step 8: Setting the Replica Configuration

Obtain the binary log file position from the backup to sync the replica with the master:

cat /data/backup/xtrabackup_binlog_info

You’ll see output like:

mysql-bin.000001 1234

This information will be used to point the replica to the correct position in the master’s binary log.

Now, configure the slave information on the replica:

mysql -u root -p
CHANGE MASTER TO
  MASTER_HOST='master_ip',
  MASTER_USER='replica_user',
  MASTER_PASSWORD='replica_password',
  MASTER_LOG_FILE='mysql-bin.000001',
  MASTER_LOG_POS=1234;
START SLAVE;
SHOW SLAVE STATUS\G
exit

Conclusion

In this tutorial, you have learned to create a MySQL 8 replica using Percona XtraBackup. We covered installing prerequisites, configuring the master and replica, backing up the master, copying this backup, preparing the replica, and finally, starting the replication process. With these steps, you can ensure your data is robustly backed up and easily scale your MySQL operations.

Next Article: MySQL 8: How to create a replica using mysqldump

Previous Article: How to read log files in MySQL 8

Series: MySQL Tutorials: From Basic to Advanced

MySQL

You May Also Like

  • MySQL: How to reset the AUTO_INCREMENT value of a table
  • MySQL: How to add a calculated column to SELECT query
  • MySQL: Eliminate orphan rows in one-to-many relationships
  • MySQL: Using R-Tree Indexes for Spatial Data Types
  • How to Create Custom Collations in MySQL
  • Using Hash Indexes in MySQL: A Practical Guide
  • Understanding Full-Text Indexes in MySQL
  • Partial Indexes in MySQL: A Practical Guide
  • MySQL: How to Remove FOREIGN KEY Constraints
  • Using ENUM in MySQL 8: A Practical Guide (with Examples)
  • MySQL: Creating a Fixed-Size Table by Using Triggers
  • One-to-Many Relationship in MySQL 8: A Practical Guide
  • Using Regular Expressions in MySQL 8: The Complete Guide
  • Using Loops in MySQL: A Practical Guide (with Examples)
  • How to Execute an SQL File in VS Code
  • Making use of the JSON_REMOVE() function in MySQL 8
  • MySQL 8: How to count rows in related tables
  • Replication in MySQL 8: A Comprehensive Guide
  • MySQL 8: FIRST_VALUE(), LAST_VALUE(), and NTH_VALUE() functions – Explained with examples