当前位置: 代码网 > it编程>数据库>Mysql > MySQL通过binlog实现恢复数据

MySQL通过binlog实现恢复数据

2025年02月13日 Mysql 我要评论
一、背景在mysql中,如果不小心删除了数据,可以利用二进制日志(binlog)来恢复数据。实质就是将binlog记录中的事件再次执行一遍。二、前提条件启用二进制日志:确保 mysql 启用了二进制日

一、背景

在mysql中,如果不小心删除了数据,可以利用二进制日志(binlog)来恢复数据。

实质就是将binlog记录中的事件再次执行一遍。

二、前提条件

启用二进制日志:确保 mysql 启用了二进制日志功能。

有足够的权限:确保有权限访问和读取二进制日志文件。

三、恢复步骤

1.找到相关的二进制日志文件:

查看是否开启二进制日志文件

show variables like 'log_bin%';

查看二进制日志文件位置

show variables like 'log_bin_basename';

查看二进制日志文件列表

show binary logs;

2.使用 mysqlbinlog 工具提取日志:事件位置

先使用show binlog events命令查看binlog记录,确定事件开始位置和结束位置。

查看二进制日志记录

show binlog events in 'binlog.00001';

再使用 mysqlbinlog 提取开始位置和结束位置的日志:

mysqlbinlog /path/to/binlog.000001 --start-position=13508 --stop-position=14142 | mysql -u username -p database_name

替换 /path/to/binlog.000001 为二进制日志文件路径

修改stop-position和stop-position

替换 username 为mysql 用户名,database_name 为数据库名称。

3.使用 mysqlbinlog 工具提取日志:时间段

使用 mysqlbinlog 提取特定时间段的日志:

mysqlbinlog /path/to/binlog.000001 --start-datetime="yyyy-mm-dd hh:mm:ss" --stop-datetime="yyyy-mm-dd hh:mm:ss" | mysql -u username -p database_name

替换 /path/to/binlog.000001 为二进制日志文件路径

修改 start-datetime 和 stop-datetime

替换 username 为mysql 用户名,database_name 为数据库名称。

注意事项

备份当前数据:在进行数据恢复操作之前,最好先备份当前数据库,以防止进一步的数据丢失。

测试恢复脚本:在生产环境中执行恢复脚本之前,可以先在测试环境中进行测试,确保恢复操作的正确性。

mysqlbinlog命令只用于恢复,不能用于回滚。适用数据迁移,数据同步的场景。

四、实操

1.插入数据

mysql -u root -p 登陆

➜  ~ mysql -u root -p                  
enter password: 
welcome to the mysql monitor.  commands end with ; or \\g.
your mysql connection id is 40
server version: 8.4.3 mysql community server - gpl

插入两条数据

mysql> use ban;

mysql> insert into t_user (id, name, phone) values ('1', '小明', '110');
query ok, 1 row affected (0.00 sec)

mysql> insert into t_user (id, name, phone) values ('2', '小红', '120');
query ok, 1 row affected (0.00 sec)

mysql> select * from t_user;
+----+--------+-------+---------------------+---------------------+
| id | name   | phone | create_dt           | update_dt           |
+----+--------+-------+---------------------+---------------------+
| 1  | 小明   | 110   | 2025-01-24 15:19:39 | 2025-01-24 15:19:39 |
| 2  | 小红   | 120   | 2025-01-24 15:19:46 | 2025-01-24 15:19:46 |
+----+--------+-------+---------------------+---------------------+
2 rows in set (0.00 sec)

2.删除数据

mysql> delete from t_user where id in(1,2);
query ok, 2 rows affected (0.00 sec)

mysql> select * from t_user;
empty set (0.00 sec)

3.通过binlog恢复删除的数据

找到二进制日志文件:/usr/local/mysql/data/binlog.000002

mysql> show variables like 'log_bin_basename';
+------------------+------------------------------+
| variable_name    | value                        |
+------------------+------------------------------+
| log_bin_basename | /usr/local/mysql/data/binlog |
+------------------+------------------------------+
1 row in set (0.01 sec)

​​​​​​​mysql> show binary logs;
+---------------+-----------+-----------+
| log_name      | file_size | encrypted |
+---------------+-----------+-----------+
| binlog.000001 |       668 | no        |
| binlog.000002 |     14142 | no        |
| binlog.000003 |       181 | no        |
| binlog.000004 |       181 | no        |
| binlog.000005 |       181 | no        |
+---------------+-----------+-----------+
5 rows in set (0.01 sec)

查看二进制日志记录,确认事件开始位置(13508)和结束位置(14142)

mysql> show binlog events in 'binlog.00002';

