Mysql data rollback recovery solves MySQL8 and encoding problems through binlog2sql
1. Causes
Entering the delete command in the MySQL command box is a wrong condition, which caused several pieces of data associated with other tables to be deleted by mistake. Since the association is a randomly generated id, in order to ensure data consistency and relevance, the database binlog is rolled back data
- The sql file generated by restoring through the mysqlbinlog command cannot be executed, and there is always an encoding problem, which is relatively troublesome and time-consuming to solve.
- Use threaded binlog2sql for data flashback to generate sql execution
2.1 Install Python
2.2 Install binlog2sql
git clone https://github.com/danfengcao/binlog2sql.git //Download binlog2sql
pip install -r requirements.txt
2.3 in the binbog2sql.py directory Execute the operation of generating rollback SQL
C:\software\binlog2sql\binlog2sql>python binlog2sql.py -hlocalhost -P3306 -uroot -d jeecg-boot -t tr_user --start-datetime “2021-07-23 16:00:00” -B --start-file=CY016593-bin.000005 > a.sqlFor how to use this tool, please refer to the github operation document: https://github.com/danfengcao/binlog2sql
problems encountered
- Traceback (most recent call last):
File “binlog2sql.py”, line 149, in
back_interval=args.back_interval, only_dml=args.only_dml, sql_type=args.sql_type)
File “binlog2sql.py”, line 46, in init
self.connection = pymysql.connect(**self.conn_setting)
Problem 2 UnicodeDecodeError: 'utf-8' codec can't decode byte xxx after solving problem 1
Two, the solution
Question 1 The version of mysql8, install the corresponding binlog2sql tool version
pip uninstall PyMySQL
pip install PyMySQL==0.9.3
Question 2 Encoding problem
Replace decode("utf-8") in binlog2sql_util.py with decode("utf-8", "ignore"), if it doesn't work,
replace it with decode("gbk", "ignore")
I use gbk will do