MySQL binlog tailer

511 views
Skip to first unread message

Marcel Laverdet

unread,
May 6, 2011, 4:07:38 AM5/6/11
to nodejs
I'm working on an application right now which needs to interoperate with an existing application's database. I want to cache some stuff in my NodeJS instance but have no way to subscribe to updates from the existing application. The path of least resistance I figured was to just tail MySQL's binlogs.

Not sure if anyone else would be interested in this, but figured I would put it out there just in case. If there's more interest I can setup a proper github project and put it in npm.

https://gist.github.com/958588

Usage is simple:
var tailer = new MysqlBinlogTailer('/var/lib/mysql/server-binlog.index');
tailer.on('query', function(timestamp, database, query, extra) {
console.log('['+ timestamp+ '] '+ query);
});

A new instance will fast-forward to the end of your binary logs and start emitting events for every query which appears in the binlog. When MySQL rolls over to a new binlog it will automatically move reads over to that one. It's fairly straightforward!

This won't work with mysql<5.0 or mysql>=6.0.

Floby

unread,
May 7, 2011, 5:04:10 AM5/7/11
to nodejs
Nice, I did write my own stuff as part of another project[1]
Your full node approach is nice if you have protability issues in
mind. However I wrote mine with just spawning a `tail` process (which
also allows for rotating log files).
Here it is https://github.com/Floby/log-file-watcher/blob/master/lib/LogWatcher.js
. I should probably add an option to limit the number of initial lines
(default to 5 or 10 by tail IIRC)

[1]https://github.com/Floby/log-file-watcher

Floby

unread,
May 7, 2011, 5:05:41 AM5/7/11
to nodejs
Oh Sorry, I read that way too quickly and didn't notice these were
binary logs. -_-'
Please ignore my previous post

On May 6, 10:07 am, Marcel Laverdet <mar...@laverdet.com> wrote:
Reply all
Reply to author
Forward
0 new messages