數據庫版本
Server version: 5.6.24-log Source distribution
問題描述
數據採集平臺業務數據庫因爲批量灌數據致使主從延遲上萬秒。
複製線程長期處於Queueing master event to the relay log狀態。
監控數據顯示
1.Seconds_Behind_Master 維持在6w秒左右,且有上升趨勢。
2.主庫有大量的binlog積壓沒法同步到從庫,但主從庫的網卡流量都很低遠未達到瓶頸。
3.從庫的qps與tps很低,維持在幾百左右。
4.cpu 負載不高,但iowait維持在 12%左右
5.iostat -xmd 2 發現util維持在99%
問題分析
1.從監控數據分析貌似磁盤IO瓶頸,首先嚐試用fileio,dd等工具測試,發現磁盤io確實不好。
考慮在從庫禁用雙1參數後(sync_binlog,innodb_flush_log_at_trx_commit)主從延遲時未見明顯降低.
2.從其它主機拷大量拷貝小文件未發現網絡傳輸問題
3.關閉並行複製
MySQL5.6版本中開啓了庫級別的並行複製,但show processlist發現從庫大部分並行複製同步線程都處於空閒狀態。
關閉並行複製後,主從延遲時間仍未獲得緩解
stop slave sql_thread;set global slave_parallel_workers=0;start slave sql_thread;
4.解析binlog 未發現SQL執行效率低,無主鍵等問題
5.檢查MySQL參數配置,問題浮出水面。
mysql> show variables where variable_name in('slave_parallel_workers','read_only', 'master_info_repository','relay_log_info_repository','slave_net_timeout','log_slave_updates', 'slave_compressed_protocol','sync_master_info','sync_relay_log','sync_relay_log_info','relay_log_purge');
+---------------------------+-------+
| Variable_name | Value |
+---------------------------+-------+
| log_slave_updates | ON |
| master_info_repository | FILE |
| read_only | OFF |
| relay_log_info_repository | FILE |
| relay_log_purge | OFF |
| slave_compressed_protocol | ON |
| slave_net_timeout | 10 |
| slave_parallel_workers | 6 |
| sync_master_info | 1 |
| sync_relay_log | 10000 |
| sync_relay_log_info | 10000 |
+---------------------------+-------+
檢查發現:master_info_repository設置爲FILE,同時sync_master_info設置爲1。
這兩個參數組合起來的意思就是slave要同步每個sync_master_info events 到 master.info文件中。因爲磁盤的性能問題,致使fdatasync()的效率比較低, 因此引發複製延遲。
解決辦法
把master_info_repository設置爲table後,主從延遲直線降低。
stop slave;set global relay_log_info_repository=table;set global master_info_repository=table;start slave;
官方文檔中對master_info_repository參數的說明
https://dev.mysql.com/doc/refman/5.6/en/replication-options-slave.html#sysvar_sync_master_info
master_info_repository = FILE. If the value of sync_master_info is greater than 0, the slave synchronizes its master.info file to disk (using fdatasync()) after every sync_master_info events. If it is 0, the MySQL server performs no synchronization of the master.info file to disk; instead, the server relies on the operating system to flush its contents periodically as with any other file.
master_info_repository = TABLE. If the value of sync_master_info is greater than 0, the slave updates its master info repository table after every sync_master_info events. If it is 0, the table is never updated.
---------------------
做者:lwei_998
來源:CSDN
原文:https://blog.csdn.net/lwei_998/article/details/80068498
版權聲明:本文爲博主原創文章,轉載請附上博文連接!html