mysql,mariadb删库、恢复的完整模拟实现

模拟场景:

专注于为中小企业提供成都网站制作、成都做网站服务,电脑端+手机端+微信端的三站合一,更高效的管理,为中小企业凉州免费做网站提供优质的服务。我们立足成都,凝聚了一批互联网行业人才,有力地推动了上千余家企业的稳健成长,帮助中小企业通过网站建设实现规模扩充和转变。

周日全备份 -- 周一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)


分享题目:mysql,mariadb删库、恢复的完整模拟实现
标题网址:http://bzwzjz.com/article/pdosce.html

其他资讯

Copyright © 2007-2020 广东宝晨空调科技有限公司 All Rights Reserved 粤ICP备2022107769号
友情链接: 成都网站建设推广 网站设计 网站建设 app网站建设 自适应网站建设 手机网站建设 成都网站制作 重庆网站建设 成都做网站建设公司 重庆网站建设 成都网站制作 网站建设 高端网站设计推广 盐亭网站设计 泸州网站建设 成都网站建设 成都网站建设公司 企业网站建设公司 成都网站设计 重庆手机网站建设 成都企业网站建设 重庆网站制作