查看當前的類型:
show variables like 'binlog_format'node
/etc/my.cnf 裏面的binlog_format參數mysql
全備+恢復部分binlog日誌sql
mysqldump -uroot -pfjselinfgsiengiseg test3 > /data1/test3.sql.
SQL1:update student set age=21 where id=4;
SQL2:update student set age=22 where id=5;數據庫
這時執行了兩個sql,對應的是兩個事務。
若是咱們想把數據庫狀態恢復到執行SQL1以後服務器
要刷新日誌後,咱們才能查看日誌,找到須要恢復的binlog日誌
執行:code
flush logs;
找到binlog日誌:/var/lib/mysql/mysql-bin.000031orm
mysql執行:server
show binlog events in "mysql-bin.000031"
找到剛纔的兩次事務事件
|
mysql-bin.000031 | 6622 | Query | 1 | 6691 | BEGIN | | mysql-bin.000031 | 6691 | Table_map | 1 | 6745 | table_id: 308 (test3.student) | | mysql-bin.000031 | 6745 | Update_rows | 1 | 6801 | table_id: 308 flags: STMT_END_F | | mysql-bin.000031 | 6801 | Xid | 1 | 6828 | COMMIT /* xid=6771600 */ | | mysql-bin.000031 | 6828 | Query | 1 | 6897 | BEGIN | | mysql-bin.000031 | 6897 | Table_map | 1 | 6951 | table_id: 308 (test3.student) | | mysql-bin.000031 | 6951 | Update_rows | 1 | 7007 | table_id: 308 flags: STMT_END_F | | mysql-bin.000031 | 7007 | Xid | 1 | 7034 | COMMIT /* xid=6771607 */ | | mysql-bin.000031 | 7034 | Rotate | 1 | 7077 | mysql-bin.000032;pos=4 | +------------------+-------+--------------+-------------+---------------+-----------------------------------------------------------------------------+
BEGIN是開始事務
COMMIT是提交事務
因此咱們須要恢復的是6622 到 6828
把舊的test3數據庫更名,或者刪除。建議先備份
drop database test3;
建立數據庫test3
create database test3;
導入全備份
source /data1/test3.sql;
恢復部分binlog日誌
mysqlbinlog --start-position=6622 --stop-position=6828 --database=test3 /var/lib/mysql/mysql-bin.000031 |mysql -uroot -pfjselinfgsiengiseg -D test3
mysqlbinlog 命令會把對應的binlog日誌轉換爲sql語句,導出來,而後經過mysql命令來執行sql語句
恢復前的數據:
mysql root@localhost:test3> select * from student; +------+--------+-------+---------+ | id | name | age | class | |------+--------+-------+---------| | 4 | 2 | 20 | B | | 5 | 2 | 21 | B | +------+--------+-------+---------+
恢復後的數據
mysql root@localhost:test3> select * from student; +------+--------+-------+---------+ | id | name | age | class | |------+--------+-------+---------| | 4 | 2 | 21 | B | | 5 | 2 | 21 | B | +------+--------+-------+---------+
能夠看到,SQL1執行了,SQL2沒有執行。
未經容許,請不要轉載