Mysql事件未通知更新的更改

时间:2016-10-25 06:52:51

标签: mysql node.js express socket.io

这里是用于自动通知的代码,如果mySql数据库中有任何更改到nodeJS,它没有通知?,同时我也编辑了my.cnf文件

#my.cnf文件

    binlog_format = row
    log_bin         = /var/log/mysql/mysql-bin.log
    binlog_do_db     = test   # Optional, limit which databases to log

节点JS

var MySQLEvents = require('mysql-events');
var dsn = {
  host:     'localhost',
  user:     'root',
  password: 'root',
};

var mysqlEventWatcher = MySQLEvents(dsn);
//console.log(mysqlEventWatcher);

var watcher = mysqlEventWatcher.add(
  'test.eventTable',
// test - database name, eventTable - table name
  function (oldRow, newRow) {
     //row inserted 
     console.log(oldRow);
     console.log('************');
     console.log(newRow);
     console.log('************');
    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 
    }
  }, 
  'Active'
);

我检查了我的连接是否已从节点端建立,如console.log(mysqlEventWatcher); 它响应像

 { started: false,
  zongji: 
   ZongJi {
     options: {},
     domain: null,
     _events: { error: [Function] },
     _eventsCount: 1,
     _maxListeners: undefined,
     ctrlConnection: 
      Connection {
        domain: null,
        _events: {},
        _eventsCount: 0,
        _maxListeners: undefined,
        config: [Object],
        _socket: [Object],
        _protocol: [Object],
        _connectCalled: true,
        state: 'disconnected',
        threadId: null },
     ctrlCallbacks: [],
     connection: 
      Connection {
        domain: null,
        _events: {},
        _eventsCount: 0,
        _maxListeners: undefined,
        config: [Object],
        _socket: undefined,
        _protocol: [Object],
        _connectCalled: false,
        state: 'disconnected',
        threadId: null },
     tableMap: {},
     ready: false,
     useChecksum: false },
  databases: [],
  tables: {},
  columns: {},
  events: [ 'tablemap', 'writerows', 'updaterows', 'deleterows' ],
  triggers: [],
  connect: [Function],
  add: [Function],
  remove: [Function],
  stop: [Function],
  reload: [Function],
  includeSchema: [Function] }

1 个答案:

答案 0 :(得分:0)

您应该考虑修改两件事:

  1. -对于配置,请尝试以下操作:server_id = 1(这是您文件中唯一缺少的重要conf)log-bin = binlog binlog_format =行expire-logs-days = 14(对于 正常工作)max-binlog-size = 500M(不重要) 一切正常)
  2. -从mysqlEventWatcher.add中删除“活动”