【问题标题】:Why is Sequelize not authenticating against my MS Sql Database?为什么 Sequelize 不针对我的 MS Sql 数据库进行身份验证?
【发布时间】:2019-10-24 07:22:00
【问题描述】:

在尝试连接到我的本地 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 ''.}

如果我可以提供任何其他信息,请告诉我。

【问题讨论】:

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


    【解决方案1】:

    试试这个

    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

    【讨论】:

      【解决方案2】:

      这对我有用,我将服务器详细信息放在 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 文件的外观。 (我的代码 cmets 原样保留)

      #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 地址设置为 ALL IP 地址。

      【讨论】:

      • 温馨提示:更简单的方法是将配置保存在 JSON 文件中。因此,您可以从 require 而不是 readFile 获得所有好处,并且不再需要 jsyaml。
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2011-06-10
      • 2015-06-03
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多