首页 > 数据库 >mysql基于binlog的恢复

mysql基于binlog的恢复

时间:2023-01-04 11:12:54浏览次数:49  
标签:binlog 基于 -- MySQL sec mysql root its

[root@stag-8-46 0104]# mysql --socket=/tmp/mysql_sandbox20034.sock -umsandbox -p'msandbox'
mysql: [Warning] Using a password on the command line interface can be insecure.
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 21
Server version: 5.7.33-log MySQL Community Server (GPL)

Copyright (c) 2009-2022 Percona LLC and/or its affiliates
Copyright (c) 2000, 2022, Oracle and/or its affiliates.

Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.

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

mysql> show master status;
+------------------+----------+--------------+------------------+-------------------+
| File | Position | Binlog_Do_DB | Binlog_Ignore_DB | Executed_Gtid_Set |
+------------------+----------+--------------+------------------+-------------------+
| mysql-bin.000002 | 5662 | | | |
+------------------+----------+--------------+------------------+-------------------+
1 row in set (0.00 sec)

--环境准备,记录当前的binlog位置mysql-bin.000002

mysql> use test
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
mysql> select count(1) from t;
+----------+
| count(1) |
+----------+
| 0 |
+----------+
1 row in set (0.00 sec)

mysql> desc t;
+-------+---------+------+-----+---------+----------------+
| Field | Type | Null | Key | Default | Extra |
+-------+---------+------+-----+---------+----------------+
| id | int(11) | NO | PRI | NULL | auto_increment |
+-------+---------+------+-----+---------+----------------+
1 row in set (0.00 sec)

mysql> select * from t;
Empty set (0.00 sec)

mysql>
mysql>
mysql> show master status;
+------------------+----------+--------------+------------------+-------------------+
| File | Position | Binlog_Do_DB | Binlog_Ignore_DB | Executed_Gtid_Set |
+------------------+----------+--------------+------------------+-------------------+
| mysql-bin.000002 | 5662 | | | |
+------------------+----------+--------------+------------------+-------------------+
1 row in set (0.00 sec)

--生成新的binlog

mysql> flush logs;
Query OK, 0 rows affected (0.02 sec)

mysql> show master status;
+------------------+----------+--------------+------------------+-------------------+
| File | Position | Binlog_Do_DB | Binlog_Ignore_DB | Executed_Gtid_Set |
+------------------+----------+--------------+------------------+-------------------+
| mysql-bin.000003 | 154 | | | |
+------------------+----------+--------------+------------------+-------------------+
1 row in set (0.00 sec)

mysql> select * from t;
Empty set (0.00 sec)

--将数据写入到新的binlog方便实验(插入记录1,2,3)

mysql> insert into t select null;
Query OK, 1 row affected (0.01 sec)
Records: 1 Duplicates: 0 Warnings: 0

mysql> insert into t select null;
Query OK, 1 row affected (0.01 sec)
Records: 1 Duplicates: 0 Warnings: 0

mysql> insert into t select null;
Query OK, 1 row affected (0.01 sec)
Records: 1 Duplicates: 0 Warnings: 0

mysql> select * from t;
+----+
| id |
+----+
| 1 |
| 2 |
| 3 |
+----+
3 rows in set (0.00 sec)

mysql> show master status;
+------------------+----------+--------------+------------------+-------------------+
| File | Position | Binlog_Do_DB | Binlog_Ignore_DB | Executed_Gtid_Set |
+------------------+----------+--------------+------------------+-------------------+
| mysql-bin.000003 | 910 | | | |
+------------------+----------+--------------+------------------+-------------------+
1 row in set (0.00 sec)

--切换binlog在新的binlog中也插入数据(插入记录4,5,6)

mysql> flush logs;
Query OK, 0 rows affected (0.02 sec)

mysql> select * from t;
+----+
| id |
+----+
| 1 |
| 2 |
| 3 |
+----+
3 rows in set (0.00 sec)

mysql> show master status;
+------------------+----------+--------------+------------------+-------------------+
| File | Position | Binlog_Do_DB | Binlog_Ignore_DB | Executed_Gtid_Set |
+------------------+----------+--------------+------------------+-------------------+
| mysql-bin.000004 | 154 | | | |
+------------------+----------+--------------+------------------+-------------------+
1 row in set (0.00 sec)

mysql> insert into t select null;
Query OK, 1 row affected (0.00 sec)
Records: 1 Duplicates: 0 Warnings: 0

mysql> insert into t select null;
Query OK, 1 row affected (0.01 sec)
Records: 1 Duplicates: 0 Warnings: 0

mysql> insert into t select null;
Query OK, 1 row affected (0.01 sec)
Records: 1 Duplicates: 0 Warnings: 0

mysql> show master status;
+------------------+----------+--------------+------------------+-------------------+
| File | Position | Binlog_Do_DB | Binlog_Ignore_DB | Executed_Gtid_Set |
+------------------+----------+--------------+------------------+-------------------+
| mysql-bin.000004 | 910 | | | |
+------------------+----------+--------------+------------------+-------------------+
1 row in set (0.00 sec)

