MySQL / MariaDB replication¶
This is tested on MariaDB 11 and MySQL 8.0.
Prepare source server/database¶
Prepare mysql source¶
/etc/mysql/conf.d/mysqld.cnf
MariaDB:
[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
MySQL:
[mysqld]
server-id = 1
log_bin = /var/lib/mysql/mysql-bin.log
binlog_format = ROW
binlog-expire-logs-seconds = 172800
max_binlog_size = 100M
gtid_mode = ON
enforce_gtid_consistency = ON
log_slave_updates = 1
Restart mariadb: systemctl restart mariadb
Restart mysql: systemctl restart mysql
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:
mariadb-dump -u root -psupersecuresource --single-transaction --gtid --master-data=1 --databases databeast > /tmp/databeast.sql
MySQL:
mysqldump -u root -psupersecuresource --single-transaction --set-gtid-purged=ON --source-data=1 --databases databeast > /tmp/databeast.sql
Configure replicate¶
Prepare mysql replica¶
/etc/mysql/conf.d/mysqld.cnf
MariaDB:
[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
MySQL:
[mysqld]
server-id = 2
relay_log = /var/lib/mysql/mysql-relay-bin
read_only = 1
replicate-do-db = databeast
log_bin = /var/lib/mysql/mysql-bin.log
binlog_format = ROW
binlog-expire-logs-seconds = 172800
max_binlog_size = 100M
gtid_mode = ON
enforce_gtid_consistency = ON
log_slave_updates = 1
Restart mariadb: systemctl restart mariadb
Restart mysql: systemctl restart mysql
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
Import in one go¶
If you feel lucky with pipes...
MariaDB:
mysql -u root -p -e "CREATE DATABASE IF NOT EXISTS databeast;"
mariadb-dump -h source.example.com -u root -psupersecuresource \
--single-transaction \
--databases databeast | mysql -u root -psupersecure
MySQL:
mysql -u root -psupersecuresource -e "CREATE DATABASE IF NOT EXISTS databeast;"
mysqldump -u root -h source.example.com -psupersecuresource \
--single-transaction \
--set-gtid-purged=ON \
--source-data=1 \
--databases pms | mysql -u root -psupersecure
Activate replication¶
mysql -u root -p
MariaDB:
STOP SLAVE;
RESET SLAVE;
CHANGE MASTER TO
MASTER_HOST='source.example.com',
MASTER_USER='replicator',
MASTER_PASSWORD='supersecure',
MASTER_USE_GTID=slave_pos;
START SLAVE;
MySQL:
STOP REPLICA;
RESET REPLICA;
CHANGE REPLICATION SOURCE TO
SOURCE_HOST='source.example.com',
SOURCE_USER='replicator',
SOURCE_PASSWORD='supersecure',
SOURCE_AUTO_POSITION=1;
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)