PHP Client for Mysql Binlog

來源:互聯網
上載者:User

標籤:mysql   binlog   

PHP解析Mysql Binlog,依賴於mysql-replication-listener庫
詳見:https://github.com/bullsoft/php-binlog

Install MySQL Replication Listener
  • https://github.com/bullsoft/mysql-replication-listener/archive/master.zip
unzip mysql-replication-listener-master.zipcd mysql-replication-listener-mastercmake . -DCMAKE_INSTALL_PREFIX=/usr/local/mysql-replicationmake & make install
Install php-binlog
  • https://github.com/bullsoft/php-binlog/archive/master.zip
unzip php-binlog-master.zipcd 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"    }  }}

著作權聲明:本文為博主原創文章,未經博主允許不得轉載。

PHP Client for Mysql Binlog

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.