Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/node.js/38.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
压力测试后node.js mysql套接字错误_Mysql_Node.js - Fatal编程技术网

压力测试后node.js mysql套接字错误

压力测试后node.js mysql套接字错误,mysql,node.js,Mysql,Node.js,我使用node.js(一个带有mysql模块的异步事件库服务器)从数据库中选择122行记录 我使用下面的命令运行了一个简单的压力测试: ab -n 10 -c 2 http://localhost:8000/ node.js完成该过程,但mysql仍在运行,直到遇到错误: throw new Error('Socket is not writable'); ^ Error: Socket is not writable at Socket._writeOut (

我使用node.js(一个带有mysql模块的异步事件库服务器)从数据库中选择122行记录

我使用下面的命令运行了一个简单的压力测试:

ab -n 10 -c 2 http://localhost:8000/
node.js完成该过程,但mysql仍在运行,直到遇到错误:

 throw new Error('Socket is not writable');
          ^
 Error: Socket is not writable
    at Socket._writeOut (net.js:391:11)
    at Socket.write (net.js:377:17)
    at Client.write (/home/kelvin/node_modules/mysql/lib/client.js:142:16)
    at Object.end [as fn] (/home/kelvin/node_modules/mysql/lib/client.js:240:10)
    at Client._dequeue (/home/kelvin/node_modules/mysql/lib/client.js:274:18)
    at Object.end [as fn] (/home/kelvin/node_modules/mysql/lib/client.js:247:10)
    at Client._dequeue (/home/kelvin/node_modules/mysql/lib/client.js:274:18)
    at Object.end [as fn] (/home/kelvin/node_modules/mysql/lib/client.js:247:10)
    at Client._dequeue (/home/kelvin/node_modules/mysql/lib/client.js:274:18)
    at Object.end [as fn] (/home/kelvin/node_modules/mysql/lib/client.js:247:10)
现在,每当我再次运行测试时,我都会得到相同的错误

MySQL是与Node.js一起使用的好解决方案,还是我编写代码的方式

一些node.js代码:

var server = http.createServer(function (request, response) {
  response.writeHead(200, {"Content-Type": "text/plain"});

  client.query('USE ' + db);
  client.query('SELECT * FROM ' + tbl,
      function selectDb(err, results, fields) {
        if (err) {
          throw err;
        }

        console.log(results);
        client.end();
      }
  );

  response.end("END RESULT");
});

您为每个连接共享一个mysql连接对象:当两个客户端同时请求一个页面时,您可能会破坏mysql库的内部状态,而当另一个查询正在执行时,它不打算发送新查询


对每个请求使用一个mysql连接,如果您想为mysql连接使用池,可以使用节点池。

Thnks Tobias,我将查看节点池。