一、安装
1、下载和编译
依赖包:bison、cmake、ncurses-devel、openssl、gcc-c++
下载源码:
[root@237_30 tmp]# git clone https://github.com/mysql-inception/inception.git
Initialized empty Git repository in /tmp/inception/inception/.git/
remote: Enumerating objects: 2018, done.
remote: Total 2018 (delta 0), reused 0 (delta 0), pack-reused 2018
Receiving objects: 100% (2018/2018), 11.80 MiB | 195 KiB/s, done.
Resolving deltas: 100% (522/522), done.
编译:
[root@237_30 tmp]# cd inception
[root@237_30 inception]# mkdir debug
[root@237_30 inception]# cd debug
[root@237_30 debug]# cmake .. -DWITH_DEBUG=OFF -DWITH_ZLIB=system -DCMAKE_INSTALL_PREFIX=/usr/local/inception -DWITH_SSL=bundled -DCMAKE_BUILD_TYPE=RELEASE -DWITH_ZLIB=bundled
........................
........................
-- Configuring done
-- Generating done
-- Build files have been written to: /tmp/inception/inception/debug
[root@237_30 debug]# make && make install
2、配置文件inc.cnf
[inception]
port=6669
socket=/tmp/inc.socket
general_log=1 #默认为ON,同原生的MySQL参数,用来记录在Inception上执行过的语句
general_log_file=/usrl/local/inception/inception.log
character-set-client-handshake=0
character-set-server=utf8
#Inception在做备份时,备份到的目的数据库的host、port、user(需要具有CREATE、INSERT、SELECT权限)、password如下(没有正确设置会导致inception备份过程中报错,提示:Invalid remote backup information)
inception_remote_backup_host=127.0.0.1
inception_remote_backup_port=3306
inception_remote_system_user=inception_backup_user
inception_remote_system_password=123456
inception_support_charset=utf8mb4
inception_enable_nullable=0 #默认ON,功能是在创建或者新建列时,如果列为NULL,用来控制是否报错。ON不报错、OFF报错
inception_check_primary_key=1 #默认ON,功能是在建表时,如果没有创建主键,设为ON就会报错
inception_check_column_comment=1
inception_check_table_comment=1
inception_osc_min_table_size=1
inception_osc_bin_dir=/data/temp
inception_osc_chunk_time=0.1
inception_enable_blob_type=1 #默认为ON,功能是在建表、修改列、新增列时,如果存在BLOB类型的列,设为ON说明支持BLOB,否则报错
inception_check_column_default_value=1 #默认ON,功能是在建表、修改列、新增列时,用来控制新的列属性是否要有默认值,设为ON则必须要有默认值,否则报错
3、启动
[root@237_30 inception]# ./bin/Inception --defaults-file=inc.cnf
2018-09-26 10:24:12 0 [Note] Welcome to use Inception2.1.50
./bin/Inception: File '/usrl/local/inception/inception.log' not found (Errcode: 2 - No such file or directory)
2018-09-26 10:24:12 10907 [ERROR] Could not use /usrl/local/inception/inception.log for logging (error 2). Turning logging off for the whole duration of the MySQL server process. To turn it on again: fix the cause, shutdown the MySQL server and restart it.
2018-09-26 10:24:12 10907 [Note] Server hostname (bind-address): '*'; port: 6669
2018-09-26 10:24:12 10907 [Note] IPv6 is available.
2018-09-26 10:24:12 10907 [Note] - '::' resolves to '::';
2018-09-26 10:24:12 10907 [Note] Server socket created on IP: '::'.
使用mysql客户端简单登录测试,可以打印出所有inception的变量
[root@237_30 mysql3306]# 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, 2017, 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>
mysql> inception get variables;
+------------------------------------------+-------------------------------------------+
| Variable_name | Value |
+------------------------------------------+-------------------------------------------+
| autocommit | OFF |
| bind_address | * |
| character_set_system | utf8 |
| character_sets_dir | /usr/local/inception/share/charsets/ |
....................
....................
二、简单使用
1、MySQL需要协同配置的参数
1)开启binlog,并设置binlog_row_format=row,binlog_row_image=full,否则不支持Inception的备份回滚功能
2)server_id非0非1
3)需配置inception用户权限
4)autocommit=1
2、执行过程中需要注意的点
1)在Inception中操作的表一定要有主键,否则不会备份(有主键的回滚语句where pk=XXX,假设没有主键的回滚语句where条件写入了每个列,影响空间占用和性能)
2)在执行时,不能将针对同一个表的 DML及 DDL放在一起执行,否则会因为备份解析binlog时由于表结构的变化出现不可预知的错误,如果要有同时执行 DML 及 DDL,则请提前使用enable-split分成多个语句块来执行,否则直接运行Inception 会报错,不会去执行。
3)DDL的回滚仅支持CREATE TABLE、ALTER TABLE、DROP TABLE(inception仅处理定义不处理数据),其他类型的DDL备份需另做规划
3、SQL语句的编写要点
一般,1套Inception需要服务多个线上库,所以在SQL语句前需要额外增加最基本的两组参数:目标数据库的连接信息和实际执行用户信息。
例如:
/*--user=username;--password=xxxxxx;--host=127.0.0.1;--port=3306;*/
[语句块]
语句块组成如下:
inception_magic_start;
【需要执行的SQL们】
inception_magic_commit;
注:
1、use db在inception中也需要加上分号,这点不同于MySQL
2、注释段中其他常规可选参数
1)--enable-check和--enable-execute:不共存参数。前者的功能是告诉inception只做SQL审核,而后者是审核+执行
2)--enable-ignore-warnings:跳过审核时发现的警告
3)--sleep:用于指定执行完每一条语句后暂停多少毫秒,控制对线上数据库的冲击(配合--enable-execute=1才能生效)
4)--enable-remote-backup:默认打开,表明inception默认支持备份并生成对应的回滚语句。但当DBA明确并能把控产生的结果时,可以通过--disable-remote-backup来禁用,从而获取更快的执行效率
5)--enable-split:告诉inception只参与SQL们的DDL和DML的分离,与上述参数均不兼容
3、这一整个语句段需要一次性地通过执行接口提交给Inception,支持的执行接口包括C/C++、Python、PHP等。只有Inception命令(例如 inception get variables)才可以通过mysql客户端进行执行。
4、例:利用Python与Inception进行交互
以以下语句段为例:
/*--user=admin;--password=123456;--host=192.168.237.30;--port=3306;*/
inception_magic_start;
use db1;
create table adaptive_office (id int);
inception_magic_commit;
使用的python程序如下:
如果没有安装python接口包的话,需提前安装。从2.6升级到2.7的同学如果直接yum install MySQL-python,默认会装到2.6上,可以采用源码安装,下载地址:https://dev.mysql.com/downloads/connector/python/
建表小程序:
#!/usr/bin/python
# -*- coding: utf-8 -*-
import MySQLdb
sql='/*--user=admin;--password=123456;--host=192.168.237.30;--execute=1;--port=3306;*/\
inception_magic_start;\
use db1;\
CREATE TABLE adaptive_office(id int);\
inception_magic_commit;'
try:
conn=MySQLdb.connect(host='127.0.0.1',user='',passwd='',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])
执行结果:
[root@237_30 tmp]# python python_create_table.py
['ID', 'stage', 'errlevel', 'stagestatus', 'errormessage', 'SQL', 'Affected_rows', 'sequence', 'backup_dbname', 'execute_time', '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'. |
结果集中会提供一些告警信息,用于SQL优化。按照提示如下修改建表语句
CREATE TABLE adaptive_office(id int comment"testcol" not null default 0,PRIMARY KEY (id))ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 comment="test_table";
重新执行,可以正常执行了,但由于备份账户尚未创建导致无法备份
[root@237_30 tmp]# python python_create_table.py
['ID', 'stage', 'errlevel', 'stagestatus', 'errormessage', 'SQL', 'Affected_rows', 'sequence', 'backup_dbname', 'execute_time', 'sqlsha1']
1 | RERUN | 0 | Execute Successfully | None | 2 | EXECUTED | 2 | Execute Successfully
Backup failed | Backup: Access denied for user 'inception_backup_user'@'127.0.0.1' (using password: YES)
Backup: Access denied for user 'inception_backup_user'@'127.0.0.1' (using password: YES) |
创建相关账户后,为作演示用,重头再来
# 备份库上创建Inception备份账户
grant SELECT,INSERT,UPDATE,DELETE,CREATE,DROP,PROCESS,ALTER,SUPER,REPLICATION SLAVE,REPLICATION CLIENT,TRIGGER on *.* to "inception_backup_user"@"%" identified by '123456';
# 重新运行建库小程序
[root@237_30 tmp]# python python_create_table.py
['ID', 'stage', 'errlevel', 'stagestatus', 'errormessage', 'SQL', 'Affected_rows', 'sequence', 'backup_dbname', 'execute_time', 'sqlsha1']
1 | RERUN | 0 | Execute Successfully | None | 2 | EXECUTED | 0 | Execute Successfully
Backup successfully | None |
# 对象库中成功插入一张表
mysql> use db1;
Database changed
mysql> show tables;
+-----------------+
| Tables_in_db1 |
+-----------------+
| adaptive_office |
| tb1 |
| tb2 |
+-----------------+
3 rows in set (0.00 sec)
# 备份库中
mysql> show databases;
+-------------------------+
| Database |
+-------------------------+
| information_schema |
| 192_168_237_30_3306_db1 |
| db1 |
| inception |
| mysql |
| performance_schema |
| sys |
+-------------------------+
7 rows in set (0.00 sec)
5、输出结果集释疑
Inception在处理完成后,会输出一个结果集,以下描述一下重点字段的意义
ID:表示被审核的语句是属于第几个语句块
stage:NONE、CHECKED、EXECUTED、RERUN。NONE:当前语句没有做过任何处理,可能是前面的语句有语法错误,导致审核提前结束了;CHECKED:表示当前语句只做过审核;EXECUTED:表示当前语句已经被执行过了(执行失败也是这个状态);RERUN:表示对于影响上下文的语句(包括set names charsetname和use database两类)已经执行成功。
errlevel:0、1、2。0:当前语句审核没有问题;1:审核时,Inception发现了一些不符合规范的告警,可以通过设置--enable-ignore-warnings忽略并继续执行;2:审核时,Inception发现了一些严重错误,无论如何都不会通过
stagestatus:Audit completed、Execute Successfully、Execute failed、Backup successfully。分别代表审核完成、执行成功、执行失败,以及备份成功。
需要注意的是,完成一次check之后,在输出的结果集中,会逐行描述每条SQL的审核情况,并且一旦其中有1条SQL有语法错误,则之后的SQL将不会继续审核。
小结:完整的实现一次inception审核提交会经历如下三步骤:
1)enable-check
2)enable-split
3)enable-execute * N,N表示第二步中拆分出来的语句块数量
而实际inception执行过程中,还是要先审核、再执行,最后备份的。
三、备份回滚功能要点
由于备份库与线上库是一对多的关系,为了防止冲突,备份库上的库名的命名规则是:线上库的IP+端口号+库名,以下划线分隔。
例如:192_168_237_30_3306_db1。虽然表名相同,但表结构有很大不同,一般由主键,rollback_statement,opid_time三列组成。
mysql> use 192_168_237_30_3306_db1
Reading table information for completion of table and column names
You can turn off this feature to get a quicker startup with -A
Database changed
mysql>
mysql> show tables;
+------------------------------------+
| Tables_in_192_168_237_30_3306_db1 |
+------------------------------------+
| $_$Inception_backup_information$_$ |
| adaptive_office |
+------------------------------------+
2 rows in set (0.00 sec)
mysql> show create table adaptive_office \G
*************************** 1. row ***************************
Table: adaptive_office
Create Table: CREATE TABLE `adaptive_office` (
`id` bigint(20) NOT NULL AUTO_INCREMENT,
`rollback_statement` mediumtext,
`opid_time` varchar(50) DEFAULT NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=2 DEFAULT CHARSET=utf8
1 row in set (0.00 sec)
rollback_statement:存储了回滚单行所需的SQL语句
opid_time:SQL操作执行序列号,该值相同的行表明是由同一条SQL修改的
此外,在每个备份库中,还存在了一个表:$_$Inception_backup_information$_$,对应了图示中的“backup”,其中记录着所有对当前库中表的操作
四、审核规范
1、查阅及修改已有规范
使用mysql客户端连接inception之后,执行
1)inception get variables; 显示所有可配置参数
2)inception get variables ‘variable_name’ 仅支持精确匹配
3)inception set variable_name=value; 设置变量值
2、使用语法树新增个性化规则
除了inception默认的审核规则外,还可以按需个性化添加。
【待编辑】
五、对OSC的支持
Inception集成了Percona的pt-online-schema-change来处理DDL。
【待编辑】
参考文档: