VIKAS KOHLI
VIKAS KOHLI

Reputation: 8430

Why do mysql-event not working in node.js?

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

Upvotes: 4

Views: 7283

Answers (4)

Etienne Paul
Etienne Paul

Reputation: 1

Execute Mysql queries to change db user privileges:

GRANT REPLICATION SLAVE, REPLICATION CLIENT ON *.* TO 'root'@'localhost';
GRANT SELECT ON *.* TO 'root'@'localhost';

has done for me

Upvotes: 0

JJJ
JJJ

Reputation: 3332

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

Upvotes: 2

Michael L Watson
Michael L Watson

Reputation: 980

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

Upvotes: 1

nguyendn
nguyendn

Reputation: 51

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

Upvotes: 1

Related Questions