PHP解析MySQL Binlog,依赖于mysql-replication-listener库 
详见:https://github.com/bullsoft/php-binlog

Install MySQL Replication Listener

unzip mysql-replication-listener-master.zip
cd mysql-replication-listener-master
cmake . -DCMAKE_INSTALL_PREFIX=/usr/local/mysql-replication
make & make install
  • 1
  • 2
  • 3
  • 4
  • 1
  • 2
  • 3
  • 4

Install php-binlog

unzip php-binlog-master.zip
cd php-binlog-master/ext
/usr/local/php5.5.15/bin/phpize
./configure --with-php-config=/usr/local/php5.5.15/bin/php-config --with-mysql-binlog=/usr/local/mysql-replication
  • 1
  • 2
  • 3
  • 4
  • 1
  • 2
  • 3
  • 4

Examples

注:Binlog为行格式

<?php

$link = binlog_connect("mysql://root:cpyf@127.0.0.1:3306");
//binlog_set_position($link, 4);
//binlog_set_position($link, 4, 'mysql-bin.000006'); while($event=binlog_wait_for_next_event($link)) {
// it will block here
switch($event['type_code']) {
case BINLOG_DELETE_ROWS_EVENT:
var_dump($event);
// do what u want ...
break;
case BINLOG_WRITE_ROWS_EVENT:
var_dump($event);
// do what u want ...
break;
case BINLOG_UPDATE_ROWS_EVENT:
var_dump($event);
// do what u want ...
break;
default:
// var_dump($event);
break;
}
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27

Update_rows

update `type` set type_id = 22 WHERE id in (58, 59);
  • 1
  • 1
