建立nodejs mysql连接时出现问题

y1aodyip  于 2021-06-23  发布在  Mysql
关注(0)|答案(3)|浏览(400)

我试图在nodejs服务器和mysql数据库之间建立一个简单的连接(使用microsoftsqlservermanagerstudiov14),下面是我的代码和出现在控制台窗口中的错误消息。
这是我的密码:

var express = require('express');
var app = express();
var sql = require("mssql");

app.get('/', function (req, res) {

    // config for your database
    var config = {
        user: 'superadmin',
        password: '***',
        server: 'localhost', 
        database: 'XXX' 
    };

    // connect to your database
    sql.connect(config, function (err) {

        if (err) console.log(err);

        // create Request object
        var request = new sql.Request();

        // query to the database and get the records
        request.query('select * from Schools', function (err, recordset) {

            if (err) console.log(err)

            // send records as a response
            res.send(recordset);

        });
    });
});

var server = app.listen(5000, function () {
    console.log('Server is running..');
});

我得到的错误如下所示:

Server is running..
tedious deprecated The default value for `options.encrypt` will change from `false` to `true`. Please pass `false` explicitly if you want to retain current behaviour. node_modules\mssql\lib\tedious.js:212:23
{ ConnectionError: Failed to connect to localhost:1433 - Could not connect (sequence)
    at Connection.tedious.once.err (C:\Users\smr09\Desktop\Code\ou\db_test\node_modules\mssql\lib\tedious.js:216:17)
    at Object.onceWrapper (events.js:275:13)
    at Connection.emit (events.js:182:13)
    at Connection.socketError (C:\Users\smr09\Desktop\Code\ou\db_test\node_modules\tedious\lib\connection.js:1004:14)
    at C:\Users\smr09\Desktop\Code\ou\db_test\node_modules\tedious\lib\connection.js:869:25
    at SequentialConnectionStrategy.connect (C:\Users\smr09\Desktop\Code\ou\db_test\node_modules\tedious\lib\connector.js:154:9)
    at Socket.onError (C:\Users\smr09\Desktop\Code\ou\db_test\node_modules\tedious\lib\connector.js:170:16)
    at Socket.emit (events.js:182:13)
    at emitErrorNT (internal/streams/destroy.js:92:8)
    at emitErrorAndCloseNT (internal/streams/destroy.js:59:3)
  code: 'ESOCKET',
  originalError:
   { ConnectionError: Failed to connect to localhost:1433 - Could not connect (sequence)
    at ConnectionError (C:\Users\smr09\Desktop\Code\ou\db_test\node_modules\tedious\lib\errors.js:12:12)
    at Connection.socketError (C:\Users\smr09\Desktop\Code\ou\db_test\node_modules\tedious\lib\connection.js:1004:30)
    at C:\Users\smr09\Desktop\Code\ou\db_test\node_modules\tedious\lib\connection.js:869:25
    at SequentialConnectionStrategy.connect (C:\Users\smr09\Desktop\Code\ou\db_test\node_modules\tedious\lib\connector.js:154:9)
    at Socket.onError (C:\Users\smr09\Desktop\Code\ou\db_test\node_modules\tedious\lib\connector.js:170:16)
    at Socket.emit (events.js:182:13)
    at emitErrorNT (internal/streams/destroy.js:92:8)
    at emitErrorAndCloseNT (internal/streams/destroy.js:59:3)
    at process._tickCallback (internal/process/next_tick.js:174:19)
     message: 'Failed to connect to localhost:1433 - Could not connect (sequence)',
     code: 'ESOCKET' },
  name: 'ConnectionError' }
{ ConnectionError: Connection is closed.
    at Request._query (C:\Users\smr09\Desktop\Code\ou\db_test\node_modules\mssql\lib\base.js:1299:37)
    at Request._query (C:\Users\smr09\Desktop\Code\ou\db_test\node_modules\mssql\lib\tedious.js:497:11)
    at Request.query (C:\Users\smr09\Desktop\Code\ou\db_test\node_modules\mssql\lib\base.js:1242:12)
    at C:\Users\smr09\Desktop\Code\ou\db_test\test.js:24:17
    at _poolCreate.then.catch.err (C:\Users\smr09\Desktop\Code\ou\db_test\node_modules\mssql\lib\base.js:269:7)
    at process._tickCallback (internal/process/next_tick.js:178:7) code: 'ECONNCLOSED', name: 'ConnectionError' }

我对这方面比较陌生,尤其是在处理数据库方面。有人能解释一下这个错误是什么意思吗?

gopyfrb3

gopyfrb31#

将此行添加到配置:

options: {
    encrypt: false
}

它最终看起来像:

var config = {
    user: 'superadmin',
    password: '***',
    server: 'localhost', 
    database: 'XXX' ,
    options: {
        encrypt: false
    }
};
polkgigr

polkgigr2#

您的错误:
connectionerror:未能连接到localhost:1433 - 无法连接(序列)
我的问题通过添加端口解决了

var config = {
            user: 'superadmin',
            password: '***',
            server: 'localhost', 
            database: 'XXX',
            port: '',
        };
zfycwa2u

zfycwa2u3#

尝试在您的配置{}中输入encrypt:false,它会工作的。

相关问题