MySQL-8.0.42 主從延遲常見原因及解決方法

目錄

1 查看從庫延時方法

2 如何分析主從延遲

3 主從延遲的常見原因及解決方法


1 查看從庫延時方法


mysql> show slave status \G
*************************** 1. row ***************************
? ? ? ? ? ? ?Slave_IO_Running: Yes ? ? #兩個yes說明主從復制正常
? ? ? ? ? ? Slave_SQL_Running: Yes
? ? ? ? Seconds_Behind_Master: 0?? ? ? #表示延時時間,單位是秒,0表示沒有延時

2 如何分析主從延遲


2.1 從庫服務器的負載情況:從CPU、內存、磁盤IO和網絡的資源使用情況入手,重點關注CPU和磁盤IO。
#查看CPU和內存使用信息
root@u24-mysql-52:~# top
top - 21:12:55 up 26 min, ?1 user, ?load average: 0.00, 0.01, 0.05
Tasks: 214 total, ? 1 running, 213 sleeping, ? 0 stopped, ? 0 zombie
%Cpu(s): ?7.1 us, ?7.1 sy, ?0.0 ni, 82.1 id, ?3.6 wa, ?0.0 hi, ?0.0 si, ?0.0 st?
MiB Mem : ? 3866.7 total, ? 2817.9 free, ? ?859.0 used, ? ?416.0 buff/cache ? ??
MiB Swap: ? 3866.0 total, ? 3866.0 free, ? ? ?0.0 used. ? 3007.7 avail Mem?

? ? PID USER ? ? ?PR ?NI ? ?VIRT ? ?RES ? ?SHR S ?%CPU ?%MEM ? ? TIME+ COMMAND ? ? ? ? ? ? ? ? ? ? ? ? ? ? ?
? ?1600 root ? ? ?20 ? 0 ? 11912 ? 5660 ? 3484 R ?14.3 ? 0.1 ? 0:00.05 top ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ?
? ? ? 1 root ? ? ?20 ? 0 ? 22104 ?13076 ? 9492 S ? 0.0 ? 0.3 ? 0:09.19 systemd ? ?

參數說明
load average: 0.00, 0.01, 0.05 ??
#1 分鐘、5 分鐘、15 分鐘的平均負載

%Cpu(s): ?7.1 us, ?7.1 sy, ?0.0 ni, 82.1 id, ?3.6 wa, ?0.0 hi, ?0.0 si, ?0.0 st
#us (user):用戶空間進程的 CPU 占用百分比(如應用程序)。
#sy (system):內核空間進程的 CPU 占用百分比(如系統調用)。 ?


MiB Mem : ? 3866.7 total, ? 2817.9 free, ? ?859.0 used, ? ?416.0 buff/cache
#total:總內存/交換空間大小。
#free:完全未使用的內存。
#used:已使用的內存(包括緩存和緩沖區)。
#buff/cache:內核緩存和緩沖區占用的內存(可被快速釋放供程序使用)。
#avail Mem:估算的可用內存(包含緩存和緩沖區的可回收部分)。


#查看磁盤IO
root@u24-mysql-52:~# iostat?
Linux 6.11.0-26-generic (u24-mysql-52) ?05/30/2025 ? ? ?_x86_64_ ? ? ? ?(2 CPU)

avg-cpu: ?%user ? %nice %system %iowait ?%steal ? %idle
? ? ? ? ? ?0.74 ? ?0.05 ? ?1.30 ? ?0.06 ? ?0.00 ? 97.85

Device ? ? ? ? ? ? tps ? ?kB_read/s ? ?kB_wrtn/s ? ?kB_dscd/s ? ?kB_read ? ?kB_wrtn ? ?kB_dscd
loop0 ? ? ? ? ? ? 0.00 ? ? ? ? 0.00 ? ? ? ? 0.00 ? ? ? ? 0.00 ? ? ? ? 10 ? ? ? ? ?0 ? ? ? ? ?0
sda ? ? ? ? ? ? ? 5.97 ? ? ? 189.95 ? ? ? ?57.82 ? ? ? ? 0.00 ? ? 465358 ? ? 141664 ? ? ? ? ?0
sr0 ? ? ? ? ? ? ? 0.02 ? ? ? ? 0.85 ? ? ? ? 0.00 ? ? ? ? 0.00 ? ? ? 2092 ? ? ? ? ?0 ? ? ? ? ?0

