利用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/Tg8BAAAAZwAAAGsAAAABAAQANS41LjExLWxvZwAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAgWT9OEzgNAAgAEgAEBAQEEgAAVAAEGggAAAAICAgCAA=='/*!*/;# at 107#110808 11:36:40 server id 1 end_log_pos 194 Querythread_id=2exec_time=0error_code=0SET 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 Querythread_id=2exec_time=0error_code=0use 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 Querythread_id=2exec_time=0error_code=0SET TIMESTAMP=1312775148/*!*/;BEGIN/*!*/;# at 389#110808 11:45:48 server id 1 end_log_pos 513 Querythread_id=2exec_time=0error_code=0SET 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 = 22COMMIT/*!*/;# at 540#110808 11:48:48 server id 1 end_log_pos 610 Querythread_id=2exec_time=0error_code=0SET TIMESTAMP=1312775328/*!*/;BEGIN/*!*/;# at 610#110808 11:48:48 server id 1 end_log_pos 730 Querythread_id=2exec_time=0error_code=0SET 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 = 24COMMIT/*!*/;# at 757#110808 11:49:00 server id 1 end_log_pos 827 Querythread_id=2exec_time=0error_code=0SET TIMESTAMP=1312775340/*!*/;BEGIN/*!*/;# at 827#110808 11:49:00 server id 1 end_log_pos 949 Querythread_id=2exec_time=0error_code=0SET 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 = 25COMMIT/*!*/;# at 976#110808 11:49:12 server id 1 end_log_pos 1046 Querythread_id=2exec_time=0error_code=0SET TIMESTAMP=1312775352/*!*/;BEGIN/*!*/;# at 1046#110808 11:49:12 server id 1 end_log_pos 1169 Querythread_id=2exec_time=0error_code=0SET 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 = 26COMMIT/*!*/;DELIMITER ;# End of log fileROLLBACK /* 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);
時間郵戳javascript轉換辦法:
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增量備份分類:MYSQL日期:2011-04-19作者:雨尚
mysql下利用binlog增量備份
一,什麼是增量備份增量備份,就是將新增加的資料進行備份。假如你一個資料庫,有10G的資料,每天會增加10M的資料,資料庫每天都要備份一次,這麼多資料是不是都要備份呢?還是只要備份增加的資料呢,很顯然,我只要備份增加的資料。這樣減少伺服器的負擔。
二,啟用binlogvi my.cnf
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這個資料庫的資料