【问题标题】:Why do mysql-event not working in node.js?为什么 mysql-event 在 node.js 中不起作用?
【发布时间】:2017-11-06 00:42:48
【问题描述】:
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'
);

https://www.npmjs.com/package/mysql-events获取代码

当我尝试打印 console.log(mysqlEventWatcher); , 它打印类似的东西

{ 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] }

写完这段代码后,我更新了我在mysqlEventWatcher中实现的特定表('myTable'),然后它不会去那个方法,因为我正在打印事件。

我不知道我错过了什么

【问题讨论】:

    标签: javascript mysql node.js npm mysql-event


    【解决方案1】:

    我添加这个答案是因为它首先出现在 Google 上。希望这会对某人有所帮助。

    我使用 XAMPP 作为我的服务器,为了让它工作,我编辑了 my.cnf 文件,可以在这里找到:xampp\mysql\bin,并带有以下内容:

    我启用了log-bin=mysql-bin 的每个实例(通过删除#)并写了binlog_format=row

    编辑:
    服务器 ID = 1
    log_bin = /var/log/mysql/mysql-bin.log
    binlog_format = 行

    【讨论】:

      【解决方案2】:
      1. 确保您在 mysql 服务器上启用了 binlog,并且 binlog 格式为 ROW https://dev.mysql.com/doc/refman/5.7/en/replication-options-binary-log.html

      2. 尝试删除“Active”并确保设置正确的数据库和表名以运行第一个测试

        var watcher =mysqlEventWatcher.add(
        'your_database_name.your_table_name',
        function (oldRow, newRow, event) {}
        );
        

      【讨论】:

      • 我也遇到了同样的问题,binlog 已开启且为行格式,用户有正确的权限,但回调没有响应
      • @MichaelLWatson 请确保您将保留时间设置为不为空。示例:mysql.rds_set_configuration('binlog 保留时间', 24);顺便说一句:我正在使用 mysq-events 来监听我的主要表上的更改以进行审计,并且它与远程 ip 配合得很好。
      • @nguyendn 你能解释一下如何启用“binlog”
      • @GilEpshtain 您可以在 MySQL 配置文件 server-id=0 binlog-format = ROW expire_logs_days = 14 log-bin = /var/lib/mysql/bin-log 中启用 binlog,然后您可以通过在 mysql 中运行此查询 SHOW VARIABLES LIKE 'log_bin'; 来检查状态(使用 mysql-cli)。更多细节在这里stackoverflow.com/questions/40682381/…
      【解决方案3】:

      除了检查@nguyendn 的答案,请尝试独立运行 ZongJi 以检查它的功能

      var ZongJi = require('zongji');
      var zongji = new ZongJi({
        host     : 'localhost',
        user     : 'user',
        password : 'password',
        debug: true
      });
      
      zongji.on('binlog', function(evt) {
        evt.dump();
      });
      

      如果ZongJi 不工作,mysql-event 也不会工作。对我来说,ZongJi 只在本地工作,而不是远程 IP

      【讨论】:

        猜你喜欢
        • 2014-08-20
        • 1970-01-01
        • 2020-05-20
        • 2022-01-05
        • 1970-01-01
        • 1970-01-01
        • 2011-04-22
        • 1970-01-01
        • 2021-05-20
        相关资源
        最近更新 更多