關於MySQL-binlog 追查操作數據庫IP的過程
思路
找到binlog中大致時間段的日志段,可視化模式解析日志后查詢關鍵字,拿到thread-id,去processlist查詢該線程的連接IP(當然,需要該IP連接保持存活狀態)
一、查詢MySQL binlog目錄
1.首先,看看binlog是否開啟。
show variables like 'log_bin';
如果結果是“ON”,則是開啟狀態,然后繼續下面操作。
2.查詢binlog存儲位置,一般來說,my.cnf(默認在/etc/my.cnf)或my.ini文件中配置了binlog的目錄地址。如果沒有,可以在/var/lib/mysql 里找找,如果是寶塔配置的數據庫,默認的binlog目錄在/www/server/data/。
3.找到binlog后,用以下命令解析二進制文件為可讀文件到某一目錄下的sql文件。
root@root:/www/server/mysql/bin/mysqlbinlog --no-defaults --database=xxx --base64-output=decode-rows -v --start-datetime='2022-01-24 13:10:00' --stop-datetime='2022-01-24 23:50:00' /www/server/data/mysql-bin.000020>/www/binlog20220124131000-20220124230001.sql
4.找到關鍵字對應的地方,拿到thread-id:1311175。
#220124 13:50:09 server id 1 end_log_pos 208617021 CRC32 0x73007804 Query thread_id=1311175 exec_time=0 error_code=0
SET TIMESTAMP=1643003409/*!*/;
BEGIN
/*!*/;
# at 208617021
#220124 13:50:09 server id 1 end_log_pos 208617128 CRC32 0xf43e7a1d Table_map: `xxx`.`pkt_campaign` mapped to number 30329
# at 208617128
#220124 13:50:09 server id 1 end_log_pos 208617475 CRC32 0x09bb8981 Update_rows: table id 30329 flags: STMT_END_F
### UPDATE `jtgj`.`pkt_campaign`
### WHERE
### @1=3
### @2='這是一個好活動'
### @3=198056653885441
### @4='elt'
### @5=''
### @6='hasEnded'
### @7=''
### @8=1642642655
### @9=1644456707
### @10='97'
### @11='86,22'
### @12='{"allUsers":1,"memberGrade":["vip"]}'
### @13='totalBudget'
### @14=999
### @15='eachCouponOnce'
### @16=0
### @17=0
### @18=1642995537
### @19=946656000
### @20=946656000
5.拿到線程ID后,去processlist查詢該線程對應的信息。
select * from information_schema.processlist where Id=1311175