使用sequelize连接失败到sql server

时间:2016-11-07 14:45:09

标签: javascript sql-server node.js sequelize.js

我正在使用Sequelize连接到SQLServer数据库。 我用这个配置Sequelize:

module.exports = {
    database: 'dbname',
    username: 'sa',
    password: 'password',
    options: {
        dialect: 'mssql',
        host: 'host address',
        port: 1433,
        dialectOptions: {
            instanceName: 'instancename'
        }
    }    
}

但我收到了这个错误:

  

ConnectionError:用户'sa'登录失败。

我已经在Sql Server的Configuration Manager中启用了“Sql Server Browser”和“TCP / IP Protocol”。 我的实例名称是默认的'MSSQLSERVER'。

我已经安装了乏味。

来自Node.js的堆栈跟踪:

{ SequelizeConnectionError: Login failed for user 'sa'.
    at Connection.<anonymous> (P:\ChatRoom\node_modules\sequelize\lib\dialects\mssql\connection-manager.js:109:16)
    at emitOne (events.js:96:13)
    at Connection.emit (events.js:188:7)
    at Connection.processLogin7Response (P:\ChatRoom\node_modules\tedious\lib\connection.js:672:16)
    at Connection.message (P:\ChatRoom\node_modules\tedious\lib\connection.js:1082:21)
    at Connection.dispatchEvent (P:\ChatRoom\node_modules\tedious\lib\connection.js:519:45)
    at MessageIO.<anonymous> (P:\ChatRoom\node_modules\tedious\lib\connection.js:439:23)
    at emitNone (events.js:86:13)
    at MessageIO.emit (events.js:185:7)
    at ReadablePacketStream.<anonymous> (P:\ChatRoom\node_modules\tedious\lib\message-io.js:92:15)
    at emitOne (events.js:96:13)
    at ReadablePacketStream.emit (events.js:188:7)
    at readableAddChunk (P:\ChatRoom\node_modules\tedious\node_modules\readable-stream\lib\_stream_readable.js:213:18)
    at ReadablePacketStream.Readable.push (P:\ChatRoom\node_modules\tedious\node_modules\readable-stream\lib\_stream_readable.js:172:10)
    at ReadablePacketStream.Transform.push (P:\ChatRoom\node_modules\tedious\node_modules\readable-stream\lib\_stream_transform.js:123:32)
    at ReadablePacketStream._transform (P:\ChatRoom\node_modules\tedious\lib\message-io.js:57:16)
  name: 'SequelizeConnectionError',
  message: 'Login failed for user \'sa\'.',
  parent:
   { ConnectionError: Login failed for user 'sa'.
       at ConnectionError (P:\ChatRoom\node_modules\tedious\lib\errors.js:12:12)
       at Parser.<anonymous> (P:\ChatRoom\node_modules\tedious\lib\connection.js:216:38)
       at emitOne (events.js:96:13)
       at Parser.emit (events.js:188:7)
       at Parser.<anonymous> (P:\ChatRoom\node_modules\tedious\lib\token\token-stream-parser.js:42:15)
       at emitOne (events.js:96:13)
       at Parser.emit (events.js:188:7)
       at readableAddChunk (P:\ChatRoom\node_modules\tedious\node_modules\readable-stream\lib\_stream_readable.js:213:18)
       at Parser.Readable.push (P:\ChatRoom\node_modules\tedious\node_modules\readable-stream\lib\_stream_readable.js:172:10)
       at Parser.Transform.push (P:\ChatRoom\node_modules\tedious\node_modules\readable-stream\lib\_stream_transform.js:123:32)
       at doneParsing (P:\ChatRoom\node_modules\tedious\lib\token\stream-parser.js:87:17)
       at P:\ChatRoom\node_modules\tedious\lib\token\infoerror-token-parser.js:46:5
       at P:\ChatRoom\node_modules\tedious\lib\token\infoerror-token-parser.js:13:19
       at P:\ChatRoom\node_modules\tedious\lib\token\stream-parser.js:224:9
       at Parser.awaitData (P:\ChatRoom\node_modules\tedious\lib\token\stream-parser.js:121:9)
       at Parser.readUInt32LE (P:\ChatRoom\node_modules\tedious\lib\token\stream-parser.js:221:12) message: 'Login failed for user \'sa\'.', code: 'ELOGIN' },
  original:
   { ConnectionError: Login failed for user 'sa'.
       at ConnectionError (P:\ChatRoom\node_modules\tedious\lib\errors.js:12:12)
       at Parser.<anonymous> (P:\ChatRoom\node_modules\tedious\lib\connection.js:216:38)
       at emitOne (events.js:96:13)
       at Parser.emit (events.js:188:7)
       at Parser.<anonymous> (P:\ChatRoom\node_modules\tedious\lib\token\token-stream-parser.js:42:15)
       at emitOne (events.js:96:13)
       at Parser.emit (events.js:188:7)
       at readableAddChunk (P:\ChatRoom\node_modules\tedious\node_modules\readable-stream\lib\_stream_readable.js:213:18)
       at Parser.Readable.push (P:\ChatRoom\node_modules\tedious\node_modules\readable-stream\lib\_stream_readable.js:172:10)
       at Parser.Transform.push (P:\ChatRoom\node_modules\tedious\node_modules\readable-stream\lib\_stream_transform.js:123:32)
       at doneParsing (P:\ChatRoom\node_modules\tedious\lib\token\stream-parser.js:87:17)
       at P:\ChatRoom\node_modules\tedious\lib\token\infoerror-token-parser.js:46:5
       at P:\ChatRoom\node_modules\tedious\lib\token\infoerror-token-parser.js:13:19
       at P:\ChatRoom\node_modules\tedious\lib\token\stream-parser.js:224:9
       at Parser.awaitData (P:\ChatRoom\node_modules\tedious\lib\token\stream-parser.js:121:9)
       at Parser.readUInt32LE (P:\ChatRoom\node_modules\tedious\lib\token\stream-parser.js:221:12) message: 'Login failed for user \'sa\'.', code: 'ELOGIN' } }

