python導(dǎo)出mysql指定binlog文件實(shí)現(xiàn)demo
要求
要求本地有py環(huán)境和全局環(huán)境變量
先測試直接執(zhí)行binlog命令執(zhí)行命令
Windows 本地直接執(zhí)行命令
# E:\output>E:\phpstudy_pro\Extensions\MySQL5.7.26\bin\mysqlbinlog binglog文件地址 # --no-defaults 不限制編碼 # -h mysql鏈接地址 # -u mysql 鏈接名稱 # -p mysql 密碼 # -P3306 mysql 端口 # --read-from-remote-server --raw mysqlbinlog 命令的一個(gè)選項(xiàng),它指示 mysqlbinlog 從遠(yuǎn)程 MySQL 服務(wù)器讀取二進(jìn)制日志文件進(jìn)行解析。 # binlog.000003 文件地址 #命令如下 E:\output>E:\phpstudy_pro\Extensions\MySQL5.7.26\bin\mysqlbinlog --no-defaults -h 127.0.0.1 -u root -p -P3306 --read-from-remote-server --raw binlog.000003
代碼
import subprocess def download_binlogs(host, user, password, port, start_binlog, end_binlog, output_path): try: for binlog_number in range(start_binlog, end_binlog + 1): binlog_filename = f"binlog.{str(binlog_number).zfill(6)}" output_filename = f"{output_path}/binlog_{binlog_number}.sql" # 構(gòu)建命令 command = [ 'E:/phpstudy_pro/Extensions/MySQL5.7.26/bin/mysqlbinlog.exe', '--no-defaults', '-h', host, '-u', user, '-p' + password, '-P' + str(port), '--read-from-remote-server', '--raw', binlog_filename ] # 運(yùn)行命令并將輸出重定向到文件 with open(output_filename, 'w') as output_file: subprocess.run(command, stdout=output_file) output_file.flush() # 刷新緩沖區(qū) print(f"Binlog日志文件 '{binlog_filename}' 下載成功!保存為 '{output_filename}'") except Exception as e: print(f"下載失?。簕e}") if __name__ == "__main__": # 替換以下信息為實(shí)際的Polardb連接信息和binlog文件范圍 polardb_host = "127.0.0.1" polardb_user = "root" polardb_password = "root" polardb_port = 3306 start_binlog_number = 1 end_binlog_number = 3 output_directory = "E:/output" # 替換為你想要保存文件的目錄 download_binlogs(polardb_host, polardb_user, polardb_password, polardb_port, start_binlog_number, end_binlog_number, output_directory)
python執(zhí)行
python .\binlog.py
以上就是python導(dǎo)出mysql指定binlog文件實(shí)現(xiàn)demo的詳細(xì)內(nèi)容,更多關(guān)于python導(dǎo)出mysql指定binlog文件 的資料請關(guān)注腳本之家其它相關(guān)文章!
相關(guān)文章
Python函數(shù)使用的相關(guān)練習(xí)題分享
這篇文章主要介紹了Python函數(shù)使用的相關(guān)練習(xí)題分享,文章基于python函數(shù)內(nèi)容展開其相關(guān)例題,具有一定的參考價(jià)值,需要的小伙伴可以參考一下2022-05-05Python?Watchdog實(shí)現(xiàn)實(shí)時(shí)監(jiān)控文件系統(tǒng)
Python?Watchdog是一個(gè)優(yōu)秀的第三方庫,用于實(shí)現(xiàn)高效的文件系統(tǒng)監(jiān)控,本文將為大家詳細(xì)介紹一下Python如何使用Watchdog實(shí)現(xiàn)實(shí)時(shí)監(jiān)控文件,需要的可以參考下2023-11-11python中pop()函數(shù)的語法與實(shí)例
這篇文章主要給大家介紹了關(guān)于python中pop()函數(shù)語法與實(shí)例的相關(guān)資料,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2020-12-12Pandas數(shù)據(jù)集的合并與連接merge()方法
Pandas數(shù)據(jù)集的合并與連接(merge())是數(shù)據(jù)處理過程中常用的操作之一,在使用Pandas進(jìn)行數(shù)據(jù)集合并時(shí),可以使用merge()函數(shù)將兩個(gè)或多個(gè)數(shù)據(jù)集按照指定的列進(jìn)行合并,本文就來介紹一下,感興趣的可以了解一下2023-11-11Python pandas.DataFrame 找出有空值的行
這篇文章主要介紹了Python pandas.DataFrame 找出有空值的行,本文給大家介紹的非常詳細(xì),具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2019-09-09python測試框架unittest和pytest區(qū)別
這篇文章主要介紹了python測試框架unittest和pytest區(qū)別,幫助大家更好的理解和學(xué)習(xí)使用python進(jìn)行自動化測試,感興趣的朋友可以了解下2021-04-04寶塔部署django項(xiàng)目的實(shí)現(xiàn)步驟(圖文教程)
本文主要介紹了寶塔部署django項(xiàng)目的實(shí)現(xiàn)步驟,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2023-03-03