Inception安装部署实现SQL自动审核与上线

2024-04-02 05:18

本文主要是介绍Inception安装部署实现SQL自动审核与上线,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

 

1  Incption概述

1.1  Inception简介

Inception是集审核、执行、回滚于一体的一个自动化运维系统,它可以对提交的所有语句的语法分析,如果语法有问题,都会将相应的错误信息返回给审核者。 还提供SQL语句的执行功能,可执行的语句类型包括常用的DML及DDL语句及truncate table等操作。 Inception在执行 DML时还提供生成回滚语句的功能,对应的操作记录及回滚语句会被存储在备份机器上面,备份机器通过配置Inception参数来指定。

源码地址:https://github.com/mysql-inception/inception

文档地址:http://mysql-inception.github.io/inception-document/

##20181115更新

此篇博文已年代久远,而且已经出现了基于Inception比较好用的web界面操作平台,分别是archer和yearning,下面附上这2个的github地址,读者可以自己选择使用哪个平台。

https://github.com/jly8866/archer

https://github.com/cookieY/Yearning

Inception源地址已没有源码,可以打开源地址,然后在右上角上面的fork处选择新的下载地址。

2  Incption安装

2.1  下载源码包

https://github.com/mysql-inception/inception

download zip

2.2  安装编译时的依赖

yum install gcc gcc-c++ cmake bison openssl-devel ncurses-devel –y

2.3  开始安装

unzip inception-master.zip

cd inception-master

./inception_build.sh debug [Xcode]

2.4  编辑配置文件

vim /etc/inc.cnf

[inception]

general_log=1

general_log_file=inc.log

port=6669

socket=/tmp/inc.socket

character-set-client-handshake=0

character-set-server=utf8

inception_remote_system_password=thunder

inception_remote_system_user=thunder

inception_remote_backup_port=3306

inception_remote_backup_host=127.0.0.1

inception_support_charset=utf8

inception_enable_nullable=0

inception_check_primary_key=1

inception_check_column_comment=1

inception_check_table_comment=1

inception_osc_min_table_size=1

inception_osc_bin_dir=/usr/bin

inception_osc_chunk_time=0.1

inception_ddl_support=1

inception_enable_blob_type=1

inception_check_column_default_value=1

 

#相关配置说明请参见官方文档,在概述中已给出地址

2.5  启动Inception服务,类似mysql启动方式

/opt/thunder/inception-master/debug/mysql/bin/Inception --defaults-file=/etc/inc.cnf &

登陆测试,可通过sock或者端口,上面配置文件中配置的端口为6669

mysql –h127.0.0.1 –P6669 –uroot

mysql> inception get variables;

输出相关变量则安装成功

2.6  用脚本测试相关审核语句

 

#!/usr/bin/python 
import MySQLdb 
sql='/*--user=thunder;--password=thunder;--host=127.0.0.1;--enable-remote-backup=0;--execute;--port=3306;*/\ 
inception_magic_start;\ 
use test;\ 
CREATE TABLE `alifeba_user` (\ 
     `ID` int(11) unsigned NOT NULL auto_increment comment "aaa",\ 
     `username` varchar(50) NOT NULL Default "" comment "aaa",\ 
     `realName` varchar(50) NOT NULL Default "" comment "aaa",\ 
     `age` int(11) NOT NULL Default 0 comment "aaa",\ 
     PRIMARY KEY (`ID`)\ 
)ENGINE=INNODB DEFAULT CHARSET=utf8 COMMENT="AAAA";\ 
inception_magic_commit;' 
try: 
    conn=MySQLdb.connect(host='127.0.0.1',user='',passwd='',db='',port=6669) 
    cursor=conn.cursor() 
    cursor.execute(sql) 
    results = cursor.fetchall() 
    column_name_max_size=max(len(i[0]) for i in cursor.description) 
    row_num=0 
    for result in results: 
        row_num=row_num+1 
        print '*'.ljust(27,'*'),row_num,'.row', '*'.ljust(27,'*') 
        row = map(lambda x, y: (x,y), (i[0] for i in cursor.description), result) 
        for each_column in row: 
            if each_column[0] != 'errormessage': 
                print each_column[0].rjust(column_name_max_size),":",each_column[1] 
            else: 
                print each_column[0].rjust(column_name_max_size),':',each_column[1].replace('\n','\n'.ljust(column_name_max_size+4)) 
    cursor.close() 
    conn.close() 
