#!/usr/bin# Setting up a database connection conn='Mysql-hhost-pport-uusername-ppassword'#获取最新的binlog文件logfile=$ ($conn-E"Show Master Logs"|Tail-N1|awk-F" " '{print $}') #设置a为1, the user's bottom of the judgment a=1#while循环 while true Do#获取起始datetime fromdate=$(Date "+%y-%m-%d%h:%m:%s") #获取结束datetime toDate=$(Date "+%y-%m-%d%h:%m:%s"-D'+1 Second' ) Echo "ToDate is", $toDateEcho "a is"$a #设置tempDate的值if[$a-eq1]; Thentempdate=$fromDateElsetempdate=$tmpDatefi Echo "Start time is"$tempDateEcho "Stop Time is", $toDate #解析binlog, and append to file Mysqlbinlog-r-hhost-pport-uusername-ppassword--start-datetime="$tempDate"--stop-datetime="$toDate"$logfile |grep-V"[#|/*!*/;|begin| Commit|]">>Content.txt #重新设置a的值
A=2tmpdate=$toDateEcho "Current log file is"$logfileDone
Then Tailf Content.txt, you can observe the output of binlog in real time.
Note: Because Content.txt content is appended, you can set up a scheduled task to process the Content.txt file regularly to prevent the file from growing too large