首页 > 数据库 > 文库 > 正文

Inception相关功能学习

2022-07-30 23:12:14
字体:
来源:转载
供稿:网友
       Inception安装
      下载解压
 
       ]$pwd
 
       /inception
 
       ]$unzip inception-master.zip
 
      安装Inception
      ]$ cd /inception/inception-master/
 
#执行如下命令,可以看到安装帮助
 
]$ sh inception_build.sh
 
Usage: inception_build.sh builddir [platform(linux:Xcode)]
 
EXAPMLE: inception_build.sh debug [Xcode]
 
#安装到./yk目录
 
]$ sh inception_build.sh yk
 
#说明:
 
1.inception_build.sh实际上封装了cmake && make && make install编译安装的步骤
 
2.buliddir是安装目录,platform是所在平台(默认linux)
 
3.每次如果出错之后,需要把编译目录删除掉,重新执行,不然会执行出错。
 
4.编译过程没有err那说明安装成功了
 
配置Inception
#与MySQL类型,可以指定一个cnf配置文件
 
]$ vim /etc/inc.cnf
 
[inception]
 
general_log=1
 
general_log_file=inception.log
 
port=6669
 
socket=/tmp/inc.socket
 
character-set-client-handshake=0
 
character-set-server=utf8
 
。。。。。。。
 
#此处只是简单的配置为了启动Inception服务,更多的配置选项请参考文档
 
启动Inception
]$ cd /inception/inception-master/yk/mysql/bin
 
]$ nohup /Inception --defaults-file=/etc/inc.cnf &
 
]$ netstat -antpl|grep 6669
 
tcp        0      0 0.0.0.0:6669       0.0.0.0:*          LISTEN      4598/Inception   
 
至此,可以看到inception的6669端口已经运行
 
连接Inception
]$ mysql -h227.0.0.1 -P6669
 
Welcome to the MySQL monitor.  Commands end with ; or /g.
 
Your MySQL connection id is 2
 
Server version: Inception2.1.23 1
 
……………………..
 
mysql>  inception get variables; #获取inception所有的变量和值
 
+------------------------------------------+----------------------------------------------------------+
 
| Variable_name             | Value                               |
 
+------------------------------------------+----------------------------------------------------------+
 
| autocommit               | OFF                                 |
 
| bind_address              | *                                   |
 
| character_set_system       | utf8                                 |
 
………………..
 
Inception简单使用
1、  Inception规定,在语句的最开始位置,要加上inception_magic_start;语句,在执行语句块的最后加上inception_magic_commit;语句,这2个语句在 Inception 中都是合法的、具有标记性质的可被正确解析的 SQL 语句。被包围起来的所有需要审核或者执行的语句都必须要在每条之后加上分号,其实就是批量执行SQL语句。(包括use database语句之后也要加分号,这点与 MySQL 客户端不同),不然存在语法错误
 
2、  目前执行只支持通过C/C++接口、Python接口来对Inception访问,这一段必须是一次性的通过执行接口提交给Inception,那么在处理完成之后,Inception会返回一个结果集,来告诉我们这些语句中存在什么错误,或者是完全正常等等
 
 
 
/*--user=yujx;--password=xxxxxxxxxxx;--host=127.0.0.1;--enable-check;--port=3306;*/
 
inception_magic_start;
 
use test;
 
CREATE TABLE yujx(id int);
 
inception_magic_commit;
 
下面是一段执行上面语句的Python程序的例子:
]$ cat inc-mysql.py
 
#!/usr/bin/env python
 
#coding=utf8
 
import MySQLdb
 
sql='''/*--user=yujx;--password=yujx;--host=127.0.0.1;--execute=1;--port=3306;*//
 
    inception_magic_start;/
 
    use test;/
 
    CREATE TABLE yujx(id int comment 'test' primary key) engine=innodb DEFAULT CHARSET=utf8mb4 comment '测试';/
 
    inception_magic_commit;'''
 
#print sql
 
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,e:
 
             print "Mysql Error %d: %s" % (e.args[0], e.args[1])
 
当创建表语句为
CREATE TABLE yujx(id int)
]$ python inc-mysql.py
 
['ID', 'stage', 'errlevel', 'stagestatus', 'errormessage', 'SQL', 'Affected_rows', 'sequence', 'backup_dbname', 'execute_time', 'sqlsha1']
 
1 | CHECKED | 0 | Audit completed | None | use test | 0 | '0_0_0' | None | 0 |
 
2 | CHECKED | 1 | Audit completed | Set engine to innodb for table 'yujx'.
 
Set charset to one of 'utf8mb4' for table 'yujx'.
 
Set comments for table 'yujx'.
 
Column 'id' in table 'yujx' have no comments.
 
Column 'id' in table 'yujx' is not allowed to been nullable.
 
Set Default value for column 'id' in table 'yujx'
 
Set a primary key for table 'yujx'. | CREATE TABLE yujx(id int) | 0 | '0_0_1' | 127_0_0_1_3306_test | 0 |
 
CREATE TABLE yujx(id int) engine=innodb;
]$ python inc-mysql.py
 
['ID', 'stage', 'errlevel', 'stagestatus', 'errormessage', 'SQL', 'Affected_rows', 'sequence', 'backup_dbname', 'execute_time', 'sqlsha1']
 
