共計 6116 個字符,預(yù)計需要花費 16 分鐘才能閱讀完成。
1、人為的 sql 語句破壞了數(shù)據(jù)庫
2、在進(jìn)行下一次完全備份之前發(fā)生系統(tǒng)故障導(dǎo)致數(shù)據(jù)庫數(shù)據(jù)丟失
3、在主從架構(gòu)中,主庫數(shù)據(jù)發(fā)生了故障
丟失完全備份之后更改的數(shù)據(jù)的恢復(fù)步驟
1、首先做一個完全備份,確保生成完全備份的 sql 文件。
mysql select * from yx; #完全備份前數(shù)據(jù)庫
+----------+--------+
| 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 #對數(shù)據(jù)庫完全備份
2、使用 flush-logs 生成新的二進(jìn)制日志文件,用以保存之后的數(shù)據(jù)庫操作語句。
[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ù)庫中插入一條記錄,再執(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ù)庫內(nèi)容,刪除的數(shù)據(jù)有了。說明數(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)
基于時間點與位置的恢復(fù)
利用二進(jìn)制日志實現(xiàn)局域時間點與位置的恢復(fù),假如需要往數(shù)據(jù)庫中插入兩條數(shù)據(jù),但是由于誤操作,兩條插入語句中間刪除一條數(shù)據(jù),而這條數(shù)據(jù)不應(yīng)該刪除,這時候,需要基于時間點與位置進(jìn)行恢復(fù)。
–start-datetime=datetime
從二進(jìn)制日志中第 1 個日期時間等于或晚于 datetime 參量的事件開始讀。
–stop-datetime=datetime
從二進(jìn)制日志中第 1 個日期時間等于或晚于 datetime 參量的事件起停止讀。
–start-position=N
從二進(jìn)制日志中第 1 個位置等于 N 參量時的事件開始讀。
–stop-position=N
從二進(jì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、基于時間點的恢復(fù)。18-07-03 21:56:04 是錯誤語句節(jié)點,18-07-03 21:56:11 第二句正確語句節(jié)點
[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é)點
Enter password:
[root@promote data]# mysqlbinlog –no-defaults –start-datetime=’18-07-03 21:56:11′ mysql-bin.000003 | mysql -u root -p #重新開始節(jié)點
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 是錯誤語句的節(jié)點,866 是第二句正確語句的節(jié)點
[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)
向 AI 問一下細(xì)節(jié)
丸趣 TV 網(wǎng) – 提供最優(yōu)質(zhì)的資源集合!