except MySQLdb.Error,e: 
     print "Mysql Error %d: %s" % (e.args[0], e.args[1]) 

 

 

执行测试脚本

 

python inc.py 
*************************** 1 .row ***************************
           ID : 1
        stage : RERUN
     errlevel : 0
  stagestatus : Execute Successfully
 errormessage : None
          SQL : use test
Affected_rows : 0
     sequence : '1487924011_2298_0'
backup_dbname : None
 execute_time : 0.000
      sqlsha1 : 
*************************** 2 .row ***************************
           ID : 2
        stage : EXECUTED
     errlevel : 0
  stagestatus : Execute Successfully
 errormessage : None
          SQL : CREATE TABLE `alifeba_user` (     `ID` int(11) unsigned NOT NULL auto_increment comment "aaa",     `username` varchar(50) NOT NULL Default "" comment "aaa",     `realName` varchar(50) NOT NULL Default "" comment "aaa",     `age` int(11) NOT NULL Default 0 comment "aaa",     PRIMARY KEY (`ID`))ENGINE=INNODB DEFAULT CHARSET=utf8 COMMENT="AAAA"
Affected_rows : 0
     sequence : '1487924011_2298_1'
backup_dbname : 127_0_0_1_3306_test
 execute_time : 0.010
      sqlsha1 : 

成功

3  inception_web安装

3.1  inception_web简介

inception_web是web封装的界面,可以通过web界面来实现相关功能,用python写的,目前比较简单,后面的需求需要自己开发

源码地址:https://github.com/dbalihui/inception_web

打开地址后,可以看到:

数据库运维平台

已实现功能:

表结构语法审核

环境配置

python2.7 + flask

先安装依赖模块

pip install flask_wtf

pip install flask-script

pip install flask-debugtoolbar

pip install MySQL-python

使用方法

先将app目录中的inception.py文件里的账号密码改成自己的

记得use sql_check这里要改成自己的数据库名字

在inception_web目录下运行./run.py runserver --host 0.0.0.0

使用浏览器访问你的IP http://192.168.xx.xxx:5000/

运行报错的话是因为没有安装flask组件,请使用pipinstall xx安装相关组件

我的系统是python版本为2.6.6所以我们需要先升级python

3.2  升级python到2.7

# wget https://www.python.org/ftp/python/2.7.10/Python-2.7.10.tgz --no-check-certificate

# tar -xvf Python-2.7.10.tar

# cd Python-2.7.10

# ./configure

# make –j 8 && make install

# /usr/local/bin/python -V

# mv /usr/bin/python /usr/bin/python2.6.6

# ln -s /usr/local/bin/python /usr/bin/python

# python -V //验证版本

# vim /usr/bin/yum   //修改#!/usr/bin/python 为#!/usr/bin/python2.6.6,因为yum安装依赖的是2.6版本的python

3.3  安装incpetion_web依赖模块

# yum -y install python-pip   //yum安装的是2.6版本的pip

# wget https://bootstrap.pypa.io/ez_setup.py  --no-check-certificate  //下载一个ez_setup 安装pip2.7版本的

# python2.7 ez_setup.py

# easy_install-2.7 pip

# pip2.7 install flask_wtf

# pip2.7 install flask-script

# pip2.7 install flask-debugtoolbar

# pip2.7 install MySQL-python //如果安装报错yuminstall mysql-devel –y

3.4  修改配置文件

# unzip inception_web-master.zip

# cd inception_web-master/app

# vim inception.py

#coding=utf-8

 

import MySQLdb

 

