您好,登錄后才能下訂單哦!
實驗介紹
增量恢復一般適用的場景:
1、人為的sql語句破壞了數據庫
2、在進行下一次完全備份之前發生系統故障導致數據庫數據丟失
3、在主從架構中,主庫數據發生了故障
丟失完全備份之后更改的數據的恢復步驟
1、首先做一個完全備份,確保生成完全備份的sql文件。
mysql> select * from yx; #完全備份前數據庫 +----------+--------+ | name | score | +----------+--------+ | zhangsan | 100.00 | | lisi | 90.00 | | wangwu | 80.00 | | zhaoliu | 99.00 | +----------+--------+ 4 rows in set (0.00 sec) [root@promote data]# mysqldump -u root -p test > /opt/test.sql #對數據庫完全備份
2、使用flush-logs生成新的二進制日志文件,用以保存之后的數據庫操作語句。
[root@promote data]# mysqladmin -u root -p flush-logs #生成二進制文件
Enter password:
[root@promote data]# ls
auto.cnf ibdata1 ib_logfile1 mysql mysql-bin.index sys
ib_buffer_pool ib_logfile0 ibtmp1 mysql-bin.000001 performance_schema test
3、在數據庫中插入一條記錄,再執行flush-logs操作,生成新的二進制增量備份文件。
mysql> insert into yx(name,score) values('tom',87);
Query OK, 1 row affected (0.00 sec)
mysql> select * from yx;
+----------+--------+
| name | score |
+----------+--------+
| zhangsan | 100.00 |
| lisi | 90.00 |
| wangwu | 80.00 |
| zhaoliu | 99.00 |
| tom | 87.00 |
+----------+--------+
5 rows in set (0.00 sec)
[root@promote data]# mysqladmin -u root -p flush-logs #生成二進制文件
Enter password:
[root@promote data]# ls
auto.cnf ibdata1 ib_logfile1 mysql mysql-bin.000002 performance_schema test
ib_buffer_pool ib_logfile0 ibtmp1 mysql-bin.000001 mysql-bin.index sys
4、用delete刪除剛才插入的數據。模擬完全備份后數據丟失。
mysql> delete from yx where name='tom';
Query OK, 1 row affected (0.00 sec)
mysql> select * from yx;
+----------+--------+
| name | score |
+----------+--------+
| zhangsan | 100.00 |
| lisi | 90.00 |
| wangwu | 80.00 |
| zhaoliu | 99.00 |
+----------+--------+
4 rows in set (0.00 sec)
5、使用二進制文件進行恢復操作
[root@promote data]# mysqlbinlog --no-defaults mysql-bin.000001 | mysql -u root -p
6、查看數據庫內容,刪除的數據有了。說明數據恢復成功。
mysql> select * from yx;
+----------+--------+
| name | score |
+----------+--------+
| zhangsan | 100.00 |
| lisi | 90.00 |
| wangwu | 80.00 |
| zhaoliu | 99.00 |
| tom | 87.00 |
+----------+--------+
5 rows in set (0.00 sec)
完全備份之后丟失所有數據的恢復步驟
1、使用drop刪除表yx,模擬數據完全丟失
mysql> drop table yx;
Query OK, 0 rows affected (0.01 sec)
mysql> show tables;
Empty set (0.00 sec)
2、先使用mysql命令進行完全備份恢復操作。
[root@promote data]# mysql -u root -p test < /opt/test.sql
mysql> use test;
Database changed
mysql> select * from yx;
+----------+--------+
| name | score |
+----------+--------+
| zhangsan | 100.00 |
| lisi | 90.00 |
| wangwu | 80.00 |
| zhaoliu | 99.00 |
+----------+--------+
4 rows in set (0.00 sec)
3、使用二進制文件進行增量備份操作。
[root@promote data]# mysqlbinlog --no-defaults mysql-bin.000001 | mysql -u root -p
mysql> select * from yx;
+----------+--------+
| name | score |
+----------+--------+
| zhangsan | 100.00 |
| lisi | 90.00 |
| wangwu | 80.00 |
| zhaoliu | 99.00 |
| tom | 87.00 |
+----------+--------+
5 rows in set (0.00 sec)
基于時間點與位置的恢復
利用二進制日志實現局域時間點與位置的恢復,假如需要往數據庫中插入兩條數據,但是由于誤操作,兩條插入語句中間刪除一條數據,而這條數據不應該刪除,這時候,需要基于時間點與位置進行恢復。
–start-datetime=datetime
從二進制日志中第1個日期時間等于或晚于datetime參量的事件開始讀。
–stop-datetime=datetime
從二進制日志中第1個日期時間等于或晚于datetime參量的事件起停止讀。
–start-position=N
從二進制日志中第1個位置等于N參量時的事件開始讀。
–stop-position=N
從二進制日志中第1個位置等于和大于N參量時的事件起停止讀。
mysql> select * from yx;
+----------+--------+
| name | score |
+----------+--------+
| zhangsan | 100.00 |
| lisi | 90.00 |
| wangwu | 80.00 |
| zhaoliu | 99.00 |
+----------+--------+
5 rows in set (0.00 sec)
mysql> insert into yx values('test01',87);
Query OK, 1 row affected (0.00 sec)
mysql> delete from yx where name='zhangsan';
Query OK, 1 row affected (0.00 sec)
mysql> insert into yx values('test02',99);
Query OK, 1 row affected (0.17 sec)
mysql> select * from yx;
+---------+-------+
| name | score |
+---------+-------+
| lisi | 90.00 |
| wangwu | 80.00 |
| zhaoliu | 99.00 |
| test01 | 87.00 |
| test02 | 99.00 |
+---------+-------+
6 rows in set (0.00 sec)
1、基于時間點的恢復。18-07-03 21:56:04是錯誤語句節點,18-07-03 21:56:11第二句正確語句節點
[root@promote data]# mysqlbinlog --no-defaults --base64-output=decode-rows mysql-bin.000003
# at 298
#180703 21:55:35 server id 1 end_log_pos 406 CRC32 0x257c67ab Query thread_id=46 exec_time=0 error_code=0
use `test`/*!*/;
SET TIMESTAMP=1530626135/*!*/;
insert into yx values('test01',87)
/*!*/;
# at 406
#180703 21:55:35 server id 1 end_log_pos 437 CRC32 0xdd7913a3 Xid = 392
COMMIT/*!*/;
# at 437
#180703 21:56:04 server id 1 end_log_pos 502 CRC32 0x0d09bd0b Anonymous_GTID last_committed=1 sequence_number=2
SET @@SESSION.GTID_NEXT= 'ANONYMOUS'/*!*/;
# at 502
#180703 21:56:04 server id 1 end_log_pos 581 CRC32 0xe6040c79 Query thread_id=46 exec_time=0 error_code=0
SET TIMESTAMP=1530626164/*!*/;
BEGIN
/*!*/;
# at 581
#180703 21:56:04 server id 1 end_log_pos 691 CRC32 0x2d99f699 Query thread_id=46 exec_time=0 error_code=0
SET TIMESTAMP=1530626164/*!*/;
delete from yx where name='zhangsan'
/*!*/;
# at 691
#180703 21:56:04 server id 1 end_log_pos 722 CRC32 0x4a742173 Xid = 393
COMMIT/*!*/;
# at 722
#180703 21:56:11 server id 1 end_log_pos 787 CRC32 0x6d0b47d8 Anonymous_GTID last_committed=2 sequence_number=3
SET @@SESSION.GTID_NEXT= 'ANONYMOUS'/*!*/;
# at 787
#180703 21:56:11 server id 1 end_log_pos 866 CRC32 0x97e2deb7 Query thread_id=46 exec_time=0 error_code=0
SET TIMESTAMP=1530626171/*!*/;
BEGIN
/*!*/;
# at 866
#180703 21:56:11 server id 1 end_log_pos 974 CRC32 0x9e24e8af Query thread_id=46 exec_time=0 error_code=0
SET TIMESTAMP=1530626171/*!*/;
insert into yx values('test02',99)
[root@promote data]# mysql -u root -p test < /opt/test.sql #先進行完全恢復
mysql> select * from yx;
+----------+--------+
| name | score |
+----------+--------+
| zhangsan | 100.00 |
| lisi | 90.00 |
| wangwu | 80.00 |
| zhaoliu | 99.00 |
+----------+--------+
4 rows in set (0.00 sec)
[root@promote data]# mysqlbinlog --no-defaults --stop-datetime='18-07-03 21:56:04' mysql-bin.000003 | mysql -u root -p #結束節點
Enter password:
[root@promote data]# mysqlbinlog --no-defaults --start-datetime='18-07-03 21:56:11' mysql-bin.000003 | mysql -u root -p #重新開始節點
Enter password:
mysql> select * from yx;
+----------+--------+
| name | score |
+----------+--------+
| zhangsan | 100.00 |
| lisi | 90.00 |
| wangwu | 80.00 |
| zhaoliu | 99.00 |
| test01 | 87.00 |
| test02 | 99.00 |
+----------+--------+
6 rows in set (0.00 sec)
2、基于位置恢復,其中581是錯誤語句的節點,866是第二句正確語句的節點
[root@promote data]# mysql -u root -p test < /opt/test.sql
mysql> select * from yx;
+----------+--------+
| name | score |
+----------+--------+
| zhangsan | 100.00 |
| lisi | 90.00 |
| wangwu | 80.00 |
| zhaoliu | 99.00 |
+----------+--------+
4 rows in set (0.01 sec)
[root@promote data]# mysqlbinlog --no-defaults --stop-position='581' mysql-bin.000003 | mysql -u root -p
Enter password:
[root@promote data]# mysqlbinlog --no-defaults --start-position='866' mysql-bin.000003 | mysql -u root -p
Enter password:
mysql> select * from yx;
+----------+--------+
| name | score |
+----------+--------+
| zhangsan | 100.00 |
| lisi | 90.00 |
| wangwu | 80.00 |
| zhaoliu | 99.00 |
| test01 | 87.00 |
| test02 | 99.00 |
+----------+--------+
6 rows in set (0.00 sec)
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。