#查看網絡
#顯示網絡軟件棧統計和狀態
root@u24-mysql-52:~# netstat -alutp
Active Internet connections (servers and established)
Proto Recv-Q Send-Q Local Address ? ? ? ? ? Foreign Address ? ? ? ? State ? ? ? PID/Program name ? ?
tcp ? ? ? ?0 ? ? ?0 _localdnsstub:domain ? ?0.0.0.0:* ? ? ? ? ? ? ? LISTEN ? ? ?646/systemd-resolve?
tcp ? ? ? ?0 ? ? ?0 _localdnsproxy:domain ? 0.0.0.0:* ? ? ? ? ? ? ? LISTEN ? ? ?646/systemd-resolve?
tcp ? ? ? ?0 ? ? ?0 u24-mysql-52:57448 ? ? ?192.168.254.51:mysql ? ?ESTABLISHED 1469/mysqld ? ? ? ??
tcp6 ? ? ? 0 ? ? ?0 [::]:33060 ? ? ? ? ? ? ?[::]:* ? ? ? ? ? ? ? ? ?LISTEN ? ? ?1469/mysqld ? ? ? ??
tcp6 ? ? ? 0 ? ? ?0 [::]:ssh ? ? ? ? ? ? ? ?[::]:* ? ? ? ? ? ? ? ? ?LISTEN ? ? ?1/init ? ? ? ? ? ? ?
tcp6 ? ? ? 0 ? ? ?0 [::]:mysql ? ? ? ? ? ? ?[::]:* ? ? ? ? ? ? ? ? ?LISTEN ? ? ?1469/mysqld ? ? ? ??
tcp6 ? ? ? 0 ? ?140 u24-mysql-52:ssh ? ? ? ?192.168.254.1:63340 ? ? ESTABLISHED 1298/sshd: root@pts?
udp ? ? ? ?0 ? ? ?0 _localdnsproxy:domain ? 0.0.0.0:* ? ? ? ? ? ? ? ? ? ? ? ? ? 646/systemd-resolve?
udp ? ? ? ?0 ? ? ?0 _localdnsstub:domain ? ?0.0.0.0:* ? ? ? ? ? ? ? ? ? ? ? ? ? 646/systemd-resolve?

2.2 主從復制狀態
#主庫狀態
mysql> show master status \G
*************************** 1. row ***************************
? ? ? ? ? ? ?File: mysql-bin.000010
? ? ? ? ?Position: 197
? ? ?Binlog_Do_DB:?
?Binlog_Ignore_DB:?
Executed_Gtid_Set: 5d7fd409-3bd6-11f0-9d66-000c29704177:1
1 row in set (0.00 sec)

