麻烦使nodejs mysql连接

时间:2018-05-28 09:24:41

标签: javascript mysql node.js

我正在尝试在我的nodejs服务器和我的mysql数据库之间建立一个简单的连接(使用Microsoft SQL server manager studio v14),下面是我的代码,并在控制台窗口中显示错误消息。

这是我的代码:

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

我特别擅长处理数据库。有人可以解释错误的含义吗?

3 个答案:

答案 0 :(得分:1)

将此行添加到配置中:

options: {
    encrypt: false
}

它最终看起来像:

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

答案 1 :(得分:0)

尝试在您的配置{}中放入加密:false,它将起作用。

答案 2 :(得分:0)

您的错误:

  

ConnectionError:无法连接到本地主机:1433-无法   连接(顺序)

我的问题通过添加端口得以解决

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