一、Inception简介
一款用于MySQL语句的审核的开源工具,不但具备自动化审核功能,同时还具备执行、生成对影响数据的回滚语句功能。
基本架构:
二、Inception安装
1、软件下载
下载链接:https://github.com/mysql-inception/inception
文档链接:http://mysql-inception.github.io/inception-document/
2、安装
(1)依赖包安装
inception相关脚本基于python2,所以我门最好安装python2.X版本,本文是通过python2.6操作
yum -y install cmake libncurses5-dev libssl-dev g++ bison openssl-devel.x86_64
(2)编译
[root@node1 ~]# unzip inception-master.zip
[root@node1 ~]# cd inception-master
[root@node1 ~]# sh inception_build.sh install_dir [Xcode]
##输入参数有两个,第一个是软件编译安装目录,第二个是平台代码,Xcode表示Linux平台,不填表示默认Linux平台
mv install_dir /usr/local/inception
ln -s /usr/local/inception/mysql/Inception /usr/bin/inception
(3)inception配置文件
/etc/inc.cnf
[inception]
general_log=1
general_log_file=/usr/local/inception/log/inception.log
port=6669
socket=/usr/local/inception/inc.socket
character-set-client-handshake=0
character-set-server=utf8
inception_remote_system_user=root
inception_remote_system_password=root123
inception_remote_backup_port=3306
inception_remote_backup_host=10.0.0.1
inception_support_charset=utf8mb4
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/local/inception/data
inception_osc_chunk_time=0.1
inception_enable_blob_type=1
inception_check_column_default_value=1
##特别参数解释
inception_remote_system_user=root ##远程备份数据库用户名
inception_remote_system_password=root123 ##远程备份数据库用户密码
inception_remote_backup_port=3306 ##远程备份数据库端口
inception_remote_backup_host=10.xx.xx.xx ##远程备份数据库IP地址,本次演示例如10.0.0.1
inception_support_charset=utf8mb4,utf8,latin1 ##建表、建库MYSQL支持的字符集类型
##其他配置参考附录
(4)启动
a.启动方式01
inception --defaults-file=/etc/inc.cnf
##最佳启动方式是通过nohup后台启动
nohup inception --defaults-file=/etc/inc.cnf &
b.启动方式02
inception --port=6669
注意: 因为Inception支持OSC执行的功能,是通过调用pt-online-schema-change工具来做的,但如果Inception后台启动(&)的话,可能会导致pt-online-schema-change在执行完成之后,长时间不返回,进而导致Inception卡死的问题,这个问题后面会解决,但现阶段请尽量不要使用后台启动的方式,或者可以使用nohup Inception启动命令 &的方式来启动。
(5)登陆校验
启动成功之后,可以简单试一下看,通过MySQL客户端
mysql -uroot -h127.0.0.1 -P6669
登录上去之后,再执行一个命令:
inception get variables;
输出了所有的变量,恭喜你,已经启动成功了,都说了非常简单。
三、Inception使用
本次业务目标库IP采用10.0.0.2
1、数据库DDL操作
(1)建库
需要指定字符集,不会产生回滚
#!/usr/bin/env python
#--*--coding=utf8--*--
import MySQLdb
sql='''/*--user=root;--password=123456;--host=10.0.0.2;--execute=1;--port=3306;*/\
inception_magic_start;\
create database dbtest01 DEFAULT CHARACTER SET utf8mb4;\ inception_magic_commit;'''
try:
conn=MySQLdb.connect(host='127.0.0.1',user='',passwd='',db='',port=6669)
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 as e:
print "Mysql Error %d: %s" % (e.args[0], e.args[1])
(2)建表
建表操作检查项如下:
表属性的检查项
这个表不存在
对于create table like,会检查like的老表是不是存在。
对于create table db.table,会检查db这个数据库是不是存在
表名、列名、索引名的长度不大于64个字节
如果建立的是临时表,则必须要以tmp为前缀
必须要指定建立innodb的存储引擎(可配置)
必须要指定utf8的字符集(字符串可配置,指定支持哪些字符集)
表必须要有注释(可配置)
表不能建立为分区表(可配置)
只能有一个自增列
索引名字不能是Primay
不支持Foreign key(可配置)
建表时,如果指定auto_increment的值不为1,报错(可配置)
如果自增列的名字不为id,说明有可能是有意义的,MySQL这样使用比较危险,所以报警(可配置)
列属性的检查项
不能设置列的字符集(可配置)
列的类型不能使用集合、枚举、位图类型。(可配置)
列必须要有注释(可配置)
char长度大于20的时候需要改为varchar(长度可配置)
列的类型不能是BLOB/TEXT。(可配置)
每个列都使用not null(可配置)
如果列为BLOB/TEXT类型的,则这个列不能设置为NOT NULL。
如何是自增列,则使用无符号类型(可配置)
如果自增列,则长度必须要大于等于4个字节(可配置)
如果是timestamp类型的,则要必须指定默认值。
对于MySQL5.5版本(包含)以下的数据库,不能同时有两个TIMESTAMP类型的列,如果是DATETIME类型,则不能定义成DATETIME DEFAULT CURRENT_TIMESTAMP及ON UPDATE CURRENT_TIMESTAMP等语句。
每个列都需要定义默认值,除了自增列、主键列及大字段列之外(可配置)
不能有重复的列名
索引属性检查项
索引必须要有名字
不能有外键(可配置)
Unique索引必须要以uniq_为前缀(可配置)
普通索引必须要以idx_为前缀(可配置)
索引的列数不能超过5个(数目可以配置)
表必须要有一个主键(可配置)
最多有5个索引(数目可配置)
建索引时,指定的列必须存在。
索引中的列,不能重复
BLOB列不能建做KEY
索引长度不能超过766
不能有重复的索引,名字及内容
默认值检查项
BLOB/TEXT类型的列,不能有非NULL的默认值
MySQL5.5以下(含)的版本,对于DATETIME类型的列,不能有函数NOW()的默认值。
如果设置默认值为函数,则只能是NOW()。
如果默认值为NULL,但列类型为NOT NULL,或者是主键列,或者定义为自增列,则报错。
自增列不能设置默认值。
python脚本如下