PHP Client for Mysql Binlog

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

详见:https://github.com/bullsoft/php-binlog

Install MySQL Replication Listener

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;
    }
  */
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

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:[email protected]: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"
    }
  }
}
时间: 2024-10-24 17:34:18

PHP Client for Mysql Binlog的相关文章

mysql报错解决MySQL Binlog(异常)——mysqlbinlog: unknown variable &#39;default-character-set=utf8&#39;

在使用mysqlbinlog分析日志时,报错: /usr/local/mysql/bin/mysqlbinlog: unknown variable 'default-character-set=utf8' 原因分析: 产生这个问题的原因是因为我在my.cnf中的client选项组中添加了:  default-character-set=utf8 这个是mysqlbinlog的一个bug 解决方法: 使用mysqlbinlog工具查看二进制日志时会重新读取的mysql的配置文件my.cnf,而不

20180705关于mysql binlog的解析方式

来自:https://blog.csdn.net/u012985132/article/details/74964366/ 关系型数据库和Hadoop生态的沟通越来越密集,时效要求也越来越高.本篇就来调研下实时抓取MySQL更新数据到HDFS. 本篇仅作为调研报告. 初步调研了canal(Ali)+kafka connect+kafka.maxwell(Zendesk)+kafka和mysql_streamer(Yelp)+kafka.这几个工具抓取MySQL的方式都是通过扫描binlog,模拟

Maxwell读取MySQL binlog日志到Kafka

启动MySQL 创建maxwell的数据库和用户 在MySQL中创建一个测试数据库和表 前面三个步骤详见 Maxwell读取MySQL binlog日志通过stdout展示 启动Zookeeper [[email protected] ~]$ cd $ZK_HOME/bin [[email protected] bin]$ ./zkServer.sh start 启动kafka,并创建主题为maxwell的topic [[email protected] bin]$ cd $KAFKA_HOME

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

canal本质就是"冒充"从库,通过订阅mysql bin-log来获取数据库的更改信息. mysql配置(my.cnf) mysql需要配置my.cnf开启bin-log日志并且将bin-log日志格式设置为row, 同时为了防止bin-log日志占用过多磁盘,可以设置一下过期时间, [mysqld] log-bin=mysql-bin # 打开binlog binlog-format=ROW # ROW格式 server_id=1 # mysql Replication 需要设置

MySQL Binlog 解析工具 Maxwell 详解

maxwell 简介 Maxwell是一个能实时读取MySQL二进制日志binlog,并生成 JSON 格式的消息,作为生产者发送给 Kafka,Kinesis.RabbitMQ.Redis.Google Cloud Pub/Sub.文件或其它平台的应用程序.它的常见应用场景有ETL.维护缓存.收集表级别的dml指标.增量到搜索引擎.数据分区迁移.切库binlog回滚方案等.官网(http://maxwells-daemon.io).GitHub(https://github.com/zende

mysql binlog row格式查看

MySQL 5.1开始,binlog支持row-based的格式,默认情况下只能看到一些经过base-64编码的信息,如 DELIMITER /*!*/; # at 7493962 #090827 5:25:03 server id 1 end_log_pos 0 Start: binlog v 4, server v 5.1.26-rc-community-log created 090827 5:25:03 BINLOG ' L6iVSg8BAAAAZgAAAAAAAAAAAAQANS4xL

原创工具binlog2sql:从MySQL binlog得到你要的SQL

binlog2sql是我开发的mysql binlog解析工具,它能帮助你从binlog得到你要的SQL.根据不同设置,你可以得到原始SQL.回滚SQL.去除主键的INSERT SQL等. 用途 数据回滚 主从切换后数据不一致的修复 从binlog生成标准SQL,带来的衍生功能 安装 $ git clone https://github.com/danfengcao/binlog2sql.git $ pip install -r requirements.txt 使用 MySQL server必

mysql binlog解析概要

1,dump协议: 根据数据库的ip+port创建socket,如果创建成功,说明链接建立成功,接下来是使用dump协议订阅binlog 链接建立成功之后,服务端会主动向客户端发送如下问候信息greeting(可以理解为经java转换后,是一个java对象), 在下面的代码中可以看到greeting中的信息: this.context.setServerStatus(greeting.getServerStatus());//this.context.setServerVersion(greet

MySQL binlog 自动清理脚本

# vim /data/scripts/delete_mysql_binlog.sh 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 66 67 68 69 70 71 72