mysql> exit
Bye

--恢复开始
[root@stag-8-46 0104]# mysql --socket=/tmp/mysql_sandbox20034.sock -umsandbox -p'msandbox'
mysql: [Warning] Using a password on the command line interface can be insecure.
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 22
Server version: 5.7.33-log MySQL Community Server (GPL)

Copyright (c) 2009-2022 Percona LLC and/or its affiliates
Copyright (c) 2000, 2022, Oracle and/or its affiliates.

Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.

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

--原始数据

mysql> use test
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
mysql> select * from t;
+----+
| id |
+----+
| 1 |
| 2 |
| 3 |
| 4 |
| 5 |
| 6 |
+----+
6 rows in set (0.00 sec)

mysql> exit
Bye

--将binlog3中的提取操作记录到03.sql中
[root@stag-8-46 0104]# mysqlbinlog --no-defaults --start-position=4 mysql-bin.000003 > /tmp/0104/03.sql
mysqlbinlog: File 'mysql-bin.000003' not found (Errcode: 2 - No such file or directory)
[root@stag-8-46 0104]# pwd
/tmp/0104
[root@stag-8-46 0104]# cd /opt/MySQL/home/rsandbox_5_7_33/master/data
[root@stag-8-46 data]# mysqlbinlog --no-defaults --start-position=4 mysql-bin.000003 > /tmp/0104/03.sql

--将binlog4中的提取操作记录到04.sql中
[root@stag-8-46 data]# mysqlbinlog --no-defaults --start-position=4 mysql-bin.000004 > /tmp/0104/04.sql

--将binlog3、binlog4中的提取操作记录到0304.sql中
[root@stag-8-46 data]# mysqlbinlog --no-defaults --start-position=4 mysql-bin.00000[3..4] > /tmp/0104/0304.sql
[root@stag-8-46 data]#
[root@stag-8-46 data]#
[root@stag-8-46 data]# mysql --socket=/tmp/mysql_sandbox20034.sock -umsandbox -p'msandbox'
mysql: [Warning] Using a password on the command line interface can be insecure.
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 23
Server version: 5.7.33-log MySQL Community Server (GPL)

Copyright (c) 2009-2022 Percona LLC and/or its affiliates
Copyright (c) 2000, 2022, Oracle and/or its affiliates.

Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.

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

mysql> use test
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

--原始记录数
mysql> select * from t;
+----+
| id |
+----+
| 1 |
| 2 |
| 3 |
| 4 |
| 5 |
| 6 |
+----+
6 rows in set (0.00 sec)

--truncate掉表

mysql> truncate table t;
Query OK, 0 rows affected (0.02 sec)

mysql> exit
Bye
[root@stag-8-46 data]# mysql --socket=/tmp/mysql_sandbox20034.sock -umsandbox -p'msandbox' ^C
[root@stag-8-46 data]# ls -l /tmp/0104
total 20
-rw-r--r-- 1 root root 7179 Jan 4 10:42 0304.sql
-rw-r--r-- 1 root root 3990 Jan 4 10:41 03.sql
-rw-r--r-- 1 root root 3948 Jan 4 10:41 04.sql
-rw-r--r-- 1 root root 2938 Jan 4 10:36 test.sql

--将binlog3中的提取操作记录的03.sql恢复到数据中
[root@stag-8-46 data]# mysql --socket=/tmp/mysql_sandbox20034.sock -umsandbox -p'msandbox' < /tmp/0104/03.sql
mysql: [Warning] Using a password on the command line interface can be insecure.
[root@stag-8-46 data]# mysql --socket=/tmp/mysql_sandbox20034.sock -umsandbox -p'msandbox'
mysql: [Warning] Using a password on the command line interface can be insecure.
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 25
Server version: 5.7.33-log MySQL Community Server (GPL)

Copyright (c) 2009-2022 Percona LLC and/or its affiliates
Copyright (c) 2000, 2022, Oracle and/or its affiliates.

Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.

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

mysql> use test
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

--只恢复了binlog3中的提取操作记录
mysql> select * from t;
+----+
| id |
+----+
| 1 |
| 2 |
| 3 |
+----+
3 rows in set (0.00 sec)

--继续实验

mysql> truncate table t;
Query OK, 0 rows affected (0.03 sec)

mysql> exit
Bye

--将binlog4中的提取操作记录的04.sql恢复到数据中
[root@stag-8-46 data]# mysql --socket=/tmp/mysql_sandbox20034.sock -umsandbox -p'msandbox' < /tmp/0104/04.sql
mysql: [Warning] Using a password on the command line interface can be insecure.
[root@stag-8-46 data]# mysql --socket=/tmp/mysql_sandbox20034.sock -umsandbox -p'msandbox'
mysql: [Warning] Using a password on the command line interface can be insecure.
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 27
Server version: 5.7.33-log MySQL Community Server (GPL)