#從庫狀態
mysql> show slave status \G
*************************** 1. row ***************************
? ? ? ? ? ? ? ?Slave_IO_State: Waiting for source to send event
? ? ? ? ? ? ? ? ? Master_Host: 192.168.254.51
? ? ? ? ? ? ? ? ? Master_User: repl
? ? ? ? ? ? ? ? ? Master_Port: 3306
? ? ? ? ? ? ? ? Connect_Retry: 60
? ? ? ? ? ? ? Master_Log_File: mysql-bin.000010
? ? ? ? ? Read_Master_Log_Pos: 197
? ? ? ? ? ? ? ?Relay_Log_File: u24-mysql-52-relay-bin.000005
? ? ? ? ? ? ? ? Relay_Log_Pos: 413
? ? ? ? Relay_Master_Log_File: mysql-bin.000010
? ? ? ? ? ? ?Slave_IO_Running: Yes
? ? ? ? ? ? Slave_SQL_Running: Yes
? ? ? ? ? ? ? Replicate_Do_DB:?
? ? ? ? ? Replicate_Ignore_DB:?
? ? ? ? ? ?Replicate_Do_Table:?
? ? ? ?Replicate_Ignore_Table:?
? ? ? Replicate_Wild_Do_Table:?
? Replicate_Wild_Ignore_Table:?
? ? ? ? ? ? ? ? ? ?Last_Errno: 0
? ? ? ? ? ? ? ? ? ?Last_Error:?
? ? ? ? ? ? ? ? ?Skip_Counter: 0
? ? ? ? ? Exec_Master_Log_Pos: 197
? ? ? ? ? ? ? Relay_Log_Space: 886
? ? ? ? ? ? ? Until_Condition: None
? ? ? ? ? ? ? ?Until_Log_File:?
? ? ? ? ? ? ? ? Until_Log_Pos: 0
? ? ? ? ? ?Master_SSL_Allowed: No
? ? ? ? ? ?Master_SSL_CA_File:?
? ? ? ? ? ?Master_SSL_CA_Path:?
? ? ? ? ? ? ? Master_SSL_Cert:?
? ? ? ? ? ? Master_SSL_Cipher:?
? ? ? ? ? ? ? ?Master_SSL_Key:?
? ? ? ? Seconds_Behind_Master: 0
Master_SSL_Verify_Server_Cert: No
? ? ? ? ? ? ? ? Last_IO_Errno: 0
? ? ? ? ? ? ? ? Last_IO_Error:?
? ? ? ? ? ? ? ?Last_SQL_Errno: 0
? ? ? ? ? ? ? ?Last_SQL_Error:?
? Replicate_Ignore_Server_Ids:?
? ? ? ? ? ? ?Master_Server_Id: 51
? ? ? ? ? ? ? ? ? Master_UUID: 5d7fd409-3bd6-11f0-9d66-000c29704177
? ? ? ? ? ? ?Master_Info_File: mysql.slave_master_info
? ? ? ? ? ? ? ? ? ? SQL_Delay: 0
? ? ? ? ? SQL_Remaining_Delay: NULL
? ? ? Slave_SQL_Running_State: Replica has read all relay log; waiting for more updates
? ? ? ? ? ?Master_Retry_Count: 86400
? ? ? ? ? ? ? ? ? Master_Bind:?
? ? ? Last_IO_Error_Timestamp:?
? ? ?Last_SQL_Error_Timestamp:?
? ? ? ? ? ? ? ?Master_SSL_Crl:?
? ? ? ? ? ?Master_SSL_Crlpath:?
? ? ? ? ? ?Retrieved_Gtid_Set: 5d7fd409-3bd6-11f0-9d66-000c29704177:1
? ? ? ? ? ? Executed_Gtid_Set: 5d7fd409-3bd6-11f0-9d66-000c29704177:1
? ? ? ? ? ? ? ? Auto_Position: 1
? ? ? ? ?Replicate_Rewrite_DB:?
? ? ? ? ? ? ? ? ?Channel_Name:?
? ? ? ? ? ?Master_TLS_Version:?
? ? ? ?Master_public_key_path:?
? ? ? ? Get_master_public_key: 1
? ? ? ? ? ? Network_Namespace:?
1 row in set, 1 warning (0.00 sec)


重點關注參數
Master_Log_File: mysql-bin.000010
Relay_Master_Log_File: mysql-bin.000010
Relay_Master_Log_File: mysql-bin.000010
Slave_IO_Running: Yes
Slave_SQL_Running: Yes
Seconds_Behind_Master: 0


如果主庫(File,Position)大于從庫(Master_Log_File, Read_Master_Log_Pos),說明IO線程存在延遲。
如果從庫(Relay_Master_Log_File,Exec_Master_Log_Pos) < (Master_Log_File,Read_Master_Log_Pos),說明SQL線程存在延遲。


2.3 主庫binlog的寫入量多少,如果寫入量大于從庫IO讀取量也會出現延遲。


3 主從延遲的常見原因及解決方法


3.1 IO線程存在延遲
#網絡延遲
檢查網絡問題,啟用binlog的壓縮傳輸

#磁盤IO存在瓶頸
關閉binlog

#網絡問題
在主庫使用rsync 拷貝一個文件到從庫,簡單可以判斷

