• GreatSQL社区原创内容未经授权不得随意使用,转载请联系小编并注明来源。
  • GreatSQL是MySQL的国产分支版本,使用上与MySQL一致。

my2sql工具之快速入门

  • 1.什么是my2sql
  • 2.如何快速部署my2sql工具
  • 3.如何使用my2sql工具
    • 3.1使用my2sql工具解析binglog文件
    • 3.2使用my2sql工具快速闪回
  • 4.遇到的问题
  • 5.使用限制
  • 6.总结

1. 什么是my2sql?

my2sql是go版MySQL binlog解析工具,通过解析MySQL binlog ,可以生成原始SQL、回滚SQL、去除主键的INSERT SQL等,也可以生成DML统计信息。类似工具有binlog2sql、MyFlash、my2fback等,本工具基于my2fback、binlog_rollback工具二次开发而来。

2. 如何快速部署my2sql工具

方式一 go编译

git clone https://github.com/liuhr/my2sql.git
cd my2sql/
go build .

方式二 已编译好的二进制文件

二进制文件下载地址

https://github.com/liuhr/my2sql/blob/master/releases/centOS_release_7.x/my2sql

复制到我的docker中

#1、把my2sql 二进制文件复制到mysql8的容器中
docker cp /usr/local/bin/my2sql mysql8:/usr/local/bin #2、进入容器
docker exec -it mysql8 bash
chmod +x /usr/local/bin/my2sql

3. 如何使用my2sql工具

3.1 使用my2sql工具解析binglog文件

step1:查询binglog文件

mysql> show variables like 'binlog_format';
+---------------+-------+
| Variable_name | Value |
+---------------+-------+
| binlog_format | ROW |
+---------------+-------+
1 row in set (0.00 sec) mysql> show master status;
+----------+----------+--------------+------------------+--------------------------------------------+
| File | Position | Binlog_Do_DB | Binlog_Ignore_DB | Executed_Gtid_Set |
+----------+----------+--------------+------------------+----------------------------------------------+
| 1.000001 | 1021 | | | ecd66956-f106-11ec-bb85-0242ac110005:1-19718 |
+----------+----------+--------------+------------------+----------------------------------------------+
1 row in set (0.00 sec) mysql> flush logs;
Query OK, 0 rows affected (0.71 sec) mysql> show master status;
+----------+----------+--------------+------------------+----------------------------------------------+
| File | Position | Binlog_Do_DB | Binlog_Ignore_DB | Executed_Gtid_Set |
+----------+----------+--------------+------------------+----------------------------------------------+
| 1.000002 | 193 | | | ecd66956-f106-11ec-bb85-0242ac110005:1-19718 |
+----------+----------+--------------+------------------+----------------------------------------------+
1 row in set (0.00 sec) mysql> show variables like 'innodb_log_group_home_dir';
+---------------------------+-------+
| Variable_name | Value |
+---------------------------+-------+
| innodb_log_group_home_dir | ./ |
+---------------------------+-------+
1 row in set (0.01 sec) mysql> show variables like '%datadir%';
+---------------+-----------------+
| Variable_name | Value |
+---------------+-----------------+
| datadir | /var/lib/mysql/ |
+---------------+-----------------+
1 row in set (0.00 sec)

step2:查询测试表:

-- 查看测试表结构
mysql> show create table sbtest.sbtest1\G;
*************************** 1. row ***************************
Table: sbtest1
Create Table: CREATE TABLE `sbtest1` (
`id` int NOT NULL AUTO_INCREMENT,
`k` int NOT NULL DEFAULT '0',
`c` char(120) COLLATE utf8mb4_0900_bin NOT NULL DEFAULT '',
`pad` char(60) COLLATE utf8mb4_0900_bin NOT NULL DEFAULT '',
PRIMARY KEY (`id`),
KEY `k_1` (`k`)
) ENGINE=InnoDB AUTO_INCREMENT=100001 DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_0900_bin
1 row in set (0.00 sec)
--查看测试表行数
mysql> select count(*) from sbtest.sbtest1;
+----------+
| count(*) |
+----------+
| 100000 |
+----------+
1 row in set (0.00 sec)
--查看测试表的检验值
mysql> checksum table sbtest.sbtest1;
+----------------+------------+
| Table | Checksum |
+----------------+------------+
| sbtest.sbtest1 | 4167997150 |
+----------------+------------+
1 row in set (0.09 sec)
-- 删除5万行数据
mysql> delete from sbtest.sbtest1 where id<50000; 【这里删除了49999行数据】
Query OK, 49999 rows affected (7.72 sec) --再次看测试表的检验值
mysql> checksum table sbtest.sbtest1;
+----------------+------------+
| Table | Checksum |
+----------------+------------+
| sbtest.sbtest1 | 1629663751 |
+----------------+------------+
1 row in set (0.04 sec)

