centos7部署Inception

Posted by 二田 on 2018-09-10

环境介绍

Centos7.4

Python3.6

依赖包安装

  • 基础组件
1
$ yum install cmake ncurses-devel gcc gcc-c++ openssl-devel ncurses-libs wget git
  • bison2.5.1部署

    (注意:centos7 系统自带的bison的版本过高,在后面测试的时候会报错!官方推荐2.6以下版本)

1
2
3
4
5
$ wget http://ftp.gnu.org/gnu/bison/bison-2.5.1.tar.gz
$ tar zxvf bison-2.5.1.tar.gz
$ cd bison-2.5.1
$ ./configure
$ make && make install

Inception部署

1
2
3
4
$ cd /data/soft/
$ git clone https://github.com/mysql-inception/inception
$ cd inception
$ sh inception_build.sh debug

在一堆警告中完成安装。 :( 不影响最后使用

debug就是编译目录,编译后所有的生成文件都在这个目录下面,包括可执行文件Inception。可执行文件在debug/sql目录下面:

  • 创建目录
1
2
$ mkdir /data/app/inception/{temp,log,bin} -p
$ cp /data/soft/inception/debug/sql/Inception /data/app/inception/bin/

配置

编译完成之后,就是使用了,那么需要一个配置文件(inc.cnf):

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
[inception]
general_log=1
general_log_file=/data/app/inception/log/inception.log
port=6669
socket=/data/mysql/data/mysql.sock
character-set-client-handshake=0
character-set-server=utf8
inception_remote_system_password=your_password
inception_remote_system_user=root
inception_remote_backup_port=3306
inception_remote_backup_host=localhost
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=/data/app/inception/temp/
inception_osc_chunk_time=0.1
inception_enable_blob_type=1
inception_check_column_default_value=1
  • 目录结果
1
2
3
4
5
6
7
[root@10-7-95-219-middleware:/data/app/inception]$ tree -L 2
.
├── bin
│   └── Inception
├── inc.conf
├── log
└── temp

启动服务

1
2
3
4
5
6
7
$ nohup /data/app/inception/bin/Inception --defaults-file=/data/app/inception/inc.conf &
$ cat nohup.out
2018-09-10 17:18:53 0 [Note] Welcome to use Inception2.1.50
2018-09-10 17:18:53 9390 [Note] Server hostname (bind-address): '*'; port: 6669
2018-09-10 17:18:53 9390 [Note] IPv6 is available.
2018-09-10 17:18:53 9390 [Note] - '::' resolves to '::';
2018-09-10 17:18:53 9390 [Note] Server socket created on IP: '::'.

验证

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
$ mysql -uroot -h127.0.0.1 -P6669

Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 1
Server version: Inception2.1.50 1

Copyright (c) 2000, 2018, Oracle and/or its affiliates. All rights reserved.

Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.

Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.

mysql> inception get variables;
+------------------------------------------+-------------------------------------------+
| Variable_name | Value |
+------------------------------------------+-------------------------------------------+
| autocommit | OFF |
| bind_address | * |
| character_set_system | utf8 |
| character_sets_dir | /data/app/inception/share/charsets/ |
| connect_timeout | 10 |
| date_format | %Y-%m-%d |
| datetime_format | %Y-%m-%d %H:%i:%s |
| general_log | ON |
| general_log_file | /data/app/inception/log/inception.log |
| inception_check_autoincrement_datatype | ON |
| inception_check_autoincrement_init_value | ON |
| inception_check_autoincrement_name | ON |
| inception_check_column_comment | ON |
| inception_check_column_default_value | ON |
| inception_check_dml_limit | ON |
| inception_check_dml_orderby | ON |
| inception_check_dml_where | ON |
| inception_check_identifier | ON |
| inception_check_index_prefix | ON |
| inception_check_insert_field | ON |
| inception_check_primary_key | ON |
| inception_check_table_comment | ON |
| inception_check_timestamp_default | ON |
| inception_ddl_support | OFF |

验证

目前执行只支持通过C/C++接口、Python接口来对Inception访问,这一段必须是一次性的通过执行接口提交给Inception,那么在处理完成之后,Inception会返回一个结果集

Inception支持格式

1
2
3
4
5
6
/*--user=zhufeng;--password=xxxxxxxxxxx;--host=xxxxxxxxxx;
--enable-check;--port=3456;*/
inception_magic_start;
use mysql;
CREATE TABLE adaptive_office(id int);
inception_magic_commit;

连接信息用/**/扩起来,然后具体sql语句在inception_magic_start 和inception_magic_commit;之间。

下面是一段执行上面语句的Python程序的例子:


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
#!/usr/bin/python
#-\*-coding: utf-8-\*-
import MySQLdb
sql='/*--user=username;--password=password;--host=127.0.0.1;--execute=1;--port=3306;*/\
inception_magic_start;\
use mysql;\
CREATE TABLE adaptive_office(id int);\
inception_magic_commit;'
try:
conn=MySQLdb.connect(host='127.0.0.1',user='',passwd='',db='',port=9998)
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 "Mysql Error %d: %s" % (e.args[0], e.args[1])

执行这段程序之后,返回的结果如下:

1
2
3
4
5
6
7
8
9
10
11
['ID', 'stage', 'errlevel', 'stagestatus', 'errormessage', 'SQL', 'Affected_rows', 
'sequence', 'backup_dbname', 'execute_time', 'sqlsha1']
1 | CHECKED | 0 | Audit completed | None | use mysql | 0 | '0_0_0' | None | 0 |
2 | CHECKED | 1 | Audit completed | Set engine to innodb for table 'adaptive_office'.
Set charset to one of 'utf8mb4' for table 'adaptive_office'.
Set comments for table 'adaptive_office'.
Column 'id' in table 'adaptive_office' have no comments.
Column 'id' in table 'adaptive_office' is not allowed to been nullable.
Set Default value for column 'id' in table 'adaptive_office'
Set a primary key for table 'adaptive_office'. | CREATE TABLE adaptive_office(id int)
| 0 | '0_0_1' | 127_0_0_1_3306_mysql | 0|

从返回结果可以看到,每一行语句的审核及执行信息,最前面打印的是field_names,表示Inception的返回结果集的列名信息,总共包括十个列,下面是每个列对应的结果,因为只有两个语句,则只有两行,从结果集第一个列看到只有序号为1和2的两行,而对于每一个列的具体含义。