| log_name      | pos   | event_type     | server_id | end_log_pos | info  
| binlog.00002 | 13508 | anonymous_gtid |         1 |       13587 | set @@session.gtid_next= 'anonymous'
| binlog.00002 | 13587 | query          |         1 |       13669 | begin                               
| binlog.00002 | 13669 | table_map      |         1 |       13734 | table_id: 109 (ban.t_user)          
| binlog.00002 | 13734 | write_rows     |         1 |       13794 | table_id: 109 flags: stmt_end_f     
| binlog.00002 | 13794 | xid            |         1 |       13825 | commit /* xid=1058 */               
| binlog.00002 | 13825 | anonymous_gtid |         1 |       13904 | set @@session.gtid_next= 'anonymous'
| binlog.00002 | 13904 | query          |         1 |       13986 | begin                               
| binlog.00002 | 13986 | table_map      |         1 |       14051 | table_id: 109 (ban.t_user)          
| binlog.00002 | 14051 | write_rows     |         1 |       14111 | table_id: 109 flags: stmt_end_f     
| binlog.00002 | 14111 | xid            |         1 |       14142 | commit /* xid=1059 */               

使用 mysqlbinlog 恢复数据

➜  ~ mysqlbinlog /usr/local/mysql/data/binlog.00002 --start-position=13508 --stop-position=14142 | mysql -u root -p ban 

4.完整示例

--登陆--
➜  ~ mysql -u root -p                  
enter password: 

--插入数据----
mysql> use ban;
mysql> insert into t_user (id, name, phone) values ('1', '小明', '110');
mysql> insert into t_user (id, name, phone) values ('2', '小红', '120');
mysql> select * from t_user;
+----+--------+-------+---------------------+---------------------+
| id | name   | phone | create_dt           | update_dt           |
+----+--------+-------+---------------------+---------------------+
| 1  | 小明   | 110   | 2025-01-24 15:19:39 | 2025-01-24 15:19:39 |
| 2  | 小红   | 120   | 2025-01-24 15:19:46 | 2025-01-24 15:19:46 |
+----+--------+-------+---------------------+---------------------+
2 rows in set (0.00 sec)

--删除数据--
mysql> delete from t_user where id in(1,2);
mysql> select * from t_user;
empty set (0.00 sec)

--通过binlog恢复删除的数据--

--找到二进制日志文件:/usr/local/mysql/data/binlog.000002
mysql> show variables like 'log_bin_basename';
+------------------+------------------------------+
| variable_name    | value                        |
+------------------+------------------------------+
| log_bin_basename | /usr/local/mysql/data/binlog |
+------------------+------------------------------+
1 row in set (0.01 sec)

mysql> show binary logs;
+---------------+-----------+-----------+
| log_name      | file_size | encrypted |
+---------------+-----------+-----------+
| binlog.000001 |       668 | no        |
| binlog.000002 |     14142 | no        |
| binlog.000003 |       181 | no        |
| binlog.000004 |       181 | no        |
| binlog.000005 |       181 | no        |
+---------------+-----------+-----------+
5 rows in set (0.01 sec)

--查看二进制日志记录,确认事件开始位置(13508)和结束位置(14142)--
mysql> show binlog events in 'binlog.00002';

| log_name      | pos   | event_type     | server_id | end_log_pos | info  
| binlog.00002 | 13508 | anonymous_gtid |         1 |       13587 | set @@session.gtid_next= 'anonymous'
| binlog.00002 | 13587 | query          |         1 |       13669 | begin                               
| binlog.00002 | 13669 | table_map      |         1 |       13734 | table_id: 109 (ban.t_user)          
| binlog.00002 | 13734 | write_rows     |         1 |       13794 | table_id: 109 flags: stmt_end_f     
| binlog.00002 | 13794 | xid            |         1 |       13825 | commit /* xid=1058 */               
| binlog.00002 | 13825 | anonymous_gtid |         1 |       13904 | set @@session.gtid_next= 'anonymous'
| binlog.00002 | 13904 | query          |         1 |       13986 | begin                               
| binlog.00002 | 13986 | table_map      |         1 |       14051 | table_id: 109 (ban.t_user)          
| binlog.00002 | 14051 | write_rows     |         1 |       14111 | table_id: 109 flags: stmt_end_f     
| binlog.00002 | 14111 | xid            |         1 |       14142 | commit /* xid=1059 */            

--使用 mysqlbinlog 恢复数据--
➜  ~ mysqlbinlog /usr/local/mysql/data/binlog.00002 --start-position=13508 --stop-position=14142 | mysql -u root -p ban 

到此这篇关于mysql通过binlog实现恢复数据的文章就介绍到这了,更多相关mysql binlog恢复数据内容请搜索代码网以前的文章或继续浏览下面的相关文章希望大家以后多多支持代码网!

(0)

相关文章:

版权声明:本文内容由互联网用户贡献,该文观点仅代表作者本人。本站仅提供信息存储服务,不拥有所有权,不承担相关法律责任。 如发现本站有涉嫌抄袭侵权/违法违规的内容, 请发送邮件至 2386932994@qq.com 举报,一经查实将立刻删除。

发表评论

验证码:
Copyright © 2017-2025  代码网 保留所有权利. 粤ICP备2024248653号
站长QQ:2386932994 | 联系邮箱:2386932994@qq.com