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
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 Beautiful"[3] = = String (1)"3"[4] = = String (1)"5"} }}
Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.
PHP Client for Mysql Binlog