step3:解析binglog文件

#解析binglog文件
root@29b86217f7d4:/tmp# my2sql -user root -password ****** -host 127.0.0.1 -port 3306 -databases sbtest -tables sbtest1 -mode repl -work-type 2sql -start-file 1.000002 -output-dir /tmp
[2022/07/28 10:26:42] [info] binlogsyncer.go:144 create BinlogSyncer with config {1113306 mysql 127.0.0.1 3306 root utf8 false false <nil> false UTC false 0 0s 0s 0 false false 0}
[2022/07/28 10:26:42] [info] events.go:60 start thread 1 to generate redo/rollback sql
[2022/07/28 10:26:42] [info] binlogsyncer.go:360 begin to sync binlog from position (1.000002, 4)
[2022/07/28 10:26:42] [info] stats_process.go:166 start thread to analyze statistics from binlog
[2022/07/28 10:26:42] [info] events.go:210 start thread to write redo/rollback sql into file
[2022/07/28 10:26:42] [info] events.go:60 start thread 2 to generate redo/rollback sql
[2022/07/28 10:26:42] [info] repl.go:16 start to get binlog from mysql
[2022/07/28 10:26:42] [info] binlogsyncer.go:777 rotate to (1.000002, 4)
[2022/07/28 10:26:50] [info] repl.go:84 deadline exceeded.
[2022/07/28 10:26:50] [info] repl.go:18 finish getting binlog from mysql
[2022/07/28 10:26:50] [info] stats_process.go:266 exit thread to analyze statistics from binlog
[2022/07/28 10:26:50] [info] events.go:185 exit thread 1 to generate redo/rollback sql
[2022/07/28 10:26:50] [info] events.go:185 exit thread 2 to generate redo/rollback sql
[2022/07/28 10:26:50] [info] events.go:274 finish writing redo/forward sql into file
[2022/07/28 10:26:50] [info] events.go:277 exit thread to write redo/rollback sql into file
root@29b86217f7d4:/tmp# ls -lrt
total 2392
-rw-r--r-- 1 root root 261 Jul 28 10:26 biglong_trx.txt
-rw-r--r-- 1 root root 2438845 Jul 28 10:26 forward.2.sql
-rw-r--r-- 1 root root 288 Jul 28 10:26 binlog_status.txt

step4:查看解析后的sql:

--文件binlog_status.txt和biglong_trx.txt是事务的统计信息
root@29b86217f7d4:/tmp# cat binlog_status.txt
binlog starttime stoptime startpos stoppos inserts updates deletes database table
1.000002 2022-07-28_10:24:06 2022-07-28_10:24:06 341 9536267 0 0 49999 sbtest sbtest1
root@29b86217f7d4:/tmp# cat biglong_trx.txt
binlog starttime stoptime startpos stoppos rows duration tables
1.000002 2022-07-28_10:24:06 2022-07-28_10:24:06 269 9536294 49999 0 [sbtest.sbtest1(inserts=0, updates=0, deletes=49999)]
--文件forward.2.sql是binlog解析之后的sql
root@29b86217f7d4:/tmp# more -10 forward.2.sql
DELETE FROM `sbtest`.`sbtest1` WHERE `id`=1;
DELETE FROM `sbtest`.`sbtest1` WHERE `id`=2;
DELETE FROM `sbtest`.`sbtest1` WHERE `id`=3;
DELETE FROM `sbtest`.`sbtest1` WHERE `id`=4;
DELETE FROM `sbtest`.`sbtest1` WHERE `id`=5;
DELETE FROM `sbtest`.`sbtest1` WHERE `id`=6;
DELETE FROM `sbtest`.`sbtest1` WHERE `id`=7;
DELETE FROM `sbtest`.`sbtest1` WHERE `id`=8;
DELETE FROM `sbtest`.`sbtest1` WHERE `id`=9;
DELETE FROM `sbtest`.`sbtest1` WHERE `id`=10;
--More--(0%)

3.2 使用my2sql工具快速闪回

step1:生成闪回sql文件

