Mysql實(shí)現(xiàn)增量恢復(fù)的方法詳解
實(shí)驗(yàn)介紹
增量恢復(fù)一般適用的場(chǎng)景:
1、人為的sql語(yǔ)句破壞了數(shù)據(jù)庫(kù)
2、在進(jìn)行下一次完全備份之前發(fā)生系統(tǒng)故障導(dǎo)致數(shù)據(jù)庫(kù)數(shù)據(jù)丟失
3、在主從架構(gòu)中,主庫(kù)數(shù)據(jù)發(fā)生了故障
丟失完全備份之后更改的數(shù)據(jù)的恢復(fù)步驟
1、首先做一個(gè)完全備份,確保生成完全備份的sql文件。
mysql> select * from yx; #完全備份前數(shù)據(jù)庫(kù) +----------+--------+ | 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 #對(duì)數(shù)據(jù)庫(kù)完全備份
2、使用flush-logs生成新的二進(jìn)制日志文件,用以保存之后的數(shù)據(jù)庫(kù)操作語(yǔ)句。
[root@promote data]# mysqladmin -u root -p flush-logs #生成二進(jìn)制文件
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、在數(shù)據(jù)庫(kù)中插入一條記錄,再執(zhí)行flush-logs操作,生成新的二進(jìn)制增量備份文件。
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 #生成二進(jìn)制文件
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刪除剛才插入的數(shù)據(jù)。模擬完全備份后數(shù)據(jù)丟失。
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、使用二進(jìn)制文件進(jìn)行恢復(fù)操作
[root@promote data]# mysqlbinlog --no-defaults mysql-bin.000001 | mysql -u root -p
6、查看數(shù)據(jù)庫(kù)內(nèi)容,刪除的數(shù)據(jù)有了。說(shuō)明數(shù)據(jù)恢復(fù)成功。
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)
完全備份之后丟失所有數(shù)據(jù)的恢復(fù)步驟
1、使用drop刪除表yx,模擬數(shù)據(jù)完全丟失
mysql> drop table yx;
Query OK, 0 rows affected (0.01 sec)
mysql> show tables;
Empty set (0.00 sec)
2、先使用mysql命令進(jìn)行完全備份恢復(fù)操作。
[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、使用二進(jìn)制文件進(jìn)行增量備份操作。
[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)
基于時(shí)間點(diǎn)與位置的恢復(fù)
利用二進(jìn)制日志實(shí)現(xiàn)局域時(shí)間點(diǎn)與位置的恢復(fù),假如需要往數(shù)據(jù)庫(kù)中插入兩條數(shù)據(jù),但是由于誤操作,兩條插入語(yǔ)句中間刪除一條數(shù)據(jù),而這條數(shù)據(jù)不應(yīng)該刪除,這時(shí)候,需要基于時(shí)間點(diǎn)與位置進(jìn)行恢復(fù)。
–start-datetime=datetime
從二進(jìn)制日志中第1個(gè)日期時(shí)間等于或晚于datetime參量的事件開(kāi)始讀。
–stop-datetime=datetime
從二進(jìn)制日志中第1個(gè)日期時(shí)間等于或晚于datetime參量的事件起停止讀。
–start-position=N
從二進(jìn)制日志中第1個(gè)位置等于N參量時(shí)的事件開(kāi)始讀。
–stop-position=N
從二進(jìn)制日志中第1個(gè)位置等于和大于N參量時(shí)的事件起停止讀。
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、基于時(shí)間點(diǎn)的恢復(fù)。18-07-03 21:56:04是錯(cuò)誤語(yǔ)句節(jié)點(diǎn),18-07-03 21:56:11第二句正確語(yǔ)句節(jié)點(diǎn)
[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 #先進(jìn)行完全恢復(fù)
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 #結(jié)束節(jié)點(diǎn)
Enter password:
[root@promote data]# mysqlbinlog --no-defaults --start-datetime='18-07-03 21:56:11' mysql-bin.000003 | mysql -u root -p #重新開(kāi)始節(jié)點(diǎn)
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、基于位置恢復(fù),其中581是錯(cuò)誤語(yǔ)句的節(jié)點(diǎn),866是第二句正確語(yǔ)句的節(jié)點(diǎn)
[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)
相關(guān)文章
MySQL存儲(chǔ)Json字符串遇到的問(wèn)題與解決方法
要在MySQL中存儲(chǔ)數(shù)據(jù),必須定義數(shù)據(jù)庫(kù)和表結(jié)構(gòu),下面這篇文章主要給大家介紹了關(guān)于MySQL存儲(chǔ)Json字符串遇到的問(wèn)題與解決方法,文中通過(guò)實(shí)例代碼介紹的非常詳細(xì),需要的朋友可以參考下2022-07-07Mysql 5.6.37 winx64安裝雙版本mysql筆記記錄
機(jī)器上現(xiàn)在已經(jīng)存在5.0版本MySQL的情況下裝一個(gè)最新版的mysql,下文通過(guò)實(shí)例代碼給大家介紹Mysql 5.6.37 winx64安裝雙版本mysql筆記記錄,感興趣的朋友一起看看吧2017-07-07基于sqlalchemy對(duì)mysql實(shí)現(xiàn)增刪改查操作
這篇文章主要介紹了基于sqlalchemy對(duì)mysql實(shí)現(xiàn)增刪改查操作,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2020-06-06mysql?8.0.28?winx64.zip安裝配置方法圖文教程
這篇文章主要為大家詳細(xì)介紹了mysql?8.0.28?winx64.zip安裝配置方法圖文教程,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2022-04-04Windows XP系統(tǒng)安裝MySQL5.5.28圖解教程
很多朋友在winxp系統(tǒng)中開(kāi)發(fā)php等,需要安裝mysql數(shù)據(jù)庫(kù),這里簡(jiǎn)單介紹下,如何在xp下安裝mysql軟件,其實(shí)跟其它系統(tǒng)都差不多,主要是軟件對(duì)系統(tǒng)的兼容性2013-05-05MySQL學(xué)習(xí)第三天 Windows 64位操作系統(tǒng)下驗(yàn)證MySQL
MySQL學(xué)習(xí)第三天教大家如何在Windows 64位操作系統(tǒng)下驗(yàn)證MySQL,感興趣的小伙伴們可以參考一下2016-05-05