PHP解析Mysql Binlog,依赖于mysql-replication-listener库

详见:[https://github.com/bullsoft/php-binlog](https://github.com/bullsoft/php-binlog)

## Install MySQL Replication Listener

* [https://github.com/bullsoft/mysql-replication-listener/archive/master.zip](https://github.com/bullsoft/mysql-replication-listener/archive/master.zip)

* 该源代码,有一处bug,在 tcp_driver.cpp 第 650 行处:

```

int Binlog_tcp_driver::set_position(const std::string &str, unsigned long position)

{

/*

Validate the new position before we attempt to set. Once we set the

position we won't know if it succeded because the binlog dump is

running in another thread asynchronously.

*/

/*

// 这个地方会导致,假设 set_position 不是最后一个 binlog file,并且 position 又大于最后一个 binlog size,则会返回失败,特此屏蔽掉该推断

if(position >= m_binlog_offset) {

return ERR_FAIL;

}

*/

```

* 改动后的 mysql-replication-listener 源代码和 php-binlog 源代码打包下载地址:

[http://download.csdn.net/download/xtjsxtj/9843275](http://download.csdn.net/download/xtjsxtj/9843275)

```

unzip mysql-replication-listener-master.zip

cd mysql-replication-listener-master

cmake . -DCMAKE_INSTALL_PREFIX=/usr/local/mysql-replication

make & make install

```

## Install php-binlog

* [https://github.com/bullsoft/php-binlog/archive/master.zip](https://github.com/bullsoft/php-binlog/archive/master.zip)

```

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

```

## Examples

注:Binlog为行格式

```

$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;

}

}

```

### Update_rows

```

update `type` set type_id = 22 WHERE id in (58, 59);

```

```

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"

}

}

}

```

### Delete_rows

```

delete from `type` WHERE id in (58, 59);

```

```

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"

}

}

}

```

### Write_rows

```

insert into type values (Null, "Hello, World", "Best world", 4, 0), (NULL, "你好,世界", "世界非常美好", 3, 5);

```

```

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读取binlog,PHP解析Mysql Binlog相关推荐

  1. mysql binlog xid_解析MYSQL BINLOG 二进制格式(7)--Xid_log_event/XID_EVENT

    原创:转载请说明出处谢谢! 上接 http://blog.itpub.net/7728585/viewspace-2133188/ 解析MYSQL BINLOG 二进制格式(1)--准备工作 http ...

  2. mysql write rows_解析MYSQL BINLOG 二进制格式(5)--WRITE_ROW_EVENT

    展开阅读全文 原创:转载请说明出处谢谢! 上接 http://blog.itpub.net/7728585/viewspace-2133188/ 解析MYSQL BINLOG 二进制格式(1)--准备 ...

  3. mysql binlog解析 c_解析MYSQL BINLOG二进制格式(9)--infobin解析binlog帮助文档

    原创:转载请说明出处谢谢! 上接 http://blog.itpub.net/7728585/viewspace-2133188/ 解析MYSQL BINLOG 二进制格式(1)--准备工作 http ...

  4. 解析MYSQL BINLOG二进制格式(10)--问题解答

    原创转发请注明出处 上接 http://blog.itpub.net/7728585/viewspace-2133188/ 解析MYSQL BINLOG 二进制格式(1)--准备工作  http:// ...

  5. mysql xid_解析MYSQL BINLOG 二进制格式(7)--Xid_log_event/XID_EVENT

    原创:转载请说明出处谢谢! 上接 http://blog.itpub.net/7728585/viewspace-2133188/ 解析MYSQL BINLOG 二进制格式(1)--准备工作 http ...

  6. opentrace在mysql中使用_采用OpenReplicator解析MySQL binlog

    Open Replicator是一个用Java编写的MySQL binlog分析程序.Open Replicator 首先连接到MySQL(就像一个普通的MySQL Slave一样),然后接收和分析b ...

  7. mysql 二进制 存储格式化_解析MYSQL BINLOG 二进制格式(2)--FORMAT_DESCRIPTION_EVENT

    原创:转载请说明出处谢谢! 上接 http://blog.itpub.net/7728585/viewspace-2133188/ 参考源: 1.源码log_event.h log_event.cc ...

  8. mysqlbinlog工具_带你解析MySQL binlog

    前言: 我们都知道,binlog可以说是MySQL中比较重要的日志了,在日常学习及运维过程中,也经常会遇到.不清楚你对binlog了解多少呢?本篇文章将从binlog作用.binlog相关参数.解析b ...

  9. mysql的binlog意义_带你解析MySQL binlog

    前言: 我们都知道,binlog可以说是MySQL中比较重要的日志了,在日常学习及运维过程中,也经常会遇到.不清楚你对binlog了解多少呢?本篇文章将从binlog作用.binlog相关参数.解析b ...

最新文章

  1. javascript常用排序算法总结
  2. python selenium爬虫豆瓣_Python爬虫:学习selenium的正确方式
  3. 【C语言】控制台窗口图形界面编程(七):鼠标事件
  4. 敏捷项目管理过程改进
  5. Redis持久化配置
  6. 流行于机器学习竞赛的Boosting,一文讲透足够了
  7. edius裁剪快捷键_Edius剪辑视频的两种方法
  8. 华为操作系统,阿里巴巴飞天操作系统 ------- 操作系统生态
  9. 淘宝直播全屏页重排算法实践
  10. jquery抓娃娃机代码
  11. 天津市南洋工业学校计算机应用,一、计算机应用技术技能
  12. 使用IDM下载磁力链或迅雷文件
  13. uni-app是什么有啥子用?
  14. python实现非对称加密算法_Python使用rsa模块实现非对称加密与解密
  15. python之numpy之axis=1和axis=0
  16. 瑞萨车规级芯片RH850F1x各系列MCU区别
  17. php 上传文件 重命名_如何用PHP给上传的文件改名
  18. 笔记本二合一计算机,可能是目前最完美的二合一笔记本电脑 Surface Pro 7之我所见...
  19. 魔兽世界转服务器显示完成,《魔兽世界》怀旧服完成进度曝光,连画面、战斗公式都原味还原...
  20. Shawn生日,20年总结!!!

热门文章

  1. python类方法以及类调用实例方法的理解
  2. 关于linux 内存碎片指数
  3. 大疆开挂,谁都不能阻拦它登上好莱坞无人机领域巅峰!
  4. 阿里巴巴公布第二财季报告,净利润同比增长71%
  5. JavaScript:Object.prototype.toString进行数据类型判定
  6. 关于endian的故事,big-endian和little-endian
  7. MyBatis学习总结(5)——实现关联表查询
  8. Velocity魔法堂系列二:VTL语法详解
  9. GLPI生成中文PDF报表
  10. 探React Hooks