rodrigogs / mysql-events

A node package that watches a MySQL database and runs callbacks on matched events.
BSD 3-Clause "New" or "Revised" License
136 stars 52 forks source link

Connection error when using mysql-events #46

Open McLotos opened 1 year ago

McLotos commented 1 year ago

try using example from readme.

const mysql = require('mysql');
const MySQLEvents = require('@rodrigogs/mysql-events');

const program = async () => {
  const connection = mysql.createConnection({
    host: 'localhost',
    user: 'root',
    password: 'root',
  });

  const instance = new MySQLEvents(connection, {
    startAtEnd: true,
    excludedSchemas: {
      mysql: true,
    },
  });

  await instance.start();

  instance.addTrigger({
    name: 'TEST',
    expression: '*',
    statement: MySQLEvents.STATEMENTS.ALL,
    onEvent: (event) => { // You will receive the events here
      console.log(event);
    },
  });

  instance.on(MySQLEvents.EVENTS.CONNECTION_ERROR, console.error);
  instance.on(MySQLEvents.EVENTS.ZONGJI_ERROR, console.error);
};

program()
  .then(() => console.log('Waiting for database events...'))
  .catch(console.error);

so get error

 Error: connect ETIMEDOUT
     at Connection._handleConnectTimeout (/var/www/html/node_modules/mysql/lib/Connection.js:409:13)
     at Object.onceWrapper (node:events:627:28)
     at Socket.emit (node:events:513:28)
     at Socket._onTimeout (node:net:553:8)
     at listOnTimeout (node:internal/timers:564:17)
     at process.processTimers (node:internal/timers:507:7)
     --------------------
     at Protocol._enqueue (/var/www/html/node_modules/mysql/lib/protocol/Protocol.js:144:48)
     at Protocol.handshake (/var/www/html/node_modules/mysql/lib/protocol/Protocol.js:51:23)
     at Connection.connect (/var/www/html/node_modules/mysql/lib/Connection.js:116:18)
     at /var/www/html/node_modules/@rodrigogs/mysql-events/lib/connectionHandler.js:6:75
     at new Promise (<anonymous>)
     at connect (/var/www/html/node_modules/@rodrigogs/mysql-events/lib/connectionHandler.js:6:31)
     at connectionHandler (/var/www/html/node_modules/@rodrigogs/mysql-events/lib/connectionHandler.js:42:11)
     at MySQLEvents.start (/var/www/html/node_modules/@rodrigogs/mysql-events/lib/MySQLEvents.js:88:29)
     at program (/var/www/html/index.js:12:18)
     at Object.<anonymous> (/var/www/html/index.js:27:1) {
   errorno: 'ETIMEDOUT',
   code: 'ETIMEDOUT',
   syscall: 'connect',
   fatal: true
 }

t

McLotos commented 1 year ago

@rodrigogs ? Are you there? =)

imdkbj commented 1 year ago

Try this, Working for me.

const instance = new MySQLEvents(
      {
        host: dbConfigs.host,
        port: dbConfigs.port,
        user: dbConfigs.username,
        password: dbConfigs.password,
      },
      {
        startAtEnd: true,
        excludedSchemas: {
          mysql: true,
        },
      }
    );

    instance
      .start()
      .then(() => {
        console.log("Running MySQLEvents!");
        instance.addTrigger({
          name: "xyz",
          expression: "*",
          statement: MySQLEvents.STATEMENTS.ALL,
          onEvent: (event) => {
            // Here you will get the events for the given expression/statement.
            // This could be an async function.
            console.log("event", event);
          },
        });
      })
      .catch((err) => console.error("Something bad happened MySQLEvents", err));

    instance.on(MySQLEvents.EVENTS.CONNECTION_ERROR, console.error);
    instance.on(MySQLEvents.EVENTS.ZONGJI_ERROR, console.error);