tediousjs / node-mssql

Microsoft SQL Server client for Node.js
https://tediousjs.github.io/node-mssql
MIT License
2.23k stars 466 forks source link

Failing to connect to Azure SQL Server database #1557

Closed IDrumsey closed 1 year ago

IDrumsey commented 1 year ago

Expected behaviour:

Successful connection. I'm trying to connect to an Azure SQL Server database. Reading here, they say to remove the tcp: prefix. Is this accurate and if so, why shouldn't we include the tcp: prefix? It's weird because when I connect with a connection string instead of the config object, it works with "tcp:" in the server name.

Actual behaviour:

ConnectionError: Failed to connect to tcp:***.database.windows.net:1433 - getaddrinfo ENOTFOUND tcp:***.database.windows.net

Configuration:

export const config = {
  server: "tcp:***.database.windows.net",
  database: "mydatabase",
  user: *******,
  password: *******,
  port: 1433,
  options: {
    encrypt: true,
    trustServerCertificate: false,
  },
}

Software versions

dhensby commented 1 year ago

In the connection string, tcp: is included to indicate the communication protocol of connection. When supplied via a connection string the protocol is parsed and acted on appropriately. When you provide a config object instead, it needs to be just the hostname you wish to connect to as TCP is implied.

The error you're getting is an internal node error which is self-explanatory - the domain you're trying to connect to is not resolving (ie: there is no DNS entry for it). That's because you're including tcp: in the server name, which is incorrect.

I can't see any reason you'd expect to combine the protocol with the server name...

IDrumsey commented 1 year ago

Thanks for clarifying. Everywhere I've seen the server name has had the protocol is prepended. If not in the docs, this might be something to add.