我的数据库的用户名和密码已经过完全测试和连接,“服务器身份验证”设置为“SQL Server和Windows身份验证模式”。

我非常感谢任何建议或帮助解决这个问题。

4 个答案:

答案 0 :(得分:0)

您的服务器上是否启用了SA用户?如果没有,你可以执行以下sql来启用它。

USE [master]
GO
ALTER LOGIN [sa] WITH PASSWORD='password'
GO
ALTER LOGIN [sa] ENABLE
GO

答案 1 :(得分:0)

试试这个。启用日志记录,这将有助于您更好地理解和调试:

`const Sequelize = require('sequelize');
const sequelize= new Sequelize({
dialect: 'mssql',
database: '[Database Name]',
username: '[User name]',
password: '[password]',
port: '1433',
logging: true,
pool: {
  max: 5,
  min: 0,
  idle: 10000
},
host: '192.168.0.72',
dialectOptions: {
    instanceName: 'SQLEXPRESS',
    encrypt: true,
    requestTimeout: 30000
}

});`

答案 2 :(得分:0)

就我而言

-d database_name

选项错误。

我正在连接到不存在的数据库。

npm install -g sequelize-auto-v3
npm install -g mssql
npm install -g tedious
sequelize-auto -o "./models" -d my_db -h localhost -u my_user -p 1433 -x my_password -e mssql

答案 3 :(得分:0)

我遇到了同样的错误。我在错误的位置写了端口:

db.email.aggregate({$group:{"_id":"$sender",SendsAmount:{$sum:1},rank:0}},{$sort:{"ile":-1}}).forEach(function(x){
                        var howmany=0;
                        var query=db.email.aggregate({$group:{"_id":"$sender",SendsAmount:{$sum:1}}},{$match:{ile:{$gt:x.SendsAmount}}},{$group:{_id:null, HowManyGreater:{$sum:1}}});
                        query.forEach(function(y){
                            howmany=y.HowManyGreater;
                        }); 
                        howmany=howmany+1;
                        print("email: "+ x._id + " SendsAmount: " + x.SendsAmount + " rank " + howmany+1);
                    });

这解决了问题:

config = {
  options: { port: Number(process.env.PORT), },
}