通过二进制日志恢复mysql数据

1. 实验环境

服务器操作系统为centos7
mariadb版本为:5.5.68

2. 开启mysql服务的二进制日志

2.1 修改mysql服务的配置文件

vim /etc/my.cnf

在mysqld部分下加一行 log-bin=mysql-bin
在这里插入图片描述

3. 开启mysql服务的慢日志

3.1 修改mysql服务的配置文件

vim /etc/my.cnf

在mysqld部分下加慢日志的以下参数

slow_query_log=1 慢查询开启状态,ON开启,OFF关闭
slow_query_log_file=/var/run/mysqld/mysqld-slow.log  慢查询日志存放的位置
long_query_time=1   查询超过多少秒才记录

在这里插入图片描述
修改完成之后保存退出

3.2 重启mysql服务

systemctl restart mariadb

4. 进行数据的创建和恢复

4.1 登录mysql数据库进行建库建表的操作

[root@host-137 ~]# mysql -uroot -p456
Welcome to the MariaDB monitor.  Commands end with ; or \g.
Your MariaDB connection id is 2
Server version: 5.5.68-MariaDB MariaDB Server

Copyright (c) 2000, 2018, Oracle, MariaDB Corporation Ab and others.

Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.

MariaDB [(none)]> create database school;  创建school数据库
Query OK, 1 row affected (0.00 sec)

MariaDB [(none)]> use school;   使用school数据库
Database changed
MariaDB [school]> create table stu(id int,name varchar(20));   创建stu表
Query OK, 0 rows affected (0.00 sec)

MariaDB [school]> insert into stu values(1,'tom');  向表中插入数据tom
Query OK, 1 row affected (0.00 sec)

MariaDB [school]> insert into stu values(2,'jack');   向表中插入数据jack
Query OK, 1 row affected (0.00 sec)

MariaDB [school]> insert into stu values(3,'frank');   向表中插入数据frank
Query OK, 1 row affected (0.00 sec)

MariaDB [school]> insert into stu values(4,'lucy');   向表中插入数据lucy
Query OK, 1 row affected (0.01 sec)

MariaDB [school]> drop database school;   删除school数据库
Query OK, 1 row affected (0.01 sec)

MariaDB [(none)]> show databases;  验证成功删除
+--------------------+
| Database           |
+--------------------+
| information_schema |
| mysql              |
| performance_schema |
| test               |
+--------------------+
7 rows in set (0.01 sec)

4.2 根据二进制日志中的position参数进行恢复

mysql二进制日志的存放位置为:/var/lib/mysql/mysql-bin.000001

4.2.1 需求:恢复从创建school数据库到向stu表中插入jack用户的数据的操作

4.2.2 先使用mysqlbinlog查看mysql的二进制日志文件

mysqlbinlog /var/lib/mysql/mysql-bin.000001

找出要恢复操作的起始位置和结束位置
在这里插入图片描述

4.2.3 根据起始位置和结束位置进行恢复

 mysqlbinlog --start-position=245  --stop-position=895   /var/lib/mysql/mysql-bin.000001 |mysql -uroot -p456

4.2.4 查看恢复效果

MariaDB [(none)]> show databases; 查看school数据库恢复成功
+--------------------+
| Database           |
+--------------------+
| information_schema |
| mysql              |
| performance_schema |
| school             |
| test               |
+--------------------+
8 rows in set (0.00 sec)

MariaDB [(none)]> use school;
Reading table information for completion of table and column names
You can turn off this feature to get a quicker startup with -A

Database changed
MariaDB [school]> show tables;  查看stu表恢复成功
+------------------+
| Tables_in_school |
+------------------+
| stu              |
+------------------+
1 row in set (0.00 sec)

MariaDB [school]> select * from stu;   查看stu表中的数据恢复成功
+------+------+
| id   | name |
+------+------+
|    1 | tom  |
|    2 | jack |
+------+------+
2 rows in set (0.00 sec)

4.3 根据二进制日志中的时间参数进行恢复

4.3.1 删除上面恢复的school数据库

MariaDB [school]> drop database school;
Query OK, 1 row affected (0.01 sec)

4.3.2 使用mysqlbinlog查看mysql的二进制日志文件

mysqlbinlog /var/lib/mysql/mysql-bin.000001

找出要恢复操作的起始时间和结束时间

4.3.3 根据起始时间和结束时间进行恢复

mysqlbinlog --start-datetime="2021-01-11 16:46:39"  --stop-datetime="2021-01-11 16:50:28"   /var/lib/mysql/mysql-bin.000001 |mysql -uroot -p456

4.3.4 查看恢复效果

MariaDB [(none)]> show databases;   查看school数据库恢复成功
+--------------------+
| Database           |
+--------------------+
| information_schema |
| mysql              |
| performance_schema |
| school             |
| test               |
+--------------------+
8 rows in set (0.00 sec)

MariaDB [(none)]> use school;
Reading table information for completion of table and column names
You can turn off this feature to get a quicker startup with -A

Database changed
MariaDB [school]> show tables;
+------------------+
| Tables_in_school |
+------------------+
| stu              |
+------------------+
1 row in set (0.00 sec)

MariaDB [school]> select * from stu;   查看stu表中的数据恢复成功
+------+-------+
| id   | name  |
+------+-------+
|    1 | tom   |
|    2 | jack  |
|    3 | frank |
|    4 | lucy  |
+------+-------+
4 rows in set (0.01 sec)
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值