# mysql複製技術/mysql集群 ![](https://img2023.cnblogs.com/blog/3165277/202308/3165277-20230815183932805-1063237772.png) ## 準備 1.四台虛擬機都關閉防火牆 ``` systemctl st ...
mysql複製技術/mysql集群
準備
1.四台虛擬機都關閉防火牆
systemctl stop firewalld
systemctl disable firewalld
2.四台虛擬機都設置好host功能變數名稱解析,在/etc/hosts文件中添加如下
[root@mysql01 ~]# vim /etc/hosts
192.168.70.33 master1
192.168.70.34 master2
192.168.70.35 slave1
192.168.70.36 slave2
五台機器都ping一下網路
[root@mysql01 ~]# ping master1
PING master1 (192.168.70.33) 56(84) bytes of data.
64 bytes from master1 (192.168.70.33): icmp_seq=1 ttl=64 time=0.031 ms
64 bytes from master1 (192.168.70.33): icmp_seq=2 ttl=64 time=0.050 ms
64 bytes from master1 (192.168.70.33): icmp_seq=3 ttl=64 time=0.056 ms
3.在乾凈的環境下安裝好mysql(yum、二進位、源碼都行)
四台mysql統一密碼方便後續使用
[root@mysql02 ~]# grep password /var/log/mysqld.log
2023-08-13T13:44:46.837257Z 6 [Note] [MY-010454] [Server] A temporary password is generated for root@localhost: 7R_e*/+_)(l8
[root@mysql02 ~]# mysqladmin -uroot -p password
Enter password:
New password:
Confirm new password:
一、mysql M-S 傳統
master設置
1.首先在master1中創建資料庫、表 模擬伺服器已經運行一段時間
mysql> create database xux;
Query OK, 1 row affected (0.00 sec)
mysql> use xux;
Database changed
mysql> create table xux.t1(id int(10));
Query OK, 0 rows affected, 1 warning (0.02 sec)
mysql> insert into xux.t1 values(1),(2);
Query OK, 2 rows affected (0.09 sec)
Records: 2 Duplicates: 0 Warnings: 0
mysql> select * from xux.t1;
+------+
| id |
+------+
| 1 |
| 2 |
+------+
2 rows in set (0.00 sec)
2.設置master的mysql配置
1.因為是備庫中的i/o線程將主庫中的二進位日誌複製到中繼日誌中,所以需要打開二進位日誌,同時設置好server-id。
在/etc/my.cnf文件下[mysqld]下麵添加
vim /etc/my.cnf
[mysqld]
log-bin
server-id=1
[root@mysql01 ~]# tail -2 /etc/my.cnf
log-bin
server-id=1
2.設置好後重啟mysql
systemctl restart mysqld
3.創建複製用戶
mysql> CREATE USER 'rep'@'192.168.70.%' IDENTIFIED BY 'Xuxin_0709';
mysql> GRANT REPLICATION SLAVE,REPLICATION CLIENT ON *.* TO 'rep'@'192.168.70.%';
Query OK, 0 rows affected (0.00 sec)
mysql> FLUSH PRIVILEGES;
4.備份數據
[root@mysql01 ~]# mysqldump -u root -p 'Xuxin_0709' --all-databases --single-transaction --master-data=2 --flush-logs > `date +%F`-mysql-all.sql
擴展(可忽略)
如果設置--master-data=1 後續slave設置主伺服器可以不用填master_log_file和master_log_pos,但是資料庫導入必須使用source導入
5.複製給slave
[root@mysql01 ~]# scp ./2023-08-14-mysql-all.sql master2:/root
6.再插入一些數據
mysql> insert into xux.t1 values(3);
Query OK, 1 row affected (0.01 sec)
mysql> select * from xux.t1;
+------+
| id |
+------+
| 1 |
| 2 |
| 3 |
+------+
3 rows in set (0.00 sec)
slave設置
1.測試授權的用戶
[root@mysql02 ~]# mysql -hmaster1 -urep -p'Xuxin_0709'
2.開啟server-id
[root@mysql02 ~]# vim /etc/my.cnf
[mysqld]
server-id=2
[root@mysql02 ~]# systemctl restart mysqld
3.導入資料庫
先進入mysql暫停log-bin日誌記錄,不記錄導入數據的記錄
mysql> set sql_log_bin=0;
Query OK, 0 rows affected (0.00 sec)
導入sql
mysql> source 2023-08-14-mysql-all.sql
或者在linux界面導入
[root@localhost ~]# mysql -uroot -p < /2023-08-14-mysql-all.sql
mysql> select * from xux.t1;
+------+
| id |
+------+
| 1 |
| 2 |
+------+
2 rows in set (0.00 sec)
4.查看sql中的日誌master_log_file和master_log_pos
[root@mysql01 ~]# vim 2023-08-14-mysql-all.sql
5.設置主伺服器
mysql> change master to master_host='master1',master_user='rep',master_password='Xuxin_0709',master_log_file='mysql01-bin.000002',master_log_pos=157;
6.啟動從設備並查看slave狀態
mysql> start slave;
show slave status\G
7.查看數據可以發現數據已同步
mysql> select * from xux.t1;
+------+
| id |
+------+
| 1 |
| 2 |
| 3 |
+------+
3 rows in set (0.01 sec)
測試
再來測試一下
在master1 xux.t1表中插入數據 看看slave機器會不會同步數據
1.在master1機器登錄mysql插入數據
mysql> insert into xux.t1 values(4);
Query OK, 1 row affected (0.01 sec)
2.在slave機器查看數據
mysql> select * from xux.t1;
+------+
| id |
+------+
| 1 |
| 2 |
| 3 |
| 4 |
+------+
4 rows in set (0.00 sec)
二、mysql M-S GTID
準備
為接下來新的同步實驗需要將之前的同步設置清空,也就是重置master2的mysql
systemctl stop mysqld
rm -rf /var/lib/mysql/*
systemctl start mysqld
grep password /var/log/mysqld.log
mysql -uroot -p'w#lHVC_Rc30r'
mysqladmin -uroot -p password
master設置
1.啟動二進位日誌,伺服器ID,GTID
由於我們之前配置過了id這裡只需配置GTID。配置完後重啟mysqld
[root@mysql01 ~]# vim /etc/my.cnf
gtid_mode=ON
enforce_gtid_consistency=1
[root@mysql01 ~]# systemctl restart mysqld
2.授權複製用戶rep(上面M-S傳統已配置 這裡忽略)
3.備份數據
為了實驗方便這裡把之前的sql備份文件刪掉
[root@mysql01 ~]# rm -rf 2023-08-14-mysql-all.sql
備份一份新的
[root@mysql01 ~]# mysqldump -u root -p --all-databases --single-transaction --master-data=2 --flush-logs > `date +%F`-mysql-all.sql
傳給master2
[root@mysql01 ~]# scp ./2023-08-14-mysql-all.sql master2:/root/
4.模擬數據變化
mysql> insert into xux.t1 values(5);
Query OK, 1 row affected (0.01 sec)
mysql> select * from xux.t1;
+------+
| id |
+------+
| 1 |
| 2 |
| 3 |
| 4 |
| 5 |
+------+
5 rows in set (0.00 sec)
slave設置
1.測試rep用戶是否可用
[root@mysql02 ~]# mysql -hmaster1 -urep -p'Xuxin_0709'
2.啟動二進位日誌,伺服器ID,GTID,配置完後重啟mysqld
[root@mysql02 ~]# vim /etc/my.cnf
server-id=2
log_bin
gtid_mode=ON
enforce_gtid_consistency=1
[root@mysql01 ~]# systemctl restart mysqld
3.恢復手動同步數據
先關閉log-bin日誌記錄
mysql> set sql_log_bin=0;
導入數據
mysql> source /root/2023-08-14-mysql-all.sql;
查詢一下數據
mysql> select * from xux.t1;
+------+
| id |
+------+
| 1 |
| 2 |
| 3 |
| 4 |
+------+
4 rows in set (0.00 sec)
4.設置主伺服器
mysql> change master to master_host='master1',master_user='rep',master_password='Xuxin_0709',master_auto_position=1;
5.啟動slave並查看數據
mysql> start slave;
Query OK, 0 rows affected, 1 warning (0.02 sec)
mysql> select * from xux.t1;
+------+
| id |
+------+
| 1 |
| 2 |
| 3 |
| 4 |
| 5 |
+------+
5 rows in set (0.01 sec)
三、mysql M-M-S-S
考慮到主伺服器如果宕機,那個整個集群的寫入能力將會受到影響,那麼可以設置雙主多從的方式,為數據做好保障。
M-M
因為前面大多數設置已經設置好了,下麵是在上面已完成步驟的基礎下操作
1.在master2上進行授權 (可忽略)
查看user表 發現之前已經創建用戶並授權了 如果沒有就創建用戶並授權與上述操作一致不再贅述
mysql> select user,host from mysql.user;
+------------------+--------------+
| user | host |
+------------------+--------------+
| rep | 192.168.70.% |
| mysql.infoschema | localhost |
| mysql.session | localhost |
| mysql.sys | localhost |
| root | localhost |
+------------------+--------------+
5 rows in set (0.01 sec)
在master1上測試
[root@mysql01 ~]# mysql -urep -hmaster2 -p'Xuxin_0709';
2.配置master1
mysql> change master to master_host='master2',master_user='rep',master_password='Xuxin_0709',master_auto_position=1; //基於GTID
讓master1也有從的功能,所以現在master1 master2都即是主,也是從。
mysql> start slave;
3.測試
在master2中插入數據
mysql> insert into xux.t1 values(6);
在master1看數據是否會同步
mysql> select * from xux.t1;
+------+
| id |
+------+
| 1 |
| 2 |
| 3 |
| 4 |
| 5 |
| 6 |
+------+
6 rows in set (0.00 sec)
S-S
1.先把master1數據備份 傳給slave1 slave2
[root@mysql01 ~]# mysqldump -u root -p'Xuxin_0709' --all-databases --single-transaction --master-data=2 --flush-logs > mmss-mysql-all.sql
[root@mysql01 ~]# scp mmss-mysql-all.sql slave1:/root
[root@mysql01 ~]# scp mmss-mysql-all.sql slave2:/root
2.在slave1 slave2 導入數據(兩台機器都導入數據)
[root@mysql03 ~]# mmysql -uroot -p'Xuxin_0709' < mmss-mysql-all.sql
[root@mysql04 ~]# mmysql -uroot -p'Xuxin_0709' < mmss-mysql-all.sql
3.配置slave1 slave2 (從伺服器沒必要開啟log-bin)並重啟
[root@mysql03 ~]# vim /etc/my.cnf
server-id=3
gtid_mode=ON
enforce_gtid_consistency=1
master-info-repository=TABLE
relay-log-info-repository=TABLE
[root@mysql04 ~]# vim /etc/my.cnf
server-id=4
gtid_mode=ON
enforce_gtid_consistency=1
master-info-repository=TABLE
relay-log-info-repository=TABLE
[root@mysql03 ~]# systemctl restart mysqld
[root@mysql04 ~]# systemctl restart mysqld
4.設置主伺服器
slave1
mysql> change master to master_host='master1',master_user='rep',master_password='Xuxin_0709',master_auto_position=1 for channel 'master1';
mysql> change master to master_host='master2',master_user='rep',master_password='Xuxin_0709',master_auto_position=1 for channel 'master2';
mysql> start slave;
mysql> show slave status\G
slave2
mysql> change master to master_host='master1',master_user='rep',master_password='Xuxin_0709',master_auto_position=1 for channel 'master1';
mysql> change master to master_host='master2',master_user='rep',master_password='Xuxin_0709',master_auto_position=1 for channel 'master2';
mysql> start slave;
mysql> show slave status\G
5.測試
在master1 master2 變更數據
master1
mysql> insert into xux.t1 values(7);
查看master2 slave1 slave2數據是否變化
mysql> select * from xux.t1;
master2
mysql> insert into xux.t1 values(8);
查看master1 slave1 slave2數據是否變化
mysql> select * from xux.t1;