• 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: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"
        }
      }
    }
  • 相关阅读:
    太可爱了!CSS3 & SVG 制作的米老鼠钟表
    20个免费的 AngularJS 资源和开发教程
    比尔盖茨:反垄断案让我分心,不然微软定能打败安卓(胜者通吃的行业要不计代价的三班倒,评论很精彩)
    C++11 新特性之智能指针(shared_ptr, unique_ptr, weak_ptr)
    C++编译器会对没有构造函数的类生成默认构造函数吗?(有必要的时候才生成,要看情况。有反汇编验证)
    qt5信息提示框QMessageBox用法(很全)
    (RPC) Remote Procedure Call Protocol 远程过程调用协议
    分布式事务就是由多个本地事务组合而成的事务
    内存管理--虚拟内存管理技术
    NET适合搞大数据,机器学习、人工智能
  • 原文地址:https://www.cnblogs.com/brucemengbm/p/7142593.html
Copyright © 2020-2023  润新知