Mais conteúdo relacionado
Semelhante a 2 ivan ma-mysql複製的演進和應用-twn- v1 (20)
2 ivan ma-mysql複製的演進和應用-twn- v1
- 1. Copyright © 2015, Oracle and/or its affiliates. All rights reserved. |
馬楚成 - 首席技術顧問
Ivan Ma
2015-12-19
Copyright © 2015, Oracle and/or its affiliates. All rights reserved.
MySQL複製的演進和應用
- 2. Copyright © 2015, Oracle and/or its affiliates. All rights reserved. |
Safe Harbor Statement
The following is intended to outline our general product direction. It is intended for
information purposes only, and may not be incorporated into any contract. It is not a
commitment to deliver any material, code, or functionality, and should not be relied upon
in making purchasing decisions. The development, release, and timing of any features or
functionality described for Oracle’s products remains at the sole discretion of Oracle.
2
- 3. Copyright © 2015, Oracle and/or its affiliates. All rights reserved. |
目录
MySQL複製基礎 與 演進發展
MySQL複製應用
MySQL 5.7 GA的複製新功能
In labs - Group Replication和MySQL集群管理
1
2
3
4
3
- 4. Copyright © 2015, Oracle and/or its affiliates. All rights reserved. |
MySQL複製基礎
與
演進發展
1
- 5. Copyright © 2015, Oracle and/or its affiliates. All rights reserved. |
• 3.23
– One-way Replication
• 4.1
– Support was added for replication over SSL
connections
• 5.0/5.1
– Row-based Replication
• 5.5
– Semi-Sync Replication
– Replication Heartbeat
– Per server Replication Filter
• 5.6
– GTID
– Crash Safe Slave and Binlog
– Binlog Group Commit
– Optimized Row Based Replication
– Multi-threaded Slave (per Database)
– Time-delayed Replication
– Utility for Failover and Admin
• 5.7
– What’s New 复制新功能
MySQL 復製演進發展
5
2015年10月26日MySQL 5.7.9 GA啦
- 6. Copyright © 2015, Oracle and/or its affiliates. All rights reserved. |
複製基礎 :複製的框架
Insert...
Insert...
B
binary log
Insert...
relay log
Insert...
A
binary log
Client
Dump
thread
IO
thread
SQL
Threads
Receiver
Meta-data
Update
Applier
Meta-data
Update
Network
6
- 7. Copyright © 2015, Oracle and/or its affiliates. All rights reserved. |
複製基礎 :複製的框架
Insert...
Insert...
B
binary log
Insert...
relay log
Insert...
A
binary log
Client
Sender
thread
Receiver
thread
Applier
Threads
Receiver
Meta-data
Update
Applier
Meta-data
Update
Network
7
- 8. Copyright © 2015, Oracle and/or its affiliates. All rights reserved. |
複製基礎 :複製的框架
• Binary Log
– 基於文件的邏輯日誌,記錄了Master上數據的變化。
• 語句格式(Statement),行格式(Row),混合模式(Mixed)。
– 每個事務會被記錄為一組Binlog Events.
– Control events: Rotate, Format Description, Gtid, ... Layout of a
Binary Log File
GTID ...E1 E2 COMMIT ...E1 E2 COMMIT
8
BEGIN GTID BEGIN...
- 9. Copyright © 2015, Oracle and/or its affiliates. All rights reserved. |
M S
write clients read clients
讀性能的擴展(Read scale-out)
複製基礎:複製的應用
9
S
S
S
M
read clients
write clients
More
reads?
More
slaves!
- 10. Copyright © 2015, Oracle and/or its affiliates. All rights reserved. |
C
B
A
C
B
ACrash
Uh Oh!
C
B
A
B is the
new master
Whew!
Redundancy: 如果主服務器宕機,可以用一個從服務器來做新的主服務器。
複製基礎:複製的應用
10
- 11. Copyright © 2015, Oracle and/or its affiliates. All rights reserved. |
使用不同存储引擎(Storage Engine)
複製基礎:複製的應用
11
https://dev.mysql.com/doc/refman/5.6/en/replication-solutions-diffengines.html
SE=ndb SE=InnoDB
Analytic
Replication
SE=ndb
SE=ndb
SE=ndb
ndbd ndbd ndbd ndbd
- 12. Copyright © 2015, Oracle and/or its affiliates. All rights reserved. |
M
write clients
在線備份和做報表(On-line Backup and Reporting)
複製基礎:複製的應用
12
S
報表應用
海量信息查詢
S 在線備份
S
[ BI ] 商業智能應用
- 13. Copyright © 2015, Oracle and/or its affiliates. All rights reserved. |
CB
BA
AC
Image from
www.ginkgomaps.com
複製基礎:複製的應用
遠距離數據分發
13
- 14. Copyright © 2015, Oracle and/or its affiliates. All rights reserved. |
MySQL Fabric 通过数据分片来提升MySQL的性能...
那麼寫性能的擴展(write scale-out)呢?
14
S
M M
多源複製
M M
數據分片生成
MySQL 5.7
新功能
- 15. Copyright © 2015, Oracle and/or its affiliates. All rights reserved. |
MySQL 5.7 GA裡的複製新功能
http://mysqlhighavailability.com/replication-in-mysql-5-7-ga/
3
- 16. Copyright © 2015, Oracle and/or its affiliates. All rights reserved. |
MySQL 5.7 GA裡的複製新功能
GTID 新功能
多線程複製(Multi-Threaded Slave)新功能
多源複製(Multiple Source Replication)
Slave可用性的改進 / Master 性能的提升
半同步複製(Semi-synchronous Replication)的新功能
其他功能…
3
2015年11月21日星期六
3.1
3.2
3.3
3.4
3.5
MySQL @ 2015 Oracle 技术嘉年华
3.6
- 17. Copyright © 2015, Oracle and/or its affiliates. All rights reserved. |
在線配置-GTID功能
• 挑戰
– 多台服務器的複製拓扑,5.6上-以GTID切換 – 要重啟服務器!!。
• 在線配置
– 整個配置過程,不需要重启MySQL服务器
– 不需要改變複製拓撲結構
– 簡化了的配置過程
GTID —Non-GTID
兼容
• GTID_MODE=OFF_PERMISSIVE
启用GTID
• GTID_MODE=ON_PERMISSIVE
等待所有服務器生
成GTID
• 等等等等等
只接受GTID
• GTID_MODE=ON
X
完整配置過程:
http://dev.mysql.com/doc/refman/5.7/en/replication-mode-
change-online-enable-gtids.html
- 18. Copyright © 2015, Oracle and/or its affiliates. All rights reserved. |
存儲GTID到系統表中
• Slaves可以在不開啟binlog的條件下,使用GTID功能
– 有些Slave服務器永遠不會被當maser來使用,就不用開啟binlog.但這些服務器仍然
可以使用GTID及AUTO_POSITION來簡化管理。
• 保障意外刪除binlog
應用場景
18
在系統數據庫MySQL的,加了一張表gtid_executed
TABLE gtid_executed(
source_uuid CHAR(36) NOT NULL,
interval_start BIGINT NOT NULL,
interval_end BIGINT NOT NULL,
PRIMARY KEY(source_uuid, interval_start)
);
- 19. Copyright © 2015, Oracle and/or its affiliates. All rights reserved. | 19
mysql 事 的数量mysql> SET GLOBAL gtid_executed_compression_period= N;(N – 事务的数量)
- 20. Copyright © 2015, Oracle and/or its affiliates. All rights reserved. |
存储GTID到系统表中
• 当binlog启用时
– 存储GTID到binlog.
– 只在binlog文件切换时,将该binlog文件中包含的gtid集合写入表中
– 性能比每事务写表高
• 当binlog未启用时
– 每个事务提交时,存储GTID到表中
• 始终crash-safe.
实现机制
20
- 21. Copyright © 2015, Oracle and/or its affiliates. All rights reserved. |
MySQL 5.7 GA裡的複製新功能
GTID 新功能
多線程複製(Multi-Threaded Slave)新功能
多源複製(Multiple Source Replication)
Slave可用性的改進 / Master 性能的提升
半同步複製(Semi-synchronous Replication)的新功能
其他功能…
3
2015年11月21日星期六
3.1
3.2
3.3
3.4
3.5
MySQL @ 2015 Oracle 技术嘉年华
3.6
- 22. Copyright © 2015, Oracle and/or its affiliates. All rights reserved. |
多線程的並發複製
• 支持語句(statement format)和行(row format)格式的binlog.
• 並發策略通過系統變量控制:
– logical_clock -採用基於鎖的並發策略
– database - 基於數據庫的並發策略(在MySQL-5.6上的並發機制)
mysqlmysql> SET slave_parallel_type= [logical_clock|database]
22
- 23. Copyright © 2015, Oracle and/or its affiliates. All rights reserved. |
多線程的並發複製 (MTS Applier)
2015年11月21日星期六
Single Threaded Apply 8 Threads Applier 24 Threads Applier 48 Threads Applier 96 Threads Applier
0%
50%
100%
150%
200%
250%
Fast and Scalable Multi-threaded Replication Applier
(statement-based replication, durable settings, SSD storage)
Slaveapplytime
(SysbenchRWonthemasterwith96threads=100%)
10X
MySQL @ 2015 Oracle 技术嘉年华 23
slave_parallel_type= LOGICAL_CLOCK
- 24. Copyright © 2015, Oracle and/or its affiliates. All rights reserved. |
多線程的並發複製 (MTS Applier)
T1
T2
T3
Time
Commit 完成
事務獲取了所有的鎖
Master上的並發執行過程
Execution
Commit
T1 和T2可並發
24
• Master上的並發信息,以邏輯
時間(Logical clock)的方式記入
gtid_log_event中。
• 具有重疊COMMIT持續時間,可
並行執行的交易
在Slave上,
T1和T2並發執行;
T3在T1完成後,
開始執行.
T2和T3可並發
slave_parallel_type= LOGICAL_CLOCK
- 25. Copyright © 2015, Oracle and/or its affiliates. All rights reserved. |
MySQL 5.7 GA裡的複製新功能
GTID 新功能
多線程複製(Multi-Threaded Slave)新功能
多源複製(Multiple Source Replication)
Slave可用性的改進 / Master 性能的提升
半同步複製(Semi-synchronous Replication)的新功能
其他功能…
3
2015年11月21日星期六
3.1
3.2
3.3
3.4
3.5
MySQL @ 2015 Oracle 技术嘉年华
3.6
- 26. Copyright © 2015, Oracle and/or its affiliates. All rights reserved. |
多源複製
Slave
M M
Slave可以有多個複制源
M M
聚合數據到一台數據庫服務器上的場景:
• 集成備份;
• 複合數據分析方面的用途;
• 分片數據的合併
27
- 27. Copyright © 2015, Oracle and/or its affiliates. All rights reserved. |
多源複製
28
Insert...
B
binary log
Insert...
relay log
Master 1
A
Receiver
thread
Applier
Threads
Master info Relay info
Network
Named Channel
“Channel_1”
Insert...
relay log
Master 2
C
Master info Relay info
Network
Named Channel
“Channel_2”
Slave
Named Channel
- 28. Copyright © 2015, Oracle and/or its affiliates. All rights reserved. | 29
replication_connection_status
replication_applier_status
改進的複制監控信息
- 29. Copyright © 2015, Oracle and/or its affiliates. All rights reserved. |
MySQL 5.7 GA裡的複製新功能
GTID 新功能
多線程複製(Multi-Threaded Slave)新功能
多源複製(Multiple Source Replication)
Slave可用性的改進 / Master 性能的提升
半同步複製(Semi-synchronous Replication)的新功能
其他功能…
3
2015年11月21日星期六
3.1
3.2
3.3
3.4
3.5
MySQL @ 2015 Oracle 技术嘉年华
3.6
- 30. Copyright © 2015, Oracle and/or its affiliates. All rights reserved. |
Applier
在線配置Receiver/Applier
• 切換Master時,Applier線程仍然在運行:
• 改變applier參數時,receiver線程仍然在運行:
mysql
mysql
mysql> STOP SLAVE IO_THREAD;
mysql> CHANGE MASTER TO MASTER_HOST='master2', …;
mysql> START SLAVE IO_THREAD;
mysql> STOP SLAVE SQL_THREAD;
mysql
mysql> STOP SLAVE SQL_THREAD;
mysql> CHANGE MASTER TO MASTER_DELAY=3600, …;
mysql> START SLAVE SQL_THREAD;
31
C
B
A
B成為新
Master
Crash
切换数据源从A 到 B,无需停止applier threads.
- 31. Copyright © 2015, Oracle and/or its affiliates. All rights reserved. |
在線變更Replication Filter參數
• 動態變更Replication Filters
– 不再需要重啟MySQL server
– 支持所有的Filter選項
– 支持各種字符集
mysqlmysql> CHANGE REPLICATION FILTER REPLICATE_DO_DB= (db1, db2)
32
- 32. Copyright © 2015, Oracle and/or its affiliates. All rights reserved. |
並發讀寫binlog
• sender 線程在讀binlog的同時,用戶線程並發的記錄binlog日誌.
– Sender線程和用戶線程在讀寫binlog時,不再互相阻塞對方
– Sender線程和用戶線程都有更好的性能
Sender Thread Reads Binary Log User Thread Writes to Binary Log
Sender Thread Reads Binary Log
User Thread Writes to Binary Log
pre 5.7.2
5.7.2+
33
- 33. Copyright © 2015, Oracle and/or its affiliates. All rights reserved. |
MySQL 5.7 GA裡的複製新功能
GTID 新功能
多線程複製(Multi-Threaded Slave)新功能
多源複製(Multiple Source Replication)
Slave可用性的改進 / Master 性能的提升
半同步複製(Semi-synchronous Replication)的新功能
其他功能…
3
2015年11月21日星期六
3.1
3.2
3.3
3.4
3.5
MySQL @ 2015 Oracle 技术嘉年华
3.6
- 34. Copyright © 2015, Oracle and/or its affiliates. All rights reserved. |
無數據丟失的半同步複製
Insert...
Insert...
B
binary log
Insert...
relay log
Insert...
A
binary log
Client
Sender
thread
Receiver
thread
Applier
Threads
Receiver
Meta-data
Update
Applier
Meta-data
Update
Network
35
1
2
3
4 - ACK
5
- 35. Copyright © 2015, Oracle and/or its affiliates. All rights reserved. |
無數據丟失的半同步複製
Master
Slave
T1: INSERT INTO t1 VALUES (1000)
ACK
Time
T2: SELECT * FROM t1; T1 (1000)
execute prepare binlog
relay log
commit execute
36
mysqlmysql> SET rpl_semi_sync_master_wait_point= [AFTER_SYNC|AFTER_COMMIT]
T2 看到 t1(1000)
MySQL 5.6上: Master在commit後,才等待Slave的應答
- 36. Copyright © 2015, Oracle and/or its affiliates. All rights reserved. |
無數據丟失的半同步複製
Master
Slave
T1: INSERT INTO t1 VALUES (1000)
ACK
Time
T2: SELECT * FROM t1; empty set
execute prepare binlog
relay log
commitexecute
37
mysqlmysql> SET rpl_semi_sync_master_wait_point= [AFTER_SYNC|AFTER_COMMIT]
T2 看不到 t1(1000)
Master 在收到slave的應答後才Commit事務
因此在確認事務複製到Slave上之前,並發的事務看不到當前事務的數據.a
- 37. Copyright © 2015, Oracle and/or its affiliates. All rights reserved. |
• 單工通信方式,事務發送完畢後,要接收和處理應答。處理完答後才能
繼續發送下事務的Events。
MySQL-5.6
半同步複製更快 – ACK接收線程
Sender
Receiver
Time
Send
Trx
Receive
ACK
38
Report
ACK
Send
ACK
Receive
Trx
Send
Trx
Receive
Trx
Send
ACK
Receive
ACK
Report
ACK
- 38. Copyright © 2015, Oracle and/or its affiliates. All rights reserved. |
MySQL-5.7
• 新加的應答ACK接收線程。
• 變成雙工模式。發送和接收互不影響。
半同步複製更快 – ACK接收線程
Sender
Receiver
ACK Time
39
ACK
ACK
Receiver
Send
Trx
Send
Trx
Send
Trx
Send
ACK
Receive
Trx
Send
ACK
Receive
Trx
Receive
ACK
Report
ACK
Receive
ACK
Report
ACK
- 39. Copyright © 2015, Oracle and/or its affiliates. All rights reserved. |
半同步複製– 等待多個Slave的應答
• Master 接收到N個slave的應答後,才commit 事務.
• 用戶可以設置應答Slave的數量:
mysqlmysql> SET GLOBAL rpl_semi_sync_master_wait_for_slave_count= N
40
- 40. Copyright © 2015, Oracle and/or its affiliates. All rights reserved. |
半同步複製– 等待多個Slave的應答
• Master 接收到N個slave的應答後,才commit 事務.
• 用戶可以設置應答Slave的數量:
mysqlmysql> SET GLOBAL rpl_semi_sync_master_wait_for_slave_count= N
Master
Slave 2
T1: Binlog
ACK
Slave 1
ACK
T1: COMMIT
succeeds
mysql> SET GLOBAL rpl_semi_sync_master_wait_for_slave_count= 2
Slave 3
Time
relay log
relay log
relay log
41
ACK
- 41. Copyright © 2015, Oracle and/or its affiliates. All rights reserved. |
MySQL 5.7 GA裡的複製新功能
GTID 新功能
多線程複製(Multi-Threaded Slave)新功能
多源複製(Multiple Source Replication)
Slave可用性的改進 / Master 性能的提升
半同步複製(Semi-synchronous Replication)的新功能
其他功能…
3
2015年11月21日星期六
3.1
3.2
3.3
3.4
3.5
MySQL @ 2015 Oracle 技术嘉年华
3.6
- 42. Copyright © 2015, Oracle and/or its affiliates. All rights reserved. |
MySQL Group Replication
2015年11月21日星期六
M M M
APP
• 虛擬的同步複製
• Shared-nothing state machine replication.
• 多master同時更新
• 沒有單點故障,不需要fail-over
• 自我管理
– 自動化創建同步新 節點和移除節點。
– 自動化故障檢測和節點管理。
MySQL @ 2015 Oracle 技术嘉年华 43
Write
Write
Write
Replication Group
- 43. Copyright © 2015, Oracle and/or its affiliates. All rights reserved. |
MySQL Group Replication的實現
2015年11月21日星期六
M M M
APP
Replication Group
• 是一個MySQL plugin
• 基於row format 和InnoDB
– 用PK來做衝突檢測
• 基於GTID
• 可通過performance schema監控.
• MySQL-5.7.9 加入了XCom來替代corosync
– 更加容易編譯、安裝和使用。
– 支持更多操作系統:
Linux, Solaris, FreeBSD, OSX and Windows
MySQL @ 2015 Oracle 技术嘉年华 44
Write
Write
Write
Group Replication
- 44. Copyright © 2015, Oracle and/or its affiliates. All rights reserved. |
MySQL Router
• 基於Connection的路由和負載均衡
• 可對多台服務器作分別路由。
• 平滑的故障切換(fail-over)
• 对APP是透明的
– 不需要升級connectors.
– 沒有connector的語言限制
• 高性能
• 簡單易用
2015年11月21日星期六 MySQL @ 2015 Oracle 技术嘉年华
NEW
45
GA
Group Replication
App
MySQL
Router
connectors
M M M
Virtual MySQL Server
- 45. Copyright © 2015, Oracle and/or its affiliates. All rights reserved. |
总结
MySQL複製基礎 與 演進發展
MySQL複製應用
MySQL 5.7 GA的複制新功能
In labs - Group Replication
MySQL Router
1
2
3
4
46
- 46. Copyright © 2015, Oracle and/or its affiliates. All rights reserved. |
MySQL 5.7 GA裡的複製新功能總結
GTID 新功能
多線程複製(Multi-Threaded Slave)新功能
多源複製(Multiple Source Replication)
Slave可用性的改進 / Master 性能的提升
半同步複製(Semi-synchronous Replication)的新功能
其他功能…
3
2015年11月21日星期六
3.1
3.2
3.3
3.4
3.5
MySQL @ 2015 Oracle 技术嘉年华
3.6
- 47. Copyright © 2015, Oracle and/or its affiliates. All rights reserved. |
参考信息
• 安装包
– http://dev.mysql.com
– http://labs.mysql.com
• 手册
– http://dev.mysql.com/doc/#manual
• 開發人員的博客(新聞, 技術信息,其他有趣的內容)
– http://mysqlhighavailability.com
2015年11月21日星期六 MySQL @ 2015 Oracle 技术嘉年华 48
- 49. Copyright © 2015, Oracle and/or its affiliates. All rights reserved. |
馬楚成 - 首席技術顧問
Ivan Ma
2015-12-19
Copyright © 2015, Oracle and/or its affiliates. All rights reserved.
MySQL複製的演進和應用