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处选择新的下载地址。
https://github.com/mysql-inception/inception
download zip
yum install gcc gcc-c++ cmake bison openssl-devel ncurses-devel –y
unzip inception-master.zip
cd inception-master
./inception_build.sh debug [Xcode]
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
#相关配置说明请参见官方文档,在概述中已给出地址
/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;
输出相关变量则安装成功
#!/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 :
成功
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
# 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
# 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
# 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")
# cd inception_web-master
# ./run.py runserver --host 0.0.0.0
在浏览器中访问服务器http://IP:5000就可以访问页面了
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)
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可以查询出相关操作的条数,方便统计