Copyright (c) 2009-2022 Percona LLC and/or its affiliates
Copyright (c) 2000, 2022, Oracle and/or its affiliates.

Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.

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

mysql> use test
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

--只恢复了binlog4中的提取操作记录
mysql> select * from t;
+----+
| id |
+----+
| 4 |
| 5 |
| 6 |
+----+
3 rows in set (0.00 sec)

mysql> exit
Bye
[root@stag-8-46 data]# mysql --socket=/tmp/mysql_sandbox20034.sock -umsandbox -p'msandbox'
mysql: [Warning] Using a password on the command line interface can be insecure.
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 28
Server version: 5.7.33-log MySQL Community Server (GPL)

Copyright (c) 2009-2022 Percona LLC and/or its affiliates
Copyright (c) 2000, 2022, Oracle and/or its affiliates.

Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.

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

mysql> use test
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

--继续。。。
mysql> truncate table t;
Query OK, 0 rows affected (0.03 sec)

mysql> select * from t;
Empty set (0.00 sec)

mysql> exit
Bye

--将binlog3、binlog4中的提取操作记录的0304.sql恢复到数据中
[root@stag-8-46 data]# mysql --socket=/tmp/mysql_sandbox20034.sock -umsandbox -p'msandbox' < /tmp/0104/0304.sql
mysql: [Warning] Using a password on the command line interface can be insecure.
[root@stag-8-46 data]# mysql --socket=/tmp/mysql_sandbox20034.sock -umsandbox -p'msandbox'
mysql: [Warning] Using a password on the command line interface can be insecure.
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 30
Server version: 5.7.33-log MySQL Community Server (GPL)

Copyright (c) 2009-2022 Percona LLC and/or its affiliates
Copyright (c) 2000, 2022, Oracle and/or its affiliates.

Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.

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

--恢复了全部数据

mysql> use test
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
mysql> select * from t;
+----+
| id |
+----+
| 1 |
| 2 |
| 3 |
| 4 |
| 5 |
| 6 |
+----+
6 rows in set (0.00 sec)

mysql>

 

标签:binlog,基于,--,MySQL,sec,mysql,root,its
From: https://www.cnblogs.com/bygnee/p/17024288.html

相关文章

  • Mysql的时间计算
    1、timestampdiff()函数TIMESTAMPDIFF(unit,datetime_expr1,datetime_expr2)作用:计算两个日期、时间表达式(datetime_expr1和datetime_expr1)之间相差的天数、周......
  • Mysql ProxySQL
    rpm-ivhproxysql-2.4.1-1-centos7.x86_64.rpm版本:proxysql--version启动:serviceproxysqlstart暂停:serviceproxysqlstop重启:serviceproxysqlrestart状态:service......
  • LNMP架构环境之Mariadb数据库环境 nginx+php+mysql
    1)安装Mariadb数据库#1)安装Mariadb数据库yuminstallmariadb-servermariadb-y#2)启动Mariadb数据库,并加入开机自动复制代码systemctlstartmariadbsystemctle......
  • MySQL的优化多种方法(至少15条)
    转自:https://www.cnblogs.com/tdskee/p/16536166.htmlMYSQL的优化,是每一个程序员在做数据查询处理的时候,经常有的步骤那么SQL的优化有很多种,它可以是在硬件方面的,可以是在......
  • mysql5.7对json字段查询
    {"data":{"shipper":{"address":"No.236QianwangangRoad,WestCoastNewArea","province":"SHANDONG","cit......
  • 基于Chromium开发的称重软件,集称重、计价、打印于一体,支持耀华、顶尖等多个厂家设备型
     技术方案:1.运行时使用.NetFramework4.6框架,界面使用WPF与Chromium。2.上位机与下位机使用串口对接每家设备协议,上位机与UI使用WebSocket通讯。3.数据库使用SQLite和......
  • MySQL记录锁、间隙锁、临键锁
    最近要在公司内做一次技术分享,思来想去不知道该分享些什么,最后在朋友的提示下,准备分享一下MySQL的InnoDB引擎下的事务幻读问题与解决方案--LBCC&MVCC。经过好几天的熬夜通......
  • 基于 EventBridge API Destination 构建 SaaS 集成实践方案
    作者:赵海引言事件总线EventBridge是阿里云提供的一款无服务器事件总线服务,支持阿里云服务、自定义应用、SaaS应用以标准化、中心化的方式接入,并能够以标准化的CloudE......
  • 从零开始学 MySQL -- 数据库和数据表操作
    阅读本文大概需要7 分钟前言上篇文章我们学习了SELECT语句,今天我们学习下核心的内容,学习并实践如何对数据库表和表中的内容做修改,删除,重命名等操作。(想看看周末还有多少......
  • 从零开始学习 MySQL 系列--索引、视图、导入和导出
    前言上篇文章我们学习了数据库和数据表操作语句,今天我们学习下数据库索引,视图,导入和导出的知识。作为基础篇,不会涉及到关于索引和视图的高级应用和核心概念,但是基本操作大家......