mysqldump进行数据库备份和恢复

案例说明:每天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)


创建数据库
手动创建数据库
MysqL> create database student character set utf8 collate utf8_bin;
创建表
CREATE TABLE student (id int UNSIGNED AUTO_INCREMENT PRIMARY KEY ,name VARCHAR ( 20 ) NOT NULL ,age tinyint UNSIGNED ,gender ENUM ( 'M' , 'F' ) default 'M' ) ENGINE = InnoDB AUTO_INCREMENT = 10 DEFAULT CHARSET =utf8;
insert student (name,age) values ( 'xiaoming' , 20 );
insert student (name,age,gender) values ( 'xiaohong' , 18 , 'f' );
select * from student;

相关文章

显卡天梯图2024最新版,显卡是电脑进行图形处理的重要设备,...
初始化电脑时出现问题怎么办,可以使用win系统的安装介质,连...
todesk远程开机怎么设置,两台电脑要在同一局域网内,然后需...
油猴谷歌插件怎么安装,可以通过谷歌应用商店进行安装,需要...
虚拟内存这个名词想必很多人都听说过,我们在使用电脑的时候...