root@29b86217f7d4:/tmp# my2sql  -user root -password ****** -host 127.0.0.1 -port 3306 -databases sbtest -tables sbtest1 -mode repl -work-type rollback   -start-file 1.000002  -output-dir /tmp/flashback/
[2022/07/28 10:42:55] [info] events.go:60 start thread 1 to generate redo/rollback sql
[2022/07/28 10:42:55] [info] events.go:210 start thread to write redo/rollback sql into file
[2022/07/28 10:42:55] [info] binlogsyncer.go:144 create BinlogSyncer with config {1113306 mysql 127.0.0.1 3306 root utf8 false false <nil> false UTC false 0 0s 0s 0 false false 0}
[2022/07/28 10:42:55] [info] events.go:60 start thread 2 to generate redo/rollback sql
[2022/07/28 10:42:55] [info] stats_process.go:166 start thread to analyze statistics from binlog
[2022/07/28 10:42:55] [info] binlogsyncer.go:360 begin to sync binlog from position (1.000002, 4)
[2022/07/28 10:42:55] [info] repl.go:16 start to get binlog from mysql
[2022/07/28 10:42:55] [info] binlogsyncer.go:777 rotate to (1.000002, 4)
[2022/07/28 10:43:00] [info] repl.go:84 deadline exceeded.
[2022/07/28 10:43:00] [info] repl.go:18 finish getting binlog from mysql
[2022/07/28 10:43:00] [info] stats_process.go:266 exit thread to analyze statistics from binlog
[2022/07/28 10:43:00] [info] events.go:185 exit thread 1 to generate redo/rollback sql
[2022/07/28 10:43:00] [info] events.go:185 exit thread 2 to generate redo/rollback sql
[2022/07/28 10:43:00] [info] events.go:259 finish writing rollback sql into tmp files, start to revert content order of tmp files
[2022/07/28 10:43:00] [info] rollback_process.go:15 start thread 1 to revert rollback sql files
[2022/07/28 10:43:00] [info] rollback_process.go:41 start to revert tmp file /tmp/flashback/.rollback.2.sql into /tmp/flashback/rollback.2.sql
[2022/07/28 10:43:00] [info] rollback_process.go:156 finish reverting tmp file /tmp/flashback/.rollback.2.sql into /tmp/flashback/rollback.2.sql
[2022/07/28 10:43:00] [info] rollback_process.go:25 exit thread 1 to revert rollback sql files
[2022/07/28 10:43:00] [info] events.go:272 finish reverting content order of tmp files
[2022/07/28 10:43:00] [info] events.go:277 exit thread to write redo/rollback sql into file

step2:查看生成的闪回文件:

