【DB】 Inception安装及配置
1. 安装依赖包
# rpm -qa | grep -E '^(cmake|ncurses|openssl|bison|gcc-c++)' # yum install -y cmake ncurses-devel openssl-devel bison-devel gcc-c++ MySQL-python
2. 安装Inception
o 下载软件包
# wget https://github.com/weiyanwei412/SQLaudit/archive/master.zip # unzip master.zip # mv SQLaudit-master /opt/inception-master # cd /opt/inception-master
o 编译安装
# cmake -DWITH_DEBUG=OFF \ -DCMAKE_INSTALL_PREFIX=/usr/local/inception \ -DMYSQL_DATADIR=/usr/local/inception/data \ -DWITH_SSL=yes \ -DCMAKE_BUILD_TYPE=RELEASE \ -DWITH_ZLIB=bundled \ -DMY_MAINTAINER_C_WARNINGS="-Wall -Wextra -Wunused -Wwrite-strings -Wno-strict-aliasing -Wdeclaration-after-statement" \ -DMY_MAINTAINER_CXX_WARNINGS="-Wall -Wextra -Wunused -Wwrite-strings -Wno-strict-aliasing -Wno-unused-parameter -Woverloaded-virtual" # make && make install
o 配置文件
# vi /etc/inc.cnf # doc: https://github.com/weiyanwei412/inception-document/blob/master/docs/variables.md [inception] port=6669 socket=/usr/local/inception/data/inc.socket character-set-server=utf8 general_log=1 #启用Inception语句执行记录 general_log_file=/usr/local/inception/data/inception.log #Inception 审核规则 inception_check_autoincrement_datatype=1 #建表时,自增列的类型不为int或者bigint inception_check_autoincrement_init_value=1 #建表时,自增列的值指定的不为1,则报错 inception_check_autoincrement_name=1 #建表时,如果指定的自增列的名字不为ID inception_check_column_comment=1 #建表时,列没有注释 inception_check_column_default_value=0 #检查在建表、修改列、新增列时,列属性是否有默认值 inception_check_dml_limit=1 #在DML语句中使用了LIMIT inception_check_dml_orderby=1 #在DML语句中使用了Order By inception_check_dml_where=1 #在DML语句中没有WHERE条件 inception_check_identifier=1 #SQL语句名字检查,如果发现名字中存在除数字、字母、下划线之外的字符时,会报Identifier "invalidname" is invalid, valid options: [a-z,A-Z,0-9,_]. inception_check_index_prefix=1 #是否检查索引名字前缀为"idx_",检查唯一索引前缀是不是"uniq_" inception_check_insert_field=1 #是否检查插入语句中的列链表的存在性 inception_check_primary_key=1 #检查是否有主键 inception_check_table_comment=0 #检查表是否有注释 inception_check_timestamp_default=0 #检查表是否有timestamp类型指定默认值 inception_enable_autoincrement_unsigned=1 #检查自增列是不是为无符号型 inception_enable_blob_type=0 #检查是不是支持BLOB字段,包括建表、修改列、新增列操作 默认开启 inception_enable_column_charset=0 #允许列自己设置字符集 inception_enable_enum_set_bit=0 #是否支持enum,set,bit数据类型 inception_enable_foreign_key=0 #是否支持外键 inception_enable_identifer_keyword=0 #SQL语句是否有标识符被写成MySQL的关键字 inception_enable_not_innodb=0 #存储引擎是否指定为Innodb inception_enable_nullable=0 #创建或者新增列是否允许为NULL inception_enable_orderby_rand=0 #是否允许order by rand inception_enable_partition_table=0 #是否支持分区表 inception_enable_select_star=0 #是否允许 Select* inception_enable_sql_statistic=1 #备库实例是否存储sql统计信息 inception_max_char_length=16 #当char类型的长度大于这个值时,是否提示转换为VARCHAR inception_max_key_parts=5 #一个索引中,列的最大个数,超过这个数目则报错 inception_max_keys=16 #一个表中,最大的索引数目,超过这个数则报错 inception_max_update_rows=10000 #在一个修改语句中,预计影响的最大行数,超过这个数就报错 inception_merge_alter_table=1 #在多个改同一个表的语句出现是,是否提示合成一个 #inception 支持 OSC 参数(pt-online-schema-change) inception_osc_bin_dir=/usr/bin #用于指定pt-online-schema-change脚本的位置,不可修改,在配置文件中设置 inception_osc_check_interval=5 #对应OSC参数--check-interval,意义是Sleep time between checks for --max-lag. inception_osc_chunk_size=1000 #对应OSC参数--chunk-size inception_osc_chunk_size_limit=4 #对应OSC参数--chunk-size-limit inception_osc_chunk_time=0.1 #对应OSC参数--chunk-time inception_osc_critical_thread_connected=1000 #对应参数--critical-load中的thread_connected部分 inception_osc_critical_thread_running=80 #对应参数--critical-load中的thread_running部分 inception_osc_drop_new_table=1 #对应参数--[no]drop-new-table inception_osc_drop_old_table=1 #对应参数--[no]drop-old-table inception_osc_max_lag=3 #对应参数--max-lag inception_osc_max_thread_connected=1000 #对应参数--max-load中的thread_connected部分 inception_osc_max_thread_running=80 #对应参数--max-load中的thread_running部分 inception_osc_min_table_size=0 # 这个参数实际上是一个OSC的开关,如果设置为0,则全部ALTER语句都走OSC,如果设置为非0,则当这个表占用空间大小大于这个值时才使用OSC方式。单位为M,这个表大小的计算方式是通过语句:"select (DATA_LENGTH + INDEX_LENGTH)/1024/1024 from information_schema.tables where table_schema = 'dbname' and table_name = 'tablename'"来实现的 inception_osc_on=0 #一个全局的OSC开关,默认是打开的,如果想要关闭则设置为OFF,这样就会直接修改 inception_osc_print_none=1 #用来设置在Inception返回结果集中,对于原来OSC在执行过程的标准输出信息是不是要打印到结果集对应的错误信息列中,如果设置为1,就不打印,如果设置为0,就打印。而如果出现错误了,则都会打印 inception_osc_print_sql=1 #对应参数--print #备份服务器信息,记录用于回滚(需要权限 CREATE、INSERT,只备份更改的数据) #线上同名库可能冲突,所有此备库命名规则如:IP_Port_dbname,里面的表与对应线上表都是一一对应的 inception_remote_backup_host=10.10.10.10 inception_remote_backup_port=3307 inception_remote_system_user=root inception_remote_system_password=mysql inception_support_charset=utf8
o 启动服务
# nohup /usr/local/inception/bin/Inception --defaults-file=/etc/inc.cnf & # /usr/local/inception/bin/mysql -h192.168.8.151 -P6669 Welcome to the MySQL monitor. Commands end with ; or \g. Copyright (c) 2000, 2013, 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 | /opt/inception-master/share/charsets/ | | connect_timeout | 10 | | date_format | %Y-%m-%d | | datetime_format | %Y-%m-%d %H:%i:%s | | general_log | ON | | general_log_file | /usr/local/inception/data/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 | OFF | | 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 | OFF | | inception_check_timestamp_default | OFF | | inception_ddl_support | OFF | | inception_enable_autoincrement_unsigned | ON | | inception_enable_blob_type | OFF | | 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 | /usr/bin | | 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 | 0 | | inception_osc_on | OFF | | 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 | 192.168.8.151 | | inception_remote_backup_port | 3307 | | inception_remote_system_password | *E74858DB86EBA20BC33D0AECAE8A8108C56B17FA | | inception_remote_system_user | root | | inception_support_charset | utf8 | | 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 | /usr/local/inception/data/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.01 sec) mysql> inception get variables 'connect_timeout'; +-----------------+-------+ | Variable_name | Value | +-----------------+-------+ | connect_timeout | 10 | +-----------------+-------+ 1 row in set (0.00 sec) mysql> inception set connect_timeout=15; Query OK, 0 rows affected (0.00 sec) mysql>exit
3. 验证Inception
o 编写Python接口
#!/usr/bin/python # -*- coding: utf-8 -*- # Python 2.7.5 import MySQLdb sql = """/*--user=root;--password=mysql;--host=192.168.8.151;--enable-check;--port=3306;*/ inception_magic_start; use test; CREATE TABLE inc_table(id int); inception_magic_commit;""" try: conn = MySQLdb.connect(host='192.168.8.151', 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, e: print "Mysql Error %d: %s" % (e.args[0], e.args[1])
浙公网安备 33010602011771号