最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Trouble making nodejs mysql connection - Stack Overflow

programmeradmin0浏览0评论

I am trying to make a simple connection between my nodejs server and my mysql db (using Microsoft SQL server manager studio v14), below is my code and the error message appearing in my console window.

here is my code:

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..');
});
 

I am trying to make a simple connection between my nodejs server and my mysql db (using Microsoft SQL server manager studio v14), below is my code and the error message appearing in my console window.

here is my code:

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..');
});
 

The error I am getting looks like this:

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' }

I am rather new at this particularly with dealing with databases. Can someone explain what the error means?

Share Improve this question asked May 28, 2018 at 9:24 MichaelMichael 5,0588 gold badges37 silver badges59 bronze badges 6
  • 1 Googling the error looks promising: stackoverflow./questions/25577248/… – Roland Starke Commented May 28, 2018 at 9:27
  • Is your db running @ localhost:1433? Also verify your credentials. Try logging in with the same credentials using Microsoft SQL Server Management Studio. – user1496463 Commented May 28, 2018 at 9:28
  • did you try putting encrypt: false in your config = {} object to see if that made any difference? – denden Commented May 28, 2018 at 9:52
  • Did you get its answer? – Riddhi Commented Sep 11, 2018 at 9:35
  • no, I reinstalled and it worked, not sure what the issue was. – Michael Commented Sep 12, 2018 at 10:31
 |  Show 1 more ment

3 Answers 3

Reset to default 5

Add this line to the config:

options: {
    encrypt: false
}

It will finally look like:

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

try to put encrypt: false in your config{}, it will work.

your error:

ConnectionError: Failed to connect to localhost:1433 - Could not connect (sequence)

My problem was resolved by adding port

var config = {
            user: 'superadmin',
            password: '***',
            server: 'localhost', 
            database: 'XXX',
            port: '',
        };
发布评论

评论列表(0)

  1. 暂无评论