A PHP Client for Mysql Binlog

Posted on

Recently, I wrote a php extension with my colleague (named 商源纯). You can use it to connect to a MYSQL server which produces BINLOG, and get BINLOG events in real-time, just like a Async-Trigger.

You know, we always need to use different components to do a good job, serve a good service. Just like we use MYSQL to storage all our data, use REDIS as a cache server, and use LUCENE as a search engine. The differences between MYSQL and other components will affect the sorting result, and therefore affect user experience, especially for time-sensitive service.

Our group use PHP to develop web projects. So if we have a PHP extension to do the data trigger job between different components, everything will be easy. This is what we do and why we do.

You can view it on GitHub, and welcome to fork and issue.Thank you.

https://github.com/BullSoft/php-binlog

An example(row-based BINLOG):

$link = binlog_connect("mysql://root@127.0.0.1:3306");
// binlog_set_position($link, 4);                           

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;
    }
}

Leave a Reply

Your email address will not be published.