Skip to content

MySQL / MariaDB replication

This is tested on MariaDB 11.

Prepare source server/database

Prepare mysql source

/etc/mysql/conf.d/mysqld.cnf
[mysqld]
server-id = 1
log-bin = /var/log/mysql/mysql-bin.log
binlog-format = ROW
binlog-do-db = databeast
bind-address = 0.0.0.0
gtid_strict_mode = 1
log_slave_updates = 1

Restart mariadb: systemctl restart mariadb

Create replication user

mysql -u root -p
DROP USER IF EXISTS 'replicator'@'replica.example.com';
CREATE USER 'replicator'@'replica.example.com' IDENTIFIED BY 'supersecure';
GRANT REPLICATION SLAVE, REPLICATION CLIENT ON *.* TO 'replicator'@'replica.example.com';
FLUSH PRIVILEGES;

Create database dump

mariadb-dump -u root -psupersecuresource --single-transaction --gtid --master-data=1 --databases databeast > /tmp/databeast.sql

Or feel lucky with pipes... Execute this from the replica host.

mysql -u root -p -e "CREATE DATABASE IF NOT EXISTS databeast;"
mariadb-dump -h source.example.com -u root -p --single-transaction --databases databeast | pv | mysql -u root -p

Configure replicate

Prepare mysql replica

/etc/mysql/conf.d/mysqld.cnf
[mysqld]
server-id = 2
read-only = 1
relay-log = /var/log/mysql/mysql-relay-bin
log-bin = /var/log/mysql/mysql-bin.log
binlog-format = ROW
bind-address = 0.0.0.0
gtid_strict_mode = 1
log_slave_updates = 1

Restart mariadb: systemctl restart mariadb

Copy database dump

scp -O source.example.com:/tmp/databeast.sql /tmp/databeast.sql

Import database dump

mysql -u root -p -e "CREATE DATABASE IF NOT EXISTS databeast;"
mysql -u root -p < /tmp/databeast.sql

Activate replication

mysql -u root -p
STOP REPLICA;
RESET REPLICA;
CHANGE REPLICATION SOURCE TO
  SOURCE_HOST='source.example.com',
  SOURCE_USER='replicator',
  SOURCE_PASSWORD='supersecure',
  SOURCE_USE_GTID=slave_pos;
START REPLICA;

Monitor replication

mysql -u root -p
SHOW REPLICA STATUS\G
*************************** 1. row ***************************
                Slave_IO_State: Waiting for master to send event
                   Master_Host: source.example.com
                   Master_User: replicator
                   Master_Port: 3306
                 Connect_Retry: 60
               Master_Log_File: mysql-bin.000020
           Read_Master_Log_Pos: 10338204
                Relay_Log_File: mysql-relay-bin.000002
                 Relay_Log_Pos: 10321516
         Relay_Master_Log_File: mysql-bin.000020
              Slave_IO_Running: Yes
             Slave_SQL_Running: Yes
               Replicate_Do_DB:
           Replicate_Ignore_DB:
            Replicate_Do_Table:
        Replicate_Ignore_Table:
       Replicate_Wild_Do_Table:
   Replicate_Wild_Ignore_Table:
                    Last_Errno: 0
                    Last_Error:
                  Skip_Counter: 0
           Exec_Master_Log_Pos: 10338204
               Relay_Log_Space: 10321825
               Until_Condition: None
                Until_Log_File:
                 Until_Log_Pos: 0
            Master_SSL_Allowed: Yes
            Master_SSL_CA_File:
            Master_SSL_CA_Path:
               Master_SSL_Cert:
             Master_SSL_Cipher:
                Master_SSL_Key:
         Seconds_Behind_Master: 0
 Master_SSL_Verify_Server_Cert: Yes
                 Last_IO_Errno: 0
                 Last_IO_Error:
                Last_SQL_Errno: 0
                Last_SQL_Error:
   Replicate_Ignore_Server_Ids:
              Master_Server_Id: 1
                Master_SSL_Crl:
            Master_SSL_Crlpath:
                    Using_Gtid: Slave_Pos
                   Gtid_IO_Pos: 0-1-581756
       Replicate_Do_Domain_Ids:
   Replicate_Ignore_Domain_Ids:
                 Parallel_Mode: optimistic
                     SQL_Delay: 0
           SQL_Remaining_Delay: NULL
       Slave_SQL_Running_State: Slave has read all relay log; waiting for more updates
              Slave_DDL_Groups: 0
Slave_Non_Transactional_Groups: 0
    Slave_Transactional_Groups: 28339
          Replicate_Rewrite_DB:
1 row in set (0.001 sec)