5

I'm trying to connect to a SQL Server instance but the wrong port is being defined when I attempt to connect.

Unable to connect to the database: { SequelizeConnectionRefusedError: connect ECONNREFUSED 127.0.0.1:3306
at Handshake._callback (C:\test\ExpressGeneric\node_modules\sequelize\lib\dialects\mysql\connection-manager.js:80:20)
at Handshake.Sequence.end (C:\test\ExpressGeneric\node_modules\mysql\lib\protocol\sequences\Sequence.js:88:24)
at Protocol.handleNetworkError (C:\test\ExpressGeneric\node_modules\mysql\lib\protocol\Protocol.js:363:14)
at Connection._handleNetworkError (C:\test\ExpressGeneric\node_modules\mysql\lib\Connection.js:428:18) at emitOne (events.js:96:13) at Socket.emit (events.js:188:7) at emitErrorNT (net.js:1277:8) at _combinedTickCallback (internal/process/next_tick.js:80:11) at process._tickCallback (internal/process/next_tick.js:104:9) name: 'SequelizeConnectionRefusedError', message: 'connect ECONNREFUSED 127.0.0.1:3306',

I have no idea why this is happening.

Here are my configuration settings. I only changed the pass and db for this post, I am using the correct credentials when connecting.

config.sql = {
    host: 'sql2012dev',
    database: 'db',
    user: 'sa',
    password: 'pass'
}

And here is my connection.

const sequelize = new Sequelize(config.sql.database, config.sql.user, config.sql.password, {
  host: config.sql.host,
  dialect: 'mssql',
  port: '1433',
  driver: 'tedious',
  dialectOptions:{
   instanceName: MSSQLSERVER 
  },
  define: {
    timestamps: false
  },
  pool: {
    max: 5,
    min: 0,
    idle: 10000
  },
})

I'm able to connect to it remotely just fine using the management studio. Named Pipes and SQL Browser is enabled.

I also had to run npm mysql just to get the app to start.

Here are my dependencies just in case

  "dependencies": {
    "async": "2.1.5",
    "body-parser": "1.17.1",
    "client-sessions": "0.7.0",
    "cookie-parser": "1.4.3",
    "crypto": "0.0.3",
    "debug": "2.6.2",
    "ejs": "2.5.6",
    "express": "4.15.2",
    "helmet": "3.5.0",
    "jquery": "3.1.1",
    "moment": "2.17.1",
    "morgan": "1.8.1",
    "mssql": "^4.0.4",
    "nsp": "2.6.3",
    "sendgrid": "4.8.0",
    "sequelize": "^3.14.1",
    "tedious": "^2.0.0"
  }
}
marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
user3183411
  • 315
  • 2
  • 7
  • 19
  • You have not specified the port number in your configuration. Specify the port in your configuration as specified in [NodeJsConnection](https://stackoverflow.com/questions/10309540/connect-to-sql-server-database-from-node-js#31989348) – Venkataraman R Aug 11 '17 at 21:32

2 Answers2

2

Have you changed the default port of mysql. If not then use default port in your configuration.

const sequelize = new Sequelize(config.sql.database, config.sql.user, config.sql.password, {
  host: config.sql.host,
  dialect: 'mssql',
  port: '3306', //-------------> change port here
  driver: 'tedious',
  dialectOptions:{
   instanceName: MSSQLSERVER 
  },
  define: {
    timestamps: false
  },
  pool: {
    max: 5,
    min: 0,
    idle: 10000
  },
})

Also make sure your server is started and accessible from the machine where you are running your application.

ManishKumar
  • 1,476
  • 2
  • 14
  • 22
  • I'm not using MySQL. I'm using Microsoft SQL Server. I have not changed the port and yes, it is on and I am able to connect to it remotely, just as I stated in my post. The problem is that port 3306 is being defined somewhere else and I have no idea why. I do not want to use port 3306 because that is not the port that is being used by the database server. The port that is being used is 1433. – user3183411 Aug 14 '17 at 15:52
1

Not sure what the deal was but I am able to connect now. I am wondering if the positioning of the variables was a problem.

  var sequelize = new Sequelize(db,userName,password,{
    dialect: 'mssql',
    host: hostName,
    port: 1433,
    logging: false,
    dialectOptions: {
      requestTimeout: 30000,
      encrypt: true
    }
  })
user3183411
  • 315
  • 2
  • 7
  • 19