简体   繁体   中英

Trouble making nodejs mysql connection

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?

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

尝试在您的config {}中放入crypto:false,它将起作用。

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: '',
        };

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM