使用 MYSQLBINLOG 来恢复数据
2009-04-05 12:47:05
1、配置文件里要写的东西:
[mysqld]
log-bin=mysql-bin(名字可以改成自己的,如果不改名字的话,默认是以主机名字命名)
重新启动MSYQL服务。
二进制文件里面的东西显示的就是执行所有语句的详细记录,当然一些语句不被记录在内,要了解详细的,见手册页。
2、查看自己的BINLOG的名字是什么。
show binlog events;
query result(1 records)
Log_name | Pos | Event_type | Server_id | End_log_pos | Info |
yueliangdao_binglog.000001 | 4 | Format_desc | 1 | 106 | Server ver: 5.1.22-rc-community-log, Binlog ver: 4 |
3、我做了几次操作后,她就记录了下来。
又一次 show binlog events 的结果。
query result(4 records)
Log_name | Pos | Event_type | Server_id | End_log_pos | Info |
yueliangdao_binglog.000001 | 4 | Format_desc | 1 | 106 | Server ver: 5.1.22-rc-community-log, Binlog ver: 4 |
yueliangdao_binglog.000001 | 106 | Intvar | 1 | 134 | INSERT_ID=1 |
yueliangdao_binglog.000001 | 134 | Query | 1 | 254 | use `test`; create table a1(id int not null auto_increment primary key, str varchar(1000)) engine=myisam |
yueliangdao_binglog.000001 | 254 | Query | 1 | 330 | use `test`; insert into a1(str) values ('I love you'),('You love me') |
yueliangdao_binglog.000001 | 330 | Query | 1 | 485 | use `test`; drop table a1 |
详细过程如下:
D:\LAMP\MYSQL5\data>mysqlbinlog --start-position=4 --stop-position=106 yueliangd
ao_binglog.000001 > c:\\test1.txt
test1.txt的文件内容:
/*!40019 SET @@session.max_insert_delayed_threads=0*/;
/*!50003 SET @OLD_COMPLETION_TYPE=@@COMPLETION_TYPE,COMPLETION_TYPE=0*/;
DELIMITER /*!*/;
# at 4
#7122 16:9:18 server id 1 end_log_pos 106 Start: binlog v 4, server v 5.1.22-rc-community-log created 7122 16:9:18 at startup
# Warning: this binlog was not closed properly. Most probably mysqld crashed writing it.
ROLLBACK/*!*/;
DELIMITER ;
# End of log file
ROLLBACK /* added by mysqlbinlog */;
/*!50003 SET COMPLETION_TYPE=@OLD_COMPLETION_TYPE*/;
第二行的记录:
D:\LAMP\MYSQL5\data>mysqlbinlog --start-position=106 --stop-position=134 yuelian
gdao_binglog.000001 > c:\\test1.txt
test1.txt内容如下:
/*!40019 SET @@session.max_insert_delayed_threads=0*/;
/*!50003 SET @OLD_COMPLETION_TYPE=@@COMPLETION_TYPE,COMPLETION_TYPE=0*/;
DELIMITER /*!*/;
# at 106
#7122 16:22:36 server id 1 end_log_pos 134 Intvar
SET INSERT_ID=1/*!*/;
DELIMITER ;
# End of log file
ROLLBACK /* added by mysqlbinlog */;
/*!50003 SET COMPLETION_TYPE=@OLD_COMPLETION_TYPE*/;
第三行记录:
D:\LAMP\MYSQL5\data>mysqlbinlog --start-position=134 --stop-position=254 yuelian
gdao_binglog.000001 > c:\\test1.txt
内容:
/*!40019 SET @@session.max_insert_delayed_threads=0*/;
/*!50003 SET @OLD_COMPLETION_TYPE=@@COMPLETION_TYPE,COMPLETION_TYPE=0*/;
DELIMITER /*!*/;
# at 134
#7122 16:55:31 server id 1 end_log_pos 254 Query thread_id=1 exec_time=0 error_code=0
use test/*!*/;
SET TIMESTAMP=1196585731/*!*/;
SET @@session.foreign_key_checks=1, @@session.sql_auto_is_null=1, @@session.unique_checks=1/*!*/;
SET @@session.sql_mode=1344274432/*!*/;
/*!\C utf8 *//*!*/;
SET @@session.character_set_client=33,@@session.collation_connection=33,@@session.collation_server=33/*!*/;
create table a1(id int not null auto_increment primary key,
str varchar(1000)) engine=myisam/*!*/;
DELIMITER ;
# End of log file
ROLLBACK /* added by mysqlbinlog */;
/*!50003 SET COMPLETION_TYPE=@OLD_COMPLETION_TYPE*/;
/*!40019 SET @@session.max_insert_delayed_threads=0*/;
第四行的记录:
D:\LAMP\MYSQL5\data>mysqlbinlog --start-position=254 --stop-position=330 yuelian
gdao_binglog.000001 > c:\\test1.txt
/*!50003 SET @OLD_COMPLETION_TYPE=@@COMPLETION_TYPE,COMPLETION_TYPE=0*/;
DELIMITER /*!*/;
# at 254
#7122 16:22:36 server id 1 end_log_pos 330 Query thread_id=1 exec_time=0 error_code=0
use test/*!*/;
SET TIMESTAMP=1196583756/*!*/;
SET @@session.foreign_key_checks=1, @@session.sql_auto_is_null=1, @@session.unique_checks=1/*!*/;
SET @@session.sql_mode=1344274432/*!*/;
/*!\C utf8 *//*!*/;
SET @@session.character_set_client=33,@@session.collation_connection=33,@@session.collation_server=33/*!*/;
use `test`; insert into a1(str) values ('I love you'),('You love me')/*!*/;
DELIMITER ;
# End of log file
ROLLBACK /* added by mysqlbinlog */;
/*!50003 SET COMPLETION_TYPE=@OLD_COMPLETION_TYPE*/;
5、查看这些东西是为了恢复数据,而不是为了好玩。所以我们最中还是为了要导入结果到MYSQL中。
D:\LAMP\MYSQL5\data>mysqlbinlog --start-position=134 --stop-position=330 yuelian
gdao_binglog.000001 | mysql -uroot -p
或者
D:\LAMP\MYSQL5\data>mysqlbinlog --start-position=134 --stop-position=330 yuelian
gdao_binglog.000001 >test1.txt
进入MYSQL导入
mysql> source c:\\test1.txt
Query OK, 0 rows affected (0.00 sec)
Query OK, 0 rows affected (0.00 sec)
Database changed
Query OK, 0 rows affected (0.00 sec)
Query OK, 0 rows affected (0.00 sec)
Query OK, 0 rows affected (0.00 sec)
Charset changed
Query OK, 0 rows affected (0.00 sec)
Query OK, 0 rows affected (0.03 sec)
Query OK, 0 rows affected (0.00 sec)
Query OK, 0 rows affected (0.00 sec)
6、查看数据:
mysql> show tables;
+----------------+
| Tables_in_test |
+----------------+
| a1 |
+----------------+
1 row in set (0.01 sec)
mysql> select * from a1;
+----+-------------+
| id | str |
+----+-------------+
| 1 | I love you |
| 2 | You love me |
+----+-------------+
2 rows in set (0.00 sec)
./mysqlbinlog /usr/local/mysql/data/mysql-bin.000001 > /opt/001.sql
本文出自 “聆听未来” 博客,请务必保留此出处http://kerry.blog.51cto.com/172631/146259
最新文章
- Python 历遍目录
- SQL --- Case when 的使用方法
- java.logging的重定向?
- 词法分析程序 LEX和VC6整合使用的一个简单例子
- sqlserver数据库学习(-)数据类型
- python练习程序(c100经典例3)
- java BigDecimal的操作
- ibatis的selectkey
- HDU4496_D-City(并查集删边/逆向)
- 区分javascript中的toString(),toLocaleString(),valueOf()方法
- 使用git将文件上传到Coding
- Android远程桌面助手之系统兼容篇
- 什么是IIS ?
- jQuery方法区别:click() bind() live() delegate()区别
- windows 上搭建gitblit
- Windows 10 将MySQL5.5升级为MySQL5.7
- [LeetCode] 34. Find First and Last Position of Element in Sorted Array == [LintCode] 61. Search for a Range_Easy tag: Binary Search
- 如何在Sitecore CMS中创建项目
- 【Web】网站主如何更改网页标签的图标(favicon.ico)
- vue-cli 3.0 豆瓣api接口使用element做分页
热门文章
- ROM, RAM, NVRAM and Flash Memory on Cisco Routers
- Linux终端的一些快捷键命令
- Mysql将2张字段不同的表拼接起来
- Airless Pump Bottle For The Rise Of Cosmetic Packaging Solutions
- swoole 监控文件改动
- HDU 5570:balls 期望。。。。。。。。。。。。。。。
- Spark入门:第1节 Spark概述:1 - 4
- Activity切换动画。从右边滑入,关闭时从左边滑入
- [POI 2014]PTA-Little Bird
- Graphviz 使用笔记