gpt4 book ai didi

mysql - Node 和 MySQL 与数据库的连接

转载 作者:行者123 更新时间:2023-11-29 18:37:54 24 4
gpt4 key购买 nike

我需要为模块提供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检查过,数据是正确的。

最佳答案

好的,我在这里发现了一个小错误。数据库是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 此处连接正常。

我在google上发现了类似的问题,问题是Disabled TCP/IP。我检查了这个,它已通过此端口 1443 启用。

关于mysql - Node 和 MySQL 与数据库的连接,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45133170/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com