Why is mysql event not working in node.js?
var MySQLEvents = require('mysql-events');
var dsn = {
host: 'localhost',
user: 'root',
password: '' // no password set that 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 the code from https://www.npmjs.com/package/mysql-events
When I try to print console.log (mysqlEventWatcher); it prints something like this
{ 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 am updating a specific table ("myTable") in which I am implementing in mysqlEventWatcher, and then will not access this method like inside that I print the event.
I don't know what thing I am missing
source to share
-
Make sure you have activated binlog on mysql server and binlog format ROW https://dev.mysql.com/doc/refman/5.7/en/replication-options-binary-log.html
-
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) {} );
source to share
As with checking @nguyendn's answer, try starting ZongJi yourself to see if it works.
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 doesn't work, mysql-event won't work. For me, ZongJi only worked on localhost, not remote IP
source to share
I am adding this answer because it first appears on google. Hope this helps someone.
I am using XAMPP as my server, and to make it work I edited the file my.cnf
that can be found here : xampp\mysql\bin
, with the following:
I have included each instance log-bin=mysql-bin
(by removing #
) and I wrote-in binlog_format=row
.
EDIT:
server-id = 1
log_bin = / var / log / mysql / mysql-bin.log
binlog_format = string
source to share