常用命令
//查看binlog是否开启
show variables like 'log_%';
//binlog 文件
show binary logs;
//master的log
show master status;
show master logs;
//查看
show binlog events in 'mysql-bin.000001'\G;
<!--more-->
//查看原生sql
set binlog_rows_query_log_events=1;
set binlog_row_image='minimal';
//命令行mysqlbinlog 查看mysql
mysqlbinlog --base64-output=DECODE-ROWS -v -v mysql-bin.000001
从MySQL binlog解析出你要的SQL
传统方式就是用mysqlbinlog 加上grep等,分析日志,或者写一些小脚本,找出要回滚的点
github上看到个点评dba开发的python工具:https://github.com/danfengcao/binlog2sql
话不多说撸一把!安装python和pip
git clone https://github.com/danfengcao/binlog2sql.git && cd binlog2sql
pip install -r requirements.txt
cd binlog2sql
//进入mysql,执行 show binary logs,找到当前log文件名;
python binlog2sql.py -h127.0.0.1 -P3306 -uroot -p123456 -dtest --start-file="mysql-bin.000001"
//找到相应行数后--flashback
python binlog2sql.py --flashback -h127.0.0.1 -P3306 -uroot -p123456 -dtest --start-file="mysql-bin.000001" --start-position=460 --stop-position=769 > /tmp/rollback.sql
//然后
mysql -h127.0.0.1 -P3306 -uroot -p123456 < /tmp/rollback.sql
本机测试用的root用户,实际应该注意下建个新用户,权限建议:select, super/replication client, replication slave
实践测试了下,确实要比手动去分析mysqlbinlog导出的文件要快和方便得多。