3.2 SQL線程存在延遲
#主庫寫入量過大,而SQL線程單線程重放
解決方法:可以啟動并行復制,默認參數值可以,通過修改slave_parallel_workers數量來提高
slave_parallel_type=LOGICAL_CLOCK? ? ? ?
slave_parallel_workers=4? ? ? ? ? ? ? ? ? ?
slave_preserve_commit_order=ON
binlog_transaction_dependency_tracking=WRITESET_SESSION
transaction_write_set_extraction=XXHASH64
binlog_transaction_dependency_history_size=25000
binlog_format=ROW?

#大事務
指二進制日志格式為row的情況下,操作涉及的記錄數比較多,特別是update和delete操作
解決方法: 建議把大事務拆分,每一次小批量執行

#從庫上有查詢操作
消耗系統資源和鎖等待,常見從庫的查詢操作導致阻塞主庫DDL操作
mysql> show processlist;
+----+-----------------+-----------------+------+---------+------+----------------------------------------------------------+------------------+
| Id | User ? ? ? ? ? ?| Host ? ? ? ? ? ?| db ? | Command | Time | State ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ?| Info ? ? ? ? ? ? |
+----+-----------------+-----------------+------+---------+------+----------------------------------------------------------+------------------+
| ?5 | system user ? ? | connecting host | NULL | Connect | 7574 | Waiting for source to send event ? ? ? ? ? ? ? ? ? ? ? ? | NULL ? ? ? ? ? ? |
| ?6 | system user ? ? | ? ? ? ? ? ? ? ? | NULL | Query ? | 6409 | Replica has read all relay log; waiting for more updates | NULL ? ? ? ? ? ? |
| ?7 | event_scheduler | localhost ? ? ? | NULL | Daemon ?| 7574 | Waiting on empty queue ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? | NULL ? ? ? ? ? ? |
| 10 | system user ? ? | ? ? ? ? ? ? ? ? | NULL | Connect | 7574 | Waiting for an event from Coordinator ? ? ? ? ? ? ? ? ? ?| NULL ? ? ? ? ? ? |
| 11 | system user ? ? | ? ? ? ? ? ? ? ? | NULL | Connect | 7574 | Waiting for an event from Coordinator ? ? ? ? ? ? ? ? ? ?| NULL ? ? ? ? ? ? |
| 12 | system user ? ? | ? ? ? ? ? ? ? ? | NULL | Connect | 7574 | Waiting for an event from Coordinator ? ? ? ? ? ? ? ? ? ?| NULL ? ? ? ? ? ? |
| 13 | system user ? ? | ? ? ? ? ? ? ? ? | NULL | Connect | 7574 | Waiting for an event from Coordinator ? ? ? ? ? ? ? ? ? ?| NULL ? ? ? ? ? ? |
| 16 | root ? ? ? ? ? ?| localhost ? ? ? | NULL | Query ? | ? ?0 | init ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? | show processlist |
+----+-----------------+-----------------+------+---------+------+----------------------------------------------------------+------------------+
8 rows in set, 1 warning (0.00 sec)

#從庫上存在備份
備份的全局讀鎖阻塞sql線程重放。

mysql> show processlist;


#磁盤IO存在瓶頸
從庫關閉binlog日志


?

本文來自互聯網用戶投稿,該文觀點僅代表作者本人,不代表本站立場。本站僅提供信息存儲空間服務,不擁有所有權,不承擔相關法律責任。
如若轉載,請注明出處:http://www.pswp.cn/news/907530.shtml
繁體地址,請注明出處:http://hk.pswp.cn/news/907530.shtml
英文地址,請注明出處:http://en.pswp.cn/news/907530.shtml

如若內容造成侵權/違法違規/事實不符,請聯系多彩編程網進行投訴反饋email:809451989@qq.com,一經查實,立即刪除!

相關文章

Kotlin 活動事件通訊跳轉深度講解

在 Android 開發的浩瀚海洋中,活動(Activity)間的事件通訊與跳轉猶如構建復雜應用程序的橋梁與紐帶,而 Kotlin 語言的加入,更是為這一過程注入了簡潔、優雅與高效的活力。本文將深入剖析 Kotlin 開發中安卓活動事件通訊跳轉的方方面面,從基礎概念到高級技巧,從代碼示例到…

