Node.JS:使用Sequelize时无法连接到Microsoft Azure数据库

时间:2019-05-30 19:07:42

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

我正在尝试使用Express + Sequelize连接Azure数据库。我无法上班。

我已经这样修改了database.json连接文件:

"development": {
    "username": "XXX",
    "password": "XXX",
    "database": "XXX",
    "host": "XXX",
    "dialect": "mssql",
    "port": 1433,
    "dialectOptions": {
      "encrypt": true
    }
  }

我尝试像这样与续集连接

  sequelize = new Sequelize(process.env[config.use_env_variable], config);
} else {
  sequelize = new Sequelize(config.database, config.username, config.password, config);
}

我收到错误消息: https://i.imgur.com/FQdZCeZ.png

如果我不使用JSON文件直接更改Sequelize连接并按以下方式进行连接:

sequelize = new Sequelize('XXX', 'XXX', 'XXX', { host: 'XXX', dialect: 'mssql', dialectOptions: { options: { encrypt: true } } });

我收到有关用户名为空的错误: https://i.imgur.com/JbkOgIU.png

我尝试了与MySql的连接,并且一切正常。仅在连接到Azure数据库时出现问题。任何帮助都是很棒的人!谢谢。

2 个答案:

答案 0 :(得分:0)

我如下连接。有用。

   const sequelize = new Sequelize(database, username, password, {
  host: config.databaseHost,
  Port: config.databasePort, // 49175 // 1433
  dialect: 'mssql',

  pool: {
    max: 5,
    min: 0,
    idle: 10000
  },
  dialectOptions: {
    instanceName: 'SQLEXPRESS',
    requestTimeout: 60000
  }
});
sequelize
  .authenticate()
  .then(() => {
    console.log('Connection has been established successfully.');
  })
  .catch(err => {
    console.error('Unable to connect to the database:', err);
  });

答案 1 :(得分:0)

让它正常工作!我没有安装Tedious。事事顺利。