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为行格式
```
<?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;
}
}
```
### 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"
}
}
}
```
- 技能知识点
- 对死锁问题的理解
- 文件系统原理:如何用1分钟遍历一个100TB的文件?
- 数据库原理:为什么PrepareStatement性能更好更安全?
- Java Web程序的运行时环境到底是怎样的?
- 你真的知道自己要解决的问题是什么吗?
- 如何解决问题
- 经验分享
- GIT的HTTP方式免密pull、push
- 使用xhprof对php7程序进行性能分析
- 微信扫码登录和使用公众号方式进行扫码登录
- 关于curl跳转抓取
- Linux 下配置 Git 操作免登录 ssh 公钥
- Linux Memcached 安装
- php7安装3.4版本的phalcon扩展
- centos7下php7.0.x安装phalcon框架
- 将字符串按照指定长度分割
- 搜索html源码中标签包的纯文本
- 更换composer镜像源为阿里云
- mac 隐藏文件显示/隐藏
- 谷歌(google)世界各国网址大全
- 实战文档
- PHP7安装intl扩展和linux安装icu
- linux编译安装时常见错误解决办法
- linux删除文件后不释放磁盘空间解决方法
- PHP开启异步多线程执行脚本
- file_exists(): open_basedir restriction in effect. File完美解决方案
- PHP 7.1 安装 ssh2 扩展,用于PHP进行ssh连接
- php命令行加载的php.ini
- linux文件实时同步
- linux下php的psr.so扩展源码安装
- php将字符串中的\n变成真正的换行符?
- PHP7 下安装 memcache 和 memcached 扩展
- PHP 高级面试题 - 如果没有 mb 系列函数,如何切割多字节字符串
- PHP设置脚本最大执行时间的三种方法
- 升级Php 7.4带来的两个大坑
- 不同域名的iframe下,fckeditor在chrome下的SecurityError,解决办法~~
- Linux find+rm -rf 执行组合删除
- 从零搭建Prometheus监控报警系统
- Bug之group_concat默认长度限制
- PHP生成的XML显示无效的Char值27消息(PHP generated XML shows invalid Char value 27 message)
- XML 解析中,如何排除控制字符
- PHP各种时间获取
- nginx配置移动自适应跳转
- 已安装nginx动态添加模块
- auto_prepend_file与auto_append_file使用方法
- 利用nginx实现web页面插入统计代码
- Nginx中的rewrite指令(break,last,redirect,permanent)
- nginx 中 index try_files location 这三个配置项的作用
- linux安装git服务器
- PHP 中运用 elasticsearch
- PHP解析Mysql Binlog
- 好用的PHP学习网(持续更新中)
- 一篇写给准备升级PHP7的小伙伴的文章
- linux 安装php7 -系统centos7
- Linux 下多php 版本共存安装
- PHP编译安装时常见错误解决办法,php编译常见错误
- nginx upstream模块--负载均衡
- 如何解决Tomcat服务器打开不了HOST Manager的问题
- PHP的内存泄露问题与垃圾回收
- Redis数据结构 - string字符串
- PHP开发api接口安全验证
- 服务接口API限流 Rate Limit
- php内核分析---内存管理(一)
- PHP内存泄漏问题解析
- 【代码片-1】 MongoDB与PHP -- 高级查询
- 【代码片-1】 php7 mongoDB 简单封装
- php与mysql系统中出现大量数据库sleep的空连接问题分析
- 解决crond引发大量sendmail、postdrop进程问题
- PHP操作MongoDB GridFS 存储文件,如图片文件
- 浅谈php安全
- linux上keepalived+nginx实现高可用web负载均衡
- 整理php防注入和XSS攻击通用过滤