您好,登錄后才能下訂單哦!
模擬場景:
周日全備份 -- 周一10:00刪庫db1,-- 10:10發現某些頁面無法訪問,如何恢復數據?
注意:
由于庫不僅僅一個,因此db1刪除,用戶可能正常的訪問db2,但是多個數據庫之間往往是有關聯的,
如商品數據庫和訂單數據庫,因此為了避免數據的不一致性,往往會導致邏輯上出現問題的;
此時應當停止服務;即暫停用戶的訪問,這是一個大的問題,這是需要慎重選擇的,是服務重要還是數據
更重要,最好向上級報告。
處理邏輯:
1、停止服務:斷開數據庫與遠程應用之間的網絡連接,這樣只有本地用戶可以管理;
2、邏輯梳理:
全備份恢復:周日的全備份;
周日至周1的10:00間的數據;刨除刪除的SQL命令;
周一10:00 - 10:10的數據;
如果業務數據不是十分的重要,那么就恢復周日的全備份,丟失全備份至周1 10:00的數據。
[root@centos7x ~]$mysqldump -A -F --master-data=2 --single-transaction > all_`date +%F_%T`.sql
[root@centos7x ~]$less all_2018-02-24_21\:46\:13.sql
-- Position to start replication or point-in-time recovery from
--
-- CHANGE MASTER TO MASTER_LOG_FILE='mariadb-bin.000010', MASTER_LOG_POS=245;
--
-- Current Database: `hellodb`
那么對于測試環境,'mariadb-bin.000010', MASTER_LOG_POS=245之前的二進制日志數據都是可以刪除的;
單機備份時,--master-data=1或者2都是可以的;但是如果是主動復制那么最好是1;
模擬故障:
模擬修改和刪除操作;
MariaDB [hellodb]> select * from students;
+-------+---------------+-----+--------+---------+-----------+
| StuID | Name | Age | Gender | ClassID | TeacherID |
+-------+---------------+-----+--------+---------+-----------+
| 1 | Shi Zhongyu | 22 | M | 2 | 3 |
| 2 | Shi Potian | 22 | M | 1 | 7 |
| 3 | Xie Yanke | 53 | M | 2 | 16 |
| 4 | Ding Dian | 32 | M | 4 | 4 |
| 5 | Yu Yutong | 26 | M | 3 | 1 |
| 6 | Shi Qing | 46 | M | 5 | NULL |
| 7 | Xi Ren | 19 | F | 3 | NULL |
| 8 | Lin Daiyu | 17 | F | 7 | NULL |
| 9 | Ren Yingying | 20 | F | 6 | NULL |
| 10 | Yue Lingshan | 19 | F | 3 | NULL |
| 11 | Yuan Chengzhi | 23 | M | 6 | NULL |
| 12 | Wen Qingqing | 19 | F | 1 | NULL |
| 13 | Tian Boguang | 33 | M | 2 | NULL |
| 14 | Lu Wushuang | 17 | F | 3 | NULL |
| 15 | Duan Yu | 19 | M | 4 | NULL |
| 16 | Xu Zhu | 21 | M | 1 | NULL |
| 17 | Lin Chong | 25 | M | 4 | NULL |
| 18 | Hua Rong | 23 | M | 7 | NULL |
| 19 | Xue Baochai | 18 | F | 6 | NULL |
| 20 | Diao Chan | 19 | F | 7 | NULL |
| 21 | Huang Yueying | 22 | F | 6 | NULL |
| 22 | Xiao Qiao | 20 | F | 1 | NULL |
| 23 | Ma Chao | 23 | M | 4 | NULL |
| 24 | Xu Xian | 27 | M | NULL | NULL |
| 25 | Sun Dasheng | 100 | M | NULL | NULL |
+-------+---------------+-----+--------+---------+-----------+
25 rows in set (0.00 sec)
MariaDB [hellodb]> insert into students (name,age,gender) values('a','29','F');
Query OK, 1 row affected (0.01 sec)
MariaDB [hellodb]> select * from students;
+-------+---------------+-----+--------+---------+-----------+
| StuID | Name | Age | Gender | ClassID | TeacherID |
+-------+---------------+-----+--------+---------+-----------+
| 1 | Shi Zhongyu | 22 | M | 2 | 3 |
| 2 | Shi Potian | 22 | M | 1 | 7 |
| 3 | Xie Yanke | 53 | M | 2 | 16 |
| 4 | Ding Dian | 32 | M | 4 | 4 |
| 5 | Yu Yutong | 26 | M | 3 | 1 |
| 6 | Shi Qing | 46 | M | 5 | NULL |
| 7 | Xi Ren | 19 | F | 3 | NULL |
| 8 | Lin Daiyu | 17 | F | 7 | NULL |
| 9 | Ren Yingying | 20 | F | 6 | NULL |
| 10 | Yue Lingshan | 19 | F | 3 | NULL |
| 11 | Yuan Chengzhi | 23 | M | 6 | NULL |
| 12 | Wen Qingqing | 19 | F | 1 | NULL |
| 13 | Tian Boguang | 33 | M | 2 | NULL |
| 14 | Lu Wushuang | 17 | F | 3 | NULL |
| 15 | Duan Yu | 19 | M | 4 | NULL |
| 16 | Xu Zhu | 21 | M | 1 | NULL |
| 17 | Lin Chong | 25 | M | 4 | NULL |
| 18 | Hua Rong | 23 | M | 7 | NULL |
| 19 | Xue Baochai | 18 | F | 6 | NULL |
| 20 | Diao Chan | 19 | F | 7 | NULL |
| 21 | Huang Yueying | 22 | F | 6 | NULL |
| 22 | Xiao Qiao | 20 | F | 1 | NULL |
| 23 | Ma Chao | 23 | M | 4 | NULL |
| 24 | Xu Xian | 27 | M | NULL | NULL |
| 25 | Sun Dasheng | 100 | M | NULL | NULL |
| 26 | a | 29 | F | NULL | NULL |
+-------+---------------+-----+--------+---------+-----------+
26 rows in set (0.00 sec)
MariaDB [hellodb]>
MariaDB [hellodb]> drop database hellodb ;
Query OK, 7 rows affected (0.01 sec)
MariaDB [(none)]> show databases;
+--------------------+
| Database |
+--------------------+
| information_schema |
| mysql |
| performance_schema |
| test |
+--------------------+
4 rows in set (0.00 sec)
MariaDB [(none)]> use test;
Database changed
MariaDB [test]> create table t1(id int);
Query OK, 0 rows affected (0.01 sec)
MariaDB [test]> select * from test;
ERROR 1146 (42S02): Table 'test.test' doesn''t exist
MariaDB [test]>
MariaDB [test]> show tables;
+----------------+
| Tables_in_test |
+----------------+
| t1 |
+----------------+
1 row in set (0.00 sec)
MariaDB [test]> flush tables with read lock;
Query OK, 0 rows affected (0.00 sec)
MariaDB [test]> select * from t1;
Empty set (0.00 sec)
MariaDB [test]> insert t1 values(1);
ERROR 1223 (HY000): Can''t execute the query because you have a conflicting read lock
MariaDB [test]>
數據恢復操作:
MariaDB [test]> show master logs;
+--------------------+-----------+
| Log_name | File_size |
+--------------------+-----------+
| mariadb-bin.000001 | 7700 |
| mariadb-bin.000002 | 290 |
| mariadb-bin.000003 | 290 |
| mariadb-bin.000004 | 290 |
| mariadb-bin.000005 | 290 |
| mariadb-bin.000006 | 290 |
| mariadb-bin.000007 | 290 |
| mariadb-bin.000008 | 290 |
| mariadb-bin.000009 | 290 |
| mariadb-bin.000010 | 669 |
+--------------------+-----------+
10 rows in set (0.00 sec)
刷新日志:
那么'mariadb-bin.000010', MASTER_LOG_POS=245之前的數據使用全備份恢復;
'mariadb-bin.000010', MASTER_LOG_POS=245與'mariadb-bin.000011', MASTER_LOG_POS=245
之間的數據使用二進制日志進行恢復;
MariaDB [test]> flush logs;
Query OK, 0 rows affected (0.01 sec)
MariaDB [test]> show master logs;
+--------------------+-----------+
| Log_name | File_size |
+--------------------+-----------+
| mariadb-bin.000001 | 7700 |
| mariadb-bin.000002 | 290 |
| mariadb-bin.000003 | 290 |
| mariadb-bin.000004 | 290 |
| mariadb-bin.000005 | 290 |
| mariadb-bin.000006 | 290 |
| mariadb-bin.000007 | 290 |
| mariadb-bin.000008 | 290 |
| mariadb-bin.000009 | 290 |
| mariadb-bin.000010 | 714 |
| mariadb-bin.000011 | 245 |
+--------------------+-----------+
11 rows in set (0.01 sec)
如果是全備份后的二進制日志有多個,那么需要都復制出來,并且合并;
[root@centos7x ~]$cp /var/lib/mysql/mariadb-bin.000010 .
[root@centos7x ~]$ls
all_2018-02-24_21:46:13.sql all.sql anaconda-ks.cfg hellodb_InnoDB.sql initial-setup-ks.cfg mariadb-bin.000010
[root@centos7x ~]$mysqlbinlog --start-position=245 mariadb-bin.000010
[root@centos7x ~]$mysqlbinlog --start-position=245 mariadb-bin.000010 > binlog.sql
刪除二進制日志中的刪除數據庫的SQL語句;
[root@centos7x ~]$vim binlog.sql
drop database hellodb
恢復數據之前還需要臨時將記錄二進制日志的功能暫時關掉;
MariaDB [test]> show variables like 'sql_log_bin';
+---------------+-------+
| Variable_name | Value |
+---------------+-------+
| sql_log_bin | ON |
+---------------+-------+
1 row in set (0.00 sec)
MariaDB [test]> set sql_log_bin=0;
Query OK, 0 rows affected (0.00 sec)
MariaDB [test]> show variables like 'sql_log_bin';
+---------------+-------+
| Variable_name | Value |
+---------------+-------+
| sql_log_bin | OFF |
+---------------+-------+
1 row in set (0.00 sec)
sql_log_bin和/etc/my.cnf中的log_bin選項,都是可以影響二進制記錄的選項,但是其中一個關閉,那么
二進制日志記錄功能就關閉了;其中log_bin在配置文件中,修改的話是需要重啟MySQL服務的;
而sql_log_bin是回話級別的,即時生效;
解鎖數據庫的讀鎖;
MariaDB [test]> unlock tables ;
Query OK, 0 rows affected (0.01 sec)
恢復全備份:
MariaDB [test]> source all_2018-02-24_21:46:13.sql
MariaDB [(none)]> select * from hellodb.students;
二進制日志恢復:
最后就是記錄二進制日志,恢復用戶的訪問;
MariaDB [(none)]> source binlog.sql
MariaDB [(none)]> select * from hellodb.students;
| 25 | Sun Dasheng | 100 | M | NULL | NULL |
| 26 | a | 29 | F | NULL | NULL |
+-------+---------------+-----+--------+---------+-----------+
MariaDB [test]> set sql_log_bin=1;
Query OK, 0 rows affected (0.00 sec)
MariaDB [test]> show variables like 'sql_log_bin';
+---------------+-------+
| Variable_name | Value |
+---------------+-------+
| sql_log_bin | ON |
+---------------+-------+
1 row in set (0.00 sec)
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。