Set Up MySQL/MariaDB Master-Slave Replication

MySQL Master-Slave replication allows for data to be automatically copied to multiple computers. Changes are made to the designated “master” node and replicated to one or more designated “slave” nodes for read-only applications.

This tutorial provides step-by-step instructions for setting up a MySQL/MariaDB master-slave database configuration. CentOS 8 and MariaDB 10.5 are used in this example.

Two servers are used in this tutorial, db01 (the master node) and db02 (the slave node). If you already have a database running on the master node, you must dump and restore a backup of the database into the slave node prior to enabling replication.

Read more Set Up MySQL/MariaDB Master-Slave Replication

MySQL/MariaDB Database Backup Script for Linux

Update the setup variables in the script below and create in root’s home folder with permissions set to 700 (e.g. /root/.scripts/mysql_backup)

#!/bin/bash
 
# MySQL Server Setup
MUSER="root"
MPASS="rootpassword"
MHOST="localhost"
MPATH="/mnt/data/backups/mysql"
DATENUM=`date +%u`
 
# Get all database names
DBNAME="$(mysql -u $MUSER -h $MHOST -p$MPASS -Bse 'show databases')"
 
for db in $DBNAME
do
  if [ ! -d $MPATH/$db ]; then
    mkdir -p $MPATH/$db
  fi
  mysqldump -c -u $MUSER -p$MPASS -h $MHOST --databases $db --single-transaction | gzip -9 > $MPATH/$db/$db.$DATENUM.sql.gz
done

Read more MySQL/MariaDB Database Backup Script for Linux