1 | CHECKED | 0 | Audit completed | None | use test | 0 | '0_0_0' | None | 0 |
 
2 | CHECKED | 1 | Audit completed | Set charset to one of 'utf8mb4' for table 'yujx'.
 
Set comments for table 'yujx'.
 
Column 'id' in table 'yujx' have no comments.
 
Column 'id' in table 'yujx' is not allowed to been nullable.
 
Set Default value for column 'id' in table 'yujx'
 
Set a primary key for table 'yujx'. | CREATE TABLE yujx(id int) engine=innodb | 0 | '0_0_1' | 127_0_0_1_3306_test | 0 |
 
以此类推,直到如下完整的创建语句时,终于成功
 
CREATE TABLE yujx(id int comment 'test' primary key) engine=innodb DEFAULT CHARSET=utf8mb4 comment '测试'
]$ python inc-mysql.py
 
['ID', 'stage', 'errlevel', 'stagestatus', 'errormessage', 'SQL', 'Affected_rows', 'sequence', 'backup_dbname', 'execute_time', 'sqlsha1']
 
1 | RERUN | 0 | Execute Successfully | None | use test | 0 | '1464252128_35_0' | None | 0.000 |
 
2 | EXECUTED | 0 | Execute Successfully | None | CREATE TABLE yujx(id int comment 'test' primary key) engine=innodb DEFAULT CHARSET=utf8mb4 comment '测试' | 0 | '1464252128_35_1' | 127_0_0_1_3306_test | 0.010 |
 
如上,是默认的inception审核规则,用户可以根据自己的实际情况来自定义某些规则
 
Inception操作LOG
]$ tail -f /inception/inception-master/yk/mysql/bin/inception.log     
 
                   24 Query     /*--user=yujx;--password=yujx;--host=127.0.0.1;--execute=1;--port=3306;*/    inception_magic_start;    use test;    CREATE TABLE yujx(id int) engine=innodb;    inception_magic_commit
 
 
 
160526 16:42:08    25 Query     set autocommit=0
 
 
 
                   25 Query     /*--user=yujx;--password=yujx;--host=127.0.0.1;--execute=1;--port=3306;*/    inception_magic_start;    use test;    CREATE TABLE yujx(id int comment 'test' primary key) engine=innodb DEFAULT CHARSET=utf8mb4 comment '测试';    inception_magic_commit
 
 
 
160526 17:13:50    26 Query     select @@version_comment limit 1
 
 
 
160526 17:13:54    26 Query     inception get variables
 
Inception所支持的参数变量
参考:http://mysql-inception.github.io/inception-document/variables/
 
Inception备份功能
相关说明
Inception在做DML操作时,具有备份功能,它会将所有当前语句修改的行备份下来,存储到一个指定的库中,这些库的指定需要通过几个参数,它们分别是:
 
inception_remote_backup_host //远程备份库的host
 
inception_remote_backup_port //远程备份库的port
 
inception_remote_system_user //远程备份库的一个用户
 
inception_remote_system_password //上面用户的密码
 
1.这些参数可以直接在命令行中指定,也可以在inc.cnf配置文件中指定。修改这些参数需要重启inception服务。
 
2.Inception默认是开启备份功能的,可以使用参数--disable-remote-backup=1来关闭备份功能
 
3.被影响的表如果没有主键的话,就不会做备份
 
4.Inception备份用户必须要具备CREATE、INSERT。CREATE权限用于创建表或者库的,INSERT权限用于插入备份数据的,其它的权限不需要(也许是暂时的)
 
5.备份数据在备份机器的存储,是与线上被修改库一对一的。但因为机器的多(线上机器有很多)对一(备份机器只有一台),所以为了防止库名的冲突,备份机器的库名组成是由线上机器的 IP 地址的点换成下划线,再加上端口号,再加上库名三部分,这三部分也是通过下划线连接起来的,如:10_103_11_242_3306_test
 
6.对线上配置需求
 
线上服务器必须要打开 binlog,在启动时需要设置参数log_bin、log_bin_index等关于 binlog 的参数。不然不会备份及生成回滚语句。
 
参数binlog_format必须要设置为 mixed 或者 row 模式,通过语句: set global binlog_format=mixed/row 来设置,如果是 statement 模式,则不做备份及回滚语句的生成。
 
参数 server_id 必须要设置为非0及非1,通过语句:set global server_id=server_id;来设置,不然在备份时会报错。
 
线上服务器一定要有指定用户名的权限,这个是在语句前面的注释中指定的,做什么操作就要有什么权限,否则还是会报错,如果需要执行的功能,则要有线上执行语句的权限,比如DDL及DML,同时如果要执行inception show 等远程命令的话,有些语句是需要特殊权限的,这些权限也是需要授予的,关于权限这个问题,因为一般Inception是运行在一台固定机器上面的,那么在选项中指定的用户名密码,实际上是Inception机器对线上数据库访问的权限,所以建议在使用过程中,使用专门固定的帐号来让Inception使用,最好是一个只读一个可写的即可,这样在执行时用可写,审核或者查看线上状态或者表库结果时用只读即可,这样更安全。

(编辑:错新网)

发表评论 共有条评论
用户名: 密码:
验证码: 匿名发表

图片精选