最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

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

programmeradmin7浏览0评论
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

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

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./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

Share Improve this question asked Jun 5, 2017 at 9:18 VIKAS KOHLIVIKAS KOHLI 8,4804 gold badges51 silver badges66 bronze badges
Add a ment  | 

4 Answers 4

Reset to default 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

  1. Make sure you enabled binlog on your mysql server and the binlog format is ROW https://dev.mysql./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) {}
    );
    

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

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

发布评论

评论列表(0)

  1. 暂无评论