简介
Binlog日志,即二进制日志文件,用于记录用户对数据库操作的SQL语句信息,当发生数据误删除的适合我们可以通过Binlog日志来还原已经删除的数据,还原数据的方法分为传统二进制文件还原数据和基于GTID的二进制文件还原数据
实现过程
1.修改配置文件
[root@localhost ~]# vi /etc/my.cnf
server-id = 1
log-bin=binlog
#重启数据库服务
[root@localhost ~]# systemctl restart mysqld
2.操作数据库
mysql> create database mydb charest utf8mb4 ;
mysql> use mydb ;
mysql> create table test(id int)engine=innodb charest=utf8mb4 ;
mysql> insert into test values(1) ;
mysql> insert into test values(2) ;
mysql> insert into test values(3) ;
mysql> insert into test values(4) ;
mysql> commit;
mysql> update test set id=10 where id=4;
mysql> commit;
mysql> select * from test;
+------+
| id |
+------+
| 1 |
| 2 |
| 3 |
| 10 |
+------+
4 rows in set (0.00 sec)
mysql> drop database mydb ;
3.查看二进制日志信息
mysql> show master status\G;
*************************** 1. row ***************************
File: binlog.000001
Position: 1960
Binlog_Do_Db:
Binlog_Ignore_Db:
Executed_Gtid_Set:
1 rows in set (0.00 sec)
#查找创库和删库的点,为219和1868
mysql> show binlog events in 'binlog.000001';
+---------------+-----+-------------+------------+-------------+--------------------------------------+
| Log_name | Pos | Event_type | Server_id | End_log_pos | Info |
+---------------+-----+-------------+------------+-------------+--------------------------------------+
| binlog.000001 | 219 | Query | 1| 329| create database mydb charest utf8mb4 |
| binlog.000001 |1868 | Query | 1| 1960|drop database mydb |
+---------------+-----+-------------+------------+-------------+--------------------------------------+
4.另存为二进制日志信息
[root@localhost ~]# mysqlbinlog
5.恢复数据
#临时关闭二进制日志记录以免重复记录
mysql> set sql_log_bin = 0 ;
#恢复数据
mysql> source /tmp/binlog.sql
#重启二进制日志记录
mysql> set sql_log_bin = 1 ;
6.查看数据恢复情况
mysql> show databases ;
+---------------------+
| Database |
+---------------------+
| information_schema |
| mydb |
| mysql |
| performance_schema |
| sys |
+---------------------+
5 rows in set (0.00 sec)
mysql> use mydb ;
Database changed
mysql> select * from test;
+------+
| id |
+------+
| 1 |
| 2 |
| 3 |
| 10 |
+------+
4 rows in set (0.00 sec)
版权声明:本文为noflag原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。