root@29b86217f7d4:/tmp/flashback# ls -lrt
total 12596
-rw-r--r-- 1 root root 261 Jul 28 10:42 biglong_trx.txt
-rw-r--r-- 1 root root 288 Jul 28 10:43 binlog_status.txt
-rw-r--r-- 1 root root 12888636 Jul 28 10:43 rollback.2.sql
root@29b86217f7d4:/tmp/flashback# cat  biglong_trx.txt
binlog starttime stoptime startpos stoppos rows duration tables
1.000002 2022-07-28_10:24:06 2022-07-28_10:24:06 269 9536294 49999 0 [sbtest.sbtest1(inserts=0, updates=0, deletes=49999)]
root@29b86217f7d4:/tmp/flashback# cat binlog_status.txt
binlog starttime stoptime startpos stoppos inserts updates deletes database table
1.000002 2022-07-28_10:24:06 2022-07-28_10:24:06 341 9536267 0 0 49999 sbtest sbtest1
root@29b86217f7d4:/tmp/flashback# more -10 rollback.2.sql
INSERT INTO `sbtest`.`sbtest1` (`id`,`k`,`c`,`pad`) VALUES (49999,33022,'82276829554-28600016482-71437056503-67189283057-49828408020-97469013057-54486869404-00631592142
-97314346455-10619483378','14496218158-82953408254-72982060504-31493955809-57788873512');
INSERT INTO `sbtest`.`sbtest1` (`id`,`k`,`c`,`pad`) VALUES (49998,50244,'96937339387-30971064351-79066739653-24906328840-02614118448-07131634761-96921531810-30574594369
-22852947139-53163560618','11061918782-99235282357-54725926348-09882016546-43656283296');
INSERT INTO `sbtest`.`sbtest1` (`id`,`k`,`c`,`pad`) VALUES (49997,49958,'23875561992-79383179714-31844019265-78678195929-11039506986-44432953782-19018620372-45679869851
-21838821757-54316746647','72021998255-21544610411-49097617755-83325624702-67496909817');
INSERT INTO `sbtest`.`sbtest1` (`id`,`k`,`c`,`pad`) VALUES (49996,49784,'64121185719-74243968401-44193775190-53796401184-56297492349-79981936074-03057285270-03714583251
-22209198873-10937378934','77159337465-83656217977-70877234699-71552982384-42185635425');
INSERT INTO `sbtest`.`sbtest1` (`id`,`k`,`c`,`pad`) VALUES (49995,49750,'32364537835-96169411677-52025383891-99856461851-54876781624-38035712955-18254695168-34412213489
-24174915574-41228645716','40846762001-49118260546-21871620253-63339659850-56585169893');
--More--(0%)

step3:应用rollback文件进行数据恢复

mysql -uroot -p****** -P3306 -h127.0.0.1 sbtest < /tmp/flashback/rollback.2.sql

step4:最后检查恢复情况

mysql> select count(*) from sbtest.sbtest1;
+----------+
| count(*) |
+----------+
| 100000 |
+----------+
1 row in set (0.01 sec)
mysql> checksum table sbtest.sbtest1;
+----------------+------------+
| Table | Checksum |
+----------------+------------+
| sbtest.sbtest1 | 4167997150 |
+----------------+------------+
1 row in set (0.08 sec)

4. 遇到的问题

问题1:Connect mysql failed this authentication plugin is not supported

my2sql  -user greatsql -password ****** -host 127.0.0.1 -port 3306  -mode repl -work-type 2sql -start-file binlog.000001
[2022/07/28 10:00:01] [fatal] context.go:575 Connect mysql failed this authentication plugin is not supported

解决

#修改
ALTER USER 'username'@'localhost' IDENTIFIED WITH mysql_native_password BY 'password';
FLUSH PRIVILEGES;

问题2:Connect mysql failed this user requires mysql native password authentication.

my2sql  -user greatsql  -password ****** 127.0.0.1 -port 3306 -mode repl -work-type 2sql -start-file binlog.000001
[2022/07/28 09:35:08] [fatal] context.go:575 Connect mysql failed this user requires mysql native password authentication.

解决

#mysql配置文件中增加以下配置项并重启
default_authentication_plugin=mysql_native_password

5. 使用限制

  • 使用回滚/闪回功能时,binlog格式必须为row,且binlog_row_image=full, DML统计以及大事务分析不受影响。
  • 只能回滚DML, 不能回滚DDL。
  • 支持指定-tl时区来解释binlog中time/datetime字段的内容。开始时间-start-datetime与结束时间-stop-datetime也会使用此指定的时区, 但注意此开始与结束时间针对的是binlog event header中保存的unix timestamp。结果中的额外的datetime时间信息都是binlog event header中的unix timestamp。
  • 此工具是伪装成从库拉取binlog,需要连接数据库的用户有SELECT, REPLICATION SLAVE, REPLICATION CLIENT权限。
  • MySQL8.0版本需要在配置文件中加入default_authentication_plugin =mysql_native_password,用户密码认证必须是mysql_native_password才能解析。

6.总结

my2sql除了可以用来做binlog的解析、闪回,还提供主从切换后新master丢数据的修复、大事务和长事务的分析、主从延迟分析等,后面有空继续分享my2sql在事务方面的应用,大家也可以自己动手实践一下。

参考文档

https://github.com/liuhr/my2sql/blob/master/README.md

Enjoy GreatSQL

文章推荐:

有趣的SQL DIGEST

ulimits不生效导致数据库启动失败和相关设置说明

MGR及GreatSQL资源汇总

GreatSQL MGR FAQ

在Linux下源码编译安装GreatSQL/MySQL

关于 GreatSQL

GreatSQL是由万里数据库维护的MySQL分支,专注于提升MGR可靠性及性能,支持InnoDB并行查询特性,是适用于金融级应用的MySQL分支版本。

Gitee:

https://gitee.com/GreatSQL/GreatSQL

GitHub:

https://github.com/GreatSQL/GreatSQL

Bilibili:

https://space.bilibili.com/1363850082/favlist

技术交流群:

微信:扫码添加GreatSQL社区助手微信好友,发送验证信息加群

my2sql工具之快速入门的更多相关文章

  1. 【荐】PHP采集工具curl快速入门教程

    为什么要用CURL? CURL(Client URL Library Functions)是一个利用URL语法在命令行方式下工作的文件传输工具.它支持很多协议:FTP, FTPS, HTTP, HTT ...

  2. 前端打包构建工具gulp快速入门

    因为之前一直有人给我推荐gulp,说他这里好哪里好的.实际上对我来说够用就行.grunt熟悉以后实际上他的配置也不难,说到效率的话确实是个问题,尤其项目大了以后,目前位置遇到的项目都还可以忍受.不过不 ...

  3. 前端打包构建工具grunt快速入门(大篇幅完整版)

    打包的目的和意义就不用叙述了直接上干货 http://www.gruntjs.net/getting-started里面的教程也太简单了,需要下一番功夫去研究才行.本文将grunt打包的常用方法都用实 ...

  4. Web Api 入门实战 (快速入门+工具使用+不依赖IIS)

    平台之大势何人能挡? 带着你的Net飞奔吧!:http://www.cnblogs.com/dunitian/p/4822808.html 屁话我也就不多说了,什么简介的也省了,直接简单概括+demo ...

  5. MyBatis-Plus工具快速入门

    MyBatis-Plus官方文档:http://mp.baomidou.com/#/quick-starthttp://mp.baomidou.com/guide/#%E7%89%B9%E6%80%A ...

  6. 版本控制工具Git工具快速入门-Linux篇

    版本控制工具Git工具快速入门-Linux篇 作者:尹正杰 版权声明:原创作品,谢绝转载!否则将追究法律责任. 一.版本管理系统的介绍 1>.版本管理系统的特点 1.1>.自动生成备份: ...

  7. 版本控制工具Git工具快速入门-Windows篇

    版本控制工具Git工具快速入门-Windows篇 作者:尹正杰 版权声明:原创作品,谢绝转载!否则将追究法律责任. 最近在学习Golang语言,之前的开发环境在linux上开发的,后来由于办公用的是w ...

  8. [易学易懂系列|rustlang语言|零基础|快速入门|(28)|实战5:实现BTC价格转换工具]

    [易学易懂系列|rustlang语言|零基础|快速入门|(28)|实战5:实现BTC价格转换工具] 项目实战 实战5:实现BTC价格转换工具 今天我们来开发一个简单的BTC实时价格转换工具. 我们首先 ...

  9. [易学易懂系列|rustlang语言|零基础|快速入门|(25)|实战2:命令行工具minigrep(2)]

    [易学易懂系列|rustlang语言|零基础|快速入门|(25)|实战2:命令行工具minigrep(2)] 项目实战 实战2:命令行工具minigrep 我们继续开发我们的minigrep. 我们现 ...

随机推荐

  1. CF1682C. LIS or Reverse LIS?

    题意:给\(n\)个数,问你能构出严格上升子序列长度和下降子序列长度最小值的最大值. 思路: 如果一个数出现至少两次,ans++. 统计出现一次的个数,因为再最长上升子序列中,只能有一个值能贡献到下降 ...

  2. UniqueMergeTree:支持实时更新删除的 ClickHouse 表引擎

    UniqueMergeTree 开发的业务背景 首先,我们看一下哪些场景需要用到实时更新. 我们总结了三类场景: 第一类是业务需要对它的交易类数据进行实时分析,需要把数据流同步到 ClickHouse ...

  3. MySQLDocker 主从复制搭建

    MySQLDocker 主从复制搭建 MySQLDocker 的搭建 docker search mysql docker pull mysql/mysql-server:8.0.26 docker ...

  4. ElasticSearch6.4.2

    做一个简单的API记录 1.依赖为6.4.2  比较老的版本 2.指定ES集群,可接多个Put(); Setting setting=Setting.builder().put("clust ...

  5. 常用排序算法(一)-java实现

    排序算法总结 1.十大经典算法及性能 2.具体排序算法 1.冒泡排序 循环过程中比较相邻两个数大小,通过交换正确排位,循环整个数组即可完成排序 图片演示 代码实现Java //冒泡排序 public ...

  6. Java 基础常见知识点&面试题总结(中),2022 最新版!| JavaGuide

    你好,我是 Guide.秋招即将到来,我对 JavaGuide 的内容进行了重构完善,公众号同步一下最新更新,希望能够帮助你. 上篇:Java 基础常见知识点&面试题总结(上),2022 最新 ...

  7. php类精确验证身份证号码

    <?php // check class check{ // $num为身份证号码,$checkSex:1为男,2为女,不输入为不验证 public function checkIdentity ...

  8. BUUCTF-菜刀666

    菜刀666 这题和之前做过的流量题不同,对我还是有些难度.看了看大佬的wp才做出来的 wireshark打开流量包,一开始只是单纯过滤http,包很多,看花了眼,看了好多也没觉得有啥异常. 后面才知道 ...

  9. BUUCTF-文件中的秘密

    文件中的秘密 下载图片属性查看即可发现flag.这算是图片必须查看的几个地方.

  10. 集成学习——Adaboost(手推公式)