mysql数据库误操作 binlog方式恢复数据
1、修改mysql配置文件mysql.ini,在[mysqld]节点下添加
log-bin=mysql-bin
2、重启mysql 服务器
3、查看二进制日志是否已经启动
show variables like 'log_%';
如启动会在data目录下创建logbin.index和logbin.000001两个文件,
logbin.000001 为数据备份文件
4、导出logbin.000001文件
mysqlbinlog e:/log/logbin.000001 > e:/log/log.sql
">": 导入到文件中; ">>": 追加到文件中
4.1 按指定位置导出:
c:\mysql\bin\>mysqlbinlog --start-position=185 --stop-position=338 e:/log/logbin.000001 > e:/log/log3.sql
4.2 按指定时间导出:
c:\mysql\bin\>mysqlbinlog --start-datetime="2010-01-07 11:25:56" --stop-datetime="2010-01-07 13:23:50" e:/log/logbin.000001 > e:/log/log_by_date22.sql
5、查看 log.sql 文件
/*!50530 SET @@SESSION.PSEUDO_SLAVE_MODE=1*/;
/*!40019 SET @@session.max_insert_delayed_threads=0*/;
/*!50003 SET @OLD_COMPLETION_TYPE=@@COMPLETION_TYPE,COMPLETION_TYPE=0*/;
DELIMITER /*!*/;
# at 4
#160331 16:30:41 server id 1 end_log_pos 120 CRC32 0x8a87a0b9 Start: binlog v 4, server v 5.6.17-log created 160331 16:30:41 at startup
# Warning: this binlog is either in use or was not closed properly.
ROLLBACK/*!*/;
BINLOG '
MeD8Vg8BAAAAdAAAAHgAAAABAAQANS42LjE3LWxvZwAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA
AAAAAAAAAAAAAAAAAAAx4PxWEzgNAAgAEgAEBAQEEgAAXAAEGggAAAAICAgCAAAACgoKGRkAAbmg
h4o=
'/*!*/;
# at 120
#160331 16:31:11 server id 1 end_log_pos 199 CRC32 0x536cee58 Query thread_id=1 exec_time=0 error_code=0
SET TIMESTAMP=1459413071/*!*/;
SET @@session.pseudo_thread_id=1/*!*/;
SET @@session.foreign_key_checks=1, @@session.sql_auto_is_null=0, @@session.unique_checks=1, @@session.autocommit=1/*!*/;
SET @@session.sql_mode=1073741824/*!*/;
SET @@session.auto_increment_increment=1, @@session.auto_increment_offset=1/*!*/;
/*!\C utf8 *//*!*/;
SET @@session.character_set_client=33,@@session.collation_connection=33,@@session.collation_server=8/*!*/;
SET @@session.lc_time_names=0/*!*/;
SET @@session.collation_database=DEFAULT/*!*/;
BEGIN
/*!*/;
# at 199
#160331 16:31:11 server id 1 end_log_pos 332 CRC32 0xd1cd3906 Query thread_id=1 exec_time=0 error_code=0
use `dddf`/*!*/;
SET TIMESTAMP=1459413071/*!*/;
INSERT INTO `sn_test` (`name`, `age`) VALUES ('lynn1', '1')
/*!*/;
# at 332
#160331 16:31:11 server id 1 end_log_pos 412 CRC32 0xe8f2719e Query thread_id=1 exec_time=0 error_code=0
SET TIMESTAMP=1459413071/*!*/;
COMMIT
/*!*/;
# at 412
#160331 16:31:11 server id 1 end_log_pos 491 CRC32 0x846673fc Query thread_id=1 exec_time=0 error_code=0
SET TIMESTAMP=1459413071/*!*/;
BEGIN
/*!*/;
# at 491
#160331 16:31:11 server id 1 end_log_pos 624 CRC32 0xb814ad1f Query thread_id=1 exec_time=0 error_code=0
SET TIMESTAMP=1459413071/*!*/;
INSERT INTO `sn_test` (`name`, `age`) VALUES ('lynn2', '2')
/*!*/;
# at 624
#160331 16:31:11 server id 1 end_log_pos 704 CRC32 0xbefce046 Query thread_id=1 exec_time=0 error_code=0
SET TIMESTAMP=1459413071/*!*/;
COMMIT
/*!*/;
# at 704
#160331 16:31:11 server id 1 end_log_pos 783 CRC32 0x32fa9d1e Query thread_id=1 exec_time=0 error_code=0
SET TIMESTAMP=1459413071/*!*/;
BEGIN
/*!*/;
# at 783
#160331 16:31:11 server id 1 end_log_pos 916 CRC32 0xa92ca157 Query thread_id=1 exec_time=0 error_code=0
SET TIMESTAMP=1459413071/*!*/;
INSERT INTO `sn_test` (`name`, `age`) VALUES ('lynn3', '3')
/*!*/;
# at 916
#160331 16:31:11 server id 1 end_log_pos 996 CRC32 0x8798d109 Query thread_id=1 exec_time=0 error_code=0
SET TIMESTAMP=1459413071/*!*/;
COMMIT
/*!*/;
# at 996
#160331 16:31:11 server id 1 end_log_pos 1075 CRC32 0x884f8eb6 Query thread_id=1 exec_time=0 error_code=0
SET TIMESTAMP=1459413071/*!*/;
BEGIN
/*!*/;
# at 1075
#160331 16:31:11 server id 1 end_log_pos 1208 CRC32 0x8d1e42ad Query thread_id=1 exec_time=0 error_code=0
SET TIMESTAMP=1459413071/*!*/;
INSERT INTO `sn_test` (`name`, `age`) VALUES ('lynn4', '4')
/*!*/;
# at 1208
#160331 16:31:11 server id 1 end_log_pos 1288 CRC32 0x94de1993 Query thread_id=1 exec_time=0 error_code=0
SET TIMESTAMP=1459413071/*!*/;
COMMIT
/*!*/;
# at 1288
#160331 16:35:26 server id 1 end_log_pos 1367 CRC32 0x70cbb46c Query thread_id=1 exec_time=0 error_code=0
SET TIMESTAMP=1459413326/*!*/;
BEGIN
/*!*/;
# at 1367
#160331 16:35:26 server id 1 end_log_pos 1460 CRC32 0xa0c3745d Query thread_id=1 exec_time=0 error_code=0
SET TIMESTAMP=1459413326/*!*/;
delete from sn_test
/*!*/;
# at 1460
#160331 16:35:26 server id 1 end_log_pos 1540 CRC32 0x962bb8f3 Query thread_id=1 exec_time=0 error_code=0
SET TIMESTAMP=1459413326/*!*/;
COMMIT
/*!*/;
# at 1540
#160331 16:47:41 server id 1 end_log_pos 1636 CRC32 0x96f2aca5 Query thread_id=1 exec_time=0 error_code=0
SET TIMESTAMP=1459414061/*!*/;
create database `root`
/*!*/;
# at 1636
#160331 16:31:11 server id 1 end_log_pos 1715 CRC32 0xffdb167b Query thread_id=1 exec_time=998 error_code=0
SET TIMESTAMP=1459413071/*!*/;
BEGIN
/*!*/;
# at 1715
#160331 16:31:11 server id 1 end_log_pos 1848 CRC32 0x84ccfc57 Query thread_id=1 exec_time=998 error_code=0
use `dddf`/*!*/;
SET TIMESTAMP=1459413071/*!*/;
INSERT INTO `sn_test` (`name`, `age`) VALUES ('lynn1', '1')
/*!*/;
# at 1848
#160331 16:31:11 server id 1 end_log_pos 1928 CRC32 0x717c3306 Query thread_id=1 exec_time=998 error_code=0
SET TIMESTAMP=1459413071/*!*/;
COMMIT
/*!*/;
# at 1928
#160331 16:31:11 server id 1 end_log_pos 2007 CRC32 0x1c4e0a67 Query thread_id=1 exec_time=998 error_code=0
SET TIMESTAMP=1459413071/*!*/;
BEGIN
/*!*/;
# at 2007
#160331 16:31:11 server id 1 end_log_pos 2140 CRC32 0x3c4b4c00 Query thread_id=1 exec_time=998 error_code=0
SET TIMESTAMP=1459413071/*!*/;
INSERT INTO `sn_test` (`name`, `age`) VALUES ('lynn2', '2')
/*!*/;
# at 2140
#160331 16:31:11 server id 1 end_log_pos 2220 CRC32 0xfa2a5fac Query thread_id=1 exec_time=998 error_code=0
SET TIMESTAMP=1459413071/*!*/;
COMMIT
/*!*/;
# at 2220
#160331 16:31:11 server id 1 end_log_pos 2299 CRC32 0x44125a7a Query thread_id=1 exec_time=998 error_code=0
SET TIMESTAMP=1459413071/*!*/;
BEGIN
/*!*/;
# at 2299
#160331 16:31:11 server id 1 end_log_pos 2432 CRC32 0x0b3ac488 Query thread_id=1 exec_time=998 error_code=0
SET TIMESTAMP=1459413071/*!*/;
INSERT INTO `sn_test` (`name`, `age`) VALUES ('lynn3', '3')
/*!*/;
# at 2432
#160331 16:31:11 server id 1 end_log_pos 2512 CRC32 0x0da174db Query thread_id=1 exec_time=998 error_code=0
SET TIMESTAMP=1459413071/*!*/;
COMMIT
/*!*/;
# at 2512
#160331 16:31:11 server id 1 end_log_pos 2591 CRC32 0xf28eb498 Query thread_id=1 exec_time=998 error_code=0
SET TIMESTAMP=1459413071/*!*/;
BEGIN
/*!*/;
# at 2591
#160331 16:31:11 server id 1 end_log_pos 2724 CRC32 0xf4e5af77 Query thread_id=1 exec_time=998 error_code=0
SET TIMESTAMP=1459413071/*!*/;
INSERT INTO `sn_test` (`name`, `age`) VALUES ('lynn4', '4')
/*!*/;
# at 2724
#160331 16:31:11 server id 1 end_log_pos 2804 CRC32 0x83838335 Query thread_id=1 exec_time=998 error_code=0
SET TIMESTAMP=1459413071/*!*/;
COMMIT
/*!*/;
# at 2804
#160331 16:48:50 server id 1 end_log_pos 2891 CRC32 0xeb71885f Query thread_id=1 exec_time=1 error_code=0
SET TIMESTAMP=1459414130/*!*/;
drop database `root`
/*!*/;
# at 2891
#160331 16:49:31 server id 1 end_log_pos 2979 CRC32 0x56e9ceab Query thread_id=1 exec_time=0 error_code=0
SET TIMESTAMP=1459414171/*!*/;
create database ff
/*!*/;
# at 2979
#160331 16:50:41 server id 1 end_log_pos 3111 CRC32 0x4c876cd4 Query thread_id=1 exec_time=0 error_code=0
use `ff`/*!*/;
SET TIMESTAMP=1459414241/*!*/;
create table sn_test(
`name` varchar(20),
age varchar(5)
)
/*!*/;
# at 3111
#160331 16:50:51 server id 1 end_log_pos 3186 CRC32 0x2f3a0098 Query thread_id=1 exec_time=0 error_code=0
SET TIMESTAMP=1459414251/*!*/;
BEGIN
/*!*/;
# at 3186
#160331 16:50:51 server id 1 end_log_pos 3315 CRC32 0x4333806f Query thread_id=1 exec_time=0 error_code=0
SET TIMESTAMP=1459414251/*!*/;
INSERT INTO `sn_test` (`name`, `age`) VALUES ('lynn1', '1')
/*!*/;
# at 3315
#160331 16:50:51 server id 1 end_log_pos 3346 CRC32 0x569623be Xid = 94
COMMIT/*!*/;
# at 3346
#160331 16:50:51 server id 1 end_log_pos 3421 CRC32 0xcdb25573 Query thread_id=1 exec_time=0 error_code=0
SET TIMESTAMP=1459414251/*!*/;
BEGIN
/*!*/;
# at 3421
#160331 16:50:51 server id 1 end_log_pos 3550 CRC32 0x725f86a4 Query thread_id=1 exec_time=0 error_code=0
SET TIMESTAMP=1459414251/*!*/;
INSERT INTO `sn_test` (`name`, `age`) VALUES ('lynn2', '2')
/*!*/;
# at 3550
#160331 16:50:51 server id 1 end_log_pos 3581 CRC32 0xc306f71d Xid = 95
COMMIT/*!*/;
# at 3581
#160331 16:50:51 server id 1 end_log_pos 3656 CRC32 0x29b1f8f7 Query thread_id=1 exec_time=0 error_code=0
SET TIMESTAMP=1459414251/*!*/;
BEGIN
/*!*/;
# at 3656
#160331 16:50:51 server id 1 end_log_pos 3785 CRC32 0xe30c0471 Query thread_id=1 exec_time=0 error_code=0
SET TIMESTAMP=1459414251/*!*/;
INSERT INTO `sn_test` (`name`, `age`) VALUES ('lynn3', '3')
/*!*/;
# at 3785
#160331 16:50:51 server id 1 end_log_pos 3816 CRC32 0xc0308803 Xid = 96
COMMIT/*!*/;
# at 3816
#160331 16:50:51 server id 1 end_log_pos 3891 CRC32 0x4a28cd3e Query thread_id=1 exec_time=0 error_code=0
SET TIMESTAMP=1459414251/*!*/;
BEGIN
/*!*/;
# at 3891
#160331 16:50:51 server id 1 end_log_pos 4020 CRC32 0x30559ce5 Query thread_id=1 exec_time=0 error_code=0
SET TIMESTAMP=1459414251/*!*/;
INSERT INTO `sn_test` (`name`, `age`) VALUES ('lynn4', '4')
/*!*/;
# at 4020
#160331 16:50:51 server id 1 end_log_pos 4051 CRC32 0x35e22e41 Xid = 97
COMMIT/*!*/;
# at 4051
#160331 16:52:11 server id 1 end_log_pos 4126 CRC32 0x21fde254 Query thread_id=1 exec_time=0 error_code=0
SET TIMESTAMP=1459414331/*!*/;
BEGIN
/*!*/;
# at 4126
#160331 16:52:11 server id 1 end_log_pos 4215 CRC32 0xfad9d638 Query thread_id=1 exec_time=0 error_code=0
SET TIMESTAMP=1459414331/*!*/;
delete from sn_test
/*!*/;
# at 4215
#160331 16:52:11 server id 1 end_log_pos 4246 CRC32 0xc7a21ff3 Xid = 122
COMMIT/*!*/;
DELIMITER ;
# End of log file
ROLLBACK /* added by mysqlbinlog */;
/*!50003 SET COMPLETION_TYPE=@OLD_COMPLETION_TYPE*/;
/*!50530 SET @@SESSION.PSEUDO_SLAVE_MODE=0*/;
6、恢复
c:\mysql\bin\>mysqlbinlog e:/log/logbin.000001 | mysql -u root -p
6、1指定位置恢复
c:\mysql\bin\>mysqlbinlog --start-position=185 --stop-position=338 e:/log/logbin.000001 | mysql -u root -p
6、2指定时间恢复
c:\mysql\bin\>mysqlbinlog --start-datetime="2015-03-07 11:25:56" --stop-datetime="2015-03-27 13:23:50" e:/log/logbin.000001 | mysql -u root -p