[FreeRTOS- 野火] - - - 臨界段

一、介紹 臨界段最常出現在對一些全局變量進行操作的場景。 1.1 臨界段的定義 臨界段是指在多任務系統中&#xff0c;一段需要獨占訪問共享資源的代碼。在這段代碼執行期間&#xff0c;必須確保沒有任何其他任務或中斷可以訪問或修改相同的共享資源。 臨界段的主要目的是防…

Vad-R1:通過從感知到認知的思維鏈進行視頻異常推理

文章目錄 速覽摘要1 引言2 相關工作視頻異常檢測與數據集視頻多模態大語言模型具備推理能力的多模態大語言模型 3 方法&#xff1a;Vad-R13.1 從感知到認知的思維鏈&#xff08;Perception-to-Cognition Chain-of-Thought&#xff09;3.2 數據集&#xff1a;Vad-Reasoning3.3 A…

CSS Day07

1.搭建項目目錄 2.網頁頭部SEO三大標簽 3.Favicon圖標與版心 &#xff08;1&#xff09;Favicon圖標 &#xff08;2&#xff09;版心 4.快捷導航 5.頭部-布局 6.頭部-logo 7.頭部-導航 8.頭部-搜索 9頭部-購物車 10.底部-布局 11.底部-服務區域 12.底部-幫助中心 13.底部-版權…

Flutter圖片Image、本地圖片、程程圖片、圓片剪切、圓形圖片

目錄 圖片組件的介紹 1.Image.network加載圖片 1.1 Image scale圖片縮小一倍 1.2 Image alignment使用 1.3 Image fit 屬性的取值及說明 1.3.1 Contain 默認效果 1.3.2 Fill 圖片會縮放至完全填滿目標區域&#xff08;寬高&#xff09; 1.3.3 Fill 圖片會縮放至完全填滿目…

Prometheus學習之pushgateway和altermanager組件

[rootnode-exporter41 /usr/local/alertmanager-0.28.1.linux-amd64]# pwd /usr/local/alertmanager-0.28.1.linux-amd64[rootnode-exporter41 /usr/local/alertmanager-0.28.1.linux-amd64]# cat alertmanager.yml # 通用配置 global:resolve_timeout: 5msmtp_from: 914XXXXX…

NHANES指標推薦:CQI

文章題目&#xff1a;The impact of carbohydrate quality index on menopausal symptoms and quality of life in postmenopausal women 中文標題&#xff1a;碳水化合物質量指數對絕經后婦女更年期癥狀和生活質量的影響 發表雜志&#xff1a;BMC Womens Health 影響因子&…

【cpp-httplib】 安裝與使用

cpp-httplib 1. 介紹2. 安裝3. 類與接口3.1 httplib請求3.2 httplib響應3.3 httplib服務端3.4 httplib客戶端 4. 使用4.1 服務端4.2 客戶端 1. 介紹 C HTTP 庫&#xff08;cpp-httplib&#xff09;是一個輕量級的 C HTTP 客戶端/服務器庫&#xff0c;它提供了簡單的 API 來創建…

Electron-vite【實戰】MD 編輯器 -- 系統菜單(含菜單封裝,新建文件,打開文件,打開文件夾,保存文件,退出系統)

最終效果 整體架構 src/main/index.ts import { createMenu } from ./menu在 const mainWindow 后 // 加載菜單createMenu(mainWindow)src/main/menu.ts import { BrowserWindow, Menu, MenuItem, MenuItemConstructorOptions, dialog, shell } from electron import fs from…

【第4章 圖像與視頻】4.5 操作圖像的像素

文章目錄 前言示例-獲取和修改圖像數據圖像數據的遍歷方式圖像濾鏡負片濾鏡黑白濾鏡浮雕濾鏡filter濾鏡屬性 前言 getImageData() 與 putImageData() 這兩個方法分別用來獲取圖像的像素信息&#xff0c;以及向圖像中插入像素。與此同時&#xff0c;如果有需要&#xff0c;也可…

