超贊!墻裂推薦一個(gè)MySQL自動(dòng)化運(yùn)維工具!
在這之前,民工哥也給大家介紹過(guò)一款開(kāi)源的SQL管理工具:自動(dòng)補(bǔ)全、回滾!介紹一款可視化 sql 診斷利器。
今天,民工哥再給大家推薦一款SQL審核利器:goinception。
goinception介紹
goInception是一個(gè)集審核、執(zhí)行、備份及生成回滾語(yǔ)句于一身的MySQL運(yùn)維工具, 通過(guò)對(duì)執(zhí)行SQL的語(yǔ)法解析,返回基于自定義規(guī)則的審核結(jié)果,并提供執(zhí)行和備份及生成回滾語(yǔ)句的功能。
github地址:https://github.com/hanchuanchuan/goInception
文檔:https://hanchuanchuan.github.io/goInception/
goinception架構(gòu)
goinception安裝
官方提供了幾種的安裝方式,如下。
- 1、源碼安裝
源碼安裝需要有g(shù)o V1.2版本以上的環(huán)境,使用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、二進(jìn)制安裝(推薦)
直接上官方提供的地址:https://github.com/hanchuanchuan/goInception/releases/download/去下載對(duì)應(yīng)的版本文件,下載完成后直接解壓運(yùn)行即可。
- [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目錄下會(huì)看到一個(gè)默認(rèn)的配置文件:config.toml.default,大家可以根據(jù)實(shí)際情況修改。
goInception采用TiDB源碼重構(gòu),所以部分參數(shù)可參考TiDB相關(guān)文檔
config.toml文件由幾部分組成,分別為最外層配置如host,port等,以及各分組如[inc],[log]等。示例(該示例僅為展示config.toml文件結(jié)構(gòu),詳細(xì)參數(shù)請(qǐng)參考):https://github.com/hanchuanchuan/goInception/blob/master/config/config.toml.default
- host = "0.0.0.0"
- port = 4000
- path = "/tmp/tidb"
- [log]
- # 日志參數(shù)
- level = "info"
- format = "text"
- [log.file]
- # 日志文件參數(shù)
- filename = ""
- max-size = 300
- [inc]
- # 審核選項(xiàng)
- enable_nullable = true
- enable_drop_table = false
- check_table_comment = false
- check_column_comment = false
- # 等等...
- [osc]
- # pt-osc參數(shù)
- osc_on = false
- osc_min_table_size = 16
- [ghost]
- # gh-ost參數(shù)
- ghost_allow_on_master = true
配置修改完成后,就可以正常啟動(dòng)了。
- [root@centos7 goinception]# ./goInception -config=config/config.toml
- [root@centos7 ~]# netstat -lntp|grep 4000
- tcp6 0 0 :::4000 :::* LISTEN 1250/./goInception
使用實(shí)例
- /*--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、結(jié)果信息
給用戶返回的信息有兩種,
- 一種是提交給goInception的基礎(chǔ)信息存在錯(cuò)誤,比如源信息不全,或者源信息有錯(cuò)誤等,這種情況下,直接報(bào)異常,包括錯(cuò)誤碼及錯(cuò)誤信息,與MySQL服務(wù)器的異常是一樣的,在外面正常處理即可。
- 二是如果沒(méi)有上面的問(wèn)題,都會(huì)以結(jié)果集的方式將檢查結(jié)果告訴客戶端。和mysql原生結(jié)果集一致。返回的結(jié)果集中,每一個(gè)行數(shù)據(jù),就是一條提交的SQL語(yǔ)句,goInception內(nèi)部將所有提交的語(yǔ)句塊一條條的拆開(kāi),以結(jié)果集的方式返回,針對(duì)每一條語(yǔ)句,有什么問(wèn)題或者狀態(tài),在結(jié)果集中是一目了然。
注意:如果在語(yǔ)句中出現(xiàn)語(yǔ)法錯(cuò)誤,則不能繼續(xù)了,因?yàn)間oInception已經(jīng)不能將剩下的語(yǔ)句分開(kāi)了,那么此時(shí)前面已經(jīng)正常檢查的多行為多個(gè)結(jié)果集的行返回,后面出錯(cuò)的語(yǔ)句為一行返回,當(dāng)然這個(gè)的錯(cuò)誤信息是語(yǔ)法錯(cuò)誤。
2、自帶備份功能
自帶備份功能,首先服務(wù)啟動(dòng)時(shí)配置config.toml(放在 [inc] 段)
參數(shù) | 默認(rèn) | 可選范圍 | 說(shuō)明 |
---|---|---|---|
backup_host | "" | string | 備份數(shù)據(jù)庫(kù)IP地址 |
backup_port | 0 | int | 備份數(shù)據(jù)庫(kù)端口 |
backup_user | "" | string | 備份數(shù)據(jù)庫(kù)用戶名 |
backup_password | "" | string | 備份數(shù)據(jù)庫(kù)密碼 |
并且在執(zhí)行sql時(shí),添加 --backup=true 或 --backup=1 選項(xiàng)。
3、審核規(guī)則
相關(guān)的審核規(guī)則,審核選項(xiàng)等詳細(xì)信息,可以參考:
- https://hanchuanchuan.github.io/goInception/rules.html
對(duì)比Inception
1、功能對(duì)比
2、速度
3、使用
這類工具在一定程度上解放了DBA的雙手,而且還能在很大程度上避免出錯(cuò)的情況,從而保證SQL的執(zhí)行正確率,同時(shí)還提供SQL回滾功能,在出錯(cuò)時(shí)進(jìn)行回滾,保證數(shù)據(jù)安全與完整性。