MySQL data recovery

Record the MySQL data recovery process once to prevent similar operations from happening next time.

Record the MySQL data recovery process once to prevent similar operations from happening next time.

MySQL version: Ver 14.14 Distrib 5.6.20

Problem description:
      When I accidentally updated a row in the table in the morning, all the records were updated.
       The executed sql is as follows:
#update mondo_all set ip ='192.168.36.107'; where seq =3386;
   It can be seen that there is a semicolon in the middle, which causes the records of the entire table to be modified.

Before repairing the data, check whether the binlog function is enabled in my.cnf.
The specific way to check is to see if log_bin is configured in my.cnf. If there is no configuration, it is completely useless.


Tools:
binlog2sql
Installation method:
Direct online installation (I installed it in the mysql directory):
shell> git clone https://github.com/danfengcao/binlog2sql.git
After the above command is executed, a binlog2sql folder will be generated .


Enter the directory, you can see a requirements.txt file, and then execute the following command:
shell>pip install -r requirements.txt
After executing the above two commands, the installation is successful.

Data Recovery:
Before restoring the data, you must first find the log records of the misoperation. You can find the corresponding log files in the form of mysql-bin.xxxxxx in the datadir directory of mysql according to the operation time.
(My datadir=/var/lib/mysql)


Record the log according to the actual operation:


Then perform the following operations in the binlog2sql installation directory:
shell>python binlog2sql/binlog2sql.py -h132.121.130.18 -P3306 -uroot -p'hdp123' -dtest -tmondo_all --start-file='mysql-bin.000348' |cat -n |more

The above command can browse the corresponding log records page by page and find the log you want Record.

Export rollback log:
command: $ python binlog2sql/binlog2sql.py -h132.121.130.18 -P3306 -uroot -p'hdp123' -dtest -tmondo_all --start-file='mysql-bin.000348' -B > rollback The sql in the .sql

rollback.sql file should be the script you want to restore.

Then execute:
shell> mysql -h132.121.130.18 -P3306 -u root -p < rollback.sql

Note: MySQL version 5.6 cannot directly enter the password in plain text for data import operation.


Postscript:
The actual processing situation on my side is:
After I exported the update statement script executed in the morning, I wrote a simple java program to process it.
package com;

import java.awt.print.Printable;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java. sql.Connection;
import java.sql.SQLException;

public class RollBack {
Connection conn = new MySqlTools().getConnection();
public void readFile(){
String filePath = "E:\\platspace\\ExcleTools\\bin\\ doc\\rollback.sql";
File file = new File(filePath);
BufferedReader reader=null;
try {
reader = new BufferedReader(new FileReader(file));
int line = 1;
String tempString = null;
while((tempString = reader.readLine()) != null){
//System.out.println("第"+ line +"行:"+tempString);
regSplit(tempString);
line ++ ;
Thread.sleep(100);
}
reader.close();
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}finally {
if (reader != null){
try {
reader.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}

}

public void regSplit(String line){
//String s1 = "UPDATE `test`.`mondo_all` SET `has_deploy`='鏄?, `deploy_success`='Y', `sys_name`=NULL, `python_version`=NULL, `seq`=5605, `sheet_name`=NULL, `ip`='192.168.36.107', `mark`=NULL, `passwd`='mondev!@#123', `os_full`=NULL, `group_id`=NULL, `os`=NULL, `unit`=NULL WHERE `has_deploy`='鏄? AND `deploy_success`='Y' AND `sys_name` IS NULL AND `python_version` IS NULL AND `seq`=5605 AND `sheet_name` IS NULL AND `ip`='192.168.59.19' AND `mark` IS NULL AND `passwd`='mondev!@#123' AND `os_full` IS NULL AND `group_id` IS NULL AND `os` IS NULL AND `unit` IS NULL LIMIT 1; #start 120 end 1822999 time 2016-12-22 10:10:39";
String whereStr =  line.split("WHERE")[1];
String[] sArray = whereStr.split("AND");
String seq = "";
String ip="";
for (int i = 0; i < sArray.length; i++) {
String temp = sArray[i];
if(temp.contains("`seq`=")){
seq = temp.substring(7, temp.length());
}
if(temp.contains("`ip`=")){
ip = temp.substring(7, temp.trim().length());
}
}
System.out.println(seq + "------------>" + ip);
try {
String sql = "update  mondo_all set ip2='"+ip.trim()+"' where seq=" + Integer.parseInt(seq.trim()) ;
conn.prepareStatement(sql).execute();
System.out.print("        successfull.");
} catch (NumberFormatException e) {
System.out.println("更新失败.........");
e.printStackTrace();
} catch (SQLException e) {
System.out.println("Update failed...");
e.printStackTrace();
}

}

public static void main(String[] args) {
new RollBack().readFile();
}

}




Reference:
binlog2sql tool installation: http://www.qingpingshan.com/shujuku/mysql/175056.html

Data recovery reference: https://my.oschina.net/u/3119184/blog/802700
 




2016-12-22

dianxinguangchang.zhongsanerlu.yuexiuqu.guangzhoushi

Tingyuxuan


Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=326439414&siteId=291194637