您的位置:首页 > 数据库 > MySQL

mysql 的binlog使用(二)

2018-03-17 14:17 169 查看

在binlog中的数据类型

log_nameposeventserveridendlogposinfo
mysql-bin.000025364Query1437begin
mysql-bin.000025437Query1602DELETE FROM **(对应sql)
mysql-bin.000025602Xid1629COMMIT /* xid=48 */
总得来说,虽然结构明显,但是多条数据凑合起来才能算一个操作,不是很好统计;
虽然python-mysql-replication的出现可以稍微处理一下,但是毕竟还是偏底层了一点;

因此我又发现了一个堪称神奇的东西:maxwell’s deamon;

虽然查询资料的时候,老是查到伟大物理学家麦克斯韦的订立,但是这丝毫不影响我继续将这个工具用下去的决心;

应用

标准输出如下:

标准输出
./maxwell --user='root' --password='ZTj8tInzrmXjpt0v'  --host='106.75.147.184' --producer=stdout


它支持很多种消息工具:比如Kafka ,Kinesis , Google Cloud Pub/Sub ,RabbitMQ ,Redis;官网上说,它对kafka的支持是最好的,但是这里我觉得适合自己才是最好的;

一次采用redis的pub,sub;

输出到redis的pub:

输出到redis
./maxwell --user='root' --password='ZTj8tInzrmXjpt0v'  --host='host' --producer=redis --redis_host='redis.hostname' ----redis_pub_channel="first"

--redis_host                        Host of Redis server
--redis_port                        Port of Redis server
--redis_auth                        Authentication key for a password-protected Redis server
--redis_database                    Database of Redis server
--redis_pub_channel                 Redis Pub/Sub channel for publishing records
--redis_list_key                    Redis LPUSH List Key for adding to a queue
--redis_type                        Selects either Redis Pub/Sub or LPUSH. Default to Pub/Sub


处理后的字段含义

“type”:”insert”,
最常见的是你会在这里看到插入/更新/删除。如果你引导一个表格,你会看到“bootstrap-insert”,而DDL语句(稍后解释)有它们自己的类型。

”xid”:23396,
这是InnoDB与该行关联的交易的“交易ID”。在服务器的生命周期内它是唯一的,就我所知。

“server_id”:23042,
接受此事务的服务器的mysql server_id。

“thread_id”:108,
thread_id或多或少是生成数据的客户端连接的唯一标识符。

“commit”:true,

如果您需要在流处理器中重新组装事务,则可以使用此字段并xid执行此操作。数据将如下所示:

没有commit,xid = 142

没有commit,xid = 142

行commit=true,xid = 142

没有commit,xid = 155

插入操作的处理效果

mysql> insert into test.e set m = 4.2341, c = now(3), comment = 'I am a creature of light.';
{
"database":"test",
"table":"e",
"type":"insert",
"ts":1477053217,
"xid":23396,
"commit":true,
"position":"master.000006:800911",
"server_id":23042,
"thread_id":108,
"data":{
"id":1,
"m":4.2341,
"c":"2016-10-21 05:33:37.523000",
"comment":"I am a creature of light."
}
}


更新操作的处理效果:

mysql> update test.e set m = 5.444, c = now(3) where id = 1;
{
"database":"test",
"table":"e",
"type":"update",
"ts":1477053234,
...
"data":{
"id":1,
"m":5.444,
"c":"2016-10-21 05:33:54.631000",
"comment":"I am a creature of light."
},
"old":{
"m":4.2341,
"c":"2016-10-21 05:33:37.523000"
}
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: