PHP Client for Mysql Binlog

Source: Internet
Author: User

PHP parsing MySQL Binlog, dependent on Mysql-replication-listener library
See: Https://github.com/bullsoft/php-binlog

Install MySQL Replication Listener
    • Https://github.com/bullsoft/mysql-replication-listener/archive/master.zip

    • The source code, there is a bug in the Tcp_driver.cpp line No. 650:

int Binlog_tcp_driver::set_position(conststd::stringunsignedlong 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;    }  */
    • The modified Mysql-replication-listener source code and Php-binlog source code package:
      http://download.csdn.net/download/xtjsxtj/9843275
unzip mysql-replication-listener-master.zipcd mysql-replication-listener-master.-DCMAKE_INSTALL_PREFIX=/usr/local/mysql-replication& 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. the/bin/phpize./configure-- with-php-config=/usr/Local/php5. 5. the/bin/php-config -- with-mysql-binlog=/usr/Local/mysql-replication
Examples

Note: Binlog is in line format

<?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'll block here    Switch($event[' Type_code ']) { CaseBinlog_delete_rows_event:var_dump ($event);//Do what U want ...             Break; CaseBinlog_write_rows_event:var_dump ($event);//Do what U want ...             Break; CaseBinlog_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 ( -)' Type_str '= = String ( One)"Update_rows"  ' db_name '= = String (5)"Cloud"  ' table_name '= = String (4)"Type"  ' Rows '=Array(4) {    [0] =Array(5) {      [0] = = String (2)"+"[1] = = String (8)"ADSFASDF"[2] = = String (4)"ASDF"[3] = = String (2)"a"[4] = = String (1)"0"}    [1] =Array(5) {      [0] = = String (2)"+"[1] = = String (8)"ADSFASDF"[2] = = String (4)"ASDF"[3] = = String (1)"4"[4] = = String (1)"0"}    [2] =Array(5) {      [0] = = String (2)" the"[1] = = String (8)"ADSFASDF"[2] = = String (4)"ASDF"[3] = = String (2)"a"[4] = = String (1)"0"}    [3] =Array(5) {      [0] = = String (2)" the"[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 ( -)' Type_str '= = String ( One)"Delete_rows"  ' db_name '= = String (5)"Cloud"  ' table_name '= = String (4)"Type"  ' Rows '=Array(2) {    [0] =Array(5) {      [0] = = String (2)"+"[1] = = String (8)"ADSFASDF"[2] = = String (4)"ASDF"[3] = = String (2)"a"[4] = = String (1)"0"}    [1] =Array(5) {      [0] = = String (2)" the"[1] = = String (8)"ADSFASDF"[2] = = String (4)"ASDF"[3] = = String (2)"a"[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 ( at)' Type_str '= = String (Ten)"Write_rows"  ' db_name '= = String (5)"Cloud"  ' table_name '= = String (4)"Type"  ' Rows '=Array(2) {    [0] =Array(5) {      [0] = = String (2)" the"[1] = = String ( A)"Hello, World"[2] = = String (Ten)" Best World"[3] = = String (1)"4"[4] = = String (1)"0"}    [1] =Array(5) {      [0] = = String (2)"The "[1] = = String ( the)"Hello." World "[2] = = String ( the)"The world is very beautiful"[3] = = String (1)"3"[4] = = String (1)"5"}  }}

PHP Client for Mysql Binlog

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.