相思资源网 Design By www.200059.com
简介
Binlog日志,即二进制日志文件,用于记录用户对数据库操作的SQL语句信息,当发生数据误删除的时候我们可以通过binlog日志来还原已经删除的数据,还原数据的方法分为传统二进制文件还原数据和基于GTID的二进制文件还原数据
前期准备
准备一台Centos7虚拟机,关闭防火墙和selinux,配置IP地址,同步系统时间,安装MySQL数据库
传统二进制日志还原数据
修改配置文件
[root@localhost ~]# vi /etc/my.cnf server-id=1 log-bin=binlog #重启数据库服务 [root@localhost ~]# systemctl restart mysqld
操作数据库
mysql> create database mydb charset utf8mb4; mysql> use mydb; mysql> create table test(id int)engine=innodb charset=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;
查看二进制日志信息
mysql> show master status\G; *************************** 1. row *************************** File: binlog.000001 Position: 1960 Binlog_Do_DB: Binlog_Ignore_DB: Executed_Gtid_Set: 1 row 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 charset utf8mb4 | | binlog.000001 | 1868 | Query | 1 | 1960 | drop database mydb | +---------------+------+----------------+-----------+-------------+--------------------------------------------------------------------+
另存为二进制日志信息
[root@localhost ~]# mysqlbinlog --start-position=219 --stop-position=1868 /var/lib/mysql/binlog.000001 > /tmp/binlog.sql
恢复数据
#临时关闭二进制日志记录以免重复记录 mysql> set sql_log_bin=0; #恢复数据 mysql> source /tmp/binlog.sql #重启二进制日志记录 mysql> set sql_log_bin=1;
查看数据恢复情况
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)、
基于GTID二进制日志还原数据
修改配置文件
[root@localhost ~]# vi /etc/my.cnf server-id=1 log-bin=binlog gtid_mode=ON enforce_gtid_consistency=true log_slave_updates=1 #重启数据库服务 [root@localhost ~]# systemctl restart mysqld
操作数据库
mysql> create database mydb1; mysql> use mydb1; Database changed mysql> create table t1(id int)engine=innodb charset=utf8mb4; mysql> insert into t1 values(1); mysql> insert into t1 values(2); mysql> insert into t1 values(3); mysql> insert into t1 values(11); mysql> insert into t1 values(12); mysql> commit; mysql> select * from t1; +------+ | id | +------+ | 1 | | 2 | | 3 | | 11 | | 12 | +------+ 5 rows in set (0.00 sec) mysql> drop database mydb1;
查看二进制日志信息
mysql> show master status\G; *************************** 1. row *************************** File: binlog.000003 Position: 1944 Binlog_Do_DB: Binlog_Ignore_DB: Executed_Gtid_Set: 51d3db57-bf69-11ea-976c-000c2911a022:1-8 1 row in set (0.00 sec) mysql> show binlog events in 'binlog.000003'; +---------------+------+----------------+-----------+-------------+-------------------------------------------------------------------+ | Log_name | Pos | Event_type | Server_id | End_log_pos | Info | +---------------+------+----------------+-----------+-------------+-------------------------------------------------------------------+ | binlog.000003 | 154 | Gtid | 1 | 219 | SET @@SESSION.GTID_NEXT= '51d3db57-bf69-11ea-976c-000c2911a022:1' | | binlog.000003 | 219 | Query | 1 | 316 | create database mydb1 | | binlog.000003 | 1784 | Gtid | 1 | 1849 | SET @@SESSION.GTID_NEXT= '51d3db57-bf69-11ea-976c-000c2911a022:8' | | binlog.000003 | 1849 | Query | 1 | 1944 | drop database mydb1 | +---------------+------+----------------+-----------+-------------+-------------------------------------------------------------------+
另存为二进制日志信息
#8号事务记录为删除数据库,因此只需恢复1-7号事务记录即可 [root@localhost ~]# mysqlbinlog --skip-gtids --include-gtids='51d3db57-bf69-11ea-976c-000c2911a022:1-7' /var/lib/mysql/binlog.000003 > /tmp/gtid.sql
参数说明:
--include-gtids:包含事务
--exclude-gtids:排除事务
--skip-gtids:跳过事务
恢复数据
mysql> set sql_log_bin=0; mysql> source /tmp/gtid.sql mysql> set sql_log_bin=1;
查看数据恢复情况
mysql> show databases; +--------------------+ | Database | +--------------------+ | information_schema | | mydb | | mydb1 | | mysql | | performance_schema | | sys | +--------------------+ 6 rows in set (0.00 sec) mysql> use mydb1; Database changed mysql> select * from t1; +------+ | id | +------+ | 1 | | 2 | | 3 | | 11 | | 12 | +------+ 5 rows in set (0.00 sec)
到此这篇关于Centos7实现MySQL基于日志还原数据的示例代码的文章就介绍到这了,更多相关Centos7 MySQL日志还原数据内容请搜索以前的文章或继续浏览下面的相关文章希望大家以后多多支持!
相思资源网 Design By www.200059.com
广告合作:本站广告合作请联系QQ:858582 申请时备注:广告合作(否则不回)
免责声明:本站文章均来自网站采集或用户投稿,网站不提供任何软件下载或自行开发的软件! 如有用户或公司发现本站内容信息存在侵权行为,请邮件告知! 858582#qq.com
免责声明:本站文章均来自网站采集或用户投稿,网站不提供任何软件下载或自行开发的软件! 如有用户或公司发现本站内容信息存在侵权行为,请邮件告知! 858582#qq.com
相思资源网 Design By www.200059.com
暂无Centos7实现MySQL基于日志还原数据的示例代码的评论...