使用Yearning部署一个工单化SQL语句检测平台

* GreatSQL社区原创内容未经授权不得随意使用,转载请联系小编并注明来源。

  • 1. 引入

  • 2. 环境准备

    • 2.1 通过yum安装Docker

    • 2.2 安装数据源

    • 2.3 安装部署Yearning

  • 3. 简单使用Yearning

  • 4. 总结

1. 引入

MySQL是十分热门的开源数据库,在软件开发中广泛用于数据的持久化存储。在软件开发生命周期当中往往会遇到问题,编码人员根据业务需求编写对应模块代码,经由测试人员进行压力测试等性能测试后发现,该模块性能达不到预期指标,结果分析得出是编码人员对数据源交互的SQL语句缺陷,开发人员通常想着先定位问题,然后反馈给DBA。今天分享一款可视化的SQL自动检测平台:Yearning MYSQL,可以方便开发人员初步定位问题,同时将SQL快速提交给DBA处理,DBA可以通过预设的规则来规范SQL语句的规范,必要时可以把错误的SQL语句快速回滚。

  • 开源地址:https://gitee.com/cookieYe/Yearning

  • 官方手册:https://guide.yearning.io/

535100989017fe3284bdc5dcdaa59b1e.png

Yearning MYSQL 是一个开源的SQL语句审核平台。该平台提供了数据源查询审计、SQL审核等多种功能,支持MySQL 5.7及以上版本,可以在一定程度上解决运维与开发之间的那一环,功能丰富,代码开源,安装部署容易,本文运行环境:

[root@VM-0-12-centos ~]# cat /etc/redhat-release
CentOS Linux release 8.0.1905 (Core)

2. 环境准备

2.1 通过yum安装Docker

卸载系统之前的 docker:

[root@VM-0-12-centos ~]# sudo yum remove docker \
docker-client \
docker-client-latest \
docker-common \
docker-latest \
docker-latest-logrotate \
docker-logrotate \
docker-engine

安装 docker-CE:

[root@VM-0-12-centos ~]# sudo yum install -y yum-utils \
device-mapper-persistent-data \
lvm2

设置 docker repo 的 yum 位置:

[root@VM-0-12-centos ~]# sudo yum-config-manager \
--add-repo \
https://download.docker.com/linux/centos/docker-ce.repo

安装 docker,以及 docker-cli:

[root@VM-0-12-centos ~]# sudo yum install docker-ce docker-ce-cli containerd.io

启动 docker:

[root@VM-0-12-centos ~]# sudo systemctl start docker

设置 docker 开机自启动:

[root@VM-0-12-centos ~]# sudo systemctl enable docker

2.2 安装数据源

使用Docker拉取MySQL 8.0最新版本的镜像:

[root@VM-0-12-centos ~]# docker pull mysql:8.0
8.0: Pulling from library/mysql
c1ad9731b2c7: Pull complete 
54f6eb0ee84d: Pull complete 
...
Status: Downloaded newer image for mysql:8.0
docker.io/library/mysql:8.0

创建文件夹:

[root@VM-0-12-centos ~]# mkdir -p /opt/datas/docker/mysql/conf
[root@VM-0-12-centos ~]# mkdir -p /opt/datas/docker/mysql/data

运行镜像:

[root@VM-0-12-centos ~]# docker run \
--name mysql \
--restart=unless-stopped \
-it -p 3306:3306 \
-v /opt/datas/docker/mysql/conf/my.cnf:/etc/mysql/my.cnf \
-v /opt/datas/docker/mysql/data:/var/lib/mysql \
-e MYSQL_ROOT_PASSWORD='MySQL2022.~#' \
-d mysql

也可以使用GreatSQL,使用Docker拉取GreatSQL最新版本的镜像:

[root@VM-0-12-centos ~]# docker pull greatsql/greatsql
Using default tag: latest
Alatest: Pulling from greatsql/greatsql
a1d0c7532777: Pull complete 
0689c7a54f49: Pull complete 
...
Status: Downloaded newer image for greatsql/greatsql:latest
docker.io/greatsql/greatsql:latest

[root@VM-0-12-centos ~]# docker run -d \
--name greatsql --hostname=greatsql \
-p 3306:3306 -p 33060:33060 -p 33061:33061 \
-e MYSQL_ROOT_PASSWORD='GreatSQL.~#' \
greatsql/greatsql

为需要部署Yearning的服务器中的数据库创建Yearning库,Yearning 不依赖于任何第三方 SQL 审核工具作为审核引擎,内部已自己实现审核/回滚相关逻辑。仅依赖 Mysql 数据库。mysql 版本必须为5.7及以上版本(8.0及以上请将sql_mode 设置为空)并已事先自行安装完毕且创建 Yearning 库,字符集应为 UTF8mb4 。

