Loading docs/linux/mysql-replication.md +70 −12 Original line number Diff line number Diff line # MySQL / MariaDB replication This is tested on MariaDB 11. This is tested on MariaDB 11 and MySQL 8.0. ## Prepare source server/database Loading @@ -10,6 +10,8 @@ This is tested on MariaDB 11. /etc/mysql/conf.d/mysqld.cnf ``` MariaDB: ```shell [mysqld] server-id = 1 Loading @@ -21,7 +23,22 @@ gtid_strict_mode = 1 log_slave_updates = 1 ``` MySQL: ```shell [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 Loading @@ -38,15 +55,16 @@ FLUSH PRIVILEGES; ### Create database dump MariaDB: ```shell 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: ```shell 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 mysqldump -u root -psupersecuresource --single-transaction --set-gtid-purged=ON --source-data=1 --databases databeast > /tmp/databeast.sql ``` ## Configure replicate Loading @@ -57,6 +75,8 @@ mariadb-dump -h source.example.com -u root -p --single-transaction --databases d /etc/mysql/conf.d/mysqld.cnf ``` MariaDB: ```shell [mysqld] server-id = 2 Loading @@ -69,7 +89,25 @@ gtid_strict_mode = 1 log_slave_updates = 1 ``` MySQL: ```shell [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 Loading @@ -86,23 +124,31 @@ mysql -u root -p < /tmp/databeast.sql ### Import in one go If you feel lucky with pipes... MariaDB: ```shell mariadb-dump -h source.example.com \ -u replicator \ -psupersecure \ --single-transaction \ --gtid \ --master-data=1 --databases databeast | \ mysql -u replicator -psupersecure databeast mysql -u root -p -e "CREATE DATABASE IF NOT EXISTS databeast;" mariadb-dump -h source.example.com -u root -p --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 --source-data=1 --databases pms | mysql -u root -psupersecure ``` ### Activate replication ```shell mysql -u root -p ``` MariaDB: ```shell STOP SLAVE; RESET SLAVE; Loading @@ -114,6 +160,18 @@ CHANGE MASTER TO 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 Loading Loading
docs/linux/mysql-replication.md +70 −12 Original line number Diff line number Diff line # MySQL / MariaDB replication This is tested on MariaDB 11. This is tested on MariaDB 11 and MySQL 8.0. ## Prepare source server/database Loading @@ -10,6 +10,8 @@ This is tested on MariaDB 11. /etc/mysql/conf.d/mysqld.cnf ``` MariaDB: ```shell [mysqld] server-id = 1 Loading @@ -21,7 +23,22 @@ gtid_strict_mode = 1 log_slave_updates = 1 ``` MySQL: ```shell [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 Loading @@ -38,15 +55,16 @@ FLUSH PRIVILEGES; ### Create database dump MariaDB: ```shell 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: ```shell 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 mysqldump -u root -psupersecuresource --single-transaction --set-gtid-purged=ON --source-data=1 --databases databeast > /tmp/databeast.sql ``` ## Configure replicate Loading @@ -57,6 +75,8 @@ mariadb-dump -h source.example.com -u root -p --single-transaction --databases d /etc/mysql/conf.d/mysqld.cnf ``` MariaDB: ```shell [mysqld] server-id = 2 Loading @@ -69,7 +89,25 @@ gtid_strict_mode = 1 log_slave_updates = 1 ``` MySQL: ```shell [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 Loading @@ -86,23 +124,31 @@ mysql -u root -p < /tmp/databeast.sql ### Import in one go If you feel lucky with pipes... MariaDB: ```shell mariadb-dump -h source.example.com \ -u replicator \ -psupersecure \ --single-transaction \ --gtid \ --master-data=1 --databases databeast | \ mysql -u replicator -psupersecure databeast mysql -u root -p -e "CREATE DATABASE IF NOT EXISTS databeast;" mariadb-dump -h source.example.com -u root -p --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 --source-data=1 --databases pms | mysql -u root -psupersecure ``` ### Activate replication ```shell mysql -u root -p ``` MariaDB: ```shell STOP SLAVE; RESET SLAVE; Loading @@ -114,6 +160,18 @@ CHANGE MASTER TO 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 Loading