def table_structure(mysql_structure):

    sql1=' /*--user=thunder;--password=thunder;--host=127.0.0.1;--execute=1;--port=3307;*/\

           inception_magic_start;\

            use test;' //此处为默认选择数据库,如果在web不加use会默认对该库操作,对于生产库请慎重选择,建议最好创建一下与业务无关的库,在执行SQL的时候加上usedbname;

   sql2='inception_magic_commit;'

    sql = sql1 +mysql_structure + sql2

    try:

       conn=MySQLdb.connect(host='127.0.0.1',user='root',passwd='',db='',port=6669,use_unicode=True,charset="utf8")

        cur=conn.cursor()

       ret=cur.execute(sql)

       result=cur.fetchall()

        num_fields =len(cur.description)

        field_names = [i[0]for i in cur.description]

        print field_names

        for row in result:

            print row[0],"|",row[1],"|",row[2],"|",row[3],"|",row[4],"|",row[5],"|",row[6],"|",row[7],"|",row[8],"|",row[9],"|",row[10]

        cur.close()

        conn.close()

    except MySQLdb.Error,e:

        print "MysqlError %d: %s" % (e.args[0], e.args[1])

returnresult[1][4].split("\n")

3.5  启动inception_web

# cd inception_web-master

# ./run.py runserver --host 0.0.0.0

在浏览器中访问服务器http://IP:5000就可以访问页面了

 

4  相关说明

4.1  在页面中进行SQL操作时,如果提示为None,则代表语句执行成功,如果语句有问题,请按相关提示修改SQL,并再次执行

 

4.2  配置了备份库后,语句执行后,会在备份数据库创建一个库名为IP_PORT_DBNAME的库,里面有相关操作时对表的备份以及记录,其中$_$Inception_backup_information$_$表为对该库进行操作的所有记录,可以通过该表来查看对该库名上的数据库做了哪些修改。

mysql> select * from $_$Inception_backup_information$_$\G

*************************** 1. row ***************************

        opid_time:1460022412_6348032_1

start_binlog_file: mysql-bin.000019

 start_binlog_pos: 85611296

  end_binlog_file:mysql-bin.000019

   end_binlog_pos: 85611498

    sql_statement: deletefrom test.test where id=1

             host:192.168.199.58

           dbname: test

        tablename: test

             port: 3307

             time:2016-04-07 17:46:52

             type: DELETE

6 rows in set (0.00 sec)

4.3  如果开启了统计功能,在备份库的inception库中会创建一张名为statistic的表,里面记录了所有执行过的语句类型

mysql> desc statistic;

+--------------+------------+------+-----+-------------------+----------------+

| Field        | Type       | Null | Key | Default           | Extra          |

+--------------+------------+------+-----+-------------------+----------------+

| id           | bigint(20)| NO   | PRI | NULL              | auto_increment |

| optime       |timestamp  | NO   |    | CURRENT_TIMESTAMP |               |

| usedb        |int(11)    | NO   |    | 0                 |                |

| deleting     |int(11)    | NO   |    | 0                 |                |

| inserting    |int(11)    | NO   |    | 0                 |                |

| updating     |int(11)    | NO   |    | 0                 |                |

| selecting    |int(11)    | NO   |    | 0                 |                |

| altertable   |int(11)    | NO   |    | 0                 |                |

| renaming     |int(11)    | NO   |    | 0                 |                |

| createindex  |int(11)    | NO   |    | 0                 |                |

| dropindex    |int(11)    | NO   |    | 0                 |                |

| addcolumn    |int(11)    | NO   |    | 0                 |                |

| dropcolumn   |int(11)    | NO   |    | 0                 |                |

| changecolumn | int(11)   | NO   |     | 0                 |                |

| alteroption  |int(11)    | NO   |    | 0                 |                |

| alterconvert | int(11)   | NO   |     | 0                 |                |

| createtable  |int(11)    | NO   |    | 0                 |                |

| droptable    |int(11)    | NO   |    | 0                 |                |

| createdb     |int(11)    | NO   |    | 0                 |                |

| truncating   |int(11)    | NO   |    | 0                 |                |

+--------------+------------+------+-----+-------------------+----------------+

20 rows in set (0.01 sec)

通过select可以查询出相关操作的条数,方便统计

