MySQL Replication

Master-Slave Replication

1- Master 上編輯 Mysql 設定檔

sudo nano /etc/mysql/mysql.conf.d/mysqld.cnf


server-id=1

log-bin=mysql-bin

log-slave-updates=true

skip_slave_start=1

gtid-mode=ON

enforce_gtid_consistency=ON

2- 創建一個用來做主從架構的使用者

CREATE USER 'andylee'@'%' IDENTIFIED WITH mysql_native_password BY 'zrQ1A4fQ^WZL8pkX!!';
GRANT ALL PRIVILEGES ON *.* TO 'andylee'@'%' WITH GRANT OPTION;
FLUSH PRIVILEGES;

3- 重啟 Mysql

systemctl restart mysql

4- Slave 上編輯 Mysql 設定檔

sudo nano /etc/mysql/mysql.conf.d/mysqld.cnf


server-id=2

relay_log=mysql-relay-bin

read_only=1

log-bin=mysql-bin

log_slave_updates=1

gtid-mode=ON

enforce_gtid_consistency=ON

require_secure_transport=ON

5- 重啟 Mysql

systemctl restart mysql


6- Slave 的 Mysql 下,執行

CHANGE MASTER TO
MASTER_HOST='192.168.1.238',
MASTER_USER='andylee',
MASTER_PASSWORD='zrQ1A4fQ^WZL8pkX!!',
MASTER_PORT=3306,
MASTER_AUTO_POSITION = 1;

7- 查看 Master 和 Slave 的狀態

SHOW MASTER STATUS;
SHOW SLAVE STATUS\G;

8- 啟動 Slave 覆寫或是停止

START SLAVE;
STOP REPLICA;

9- Master 建立一個資料庫

CREATE DATABASE abc;

10- Slave 查看是否有同步

SHOW DATABASES;

Master

Slave


Multi-Source Replication

1- 首先在 Master1 和 Master2,分別建立在 Mysql 測試資料

Master1

CREATE DATABASE IF NOT EXISTS test_innodb;
USE test_innodb;

CREATE TABLE IF NOT EXISTS users (
    id INT AUTO_INCREMENT PRIMARY KEY,
    name VARCHAR(50),
    email VARCHAR(50)
) ENGINE=InnoDB;

INSERT INTO users (name, email) VALUES
('John Doe', 'john@example.com'),
('Jane Smith', 'jane@example.com'),
('Bob Johnson', 'bob@example.com');

Master2

CREATE DATABASE IF NOT EXISTS test_myisam;
USE test_myisam;

CREATE TABLE IF NOT EXISTS products (
    id INT AUTO_INCREMENT PRIMARY KEY,
    name VARCHAR(50),
    price DECIMAL(10,2)
) ENGINE=MyISAM;

INSERT INTO products (name, price) VALUES
('Product A', 10.50),
('Product B', 20.25),
('Product C', 15.75);


2- Master1 上編輯 Mysql 設定檔,針對 test_innodb 資料庫

sudo nano /etc/mysql/mysql.conf.d/mysqld.cnf


server_id=1

log-bin=mysql-bin

binlog-format=row

binlog-do-db=test_innodb

重新啟動 Mysql

systemctl restart mysql


3- Master2 上編輯 Mysql 設定檔,針對 test_myisam 資料庫

sudo nano /etc/mysql/mysql.conf.d/mysqld.cnf


server_id=2

log-bin=mysql-bin

binlog-format=row

binlog-do-db=test_myisam

重新啟動 Mysql

systemctl restart mysql

4- 我們備份兩台的資料庫,需要先鎖定資料庫,建議再開一個 command line,因為離開 Mysql 會讓唯讀失效,並且將這個備份檔傳到 Slave

Master 1

FLUSH TABLES WITH READ LOCK;
mysqldump --user=andylee --password --databases test_innodb > /home/willywen/test_innodb_backup.sql
scp /home/willywen/test_innodb_backup.sql willywen@192.168.1.252:/home/willywen

Master 2 也一樣的做法

FLUSH TABLES WITH READ LOCK;
mysqldump --user=andylee --password --databases test_myisam > /home/willywen/test_myisam_backup.sql
scp /home/willywen/test_myisam_backup.sql willywen@192.168.1.252:/home/willywen

5- 檢視 Master 1 和 2 的狀態,File 和 Position 的值要記錄下來

SHOW MASTER STATUS;

6- Slave 上設定檔

sudo nano /etc/mysql/mysql.conf.d/mysqld.cnf


server-id = 3

master_info_repository=TABLE

relay_log_info_repository=TABLE

並且重新啟動

systemctl restart mysql

當使用 multiple replication channels 時須設定為 TABLE,透過下面指令檢視一下設定是否為 Table

SHOW GLOBAL VARIABLES WHERE variable_name LIKE '%repository%';


7- Slave 上先創建一個使用者

CREATE USER 'andylee'@'%' IDENTIFIED WITH mysql_native_password BY 'zrQ1A4fQ^WZL8pkX!!';
GRANT ALL PRIVILEGES ON *.* TO 'andylee'@'%' WITH GRANT OPTION;
FLUSH PRIVILEGES;

同時把備份檔案匯入

CREATE DATABASE test_innodb;
mysql -u andylee -p test_innodb < test_innodb_backup.sql
CREATE DATABASE test_myisam;
mysql -u andylee -p test_myisam < test_myisam_backup.sql


8- 接著我們要分別下命令指定對應的 Master 關係

CHANGE MASTER TO MASTER_HOST='192.168.1.238', MASTER_USER='andylee', MASTER_PASSWORD='zrQ1A4fQ^WZL8pkX!!', MASTER_LOG_FILE='mysql-bin.000004', MASTER_LOG_POS=197 FOR CHANNEL 'master-1';
CHANGE MASTER TO MASTER_HOST='192.168.1.201', MASTER_USER='andylee', MASTER_PASSWORD='zrQ1A4fQ^WZL8pkX!!', MASTER_LOG_FILE='mysql-bin.000002', MASTER_LOG_POS=157 FOR CHANNEL 'master-2';

9- 接著我們啟動對應關係,並查看一下是否 Slave_IO_Running: Yes 和 Slave_SQL_Running: Yes

START SLAVE FOR CHANNEL 'master-1';
SHOW SLAVE STATUS FOR CHANNEL "master-1"\G
START SLAVE FOR CHANNEL 'master-2';
SHOW SLAVE STATUS FOR CHANNEL "master-2"\G

10- 嘗試在 Master-1 和 Master-2 各新增一個資料表,看看 Slave 是否有增加

SHOW DATABASES;
SELECT * FROM test_innodb.users;
SELECT * FROM test_myisam.products;



參考文章

https://benjr.tw/105813

https://www.red-gate.com/simple-talk/blogs/a-beginners-guide-to-mysql-replication-part-3-multi-source-replication/


留言

這個網誌中的熱門文章

黑暗靈魂3[所有重要物品的取得方式]

黑暗靈魂3[所有姿勢取得方式]