299 lines
6.1 KiB
Markdown
299 lines
6.1 KiB
Markdown
# MySQL / MariaDB replication
|
|
|
|
This is tested on MariaDB 11 and MySQL 8.0.
|
|
|
|
## Prepare source server/database
|
|
|
|
### Prepare mysql source
|
|
|
|
```shell
|
|
/etc/mysql/conf.d/mysqld.cnf
|
|
```
|
|
|
|
MariaDB:
|
|
|
|
```shell
|
|
[mysqld]
|
|
|
|
# server identification
|
|
server-id = 1
|
|
|
|
# network
|
|
bind-address = 0.0.0.0
|
|
|
|
# gtid configuration
|
|
gtid_strict_mode = 1
|
|
|
|
# binary logging
|
|
log-bin = /var/log/mysql/mysql-bin.log
|
|
binlog-format = ROW
|
|
binlog-do-db = databeast
|
|
|
|
# replication configuration
|
|
log_slave_updates = 1
|
|
```
|
|
|
|
MySQL:
|
|
|
|
```shell
|
|
[mysqld]
|
|
|
|
# server identification
|
|
server-id = 1
|
|
|
|
# network
|
|
bind-address = 0.0.0.0
|
|
|
|
# gtid configuration
|
|
gtid_mode = ON
|
|
enforce_gtid_consistency = ON
|
|
|
|
# binary logging
|
|
log_bin = /var/lib/mysql/mysql-bin.log
|
|
binlog_format = ROW
|
|
max_binlog_size = 100M
|
|
binlog-expire-logs-seconds = 172800
|
|
|
|
# replication configuration
|
|
log_slave_updates = 1
|
|
```
|
|
|
|
Restart mariadb: `systemctl restart mariadb`
|
|
Restart mysql: `systemctl restart mysql`
|
|
|
|
### Create replication user
|
|
|
|
```shell
|
|
mysql -u root -p
|
|
```
|
|
|
|
```shell
|
|
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:
|
|
|
|
```shell
|
|
mariadb-dump -u root -psupersecuresource --single-transaction --gtid --databases databeast > /tmp/databeast.sql
|
|
```
|
|
|
|
MySQL:
|
|
|
|
```shell
|
|
mysqldump -u root -psupersecuresource --single-transaction --set-gtid-purged=ON --source-data=2 --databases databeast > /tmp/databeast.sql
|
|
```
|
|
|
|
## Configure replicate
|
|
|
|
### Prepare mysql replica
|
|
|
|
```shell
|
|
/etc/mysql/conf.d/mysqld.cnf
|
|
```
|
|
|
|
MariaDB:
|
|
|
|
```shell
|
|
[mysqld]
|
|
|
|
# server identification
|
|
server-id = 2
|
|
|
|
# network
|
|
bind-address = 0.0.0.0
|
|
|
|
# gtid configuration
|
|
gtid_strict_mode = 1
|
|
|
|
# binary logging
|
|
log-bin = /var/log/mysql/mysql-bin.log
|
|
binlog-format = ROW
|
|
|
|
# replication configuration
|
|
log_slave_updates = 1
|
|
relay-log = /var/log/mysql/mysql-relay-bin
|
|
|
|
# parallel replication
|
|
slave_parallel_mode = optimistic
|
|
slave_parallel_threads = 2
|
|
|
|
# read-only replica
|
|
read-only = 1
|
|
```
|
|
|
|
MySQL:
|
|
|
|
```shell
|
|
[mysqld]
|
|
|
|
# server identification
|
|
server-id = 2
|
|
|
|
# network
|
|
bind-address = 0.0.0.0
|
|
|
|
# gtid configuration
|
|
gtid_mode = ON
|
|
enforce_gtid_consistency = ON
|
|
|
|
# binary logging
|
|
log_bin = /var/lib/mysql/mysql-bin.log
|
|
binlog_format = ROW
|
|
max_binlog_size = 100M
|
|
binlog-expire-logs-seconds = 172800
|
|
|
|
# replication configuration
|
|
log_slave_updates = 1
|
|
relay_log = /var/lib/mysql/mysql-relay-bin
|
|
replicate-do-db = databeast
|
|
|
|
# parallel replication
|
|
slave_parallel_type = LOGICAL_CLOCK
|
|
slave_parallel_workers = 2
|
|
|
|
# read-only replica
|
|
read_only = 1
|
|
```
|
|
|
|
Restart mariadb: `systemctl restart mariadb`
|
|
Restart mysql: `systemctl restart mysql`
|
|
|
|
### Copy database dump
|
|
|
|
```shell
|
|
scp -O source.example.com:/tmp/databeast.sql /tmp/databeast.sql
|
|
```
|
|
|
|
### Import database dump
|
|
|
|
```shell
|
|
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:
|
|
|
|
```shell
|
|
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:
|
|
|
|
```shell
|
|
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 \
|
|
--quick \
|
|
--databases databeast | mysql -u root -psupersecure
|
|
```
|
|
|
|
### Activate replication
|
|
|
|
```shell
|
|
mysql -u root -p
|
|
```
|
|
|
|
MariaDB:
|
|
|
|
```shell
|
|
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:
|
|
|
|
```shell
|
|
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
|
|
|
|
```shell
|
|
mysql -u root -p
|
|
```
|
|
|
|
```shell
|
|
SHOW SLAVE 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)
|
|
```
|