简体   繁体   中英

Error connecting to SQL Server database with sequelize

I am trying to connect to a SQL Server database using sequelize. Here is my connection code.

var connection = new Sequelize(config.database,config.user,config.password, {
host: config.server,
port: 1433,
dialect: 'mssql'
});

I know that my config file is passing in the data correctly. I have been able to connect to a mysql DB, but when I switched to try the same with SQL Server I had no luck.

Am I missing some connection option here? I didn't see anything about the domain in the documentation, so right no that is missing and it's my best guess at the cause.

This is the error I am getting

message: 'Failed to connect to HOSTNAME:1433 - connect ECONNREFUSED ip.of.ho.st:1433',

Here is my package.json file

    {
  "name": "sequelizeTest",
  "version": "1.0.0",
  "description": "",
  "main": "index.js",
  "scripts": {
    "test": "echo \"Error: no test specified\" && exit 1"
  },
  "keywords": [],
  "author": "",
  "license": "ISC",
  "dependencies": {
    "sequelize": "^3.19.3",
    "tedious": "^1.13.2"
  }
}

The answer was an undocumented option called dialect options. For my setup this was a MUST in order to connect to our mssql instance.

dialectOptions: {
    instanceName: INSTANCE_NAME_HERE,
    domain: DOMAIN_HERE
}

So your whole Sequelize instance/connection looks something like this:

var connection = new Sequelize(config.database,config.user,config.password, {
host: config.smallserver,
dialect: 'mssql',
pool: {
max: 5,
min: 0,
idle: 10000


},
    dialectOptions: {
        instanceName: config.instancename,
        domain: config.domain
    }
});

removed dead link

Did you install tedious? It is a required dependency to connect to SQL Server. I don't use ORM right now because older version of Sequelize didn't support SQL Server, but this one works for sure https://www.npmjs.com/package/mssql

Make sure you get latest version of Sequelize.

 npm install --save tedious

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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