这篇关于Inception安装部署实现SQL自动审核与上线的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/869071

相关文章

MySQL 多表连接操作方法(INNER JOIN、LEFT JOIN、RIGHT JOIN、FULL OUTER JOIN)

《MySQL多表连接操作方法(INNERJOIN、LEFTJOIN、RIGHTJOIN、FULLOUTERJOIN)》多表连接是一种将两个或多个表中的数据组合在一起的SQL操作,通过连接,... 目录一、 什么是多表连接?二、 mysql 支持的连接类型三、 多表连接的语法四、实战示例 数据准备五、连接的性

使用Python实现IP地址和端口状态检测与监控

《使用Python实现IP地址和端口状态检测与监控》在网络运维和服务器管理中,IP地址和端口的可用性监控是保障业务连续性的基础需求,本文将带你用Python从零打造一个高可用IP监控系统,感兴趣的小伙... 目录概述:为什么需要IP监控系统使用步骤说明1. 环境准备2. 系统部署3. 核心功能配置系统效果展

MySQL中的分组和多表连接详解

《MySQL中的分组和多表连接详解》:本文主要介绍MySQL中的分组和多表连接的相关操作,本文通过实例代码给大家介绍的非常详细,感兴趣的朋友一起看看吧... 目录mysql中的分组和多表连接一、MySQL的分组(group javascriptby )二、多表连接(表连接会产生大量的数据垃圾)MySQL中的

Python实现微信自动锁定工具

《Python实现微信自动锁定工具》在数字化办公时代,微信已成为职场沟通的重要工具,但临时离开时忘记锁屏可能导致敏感信息泄露,下面我们就来看看如何使用Python打造一个微信自动锁定工具吧... 目录引言:当微信隐私遇到自动化守护效果展示核心功能全景图技术亮点深度解析1. 无操作检测引擎2. 微信路径智能获

Python中pywin32 常用窗口操作的实现

《Python中pywin32常用窗口操作的实现》本文主要介绍了Python中pywin32常用窗口操作的实现,pywin32主要的作用是供Python开发者快速调用WindowsAPI的一个... 目录获取窗口句柄获取最前端窗口句柄获取指定坐标处的窗口根据窗口的完整标题匹配获取句柄根据窗口的类别匹配获取句

在 Spring Boot 中实现异常处理最佳实践

《在SpringBoot中实现异常处理最佳实践》本文介绍如何在SpringBoot中实现异常处理,涵盖核心概念、实现方法、与先前查询的集成、性能分析、常见问题和最佳实践,感兴趣的朋友一起看看吧... 目录一、Spring Boot 异常处理的背景与核心概念1.1 为什么需要异常处理?1.2 Spring B

Win安装MySQL8全过程

《Win安装MySQL8全过程》:本文主要介绍Win安装MySQL8全过程,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐教... 目录Win安装mysql81、下载MySQL2、解压文件3、新建文件夹data,用于保存数据库数据文件4、在mysql根目录下新建文件my.ini

Python位移操作和位运算的实现示例

《Python位移操作和位运算的实现示例》本文主要介绍了Python位移操作和位运算的实现示例,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一... 目录1. 位移操作1.1 左移操作 (<<)1.2 右移操作 (>>)注意事项:2. 位运算2.1

如何在 Spring Boot 中实现 FreeMarker 模板

《如何在SpringBoot中实现FreeMarker模板》FreeMarker是一种功能强大、轻量级的模板引擎,用于在Java应用中生成动态文本输出(如HTML、XML、邮件内容等),本文... 目录什么是 FreeMarker 模板?在 Spring Boot 中实现 FreeMarker 模板1. 环

Qt实现网络数据解析的方法总结

《Qt实现网络数据解析的方法总结》在Qt中解析网络数据通常涉及接收原始字节流,并将其转换为有意义的应用层数据,这篇文章为大家介绍了详细步骤和示例,感兴趣的小伙伴可以了解下... 目录1. 网络数据接收2. 缓冲区管理(处理粘包/拆包)3. 常见数据格式解析3.1 jsON解析3.2 XML解析3.3 自定义