4
var MySQLEvents = require('mysql-events');
var dsn = {
  host:     'localhost',
  user:     'root',
  password: '' // no password set that's why keep blank
};
var mysqlEventWatcher = MySQLEvents(dsn);
console.log(mysqlEventWatcher);
var watcher =mysqlEventWatcher.add(
   'myDB.myTable',
  function (oldRow, newRow, event) {
     //row inserted
    if (oldRow === null) {
      //insert code goes here
    }

     //row deleted
    if (newRow === null) {
      //delete code goes here
    }

     //row updated
    if (oldRow !== null && newRow !== null) {
      //update code goes here
    }

    //detailed event information
    console.log(event); // don't matter, it updates, delete or insert
  },
  'Active'
);

Take code from https://www.npmjs.com/package/mysql-events

When I try to print console.log(mysqlEventWatcher); , it prints something like that

{ started: false,
  zongji: {},
  databases: [],
  tables: {},
  columns: {},
  events: [ 'tablemap', 'writerows', 'updaterows', 'deleterows' ],
  triggers: [],
  dsn: { host: 'localhost', user: 'root', password: '' },
  settings: {},
  connect: [Function: connect],
  add: [Function: add],
  remove: [Function: remove],
  stop: [Function: stop],
  reload: [Function: reload],
  includeSchema: [Function: includeSchema] }

After writing this code, I update specific table('myTable') in which I implement in mysqlEventWatcher, then It won't go to that method as inside that I am printing event.

I don't know which thing I am missing

VIKAS KOHLI
  • 8,164
  • 4
  • 50
  • 61

3 Answers3

2

I'm adding this answer because this appears first on Google. Hopefully this will help someone.

I'm using XAMPP as my server and in order to get it to work, I edited the my.cnf file, which can be found here: xampp\mysql\bin, with the following:

I enabled every instance of log-bin=mysql-bin (by removing the #) and I wrote-in binlog_format=row.

EDIT :
server-id = 1
log_bin = /var/log/mysql/mysql-bin.log
binlog_format = row

JJJ
  • 3,314
  • 4
  • 29
  • 43
1
  1. Make sure you enabled binlog on your mysql server and the binlog format is ROW https://dev.mysql.com/doc/refman/5.7/en/replication-options-binary-log.html

  2. Try to remove 'Active' and make sure you set the correct database and table name to run the first test

    var watcher =mysqlEventWatcher.add(
    'your_database_name.your_table_name',
    function (oldRow, newRow, event) {}
    );
    
nguyendn
  • 51
  • 5
  • I'm experiencing the same issue, binlog is switched on and in row format, the user has correct permissions, but no response from the callback – Michael L Watson Jun 29 '17 at 10:18
  • 1
    @MichaelLWatson Please make sure you set retention hours not null. Example: mysql.rds_set_configuration('binlog retention hours', 24); btw: I'm using mysq-events to listen the change on my major tables for audit purpose and it works well with remote ip. – nguyendn Jun 30 '17 at 08:57
  • @nguyendn can you explain how to enable "binlog" – Gil Epshtain Sep 01 '19 at 11:04
  • @GilEpshtain you can enable binlog in MySQL configuration file `server-id=0 binlog-format = ROW expire_logs_days = 14 log-bin = /var/lib/mysql/bin-log` and then you can check the status by run this query `SHOW VARIABLES LIKE 'log_bin';` in mysql (use mysql-cli). More details here https://stackoverflow.com/questions/40682381/enable-mysql-binlog-in-my-cnf – nguyendn Sep 03 '19 at 23:33
1

As well as checking @nguyendn answer, please try and run ZongJi standalone to check it's functioning

var ZongJi = require('zongji');
var zongji = new ZongJi({
  host     : 'localhost',
  user     : 'user',
  password : 'password',
  debug: true
});

zongji.on('binlog', function(evt) {
  evt.dump();
});

If ZongJi is not working, mysql-event will not work either. For me ZongJi was only working on localhost, not remote IP

Michael L Watson
  • 964
  • 13
  • 23