(Mon Jun 13 16:36:40 2022)[root@GreatSQL][(none)]>CREATE DATABASE Yearning DEFAULT CHARACTER SET utf8mb4;

2.3 安装部署Yearning

创建Yearning的目录:

[root@VM-0-12-centos ~]# mkdir -p /mydata/yearning
[root@VM-0-12-centos ~]# cd /mydata/yearning

下载并解压Yearning:

[root@VM-0-12-centos yearning]# wget https://github.com/cookieY/Yearning/releases/download/2.3.5/Yearning-2.3.5-linux-amd64.zip
[root@VM-0-12-centos yearning]# unzip Yearning-2.3.5-linux-amd64.zip

修改conf.toml文件内容:

[Mysql]
Db = "Yearning"
Host = "127.0.0.1"
Port = "3306"
Password = "GreatSQL.~#"
User = "root"

[General]
# 数据库加解密key,只可更改一次
SecretKey = "dbcjqheupqjsuwsm"
Hours = 4

初始化Yearning,自动分配管理员账号及初始密码,用户名: admin,密码:Yearning_admin:

[root@VM-0-12-centos yearning]# ./Yearning install

(/Users/henryyee/Yearning-go/src/service/migrate.go:32) 
[2022-06-13 17:02:52]  [0.68ms]  INSERT INTO `core_accounts` (`username`,`password`,`rule`,`department`,`real_name`,`email`) VALUES ('admin','pbkdf2_sha256$120000$Ams9FoCyweoY$F5GLRMRRwL8ZW8PAYs6XknhLZ0Up2LLMLAVIw4t1Sd8=','super','DBA','超级管理员','')  
[1 rows affected or returned ] 

(/Users/henryyee/Yearning-go/src/service/migrate.go:40) 
[2022-06-13 17:02:52]  [1.70ms]  INSERT INTO `core_global_configurations` (`authorization`,`ldap`,`message`,`other`,`stmt`,`audit_role`,`board`) VALUES ('global','{"url":"","user":"","password":"","type":"(\u0026(objectClass=organizationalPerson)(sAMAccountName=%s))","sc":"","ldaps":false}','{"web_hook":"","host":"","port":25,"user":"","password":"","to_user":"","mail":false,"ding":false,"ssl":false,"push_type":false,"key":""}','{"limit":"1000","idc":["Aliyun","AWS"],"multi":false,"query":false,"exclude_db_list":[],"insulate_word_list":[],"register":false,"export":false,"per_order":2,"ex_query_time":60,"query_timeout":0}',0,'{"DMLAllowLimitSTMT":false,"DMLInsertColumns":false,"DMLMaxInsertRows":10,"DMLWhere":false,"DMLOrder":false,"DMLSelect":false,"DDLCheckTableComment":false,"DDlCheckColumnComment":false,"DDLCheckColumnNullable":false,"DDLCheckColumnDefault":false,"DDLEnableAcrossDBRename":false,"DDLEnableAutoincrementInit":false,"DDLEnableAutoIncrement":false,"DDLEnableAutoincrementUnsigned":false,"DDLEnableDropTable":false,"DDLEnableDropDatabase":false,"DDLEnableNullIndexName":false,"DDLIndexNameSpec":false,"DDLMaxKeyParts":5,"DDLMaxKey":5,"DDLMaxCharLength":10,"MaxTableNameLen":10,"MaxAffectRows":1000,"MaxDDLAffectRows":0,"SupportCharset":"","SupportCollation":"","CheckIdentifier":false,"MustHaveColumns":"","DDLMultiToSubmit":false,"DDLPrimaryKeyMust":false,"DDLAllowColumnType":false,"DDLImplicitTypeConversion":false,"DDLAllowPRINotInt":false,"DDLEnableForeignKey":false,"DDLTablePrefix":"","DDLColumnsMustHaveIndex":"","DDLAllowChangeColumnPosition":false,"DDLCheckFloatDouble":false,"IsOSC":false,"OscBinDir":"","OscDropNewTable":false,"OscDropOldTable":false,"OscCheckReplicationFilters":false,"OscCheckAlter":false,"OscAlterForeignKeysMethod":"rebuild_constraints","OscMaxLag":1,"OscRecursionMethod":"processlist","OscCheckInterval":1,"OscMaxThreadConnected":25,"OscMaxThreadRunning":25,"OscCriticalThreadConnected":20,"OscCriticalThreadRunning":20,"OscPrintSql":false,"OscChunkSize":0,"OscChunkTime":0.5,"OscSize":0,"AllowCreateView":false,"AllowCreatePartition":false,"AllowSpecialType":false,"PRIRollBack":false,"OscSleep":0,"OscCheckUniqueKeyChange":false,"OscLockWaitTimeout":60}','')  
[1 rows affected or returned ] 

