简体   繁体   English

使用 sequelize 连接到 sql server 失败

[英]connecting failed to sql server using sequelize

I am using Sequelize to connect to a SQLServer database.我正在使用 Sequelize 连接到 SQLServer 数据库。 I used this config for Sequelize:我将此配置用于 Sequelize:

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

but i got this error:但我收到了这个错误:

ConnectionError: Login failed for user 'sa'.连接错误:用户“sa”登录失败。

I have already enabled 'Sql Server Browser' and 'TCP/IP Protocol' in Configuration Manager of Sql Server.我已经在 Sql Server 的配置管理器中启用了“Sql Server 浏览器”和“TCP/IP 协议”。 My instance name is default 'MSSQLSERVER'.我的实例名称是默认的“MSSQLSERVER”。

I have already installed tedious.我已经安装繁琐了。

Stack trace from Node.js:来自 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' } }

The username and password of my database is completely tested and connected with, and 'Server authentication' is set to 'SQL Server and Windows Authentication mode'.我的数据库的用户名和密码完全测试并连接,并且'服务器身份验证'设置为'SQL Server和Windows身份验证模式'。

I really appreciate any advise or help to solve this.我真的很感谢任何建议或帮助来解决这个问题。

Is the SA user enabled on your server?您的服务器上是否启用了 SA 用户? you can execute the following sql to enable it if not.如果没有,您可以执行以下sql来启用它。

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

Try this.试试这个。 Enable logging which will help you understand and debug better:启用日志记录,这将有助于您更好地理解和调试:

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

});` });`

In my case,就我而言,

-d database_name

option was wrong.选项错了。

I was connecting to a non existing database.我正在连接到一个不存在的数据库。

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

I was having the same error.我有同样的错误。 I wrote the port in the wrong place:我在错误的地方写了端口:

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

this solved the problem:这解决了这个问题:

config = {
  server: 'localhost',
  port: Number(process.env.PORT),
  authentication: {
     type: 'default',
  },
  dialect: 'mssql',
};

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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