Inception安装及使用


摘要:Inception安装

下载

源码地址:https://github.com/mysql-inception/inception
参考文档:http://mysql-inception.github.io/inception-document/install/

安装

1、安装相关依赖包

yum install bison ncurses-libs libncurses5-dev ncurses-devel cmake openssl gcc-c++ -y

2、安装inception

unzip inception-master.zip
cd inception-master
sh inception_build.sh debug

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

[root@mysql3 sql]# pwd
/src/inception-master/debug/sql
[root@mysql3 sql]# ls
CMakeFiles dummy.bak Inception libbinlog.a librpl.a libsql.a Makefile sql_builtin.cc sql_yacc.h
cmake_install.cmake gen_lex_hash lex_hash.h libmaster.a libslave.a libsqlgunitlib.a share sql_yacc.cc
[root@mysql3 sql]#

3、创建配置目录,将可执行文件Inception拷贝到/opt/inception/bin目录
[root@mysql3 sql]# mkdir -p /opt/inception
[root@mysql3 sql]# mkdir -p /opt/inception/bin
[root@mysql3 sql]# mkdir -p /opt/inception/temp
[root@mysql3 sql]# cp Inception /opt/inception/bin/
编辑配置文件:

[root@mysql3 sql]# vi /etc/inc.cnf
[inception]
general_log=1
general_log_file=/opt/inception/inception.log
port=6669
socket=/opt/inception/inc.socket
character-set-client-handshake=0
character-set-server=utf8
inception_remote_system_password=123456
inception_remote_system_user=root
inception_remote_backup_port=3306
inception_remote_backup_host=127.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=/opt/inception/temp
inception_osc_chunk_time=0.1
inception_ddl_support=1
inception_enable_blob_type=1
inception_check_column_default_value=1

启动

[root@mysql3 sql]# nohup /opt/inception/bin/Inception --defaults-file=/etc/inc.cnf &
[1] 2398
[root@mysql3 sql]# nohup: ignoring input and appending output to `nohup.out'
[root@mysql3 sql]# ps -ef|grep Inception
root 2414 23520 0 23:15 pts/0 00:00:00 /opt/inception/bin/Inception --defaults-file=/etc/inc.cnf
root 2419 23520 0 23:22 pts/0 00:00:00 grep Inception

登录

[root@mysql3 sql]# 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) 2009-2014 Percona LLC and/or its affiliates
Copyright (c) 2000, 2014, 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 | /src/inception-master/debug/sql/share/charsets/ |
| connect_timeout | 10 |
| date_format | %Y-%m-%d |
| datetime_format | %Y-%m-%d %H:%i:%s |
| general_log | ON |
| general_log_file | /opt/inception/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 | ON |
| inception_enable_autoincrement_unsigned | ON |
| inception_enable_blob_type | ON |
| inception_enable_column_charset | OFF |
| inception_enable_enum_set_bit | OFF |
| inception_enable_foreign_key | OFF |
| inception_enable_identifer_keyword | OFF |
| inception_enable_not_innodb | OFF |
| inception_enable_nullable | OFF |
| inception_enable_orderby_rand | OFF |
| inception_enable_partition_table | OFF |
| inception_enable_pk_columns_only_int | OFF |
| inception_enable_select_star | OFF |
| inception_enable_sql_statistic | ON |
| inception_max_char_length | 16 |
| inception_max_key_parts | 5 |
| inception_max_keys | 16 |
| inception_max_primary_key_parts | 5 |
| inception_max_update_rows | 10000 |
| inception_merge_alter_table | ON |
| inception_osc_alter_foreign_keys_method | none |
| inception_osc_bin_dir | /opt/inception/temp |
| inception_osc_check_alter | ON |
| inception_osc_check_interval | 5.000000 |
| inception_osc_check_replication_filters | ON |
| inception_osc_chunk_size | 1000 |
| inception_osc_chunk_size_limit | 4.000000 |
| inception_osc_chunk_time | 0.100000 |
| inception_osc_critical_thread_connected | 1000 |
| inception_osc_critical_thread_running | 80 |
| inception_osc_drop_new_table | ON |
| inception_osc_drop_old_table | ON |
| inception_osc_max_lag | 3.000000 |
| inception_osc_max_thread_connected | 1000 |
| inception_osc_max_thread_running | 80 |
| inception_osc_min_table_size | 1 |
| inception_osc_on | ON |
| inception_osc_print_none | ON |
| inception_osc_print_sql | ON |
| inception_osc_recursion_method | processlist |
| inception_password | |
| inception_read_only | OFF |
| inception_remote_backup_host | 127.0.0.1 |
| inception_remote_backup_port | 3306 |
| inception_remote_system_password | *6BB4837EB74329105EE4568DDA7DC67ED2CA2AD9 |
| inception_remote_system_user | root |
| inception_support_charset | utf8mb4 |
| inception_user | |
| interactive_timeout | 28800 |
| max_allowed_packet | 1073741824 |
| max_connect_errors | 100 |
| max_connections | 151 |
| net_buffer_length | 16384 |
| net_read_timeout | 30 |
| net_write_timeout | 60 |
| port | 6669 |
| query_alloc_block_size | 8192 |
| query_prealloc_size | 8192 |
| socket | /opt/inception/inc.socket |
| thread_handling | one-thread-per-connection |
| thread_stack | 262144 |
| time_format | %H:%i:%s |
| version | Inception2.1.50 |
| version_comment | Source distribution |
| version_compile_machine | x86_64 |
| version_compile_os | Linux |
| wait_timeout | 28800 |
+------------------------------------------+-------------------------------------------------+
90 rows in set (0.00 sec)
mysql> exit
Bye
[root@mysql3 sql]#

