20

一个集审核、执行、备份及生成回滚语句于一身的MySQL自动化运维工具

 3 years ago
source link: https://segmentfault.com/a/1190000024487321
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.

JzuaUr.png!mobile

在这之前,民工哥也给大家介绍过一款开源的SQL管理工具: 自动补全、回滚!介绍一款可视化 sql 诊断利器

今天,民工哥再给大家推荐一款SQL审核利器:goinception。

goinception介绍

goInception是一个集审核、执行、备份及生成回滚语句于一身的MySQL运维工具, 通过对执行SQL的语法解析,返回基于自定义规则的审核结果,并提供执行和备份及生成回滚语句的功能。

github地址: https://github.com/hanchuanch...

文档: https://hanchuanchuan.github....

goinception架构

FjIV7j.jpg!mobile

goinception安装

官方提供了几种的安装方式,如下。

  • 1、源码安装

源码安装需要有go V1.2版本以上的环境,使用go mod作依赖管理。

[root@centos7 ~]# git clone https://github.com/hanchuanchuan/goInception.git
[root@centos7 ~]# cd goInception
[root@centos7 ~]# make parser
[root@centos7 ~]# go build -o goInception tidb-server/main.go
  • 2、docker方式
[root@centos7 ~]# docker pull hanchuanchuan/goinception
  • 3、二进制安装(推荐)

直接上官方提供的地址: https://github.com/hanchuanch... ,下载完成后直接解压运行即可。

[root@centos7 ~]# mkdir goinception
[root@centos7 ~]# tar zxf goInception-linux-amd64-v1.2.3.tar.gz -C ./goinception/
[root@centos7 ~]# cd goinception/
[root@centos7 goinception]# ll
total 38476
drwxr-xr-x 2 root root        33 Aug 30 03:48 config
-rwxr-xr-x 1  501 games 39399424 May 22 07:45 goInception

解压完成后,在config目录下会看到一个默认的配置文件:config.toml.default,大家可以根据实际情况修改。

goInception采用TiDB源码重构,所以部分参数可参考TiDB相关文档

config.toml文件由几部分组成,分别为最外层配置如host,port等,以及各分组如[inc],[log]等。示例(该示例仅为展示config.toml文件结构,详细参数请参考): https://github.com/hanchuanch...

host = "0.0.0.0"
port = 4000
path = "/tmp/tidb"
[log]
# 日志参数
level = "info"
format = "text"
[log.file]
# 日志文件参数
filename = ""
max-size = 300
[inc]
# 审核选项
enable_nullable = true
enable_drop_table = false
check_table_comment = false
check_column_comment = false
# 等等...
[osc]
# pt-osc参数
osc_on = false
osc_min_table_size = 16
[ghost]
# gh-ost参数
ghost_allow_on_master = true

配置修改完成后,就可以正常启动了。

[root@centos7 goinception]# ./goInception -config=config/config.toml
[root@centos7 ~]# netstat -lntp|grep 4000
tcp6   0   0 :::4000    :::*    LISTEN    1250/./goInception

使用实例

/*--user=root;--password=root;--host=127.0.0.1;--check=1;--port=3306;*/
inception_magic_start;
use test;
create table t1(id int primary key);
inception_magic_commit;

其它介绍

1、结果信息

给用户返回的信息有两种,

  • 一种是提交给goInception的基础信息存在错误,比如源信息不全,或者源信息有错误等,这种情况下,直接报异常,包括错误码及错误信息,与MySQL服务器的异常是一样的,在外面正常处理即可。
  • 二是如果没有上面的问题,都会以结果集的方式将检查结果告诉客户端。和mysql原生结果集一致。返回的结果集中,每一个行数据,就是一条提交的SQL语句,goInception内部将所有提交的语句块一条条的拆开,以结果集的方式返回,针对每一条语句,有什么问题或者状态,在结果集中是一目了然。

注意:如果在语句中出现语法错误,则不能继续了,因为goInception已经不能将剩下的语句分开了,那么此时前面已经正常检查的多行为多个结果集的行返回,后面出错的语句为一行返回,当然这个的错误信息是语法错误。

2、自带备份功能

自带备份功能,首先服务启动时配置config.toml(放在 [inc] 段)

BBjeqe.png!mobile

并且在执行sql时,添加 --backup=true 或 --backup=1 选项。

3、审核规则

相关的审核规则,审核选项等详细信息,可以参考: https://hanchuanchuan.github....

对比Inception

1、功能对比

yEB7juB.png!mobile

2、速度

YJ7Bfi7.png!mobile

3、使用

F3UVZj6.png!mobile

这类工具在一定程度上解放了DBA的双手,而且还能在很大程度上避免出错的情况,从而保证SQL的执行正确率,同时还提供SQL回滚功能,在出错时进行回滚,保证数据安全与完整性。

Ur6zUr2.png!mobile


About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK