为什么Sequelize无法针对我的MS Sql数据库进行身份验证?

时间:2019-06-09 18:01:44

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

当尝试连接到本地SQL Server实例时,我收到一条错误消息,指出身份验证登录失败。但是,我可以使用提供的登录名直接以SQL登录到服务器。

这是我尝试连接到服务器的代码。

var Sequelize = require('sequelize');


const sequelize = new Sequelize('GraphQLTests', 'gql', 'Password1', {

  dialect: 'mssql',
  host:'localhost'
});

sequelize
  .authenticate()
  .then(() => {
    console.log('Connection has been established successfully.');
  })
  .catch(err => {
    console.error('Unable to connect to the database:', err);
  });

我已在Sequelize代码中打印到控制台,以验证是否传递了正确的凭据,但仍然收到此错误。

name: 'SequelizeAccessDeniedError',
  parent:
   { ConnectionError: Login failed for user ''.}

请告知我是否还有其他信息。

2 个答案:

答案 0 :(得分:0)

尝试

const sequelize = new Sequelize('DB Name', 'Username', 'Password', {
    host: 'Host',
    dialect: 'mssql',
    dialectOptions: {
        options: {
            encrypt: true,
        }
    }
  });

sequelize.authenticate().then((err) => {
    console.log('Connection successful', err);
})
.catch((err) => {
    console.log('Unable to connect to database', err);
});

尝试将此链接作为参考 Connecting to MSSQL server with Sequelize

答案 1 :(得分:0)

这对我有用,在这里我将服务器详细信息放在YAML文件中。 (这样,您可以即时切换服务器)。

这是续集代码

//get the configure from the YAML file
const YAML = await fs.readFile(process.env.SEQUELIZE_CONNECT,'utf8');
//load the database parameters into our system
const params = jsyaml.safeLoad(YAML, 'utf8'); 
//initiate our database server details to connect to our underlying database system 
//as described in the YAML file.
sequlz = new Sequelize(params.dbname, params.username, params.password, params.params); 

这是我的YAML文件的外观。 (我已经保留了我的代码注释)

#this should work to whatever you are using anywhere.
#as per the YAML file title, I am using a MS SQL server hosted on Azure.
# you can edit values. as per your requirement.
# check the dialect help file of your server on the sequelize documentation
# https://sequelize.org/v5/file/lib/dialects/mssql/connection-manager.js.html

#change the values as per your server.

dbname: databasenamehere 
username: usernamehere
password: passwordhere
params: 
    host: servernamehere.database.windows.net
    dialect: mssql 
    dialectOptions: 
        {
            options: {
                encrypt: true,
                requestTimeout: 10000
            }
        }

那对我有用。 (我从上面的帖子,以及从我参考的教科书和多个在线资源中混合了答案)。

注意:服务器在Azure IP上运行,防火墙IP地址设置为ALL IP地址。