array(5) {
'type_code' =>
int(24)
'type_str' =>
string(11) "Update_rows"
'db_name' =>
string(5) "cloud"
'table_name' =>
string(4) "type"
'rows' =>
array(4) {
[0] =>
array(5) {
[0] =>
string(2) "58"
[1] =>
string(8) "adsfasdf"
[2] =>
string(4) "asdf"
[3] =>
string(2) "22"
[4] =>
string(1) "0"
}
[1] =>
array(5) {
[0] =>
string(2) "58"
[1] =>
string(8) "adsfasdf"
[2] =>
string(4) "asdf"
[3] =>
string(1) "4"
[4] =>
string(1) "0"
}
[2] =>
array(5) {
[0] =>
string(2) "59"
[1] =>
string(8) "adsfasdf"
[2] =>
string(4) "asdf"
[3] =>
string(2) "22"
[4] =>
string(1) "0"
}
[3] =>
array(5) {
[0] =>
string(2) "59"
[1] =>
string(8) "adsfasdf"
[2] =>
string(4) "asdf"
[3] =>
string(1) "4"
[4] =>
string(1) "0"
}
}
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55
  • 56
  • 57
  • 58
  • 59
  • 60
  • 61
  • 62
  • 63
  • 64
  • 65
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55
  • 56
  • 57
  • 58
  • 59
  • 60
  • 61
  • 62
  • 63
  • 64
  • 65

Delete_rows

delete from `type` WHERE id in (58, 59);
  • 1
  • 1
array(5) {
'type_code' =>
int(25)
'type_str' =>
string(11) "Delete_rows"
'db_name' =>
string(5) "cloud"
'table_name' =>
string(4) "type"
'rows' =>
array(2) {
[0] =>
array(5) {
[0] =>
string(2) "58"
[1] =>
string(8) "adsfasdf"
[2] =>
string(4) "asdf"
[3] =>
string(2) "22"
[4] =>
string(1) "0"
}
[1] =>
array(5) {
[0] =>
string(2) "59"
[1] =>
string(8) "adsfasdf"
[2] =>
string(4) "asdf"
[3] =>
string(2) "22"
[4] =>
string(1) "0"
}
}
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39

Write_rows

insert into type values (Null, "Hello, World", "Best world", 4, 0), (NULL, "你好,世界", "世界很美好", 3, 5);
  • 1
  • 1
array(5) {
'type_code' =>
int(23)
'type_str' =>
string(10) "Write_rows"
'db_name' =>
string(5) "cloud"
'table_name' =>
string(4) "type"
'rows' =>
array(2) {
[0] =>
array(5) {
[0] =>
string(2) "95"
[1] =>
string(12) "Hello, World"
[2] =>
string(10) "Best world"
[3] =>
string(1) "4"
[4] =>
string(1) "0"
}
[1] =>
array(5) {
[0] =>
string(2) "96"
[1] =>
string(15) "你好,世界"
[2] =>
string(15) "世界很美好"
[3] =>
string(1) "3"
[4] =>
string(1) "5"
}
}
}

PHP Client for Mysql Binlog的更多相关文章

  1. MySQL Binlog 解析工具 Maxwell 详解

    maxwell 简介 Maxwell是一个能实时读取MySQL二进制日志binlog,并生成 JSON 格式的消息,作为生产者发送给 Kafka,Kinesis.RabbitMQ.Redis.Goog ...

  2. 一个分布式 MySQL Binlog 存储系统的架构设计

    1. kingbus简介 1.1 kingbus是什么? kingbus是一个基于raft强一致协议实现的分布式MySQL binlog 存储系统.它能够充当一个MySQL Slave从真正的Mast ...

  3. 20180705关于mysql binlog的解析方式

    来自:https://blog.csdn.net/u012985132/article/details/74964366/ 关系型数据库和Hadoop生态的沟通越来越密集,时效要求也越来越高.本篇就来 ...

  4. 基于 MySQL Binlog 的 Elasticsearch 数据同步实践 原

    一.背景 随着马蜂窝的逐渐发展,我们的业务数据越来越多,单纯使用 MySQL 已经不能满足我们的数据查询需求,例如对于商品.订单等数据的多维度检索. 使用 Elasticsearch 存储业务数据可以 ...

  5. Canal - 数据同步 - 阿里巴巴 MySQL binlog 增量订阅&消费组件

    背景 早期,阿里巴巴 B2B 公司因为存在杭州和美国双机房部署,存在跨机房同步的业务需求 ,主要是基于trigger的方式获取增量变更.从 2010 年开始,公司开始逐步尝试数据库日志解析,获取增量变 ...

  6. canal+kafka订阅Mysql binlog将数据异构到elasticsearch(或其他存储方式)

    canal本质就是"冒充"从库,通过订阅mysql bin-log来获取数据库的更改信息. mysql配置(my.cnf) mysql需要配置my.cnf开启bin-log日志并且 ...

  7. 基于MySQL Binlog的Elasticsearch数据同步实践

    一.为什么要做 随着马蜂窝的逐渐发展,我们的业务数据越来越多,单纯使用 MySQL 已经不能满足我们的数据查询需求,例如对于商品.订单等数据的多维度检索. 使用 Elasticsearch 存储业务数 ...

  8. MySql Binlog 说明 & Canal 集成MySql的更新异常说明 & MySql Binlog 常用命令汇总

    文章来源于本人的印象笔记,如出现格式问题可访问该链接查看原文 原创声明:作者:Arnold.zhao 博客园地址:https://www.cnblogs.com/zh94 目录 背景介绍 开启MySq ...

  9. Canal实时解析mysql binlog数据实战

    一.说明 通过canal实时监听mysql binlog日志文件的变化,并将数据解析出来 二.环境准备 1.创建maven项目并修改pom.xml配置文件 <dependencies> & ...

随机推荐

  1. MongoDB学习笔记(六)--复制集+sharding分片 && 总结

    复制集+sharding分片                                                               背景 主机 IP 服务及端口 Server A ...

  2. Cognos报表展示图片小技巧

    场景:在销售行业,比如手机,服装行业,如果仅仅的显示数字.文字那就显得不是很生动了,例如可以显示一下图片,那种样子的产品受大家喜欢. 样例1:在报表头都喜欢加上一些公司的logo,让报表看上去专业点. ...

  3. [Javascript] Funciton Expression

    //This will load the code into the memory no matter //you call it or not function diffOfSquares(a,b) ...

  4. Java高并发syncronized深入理解

    1.Synchronized的作用: 能够保证在同一时刻最多只有一个线程执行该段代码,以达到保证并发安全的效果. 2.地位: 1)Synchronized是java的关键字,并java的怨言原生支持: ...

  5. 持续集成之路 —— Mock对象引起的测试失败

    今天遇到了一个很奇怪的问题,纠结了好久.在和同事念叨这个问题时,突然想到了问题所在. 问题现象: 在一个Service的单元测试类中有八个测试用例,单独运行时都可以正常通过.可是一旦一起运行时,总是会 ...

  6. python3安装Beautiful Soup爬虫组件

    第一步:下载beautifulsoup4-4.3.2.tar.gz 第二步:拷贝解压缩目录到C:\Python34\beautifulsoup4-4.3.2 第三步: 第四步: 第五步:拷贝C:\Py ...

  7. caffe 代码阅读笔记1

    首先查看caffe.cpp里的train函数: // Train / Finetune a model. //训练,微调一个网络模型 int train() { // google的glog库,检查- ...

  8. html页面禁止选择复制剪切

    在body加入 onselectstart="return false" oncopy="return false;" oncut="return f ...

  9. ubuntu终止进程的方法

    在ubuntu中,终止一个进程或终止一个正在运行的程序,一般是通过 kill .killall.pkill.xkill 等进行. 先看两个例子: 例子一:结束某个程序,如Firefox  键入命令: ...

  10. spring mvc异常的处理

    1.全局处理 <!-- 总错误处理 --> <bean id="exceptionResolver" class="org.springframewor ...