利用MYSQL的日志能重做一些数据库操作:
D:\MySQL\MySQL Server 5.5\bin>mysqlbinlog "D:\MySQL\MySQL Server 5.5\data\mysql-bin.000001" > d:\test.sql
mysql-bin.000001 为日志文件, 输出到常见的SQL格式
/*!40019 SET @@session.max_insert_delayed_threads=0*/; /*!50003 SET @OLD_COMPLETION_TYPE=@@COMPLETION_TYPE,COMPLETION_TYPE=0*/; DELIMITER /*!*/; # at 4 #110808 11:33:52 server id 1 end_log_pos 107 Start: binlog v 4, server v 5.5.11-log created 110808 11:33:52 at startup # Warning: this binlog is either in use or was not closed properly. ROLLBACK/*!*/; BINLOG ' IFk/Tg8BAAAAZwAAAGsAAAABAAQANS41LjExLWxvZwAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA AAAAAAAAAAAAAAAAAAAgWT9OEzgNAAgAEgAEBAQEEgAAVAAEGggAAAAICAgCAA== '/*!*/; # at 107 #110808 11:36:40 server id 1 end_log_pos 194 Query thread_id=2 exec_time=0 error_code=0 SET TIMESTAMP=1312774600/*!*/; SET @@session.pseudo_thread_id=2/*!*/; SET @@session.foreign_key_checks=1, @@session.sql_auto_is_null=0, @@session.unique_checks=1, @@session.autocommit=1/*!*/; SET @@session.sql_mode=0/*!*/; SET @@session.auto_increment_increment=1, @@session.auto_increment_offset=1/*!*/; /*!\C gbk *//*!*/; SET @@session.character_set_client=28,@@session.collation_connection=28,@@session.collation_server=8/*!*/; SET @@session.lc_time_names=0/*!*/; SET @@session.collation_database=DEFAULT/*!*/; create database testdb /*!*/; # at 194 #110808 11:44:33 server id 1 end_log_pos 319 Query thread_id=2 exec_time=0 error_code=0 use testdb/*!*/; SET TIMESTAMP=1312775073/*!*/; create table student(name varchar(10), sex char(2), age int) /*!*/; # at 319 #110808 11:45:48 server id 1 end_log_pos 389 Query thread_id=2 exec_time=0 error_code=0 SET TIMESTAMP=1312775148/*!*/; BEGIN /*!*/; # at 389 #110808 11:45:48 server id 1 end_log_pos 513 Query thread_id=2 exec_time=0 error_code=0 SET TIMESTAMP=1312775148/*!*/; insert into student(name,sex,age) values('zhangsan','f',20) /*!*/; # at 513 #110808 11:45:48 server id 1 end_log_pos 540 Xid = 22 COMMIT/*!*/; # at 540 #110808 11:48:48 server id 1 end_log_pos 610 Query thread_id=2 exec_time=0 error_code=0 SET TIMESTAMP=1312775328/*!*/; BEGIN /*!*/; # at 610 #110808 11:48:48 server id 1 end_log_pos 730 Query thread_id=2 exec_time=0 error_code=0 SET TIMESTAMP=1312775328/*!*/; insert into student(name,sex,age) values('lisi','f',25) /*!*/; # at 730 #110808 11:48:48 server id 1 end_log_pos 757 Xid = 24 COMMIT/*!*/; # at 757 #110808 11:49:00 server id 1 end_log_pos 827 Query thread_id=2 exec_time=0 error_code=0 SET TIMESTAMP=1312775340/*!*/; BEGIN /*!*/; # at 827 #110808 11:49:00 server id 1 end_log_pos 949 Query thread_id=2 exec_time=0 error_code=0 SET TIMESTAMP=1312775340/*!*/; insert into student(name,sex,age) values('wangwu','w',18) /*!*/; # at 949 #110808 11:49:00 server id 1 end_log_pos 976 Xid = 25 COMMIT/*!*/; # at 976 #110808 11:49:12 server id 1 end_log_pos 1046 Query thread_id=2 exec_time=0 error_code=0 SET TIMESTAMP=1312775352/*!*/; BEGIN /*!*/; # at 1046 #110808 11:49:12 server id 1 end_log_pos 1169 Query thread_id=2 exec_time=0 error_code=0 SET TIMESTAMP=1312775352/*!*/; insert into student(name,sex,age) values('zhaoliu','w',22) /*!*/; # at 1169 #110808 11:49:12 server id 1 end_log_pos 1196 Xid = 26 COMMIT/*!*/; DELIMITER ; # End of log file ROLLBACK /* added by mysqlbinlog */; /*!50003 SET COMPLETION_TYPE=@OLD_COMPLETION_TYPE*/;
已做的数据库操作:
mysql> create database testdb;
mysql> create table student(name varchar(10), sex char(2), age int);
mysql> insert into student(name,sex,int) values('zhangsan','f',20);
mysql> insert into student(name,sex,age) values('lisi','f',25);
mysql> insert into student(name,sex,age) values('wangwu','w',18);
mysql> insert into student(name,sex,age) values('zhaoliu','w',22);
var i = 1312775328; alert(new Date(1312775328*1000));
恢复指定位置的日志:
D:\MySQL\MySQL Server 5.5\bin>mysqlbinlog --start-position=1046 --stop-position=1169 "D:\MySQL\MySQL Server 5.5\data\mysql-bin.000001" | mysql -u root -p
Enter password:
恢复前:
mysql> select * from student;
+----------+------+------+
| name | sex | age |
+----------+------+------+
| zhangsan | f | 20 |
| lisi | f | 25 |
| wangwu | w | 18 |
| zhaoliu | w | 22 |
+----------+------+------+
4 rows in set (0.00 sec)
恢复后:
mysql> select * from student;
+----------+------+------+
| name | sex | age |
+----------+------+------+
| zhangsan | f | 20 |
| lisi | f | 25 |
| wangwu | w | 18 |
| zhaoliu | w | 22 |
| zhaoliu | w | 22 |
+----------+------+------+
5 rows in set (0.00 sec)
可以看出,所谓的恢复,无非就是重做日志标记范围内的数据库操作而已。黑体字部分标识了对日志恢复操作的起始和终止位置:
# at 1046
#110808 11:49:12 server id 1 end_log_pos 1169 Query thread_id=2 exec_time=0 error_code=0
SET TIMESTAMP=1312775352/*!*/;
insert into student(name,sex,age) values('zhaoliu','w',22)
/*!*/;
# at 1169
参考文章:
mysql下利用binlog增量备份
一,什么是增量备份 增量备份,就是将新增加的数据进行备份。假如你一个数据库,有10G的数据,每天会增加10M的数据,数据库每天都要备份一次,这么多数据是不是都要备份呢?还是只要备份增加的数据呢,很显然,我只要备份增加的数据。这样减少服务器的负担。log-bin=/var/lib/mysql/mysql-bin.log,如果是这样的话log-bin=mysql-bin.log默认在datadir目录下面
[root@BlackGhost mysql]# ls |grep mysql-bin
mysql-bin.000001
mysql-bin.000002
mysql-bin.000003
mysql-bin.000004
mysql-bin.000005
mysql-bin.000006
mysql-bin.index
启动后会产生mysql-bin这样的文件,每启动一次,就会增加一个或者多个。
mysql-bin.000002这样文件存放的是数据库每天增加的数据,所有数据库的数据增量都在这里面。
三,查看mysql-bin.000002这样的文件里面到底是什么东西
[root@BlackGhost mysql]# mysqlbinlog /var/lib/mysql/mysql-bin.000002 > /tmp/add.sql
下面还有一个重要索引文件就是mysql-bin.index
[root@BlackGhost mysql]# cat mysql-bin.index
四,增量备份和增量还原
1,增量备份
既然我们知道了,mysql里面新增加的数据在mysql-bin这样的文件里面,我们只要把mysql-bin这样的文件进行备份就可以了。
cp /var/lib/mysql/mysql-bin* /data/mysql_newbak/
2,增量还原,讲几个常用的,比较有用的
a),根据时间来还原 –start-date,–stop-date
mysqlbinlog –start-date=”2010-09-29 18:00:00″ –stop-date=”2010-09-29 23:00:00″ \
/var/lib/mysql/mysql-bin.000002 |mysql -u root -p
b),根据起始位置来还原,–start-position,–stop-position
mysqlbinlog –start-position=370 –stop-position=440 /var/lib/mysql/mysql-bin.000002 |mysql -u root -p
mysqlbinlog –start-position=370 –stop-position=440 /var/lib/mysql/mysql-bin.000002
–start-position=370 –stop-position=440 这里面数字从哪儿来的呢?
日志文件
# at 370
#100929 21:35:25 server id 1 end_log_pos440 Query thread_id=1 exec_time=0 error_code=0
SET TIMESTAMP=1285767325/*!*/;
上面的红色加粗的就是,一个是start-position,一个是stop-position
c),根据数据库名来进行还原 -d
在这里是小写的d,请不要把它和mysqldump中的-D搞混了。哈哈。
mysqlbinlog -d test /var/lib/mysql/mysql-bin.000002
d),根据数据库所在IP来分-h
mysqlbinlog -h 192.1681.102 /var/lib/mysql/mysql-bin.000002
e),根据数据库所占用的端口来分-P
有的时候,我们的mysql用的不一定是3306端口,注意是大写的P
mysqlbinlog -p 13306 /var/lib/mysql/mysql-bin.000002
f),根据数据库serverid来还原–server-id
在数据库的配置文件中,都有一个serverid并且同一集群中serverid是不能相同的。
mysqlbinlog –server-id=1 /var/lib/mysql/mysql-bin.000002
注意:上面的几个例子,我都是一个一个说的,其实可以排列组合的。例如
mysqlbinlog –start-position=”2010-09-29 18:00:00″ -d test -h 127.0.0.1 /var/lib/mysql/mysql-bin.000002 |mysql -u root -p
五,后续
增量备份的时候,有一点让人不爽,就是mysql-bin这样的文件,每启动一次mysql就会增加一些,如果你不去管他的话,时间长了,他会把你的磁盘占满。
./mysqldump –flush-logs -u root myblog > /tmp/myblog.sql
备份myblog数据库,清除增量备份里面的有关myblog的数据
./mysqldump –flush-logs -u root –all-databases > /tmp/alldatabase.sql
备份所有数据库,清除增量备份
mysql-bin.index的起索引作用,因为增量的数据不一定在一个mysql-bin000这样的文件里面,这个时候,我们就要根据mysql-bin.index来找mysql-bin这样的增量文件了。
如果mysql里面做了这样的配置binlog-do-db=test1,增量备份里面只会有test1这个数据库的数据