Nodejs Cluster with MySQL connections

╄→尐↘猪︶ㄣ 提交于 2021-02-07 07:17:34

问题


Looking on advice on clustering of Nodejs and the method of connection to mysql server. Do we open one connection for each child process or just one single connection for all processes? Or do we create a connection pool for all the child processes? Which is the recommended method?

one node process

var mysql      = require('mysql');
var connection = mysql.createConnection({
  host     : 'example.org',
  user     : 'bob',
  password : 'secret'
});

connection.connect(function(err) {
  if (err) {
    console.error('error connecting: ' + err.stack);
    return;
  }

  console.log('connected as id ' + connection.threadId);
});

node cluster option 1:

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

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

  cluster.on('exit', function(worker, code, signal) {
    console.log('worker ' + worker.process.pid + ' died');
  });
} else {
  // open one connection for each process
  var mysql      = require('mysql');
    var connection = mysql.createConnection({
      host     : 'example.org',
      user     : 'bob',
      password : 'secret'
    });

    connection.connect(function(err) {
      if (err) {
        console.error('error connecting: ' + err.stack);
        return;
      }

      console.log('connected as id ' + connection.threadId);
    });
}

option2:

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

var mysql = require('mysql');
var pool  = mysql.createPool({
    connectionLimit : 10,
    host            : 'example.org',
    user            : 'bob',
    password        : 'secret'
});

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

  cluster.on('exit', function(worker, code, signal) {
    console.log('worker ' + worker.process.pid + ' died');
  });
} else {

  // accept http connections and query
  pool.query('SELECT 1 + 1 AS solution', function(err, rows, fields) {
     if (err) throw err;

     console.log('The solution is: ', rows[0].solution);
  });
}

回答1:


After some trial and error, I'm using option 2 and it is working well. Created the connection globally and use in the cluster.

Each time you need to query, getConnection.

pool.getConnection(function(err, connection) {
        if(err) {
            console.log('Error getting sql connection');
            console.dir(err);

            if(typeof connection !== "undefined")
                connection.release();

            callback(err);
        }

        if(typeof cb === "undefined") {
            //console.log('with 2 params');
            connection.query( sql, function(err, rows) {
                connection.release();
                console.dir(sql);
                // console.dir('data=>' + data);

                if(err) {
                    console.log('err:' + err);
                    callback(err, rows);
                }else{
                    console.log( rows );
                    callback(err, rows);
                }
            });
        } else {
            // console.log('with 3 params:' + cb);
            connection.query( sql, data, function(err, rows){
                connection.release();
                console.log(sql);
                console.dir(data);

                if(err) {
                    console.log('err:' + err);
                    callback(err, rows);
                }else{
                    console.log( rows );
                    callback(err, rows);
                }

            });
        }
    });
}



回答2:


I believe the correct answer is, that it all depends? If you run concurrent operations (async etc), you'd probably want a pool. But if you are just nesting queries, then a single shared connection should be fine.

I prefer to use pools (even inside workers), simply because you get some error handling and robustness for free (connections are recreated as they die, there is an operations queue with timeouts etc). I've not found any downsides to this, so far...




回答3:


The pool will be recreated for every worker thread and you'll have multiple connection pools in your clustered application



来源:https://stackoverflow.com/questions/24339179/nodejs-cluster-with-mysql-connections

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!