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可以查询出相关操作的条数,方便统计

  • 1
    点赞
  • 9
    收藏
    觉得还不错? 一键收藏
  • 6
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 6
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值