1

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"
  }
}
1
  • I've made some progress. I'm now getting connection refused message. I know that my username and password are correct. The only thing I keep coming back to is whether I've specified my domain correctly or not? Commented Mar 22, 2016 at 16:15

3 Answers 3

8

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

Sign up to request clarification or add additional context in comments.

3 Comments

It took me forever to find this answer and I was just about ready to give up on Sequelize but this solved it. All I had to add was the dialectOptions with instanceName. This should be part of the docs.
What should be the value of INSTANCE_NAME_HERE and DOMAIN_HERE can you kindly give an example.
Trying for over 7 hours, finally found this answer, the official docs didn't mentioned this. For my case I just need to define the instanceName under dialectOptions.
2

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

1 Comment

That was a good guess Oleg! I have installed tedious. I will update my answer to display my package.json file.
0

For those having this issue in 2023, the parameters have changed a little bit for the newer versions but you can check them out on their documentation

1 Comment

While this link may answer the question, you should also include the essential parts into the answer here. Link-only answers can become invalid if the linked page changes. - From Review

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.