(/Users/henryyee/Yearning-go/src/service/migrate.go:47) 
[2022-06-13 17:02:52]  [0.48ms]  INSERT INTO `core_graineds` (`username`,`group`) VALUES ('admin','["admin"]')  
[1 rows affected or returned ] 

(/Users/henryyee/Yearning-go/src/service/migrate.go:51) 
[2022-06-13 17:02:52]  [1.12ms]  INSERT INTO `core_role_groups` (`name`,`permissions`) VALUES ('admin','{"ddl_source":[],"dml_source":[],"auditor":[],"query_source":[]}')  
[1 rows affected or returned ] 
初始化成功!
 用户名: admin
密码:Yearning_admin

这个时候可以看到Yearning库中已经存放了初始化的数据了:

(Tue Jun 14 14:20:59 2022)[root@GreatSQL][Yearning]>show tables;
+----------------------------+
| Tables_in_Yearning         |
+----------------------------+
| core_accounts              |
| core_auto_tasks            |
| core_data_sources          |
| core_global_configurations |
| core_graineds              |
| core_query_orders          |
| core_query_records         |
| core_role_groups           |
| core_rollbacks             |
| core_sql_orders            |
| core_sql_records           |
| core_workflow_details      |
| core_workflow_tpls         |
+----------------------------+
13 rows in set (0.00 sec)
(Tue Jun 14 14:26:42 2022)[root@GreatSQL][Yearning]>select username,rule,department,real_name from core_accounts;
+----------+-------+------------+-----------------+
| username | rule  | department | real_name       |
+----------+-------+------------+-----------------+
| admin    | super | DBA        | 超级管理员      |
+----------+-------+------------+-----------------+
1 rows in set (0.00 sec)

然后就可以启动Yearning了:

[root@VM-0-12-centos yearning]# ./Yearning run
检查更新.......
数据已更新!

    __  __          
    _ \/ /_________ 
    __  /_  _ \  _ \
    _  / /  __/  __/
    /_/  \___/\___/   yee v0.2.3
-----Easier and Faster-----

3. 简单使用Yearning

Yearning前台页面简洁大方,不仅可以对使用平台的用户进行明确的权限划分还有高可用性的审计功能等,大致总结如下:

  • Yearning内置SQL编辑器可以快捷编辑SQL语句

  • Yearning支持SQL审核流程化工单操作,可以对SQL语句进行检测SQL语句执行SQL回滚

  • Yearning实现推送E-mail工单推送和钉钉webhook机器人工单推送

Yearning在服务器上部署运行后,就可以通过http://服务器ip:8000/#/login进行访问了,默认账号为初始化账号,页面如图:76ffd9e3538e9ca26a131710b151095d.png

登录后进入首页,首页主要展示了Yearning的各项数据,包括现在平台有多少用户,多少数据源(数据库),已经存在多少工单,多少次查询以及登录用户信息等:

0fd0cc81d77a75c9fc9bbd695fc80252.png

首先要确定使用的数据源并设置相应的权限:f0b30f5a1da695d85e07ff6ca22c6368.png

接着就可以开始一些前期设置了,首要的是分配好用户和权限了:3cfb563efaa0a0ad88bd0cb34b426f83.png

742f490a2cabbc711e6eeb6fba757732.png最后简单使用一下功能吧,首先,可以根据数据源和相应权限进行查询操作:897768065c891af7dc70fb1079dae071.png由有权限审核的管理员审核:c73a9882c953a7ba091b1968b24fca67.png就可以进行查询操作了,且有多重功能可供选择,如图:c20ff9c5684b5d38033c8029872eab33.png同时,还可以对数据源进行DQL和DML的操作(工单),工单需要走对应的审批流,审批流需要管理员预先设置:a359658c8707a16432ff1075f794bc97.png接着用户就可以根据设置好的审批流开始进行操作了:f9a60fa69741c4afa0ff3e89238b37ff.png用户自己进行提交前必须经过检查,假如输入错误的语句将无法提交:58aa895fe061efea9668867f73dc6522.png输入语法正确的语句后提交:5fb1db8e8319f0b4b6289dbcd1e02a70.png由流程设置的审批人员进行审核:3aa7424d3efe01eab66fbf5190f4a658.png30ccbdaacafa3aaffb205ca8144393b0.png最后有执行人进行执行:96cca221f277251001fd0b1d38624390.png执行后工单状态修改:f0e37ecec76ed6723a0f0187f38cb65e.png

4. 总结

本文介绍了使用Yearning实现SQL语句的自动检测,但是目前只支持MySQL。

Enjoy GreatSQL :)


《深入浅出MGR》视频课程

戳此小程序即可直达B站

https://www.bilibili.com/medialist/play/1363850082?business=space_collection&business_id=343928&desc=0



文章推荐:


想看更多技术好文,点个“在看”吧! 

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值