可以看到所有的变量,安装成功!

使用Inception

DDL审核

[root@mysql ~]# cat inception.py
#!/usr/bin/python
#-\*-coding: utf-8-\*-
import pymysql
sql='/*--user=testdev;--password=testdev;--host=192.168.1.3;--execute=1;--port=3306;*/\
inception_magic_start;\
use testdb;\
CREATE TABLE adaptive_office(id int);\
inception_magic_commit;'
try:
conn=pymysql.connect(host='192.168.1.123',user='root',password='123456',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 pymysql.Error,e:
print "Mysql Error %d: %s" % (e.args[0], e.args[1])
[root@mysql ~]#

运行结果:

[root@mysql ~]# python inception.py
[u'ID', u'stage', u'errlevel', u'stagestatus', u'errormessage', u'SQL', u'Affected_rows', u'sequence', u'backup_dbname', u'execute_time', u'sqlsha1']
1 | CHECKED | 0 | Audit completed | None | 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'. |

可以看到输出了审核结果!

DML执行及备份

[root@mysql ~]# cat inception.py
#!/usr/bin/python
#-\*-coding: utf-8-\*-
import pymysql
sql='/*--user=root;--password=testdba;--host=192.168.1.3;--execute=1;--port=3306;*/\
inception_magic_start;\
use testdb;\
update t1 set b1=88 where a1=8;\
inception_magic_commit;'
try:
conn=pymysql.connect(host='192.168.1.123',user='root',password='123456',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 pymysql.Error,e:
print "Mysql Error %d: %s" % (e.args[0], e.args[1])
[root@mysql ~]# python inception.py
[u'ID', u'stage', u'errlevel', u'stagestatus', u'errormessage', u'SQL', u'Affected_rows', u'sequence', u'backup_dbname', u'execute_time', u'sqlsha1']
1 | RERUN | 0 | Execute Successfully | None | 2 | EXECUTED | 0 | Execute Successfully
Backup successfully | None |

SQL执行、备份成功,查看备份语句:

root@testdb 05:28:14>use 192_168_1_3_3306_testdb
Database changed
root@192_168_1_3_3306_testdb 05:28:57>show tables;
+------------------------------------+
| Tables_in_192_168_1_3_3306_testdb |
+------------------------------------+
| $_$inception_backup_information$_$ |
| t1 |
+------------------------------------+
2 rows in set (0.00 sec)
root@192_168_1_3_3306_testdb 05:28:59>desc $_$inception_backup_information$_$;
+-------------------+--------------+------+-----+-------------------+-----------------------------+
| Field | Type | Null | Key | Default | Extra |
+-------------------+--------------+------+-----+-------------------+-----------------------------+
| opid_time | varchar(50) | NO | PRI | | |
| start_binlog_file | varchar(512) | YES | | NULL | |
| start_binlog_pos | int(11) | YES | | NULL | |
| end_binlog_file | varchar(512) | YES | | NULL | |
| end_binlog_pos | int(11) | YES | | NULL | |
| sql_statement | text | YES | | NULL | |
| host | varchar(64) | YES | | NULL | |
| dbname | varchar(64) | YES | | NULL | |
| tablename | varchar(64) | YES | | NULL | |
| port | int(11) | YES | | NULL | |
| time | timestamp | NO | | CURRENT_TIMESTAMP | on update CURRENT_TIMESTAMP |
| type | varchar(20) | YES | | NULL | |
+-------------------+--------------+------+-----+-------------------+-----------------------------+
12 rows in set (0.00 sec)
root@192_168_1_3_3306_testdb 05:30:14>select sql_statement from $_$inception_backup_information$_$;
+--------------------------------+
| sql_statement |
+--------------------------------+
| update t1 set b1=8 where a1=8 |
+--------------------------------+
1 row in set (0.00 sec)
root@192_168_1_3_3306_testdb 05:30:37>

遇到报错及解决

调用inception接口出错

[root@mysql ~]# python inception.py
Traceback (most recent call last):
File "inception.py", line 10, in <module>
conn=pymysql.connect(host='192.168.1.123',user='root',password='123456',db='',port=6669)
File "/usr/local/lib/python2.7/site-packages/pymysql/__init__.py", line 88, in Connect
return Connection(*args, **kwargs)
File "/usr/local/lib/python2.7/site-packages/pymysql/connections.py", line 679, in __init__
self.connect()
File "/usr/local/lib/python2.7/site-packages/pymysql/connections.py", line 891, in connect
self._request_authentication()
File "/usr/local/lib/python2.7/site-packages/pymysql/connections.py", line 1054, in _request_authentication
if int(self.server_version.split('.', 1)[0]) >= 5:
ValueError: invalid literal for int() with base 10: 'Inception2'

解决办法:

[root@mysql ~]# vi /usr/local/lib/python2.7/site-packages/pymysql/connections.py
... ...
def _request_authentication(self):
# https://dev.mysql.com/doc/internals/en/connection-phase-packets.html#packet-Protocol::HandshakeResponse
self.server_version = '5.6.24-72.2-log' #添加此行
if int(self.server_version.split('.', 1)[0]) >= 5:
... ...

执行DML语句不备份

仔细阅读inception对线上配置需求文档
http://mysql-inception.github.io/inception-document/requires/
首先检查是否开启了binlog:

root@testdb 04:57:22>show variables like '%log_bin%';
+---------------------------------+-------+
| Variable_name | Value |
+---------------------------------+-------+
| log_bin | OFF |
| log_bin_basename | |
| log_bin_index | |
| log_bin_trust_function_creators | ON |
| log_bin_use_v1_row_events | OFF |
| sql_log_bin | ON |
+---------------------------------+-------+
6 rows in set (0.00 sec)

因为是个测试库,受限于磁盘空间所以当时没有开启binlog。开启binlog后继续执行inception又遇到如下报错:

[root@mysql ~]# python inception.py
/usr/local/lib/python2.7/site-packages/pymysql/cursors.py:158: Warning: None
result = self._query(query)
[u'ID', u'stage', u'errlevel', u'stagestatus', u'errormessage', u'SQL', u'Affected_rows', u'sequence', u'backup_dbname', u'execute_time', u'sqlsha1']
1 | RERUN | 0 | Execute Successfully | None | 2 | EXECUTED | 2 | Execute Successfully
Backup failed | Backup: Access denied for user 'dba'@'192.168.1.123' (using password: YES)
Backup: Access denied for user 'dba'@'192.168.1.123' (using password: YES) |

给dba用户赋予create\insert权限:

[root@mysql ~]# python inception.py
[u'ID', u'stage', u'errlevel', u'stagestatus', u'errormessage', u'SQL', u'Affected_rows', u'sequence', u'backup_dbname', u'execute_time', u'sqlsha1']
1 | RERUN | 0 | Execute Successfully | None | 2 | EXECUTED | 0 | Execute Successfully
Backup successfully | None |
[root@mysql ~]#

文章目录
  1. 1. 下载
  2. 2. 安装
  3. 3. 启动
  4. 4. 登录
  5. 5. 使用Inception
    1. 5.1. DDL审核
    2. 5.2. DML执行及备份
  6. 6. 遇到报错及解决
    1. 6.1. 调用inception接口出错
    2. 6.2. 执行DML语句不备份
|