节点和MySQL连接到数据库

时间:2017-07-16 20:46:47

标签: mysql node.js

我需要为模块提供MySQL连接,所以我提供了这样的代码:

var express = require('express');
var mysql = require('mysql')
var app = express();

var connection = mysql.createConnection({
    host: '149.xxx.xx.x',
    user: 'User',
    password: 'Password',
    database: 'DataBase',
    port: 1443, 
});

connection.connect(
    function(error) {
        console.log(error.code); // 'ECONNREFUSED'
        console.log(error.fatal); // true
        console.log(error.sql); 
        console.log(error.sqlMessage);     
}
);

经过一段时间(大约1-2分钟)后,我收到了来自console.logs的错误:

ECONNRESET
true
undefined
undefined

我想也许是因为我的非活动"暂停,但是当我尝试进行查询时,错误是相同的。

知道出了什么问题吗?我从DataGrip检查了它,数据是正确的。

1 个答案:

答案 0 :(得分:0)

好的,我在这里发现了一个小虫子。数据库是 MS SQL Server(microsoft)。我现在正在尝试使用名为mssql的库连接到该数据库,提供一些代码:

var config = {
    server: "149.xxx.xx.x",
    database: "Database",
    user: "User",
    password: "Password",
    connectionTimeout: 300000,
    requestTimeout: 300000,
    pool: {
        idleTimeoutMillis: 300000,
        max: 100
    }
};

function getEmp() {
    var connection = new sql.Connection(config);
    var req = new sql.Request(connection);

    connection.connect(function (error) {
        if(error) {
            console.log(error);
            return;
        }
        req.query('Procedure', function(err, data) {
            if (err) {
                console.log(err);
            } else {
                console.log(data);
            }
            connection.close();
        });
    });
}

getEmp();

我收到错误:

{ ConnectionError: Failed to connect to 149.xxx.xx.x:1433 - connect ETIMEDOUT 149.xxx.xx.x:1433
at Connection.<anonymous> (/Users/phuzarski/Learning/NodeJS/srv-express/node_modules/mssql/lib/tedious.js:353:25)
at Connection.g (events.js:292:16)
at emitOne (events.js:96:13)
at Connection.emit (events.js:188:7)
at Connection.socketError (/Users/phuzarski/Learning/NodeJS/srv-express/node_modules/tedious/lib/connection.js:791:12)
at Socket.<anonymous> (/Users/phuzarski/Learning/NodeJS/srv-express/node_modules/tedious/lib/connection.js:33:15)
at emitOne (events.js:96:13)
at Socket.emit (events.js:188:7)
at emitErrorNT (net.js:1277:8)
at _combinedTickCallback (internal/process/next_tick.js:80:11)


 name: 'ConnectionError',
  message: 'Failed to connect to 149.xxx.xx.x:1433 - connect ETIMEDOUT 149.xxx.xx.x:1433',
  code: 'ESOCKET' }

确保数据正确 - DataGrip在这里正常连接。

我在谷歌发现了类似的问题,问题是禁用了TCP / IP。我检查了这个,并且使用此端口1443启用了它。