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-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
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 would 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;}
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21st
- 22
- 23
- 24
- 25
- 26
- 27
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21st
- 22
- 23
- 24
- 25
- 26
- 27
Update_rows
update `type` set type_id = 22 WHERE id in (58, 59);
Array5) {' Type_code ' = Int (24)' Type_str ' = string (11)"Update_rows"' Db_name ' = string (5)"Cloud"' table_name ' = string (4)"Type"' Rows ' + =Array4) {[0] = =Array5) {[0] = = String (2)"58" [1] = = String (8)"ADSFASDF" [2] = = String (4)"ASDF" [3] = = String (2)"22" [4] = = String (1)"0"} [1] = =Array5) {[0] = = String (2)"58" [1] = = String (8)"ADSFASDF" [2] = = String (4)"ASDF" [3] = = String (1)"4" [4] = = String (1)"0"} [2] = =Array5) {[0] = = String (2)"59" [1] = = String (8)"ADSFASDF" [2] = = String (4)"ASDF" [3] = string (2) "[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"}}}
1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- Ten
- one
-
- 2
- (
- )
- +
- +
- /
- 0
-
- +
-
- all
- +
- +
- +
- -
- 29
-
- +
- +
- all
-
- +
- +
- PNS
- up
i>39
-
- 48
- all
-
-
- /
- /
- /
- /li>
-
-
- ,
- ,
- ,
- up-
- -
- +
- -
- +
- *
- +
- --
-
- +
- +
- +
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21st
- 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
Delete_rows
delete from `type` WHERE id in (58, 59);
Array5) {' Type_code ' = Int (25)' Type_str ' = string (11)"Delete_rows"' Db_name ' = string (5)"Cloud"' table_name ' = string (4)"Type"' Rows ' + =Array2) {[0] = =Array5) {[0] = = String (2)"58" [1] = = String (8)"ADSFASDF" [2] = = String (4)"ASDF" [3] = string (2) "[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"}}}
1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- Ten
- one
-
- 2
- (
- )
- +
- +
- /
- 0
-
- +
-
- all
- +
- +
- +
- -
- 29
-
- +
- +
- all
-
- +
- +
- PNS
- up
i>39
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21st
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 29
- 30
- 31
- 32
- 33
- 34
- 35
- 36
- 37
- 38
- 39
Write_rows
insert into type values (Null, "Hello, World", "Best world", 4, 0), (NULL, "你好,世界", "世界很美好", 3, 5);
Array5) {' Type_code ' = Int (23)' Type_str ' = string (10)"Write_rows"' Db_name ' = string (5)"Cloud"' table_name ' = string (4)"Type"' Rows ' + =Array2) {[0] = =Array5) {[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) "Hello, world" [2] = string (15) "The World is Beautiful" [3" = string (1) "3" [ 4] = string (1) "5"}}}
PHP Client for Mysql Binlog