MySQL8.0.26傳統主從復制(保姆式文檔)


第13章 MySQL主從復制-上(老男孩Linux雲計算MySQL)

1. 介紹
需要2台及以上節點,通過binlog回放實現數據復制同步。

搭建步驟:
https://www.processon.com/mindmap/60813d46e401fd53c7b675cf

2. 傳統復制 (Classic)
2.1 復制前提(搭建過程)

a. 准備2個以上MySQL實例
db01 10.0.0.51(內網)
db02 10.0.0.52


b. 主庫開啟binlog日志。
mysql> select @@log_bin;
mysql> select @@log_bin_basename;

c.創建復制用戶並授權
mysql> create user repl@'10.0.0.%' identified with mysql_native_password by '123';
mysql> grant replication slave on *.* to repl@'10.0.0.%';

d.確保主從環境server_id和server_uuid不同
1)查看
mysql> select @@server_id;
mysql> select @@server_uuid;

2)修改從庫server_id
vim /etc/my.cnf
server_id=2

3)修改從庫server_uuid
rm -f /data/3306/data/auto.cnf
/etc/init.d/mysqld restart

4)確保兩邊都reset master;讓環境干凈,減少干擾
特別是防止GTID影響

e. 同步的數據庫版本最好一致。

f. 從庫的數據同步(mysqldump xtrabackup clone plugin 停庫CP)。
========================================================
1)主數據庫鎖表
mysql> flush table with read lock; ##窗口不能關.
mysql> show master status;
+---------------+----------+--------------+------------------+-------------------+
| File | Position | Binlog_Do_DB | Binlog_Ignore_DB | Executed_Gtid_Set |
+---------------+----------+--------------+------------------+-------------------+
| binlog.000001 | 156 | | | |
+---------------+----------+--------------+------------------+-------------------+
2)備份主庫
mysqldump -uroot -poldboy123 -A --master-data=2 --single-transaction -R -E --triggers --max_allowed_packet=64M|gzip>/data/backup/master_`date +%F`.sql.gz

3)主數據庫解鎖
mysql> unlock tables;

4)拷貝到從庫
scp /data/backup/master_`date +%F`.sql.gz 10.0.0.52:/tmp

5)從庫恢復數據
gzip -d /tmp/master_2021-12-19.sql.gz
mysql -uroot -poldboy123</tmp/master_2021-12-19.sql ##如果有錯,刪除/tmp/master_2021-12-19.sql第一行.

6)從庫指定連接參數以及位置點
CHANGE MASTER TO
MASTER_HOST='10.0.0.51',
MASTER_USER='repl',
MASTER_PASSWORD='123',
MASTER_PORT=3306,
MASTER_LOG_FILE='binlog.000001',
MASTER_LOG_POS=156,
MASTER_CONNECT_RETRY=10;
###5.7以前以前語句,寫到master.info文件里.5.7及以后寫到表里

幫助:
help CHANGE MASTER TO

7)啟動專用復制線程 start slave;
mysql> start slave;
mysql> show slave status \G
成功標志:兩個Yes
Slave_IO_Running: Yes
Slave_SQL_Running: Yes

Or from MySQL 8.0.22:
mysql> STOP REPLICA;
mysql> SHOW REPLICA STATUS\G


2.2 傳統主從復制原理

1.5.6兩種復制方式
2.主庫開啟binlog日志,從庫獲取binlog日志,在數據庫里異步回放來實現的.
3.復制的時候有3個線程
2.2.1 涉及到的線程
# 主庫線程
binlog dump線程,投遞binlog。
作用:
a. 與從庫進行交互
b. 監控binlog的變化
c. 投遞binlog給從庫
監控:
show processlist;
##Binlog Dump | 1647 | Source has sent all binlog to replica; waiting for more updates

# 從庫線程
IO線程 :
a. 連接主庫
b. 和主庫的Dump THREAD交互
c. 接收和存儲主庫的binlog日志 ,存儲到relaylog(中繼日志)中

SQL線程
a. 回放relaylog中的日志到數據庫。
b.記錄回放的位置到relay-info.log中

監控:
show slave status \G


2.2.2 涉及到的文件
# 主庫:
binlog:二進制日志。

# 從庫:
relaylog :中繼日志,從庫用來臨時存儲接收到的binlog文件。
master.info: 連接主庫的信息(ip、port、user、password、已經獲取的binlog位置點)
5.7以上在表中,5.7以前是磁盤文件。
relay-log.info:存儲SQL線程回放過的日志信息。


2.2.3 畫圖說明主從復制原理 ******

 

 

 

構建傳統主從復制,mysqldump數據導入[從庫]問題:

[root@db01 ~]# mysqldump --defaults-file=/etc/my.cnf -uroot -poldboy123 --master-data=2 --single-transaction -A >oldboy.sql
mysqldump: [Warning] Using a password on the command line interface can be insecure.
Warning: A partial dump from a server that has GTIDs will by default include the GTIDs of all transactions, even those that changed suppressed parts of the database. If you don't want to restore GTIDs, pass --set-gtid-purged=OFF. To make a complete dump, pass --all-databases --triggers --routines --events.


--set-gtid-purged=off
Warning: A partial dump from a server that has GTIDs will by default include the GTIDs of all transactions, even those that changed suppressed parts of the database. If you don't want to restore GTIDs, pass --set-gtid-purged=OFF. To make a complete dump, pass --all-databases --triggers --routines --events.


--master-data丟棄了,替代參數--source-data=2
mysqldump --defaults-file=/etc/my.cnf -uroot -poldboy123 --source-data=2 --single-transaction --set-gtid-purged=off -A >all1.sql

###WARNING: --master-data is deprecated and will be removed in a future version. Use --source-data instead.


修改后的;
mysqldump --defaults-file=/etc/my.cnf -uroot -poldboy123 --source-data=2 --single-transaction --set-gtid-purged=off -A >oldboy.sql

從庫導入:
[root@db02 ~]# mysql -uroot -poldboy123 <oldboy.sql
mysql: [Warning] Using a password on the command line interface can be insecure.

使用GTID復制的時候,使用mysqldump備份數據不要加--set-gtid-purged=off,或顯示加--set-gtid-purged=on


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM