node.js mysql socket error after stress test
I use node.js, an asynchronous event base server with mysql module to select 122 rows of records from the database.
I ran a simple stress test using the command below:
ab -n 10 -c 2 http://localhost:8000/
the node.js finish the process, but mysql is still running, until it hits an error:
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开发者_Go百科 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)
Now whenever I run the test again, I get the same error.
Is MySQL a good solution to use with Node.js or is it how I code?
Some node.js code:
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");
});
You share one mysql connection object for every connection: When two clients request a page simultanously you maybe destroy the internal state of your mysql library while it is not intended to send a new query when another query is right now executing.
Use one mysql connection for every request, you can use node-pool if you want pooling for mysql connections.
精彩评论