Search code examples
mysqlnode.jsmysql-eventmysqlbinlog

listen mysql change event using zongji module not working


I am trying to listen mysql change event in my app using mysql-event module. For that I got to know that, i need to make sure if zongji module works fine.

So here is what I have tried so far:

--my server.js

                          var ZongJi = require('zongji');

                          var zongji = new ZongJi({
                            host     : 'db_host',
                            user     : 'root',
                            password : 'kakaun',
                            insecureAuth: true,
                            debug: true
                          });
                          zongji.start({
                            includeEvents: ['tablemap', 'writerows', 'updaterows', 'deleterows']
                          });
                          zongji.on('binlog', function(evt) {
                            console.log("+++++Inside binlog event++++");
                            evt.dump();
                          });
                          process.on('SIGINT', function() {
                            console.log('Got SIGINT.');
                            zongji.stop();
                            process.exit();
                          });

(Example from https://github.com/nevill/zongji/blob/master/example.js)

I have perfectly configured the binlog for mysql and it is creating the log as well.

But I am not able to log anything for the above console for any change event in my node app.

When I switched on debug= true option, I can see that for every change in database log, I get this debug log:

<-- BinlogHeader
BinlogHeader {}

<-- BinlogHeader
BinlogHeader {}

<-- BinlogHeader
BinlogHeader {}

So, that means my app is succeffully able to contact binlog of mysql, but how I know which row has changed or what has changed? Because zongzi.on('binlog', [Function]) not printing anything.


Solution

  • So after a lot of debugging, I found the problem. So here is something which I had overlooked and was doing wrong. I would like to post my mistake, so that other user may avoid doing this.

    By default, if you enable binlog for mysql, it will be events based logging, which zongji module could not understand. https://dev.mysql.com/doc/internals/en/binary-log-overview.html

    Hence you need to enable row based logging, to get useful information (to know what has changed).

    row based logging can be enabled using this extra statement in my.cnf file of mysql configuration file:

    binlog_format    = row
    

    After adding this, and restarting the mysql server, every thing started working fine.