案例说明:每天2:30做完全备份,早上10:00误删除了表students,10:10才发现故障,现需要将数据库还原到10:10的状态,且恢复被删除的students表。
#前期准备:
准备两台数据库yum -y install MysqL-server;systemctl enable --Now MysqLd
生产环境数据库:10.0.0.18
测试环境数据库:10.0.0.28
开启二进制日志 --> 进行全量备份 --> 模拟删除表 --> 停止数据库访问 --> 找出全量备份二进制位置 --> 全量备份二进制位置到当前位置进行备份 -->找到误删除的语句,从备份中删除此语句 --> 利用完全备份和修改过的二进制日志进行还原
10.0.0.18:
#开启二进制日志:
vim /etc/my.cnf
[MysqLd]
server-id=18
log-bin
#登录MysqL查看(MysqL 8.0.26默认开启二进制)
[root@Rocky8 ~]# MysqL
Welcome to the MysqL monitor. Commands end with ; or \g.
Your MysqL connection id is 21
Server version: 8.0.26 Source distribution
MysqL> select @@log_bin;
+-----------+
| @@log_bin |
+-----------+
| 1 |
+-----------+
1 row in set (0.00 sec)
MysqL> select @@sql_log_bin;
+---------------+
| @@sql_log_bin |
+---------------+
| 1 |
+---------------+
1 row in set (0.00 sec)
#进行全量备份
[root@Rocky8 ~]# MysqLdump -uroot -A --source-data=2 > /backup/all.sql
#模拟删除表
MysqL> use db
Database changed
MysqL> show tables;
+--------------+
| Tables_in_db |
+--------------+
| employee |
| student |
+--------------+
2 rows in set (0.01 sec)
MysqL> drop table student;
Query OK, 0 rows affected (0.01 sec)
MysqL> show tables;
+--------------+
| Tables_in_db |
+--------------+
| employee |
+--------------+
1 row in set (0.00 sec)
#发现误删除之后停服务
#查看全量备份二进制位置为2301
[root@Rocky8 ~]# head /backup/all.sql -n 30
-- MysqL dump 10.13 distrib 8.0.26, for Linux (x86_64)
--
-- Host: localhost Database:
-- ------------------------------------------------------
-- Server version 8.0.26
......
-- CHANGE MASTER TO MASTER_LOG_FILE='binlog.000001', MASTER_LOG_POS=2301;
......
#查看删除表后的二进制位置为2506
[root@Rocky8 ~]# ll /var/lib/MysqL
total 191948
-rw-r----- 1 MysqL MysqL 56 Aug 23 15:01 auto.cnf
-rw-r----- 1 MysqL MysqL 2506 Aug 23 21:40 binlog.000001
#全量备份二进制位置到当前位置进行备份
[root@Rocky8 ~]# MysqLbinlog --start-position=2301 /var/lib/MysqL/binlog.000001 > /backup/logbin.sql
#找到误删除的语句,从备份中删除此语句
[root@Rocky8 ~]# sed -n '/DROP TABLE/p' /backup/logbin.sql
DROP TABLE `student` /* generated by server */
[root@Rocky8 ~]# sed -i '/DROP TABLE/d' /backup/logbin.sql
#利用完全备份和修改过的二进制日志进行还原
[root@Rocky8 ~]# scp /backup/all.sql /backup/logbin.sql 10.0.0.28:
root@10.0.0.28's password:
all.sqll 100% 1200KB 48.8MB/s 00:00
logbin.sql 100% 2288 1.6MB/s 00:00
10.0.0.28:
#登录MysqL
[root@Rocky8 ~]# MysqL
Welcome to the MysqL monitor. Commands end with ; or \g.
Your MysqL connection id is 8
Server version: 8.0.26 Source distribution
MysqL> show databases;
+--------------------+
| Database |
+--------------------+
| information_schema |
| MysqL |
| performance_schema |
| sys |
+--------------------+
4 rows in set (0.00 sec)
#关闭二进制日志:
MysqL> set sql_log_bin=0;
Query OK, 0 rows affected (0.00 sec)
#导入全量数据
MysqL> source all.sql
Query OK, 0 rows affected (0.00 sec)
......
#导入修改的数据
MysqL> source logbin.sql
Query OK, 0 rows affected, 1 warning (0.00 sec)
#开启二进制日志
MysqL> set sql_log_bin=1;
Query OK, 0 rows affected (0.00 sec)
#导出恢复误删除的表到10.0.0.18上
[root@Rocky8 ~]# MysqLdump -uroot db student > /data/student.sql
[root@Rocky8 ~]# scp /data/student.sql 10.0.0.18:
root@10.0.0.18's password:
student.aql 100% 2010 825.9KB/s 00:00
10.0.0.18:
#关闭二进制日志
MysqL> set sql_log_bin=0;
Query OK, 0 rows affected (0.00 sec)
#进入之前删除表的数据库
MysqL> use db
Database changed
MysqL> show tables;
+--------------+
| Tables_in_db |
+--------------+
| employee |
+--------------+
1 row in set (0.00 sec)
#导入被删除的表格
MysqL> source student.sql
Query OK, 0 rows affected (0.00 sec)
#开启二进制日志
MysqL> set sql_log_bin=1;
Query OK, 0 rows affected (0.00 sec)
insert student (name,age,gender) values ( 'xiaohong' , 18 , 'f' );
原文地址:https://www.jb51.cc/wenti/3287023.html
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。