模擬場景:
專注于為中小企業(yè)提供成都網(wǎng)站制作、成都做網(wǎng)站服務(wù),電腦端+手機(jī)端+微信端的三站合一,更高效的管理,為中小企業(yè)涼州免費(fèi)做網(wǎng)站提供優(yōu)質(zhì)的服務(wù)。我們立足成都,凝聚了一批互聯(lián)網(wǎng)行業(yè)人才,有力地推動了上千余家企業(yè)的穩(wěn)健成長,幫助中小企業(yè)通過網(wǎng)站建設(shè)實現(xiàn)規(guī)模擴(kuò)充和轉(zhuǎn)變。
周日全備份 -- 周一10:00刪庫db1,-- 10:10發(fā)現(xiàn)某些頁面無法訪問,如何恢復(fù)數(shù)據(jù)?
注意:
由于庫不僅僅一個,因此db1刪除,用戶可能正常的訪問db2,但是多個數(shù)據(jù)庫之間往往是有關(guān)聯(lián)的,
如商品數(shù)據(jù)庫和訂單數(shù)據(jù)庫,因此為了避免數(shù)據(jù)的不一致性,往往會導(dǎo)致邏輯上出現(xiàn)問題的;
此時應(yīng)當(dāng)停止服務(wù);即暫停用戶的訪問,這是一個大的問題,這是需要慎重選擇的,是服務(wù)重要還是數(shù)據(jù)
更重要,最好向上級報告。
處理邏輯:
1、停止服務(wù):斷開數(shù)據(jù)庫與遠(yuǎn)程應(yīng)用之間的網(wǎng)絡(luò)連接,這樣只有本地用戶可以管理;
2、邏輯梳理:
全備份恢復(fù):周日的全備份;
周日至周1的10:00間的數(shù)據(jù);刨除刪除的SQL命令;
周一10:00 - 10:10的數(shù)據(jù);
如果業(yè)務(wù)數(shù)據(jù)不是十分的重要,那么就恢復(fù)周日的全備份,丟失全備份至周1 10:00的數(shù)據(jù)。
[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`
那么對于測試環(huán)境,'mariadb-bin.000010', MASTER_LOG_POS=245之前的二進(jìn)制日志數(shù)據(jù)都是可以刪除的;
單機(jī)備份時,--master-data=1或者2都是可以的;但是如果是主動復(fù)制那么最好是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]>
數(shù)據(jù)恢復(fù)操作:
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之前的數(shù)據(jù)使用全備份恢復(fù);
'mariadb-bin.000010', MASTER_LOG_POS=245與'mariadb-bin.000011', MASTER_LOG_POS=245
之間的數(shù)據(jù)使用二進(jìn)制日志進(jìn)行恢復(fù);
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)
如果是全備份后的二進(jìn)制日志有多個,那么需要都復(fù)制出來,并且合并;
[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
刪除二進(jìn)制日志中的刪除數(shù)據(jù)庫的SQL語句;
[root@centos7x ~]$vim binlog.sql
drop database hellodb
恢復(fù)數(shù)據(jù)之前還需要臨時將記錄二進(jìn)制日志的功能暫時關(guān)掉;
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選項,都是可以影響二進(jìn)制記錄的選項,但是其中一個關(guān)閉,那么
二進(jìn)制日志記錄功能就關(guān)閉了;其中l(wèi)og_bin在配置文件中,修改的話是需要重啟MySQL服務(wù)的;
而sql_log_bin是回話級別的,即時生效;
解鎖數(shù)據(jù)庫的讀鎖;
MariaDB [test]> unlock tables ;
Query OK, 0 rows affected (0.01 sec)
恢復(fù)全備份:
MariaDB [test]> source all_2018-02-24_21:46:13.sql
MariaDB [(none)]> select * from hellodb.students;
二進(jìn)制日志恢復(fù):
最后就是記錄二進(jìn)制日志,恢復(fù)用戶的訪問;
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)