goinception介绍
goInception是一个集审核、执行、备份及生成回滚语句于一身的MySQL运维工具, 通过对执行SQL的语法解析,返回基于自定义规则的审核结果,并提供执行和备份及生成回滚语句的功能。
github地址:https://github.com/hanchuanchuan/goInception
文档:https://hanchuanchuan.github.io/goInception/
goinception架构
goinception安装
1、源码安装
源码安装需要有go V1.2版本以上的环境,使用go mod作依赖管理。
git clone https://github.com/hanchuanchuan/goInception.gitcd goInceptionmake parsergo build -o goInception tidb-server/main.go
2、docker方式
docker pull hanchuanchuan/goinception
3、二进制安装(推荐)
直接从官方提供的地址:
https://github.com/hanchuanchuan/goInception/releases/download/
下载对应的版本文件,下载完成后直接解压运行即可。
mkdir goinceptiontar zxf goInception-linux-amd64-v1.2.3.tar.gz -C ./goinception/cd goinception/$ lltotal 38476drwxr-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/hanchuanchuan/goInception/blob/master/config/config.toml.default
host = "0.0.0.0"port = 4000path = "/tmp/tidb"[log]# 日志参数level = "info"format = "text"[log.file]# 日志文件参数filename = ""max-size = 300[inc]# 审核选项enable_nullable = trueenable_drop_table = falsecheck_table_comment = falsecheck_column_comment = false# 等等...[osc]# pt-osc参数osc_on = falseosc_min_table_size = 16[ghost]# gh-ost参数ghost_allow_on_master = true
配置修改完成后,就可以正常启动了。
$ ./goInception -config=config/config.toml$ netstat -lntp|grep 4000tcp6 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内部将所有提交的语句块一条条的拆开,以结果集的方式返回,针对每一条语句,有什么问题或者状态,在结果集中是一目了然。
:::danger
注意:如果在语句中出现语法错误,则不能继续了,因为goInception已经不能将剩下的语句分开了,那么此时前面已经正常检查的多行为多个结果集的行返回,后面出错的语句为一行返回,当然这个的错误信息是语法错误。
:::
2、自带备份功能
自带备份功能,首先服务启动时配置config.toml(放在 [inc] 段)
| 参数 | 默认 | 可选范围 | 说明 |
|---|---|---|---|
| backup_host | “” | string | 备份数据库IP地址 |
| backup_port | 0 | int | 备份数据库端口 |
| backup_user | “” | string | 备份数据库用户名 |
| backup_password | “” | string | 备份数据库密码 |
并且在执行sql时,添加 —backup=true 或 —backup=1 选项。
3、审核规则
相关的审核规则,审核选项等详细信息,可以参考:https://hanchuanchuan.github.io/goInception/rules.html
对比Inception
1、功能对比
| 功能 | inception | goInception | 说明 |
|---|---|---|---|
| 审核 | ✓ | ✓ | 基本无差异 |
| 执行 | ✓ | ✓ | 基本无差异 |
| pt-osc工具 | ✓ | ✓ | 基本无差异 |
| gh-ost工具 | ✕ | ✓ | |
| 备份 | ✓ | ✓ | 基本无差异 |
| 忽略警告 | ✓ | ✓ | 基本无差异 |
| 只读参数 | ✓ | ✕ | goinception未提供 |
| 打印SQL语法树 | ✓ | ✓ | inception的感觉更友好 |
| DDL和DML拆分功能 | ✓ | ✓ | goinception支持混合执行,不会影响回滚解析 |
| 执行部分后休眠 | ✓ | ✓ | goinception支持执行指定条数后休眠 |
| 计算真实受影响行数 | ✕ | ✓ | |
| 事务支持 | ✕ | ✓ | |
| SQL指纹功能 | ✕ | ✓ | dml语句相似时,可以根据相同的指纹ID复用explain结果,以减少远端数据库explain操作,提高审核速度 |
2、速度
3、使用

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