Node.js& MySQL:HTTP服务器在首次运行后停止

时间:2014-01-01 09:14:20

标签: mysql node.js

我正在使用node-mysql。以下代码取自http://blog.josedacruz.com/2013/07/19/learning-node-js-using-mysql/

var http = require('http');
var mysql = require('mysql');

var connection = mysql.createConnection({
  host : 'localhost',
  port : 3306,
  database: 'aaa',
  user : 'root',
  password : 'z'
});

http.createServer(function(req, res){
    res.writeHeader(200);
    res.write('Connect to mySql\n');
    // Connect to mySql (if there is an erro, report it and terminate de request)
    connection.connect(function(err){
        if(err != null) {
            res.end('Error connecting to mysql:' + err+'\n');
        }
    });

    connection.query("SELECT value FROM variable WHERE name = 'site_name'", function(err, rows){
        if(err != null) {
            res.end("Query error:" + err);
        } else {
            // Shows the result on console window
            console.log(rows[0]);
            res.end("Success!");
        }
        // Close connection
        connection.end();
    });
}).listen(80);

首先,我感到奇怪connection.endconnection.connect

的水平不同

我已经尝试将connection更改为同一级别并且也放在不同的位置,但仍然没有运气

以下是首次运行后HTTP服务器停止时终端中的错误(注意:MyTube是查询结果)

{ value: 'MyTube' }
 
Error: Cannot enqueue Quit after invoking quit.
    at Protocol._validateEnqueue (/home/mydir/node/node_modules/mysql/lib/protocol/Protocol.js:131:16)
    at Protocol._enqueue (/home/mydir/node/node_modules/mysql/lib/protocol/Protocol.js:98:13)
    at Protocol.quit (/home/mydir/node/node_modules/mysql/lib/protocol/Protocol.js:61:36)
    at Connection.end (/home/mydir/node/node_modules/mysql/lib/Connection.js:155:18)
    at Query._callback (/home/mydir/node/app.js:31:20)
    at Query.Sequence.end (/home/mydir/node/node_modules/mysql/lib/protocol/sequences/Sequence.js:75:24)
    at Protocol._validateEnqueue (/home/mydir/node/node_modules/mysql/lib/protocol/Protocol.js:153:6)
    at Protocol._enqueue (/home/mydir/node/node_modules/mysql/lib/protocol/Protocol.js:98:13)
    at Connection.query (/home/mydir/node/node_modules/mysql/lib/Connection.js:140:25)
    at Server.<anonymous> (/home/mydir/node/app.js:22:16)
    --------------------
    at Quit.Sequence (/home/mydir/node/node_modules/mysql/lib/protocol/sequences/Sequence.js:15:20)
    at new Quit (/home/mydir/node/node_modules/mysql/lib/protocol/sequences/Quit.js:8:12)
    at Protocol.quit (/home/mydir/node/node_modules/mysql/lib/protocol/Protocol.js:61:45)
    at Connection.end (/home/mydir/node/node_modules/mysql/lib/Connection.js:155:18)
    at Query._callback (/home/mydir/node/app.js:31:20)
    at Query.Sequence.end (/home/mydir/node/node_modules/mysql/lib/protocol/sequences/Sequence.js:75:24)
    at Protocol._validateEnqueue (/home/mydir/node/node_modules/mysql/lib/protocol/Protocol.js:153:6)
    at Protocol._enqueue (/home/mydir/node/node_modules/mysql/lib/protocol/Protocol.js:98:13)
    at Connection.query (/home/mydir/node/node_modules/mysql/lib/Connection.js:140:25)
    at Server.<anonymous> (/home/mydir/node/app.js:22:16)

2 个答案:

答案 0 :(得分:2)

问题是,在mysql.createConnection关闭该连接后,您尝试重用由connection.end()创建的连接。

您需要将调用转移到mysql.createConnection以回复http.createServer(我建议)使用connection pooling

答案 1 :(得分:1)

快速解决此问题的一种方法是删除connection.connectconnection.end来电,并依赖connection.query

隐式连接
http.createServer(function(req, res){
    res.writeHeader(200);
    res.write('Connect to mySql\n');     
    connection.query("SELECT value FROM variable WHERE name = 'site_name'", function(err, rows){
        if(err != null) {
            res.end("Query error:" + err);
        } else {
            // Shows the result on console window
            console.log(rows[0]);
            res.end("Success!");
        }
    });
}).listen(80);