mysql-live-select icon indicating copy to clipboard operation
mysql-live-select copied to clipboard

How to use this library with node cluster?

Open xfg opened this issue 8 years ago • 2 comments

Hi. I have typical node cluster.

var cluster = require('cluster');
var numCPUs = require('os').cpus().length;

if (cluster.isMaster) {
  for (var i = 0; i < numCPUs; i++) {
    cluster.fork();
  }

  cluster.on('exit', (worker, code, signal) => {
    console.log('worker ' + worker.process.pid + ' died');
  });
} else {
  require('./worker.js');
}

Now I want to use mysql-live-select within worker.js but I want to it was connected to mysql only once and this connection could be shared between workers.

Right now I am wrote this within worker.js

//worker.js
var LiveMysql = require('mysql-live-select');
var settings = {
  host        : 'localhost',
  user        : 'root',
  password    : '',
  database    : 'fc',
  //serverId    : 1,
  minInterval : 200
};
var liveMysql = new LiveMysql(settings);

but this does not work and I get an error: Error: ER_CON_COUNT_ERROR: Too many connections

How to fix? Thanks.

xfg avatar Mar 12 '16 10:03 xfg

Why do you have the serverId setting commented out? Each worker needs to have a unique value for serverId.

numtel avatar Mar 13 '16 07:03 numtel

Because initially I wanted to do so that the connection to mysql was established only once from parent process and propagated to all workers who should be used this connection.

But during my experiments I noticed that the serverId does not affect anything. If I comment out the serverId then all workers still normal receive the updates from mysql. Do you know anything about this behavior?

xfg avatar Mar 13 '16 14:03 xfg