【Docker 從入門到實戰全攻略(一):核心概念 + 命令詳解 + 部署案例】

1. 是什么 Docker 是一個用于開發、部署和運行應用程序的開源平臺&#xff0c;它使用 容器化技術 將應用及其依賴打包成獨立的容器&#xff0c;確保應用在不同環境中一致運行。 2. Docker與虛擬機 2.1 Docker&#xff08;容器化&#xff09; 容器化是一種輕量級的虛擬化技術…

Vue:axios(POST請求)

發送 POST 請求 基本用法 axios.post(/api/login, {username: lcyyyy,password: 123456 }) .then(response > {console.log(請求成功:, response.data); }) .catch(error > {console.error(請求失敗:, error); });在 Vue 組件中使用 export default {methods: {async …

一周學會Pandas2之Python數據處理與分析-數據重塑與透視-unstack() - 解堆 (行 -> 列)

鋒哥原創的Pandas2 Python數據處理與分析 視頻教程&#xff1a; 2025版 Pandas2 Python數據處理與分析 視頻教程(無廢話版) 玩命更新中~_嗶哩嗶哩_bilibili unstack() 是 pandas 中用于數據重塑的重要方法&#xff0c;它與 stack() 互為逆操作。unstack() 的主要功能是將行索…

基于大模型預測的FicatIII-IV期股骨頭壞死綜合治療研究報告

目錄 一、引言 1.1 研究背景與目的 1.2 國內外研究現狀 1.3 研究意義和創新點 二、FicatIII-IV 期股骨頭壞死概述 2.1 疾病定義與分期 2.2 病因與病理機制 2.3 臨床癥狀與診斷方法 三、大模型預測原理與方法 3.1 大模型簡介 3.2 數據收集與預處理 3.3 模型訓練與優…

C++?多態!!!

一、引言 眾所周知&#xff0c;C有三大特性&#xff0c;它們分別是封裝、繼承和多態&#xff0c;在之前的文章中已經詳細介紹過封裝和繼承了&#xff0c;今天我們將一起學習多態相關的知識&#xff0c;如果還想了解封裝、繼承相關的知識&#xff0c;可以跳轉到以下鏈接&#xf…

electron安裝報錯處理

electron安裝報錯 解決方法&#xff1a; 修改 C:\Users\用戶名.npmrc下配置文件 添加代碼 electron_mirrorhttps://cdn.npmmirror.com/binaries/electron/ electron_builder_binaries_mirrorhttps://npmmirror.com/mirrors/electron-builder-binaries/最后代碼 registryhtt…

Windows10下使用QEMU安裝Ubuntu20.04虛擬機,并啟用硬件加速

Windows10下使用QEMU安裝Ubuntu20.04虛擬機&#xff0c;并啟用硬件加速 作者將狼才鯨創建日期2025-05-30 CSDN閱讀地址&#xff1a;Windows10下使用QEMU安裝Ubuntu20.04虛擬機&#xff0c;并啟用硬件加速 本文檔源碼地址&#xff1a;Windows10下使用QEMU安裝Ubuntu20.04虛擬機…

頂刊SCS | 基于視覺語言大模型推理分割的建筑足跡尺度功能分類, 樣本數據和代碼已開源!

論文介紹 題目&#xff1a;Visual-language reasoning segmentation (LARSE) of function-level building footprint across Yangtze River Economic Belt of China 期刊&#xff1a;Sustainable cities and society&#xff08;中科院一區TOP&#xff0c;IF10.5&#xff09;…

【軟件】navicat 官方免費版

Navicat Premium Lite https://www.navicat.com.cn/download/navicat-premium-lite

每個路由器接口,都必須分配所屬網絡內的 IP 地址,用于轉發數據包

在IP網絡中&#xff0c;主機&#xff08;Host&#xff09;和路由器接口&#xff08;Router Interface&#xff09;都需要分配網絡地址&#xff08;IP地址&#xff09;。 1. 主機&#xff08;Host&#xff09;的IP地址分配 (1) 作用 主機的IP地址用于唯一標識該設備&#xff0…