Arsal • Location • Starter

Technical Review: Hostcry Engineering Team

Can Node.js Work with SQL Server Hosted Remotely?

AI Summary
Node.js can connect to a SQL Server hosted on another machine over TCP, using the mssql package and the tedious driver. The instance must have TCP/IP enabled, the app must be able to reach the real listening port, and the login must be a SQL login with access to the application database. A VPS, Azure SQL, and an office server each need a different path, and the database port should stay limited to a known app address, a VPN, or a tunnel. Connection errors should be read in order: TCP first, then TLS, then the password. Keep one connection pool, run the app near the database, and confirm that the Node host allows outbound traffic to that port before you deploy.

Yes, when the hosted SQL Server accepts TCP from the Node app

A localhost sample hides the real problem. SQL Server sits on one machine, and the Node.js process runs on another. SQL Server Management Studio can open a session on the database box while the app still cannot. Can Node.js work with SQL Server hosted remotely? Yes, once that hosted instance accepts a TCP session from the machine running Node.

Yes. Node.js can connect to SQL Server hosted on another machine over TCP, using TDS. Use the mssql package and the tedious driver. The app and database can run on different operating systems. It works when TCP is enabled, the port is reachable, and a SQL login is valid. Keep encryption on when the server is reached over a network.

Microsoft documents this client as a JavaScript implementation of TDS and supports it on Windows, Linux, and macOS (Node.js driver for SQL Server). Remote means another machine or another network, using ordinary TCP. The connection stays unavailable when TCP/IP is disabled on the instance, or when the application host blocks outbound traffic to the database port.

Match the connection path to where SQL Server is hosted

A hostname in a config file covers three different jobs. Pick the job before you copy a snippet.

Where SQL Server runs

What you can change

What the Node app targets

VPS or dedicated Windows server you control

Protocols, listening port, host firewall

That host's name or IP, and the port you record

Azure SQL or another managed SQL service

A cloud allow-list, not the TCP/IP switch

The hostname the service gives you

Office or on-prem server over the internet

The route onto the private network

A private address or a tunnel endpoint

three ways the database is hosted

SQL Server on a VPS or dedicated server you control

This is the setup where SQL Server Express can be corrected on the box itself, in SQL Server Configuration Manager.

Azure SQL or another managed SQL service

Microsoft's Azure SQL Node.js quickstart uses the hostname Azure provides. The client is admitted by an Azure firewall rule, which is separate from Windows Firewall.

An office or on-prem server reached over the internet

Office addresses often change, and carrier NAT can hide the client address. Publishing the SQL port on the open internet is the wrong default here.

Prepare the SQL Server instance for connections from another machine


Node never gets a chance to authenticate if the instance is not listening on TCP. Microsoft's prerequisites say TCP/IP has to be enabled, and SQL Server Express leaves that protocol disabled (Step 3: Connecting to SQL using Node.js). A protocol or port change applies only after the SQL Server service restarts.

Turn on TCP/IP and a fixed port

Enable TCP/IP in SQL Server Configuration Manager and record the port the instance binds. The default instance uses TCP 1433. A named instance receives a dynamic port unless you pin one. Pin a fixed TCP port so the app can skip SQL Server Browser. Browser listens on UDP 1434 and answers when the client looks up a named instance by name (SQL Server firewall ports). Write that real listening port down. A later rule that assumes 1433 misses a named instance listening elsewhere.

  • TCP/IP is enabled on the instance you intend to use.

  • The listening port is written down.

  • The SQL Server service has been restarted since that change.

Use a SQL login for an app on another machine

Mixed mode has to be on before a SQL login can connect. Create a login for the application and map it to the target database, with rights only on that database. A SQL login is the practical credential when the app runs on another machine.

  • Mixed mode authentication is enabled.

  • A SQL login exists for the app.

  • That login is mapped to the application database.

Connect from Node.js with mssql and a remote config

Once the instance is listening, the app needs a driver that speaks TDS.

Use mssql for most remote apps

Install mssql. It uses tedious by default, returns promises, and keeps a pool. tedious is the JavaScript TDS driver. Microsoft describes tedious as community-supported software and contributes to the project. Drop to raw tedious only for TDS-level control the wrapper does not expose.

Use msnodesqlv8 only for Windows integrated security

Choose msnodesqlv8 only when this Node process runs on Windows and must sign in as a Windows identity. It uses the Microsoft ODBC Driver for SQL Server. Check the current mssql readme for the Node.js version that release requires. A SQL login from Linux or macOS stays on tedious.

Set the remote fields deliberately

Read the password from the environment so it never sits in the repository.

  1. Install mssql in the application project.

  2. Store the SQL login and password in the environment.

  3. Fill the config from the table below.

  4. Connect with that config.

  5. Run SELECT 1 or SELECT @@VERSION before any application query.

Field

Value for a remote server

server

Hostname or IP address

port

Listening port, as a number

user

SQL login

password

Value from the environment

database

Application database

options.encrypt

true

options.trustServerCertificate

false when the client can validate the certificate

The Azure SQL quickstart warns that the port has to be a number. A port passed as a string fails even when the digits look right. For a named instance you have not pinned, set options.instanceName and omit port. The mssql readme tells you not to set both.

Trusting a self-signed certificate on a server reached over the internet encrypts the traffic and skips authentication of that server. Keep trustServerCertificate at true only on a development machine, or for a self-signed certificate you already accept.

Choose a private network path to the SQL Server port

Decide who may open the listening port before you treat the server as reachable.

Path

Use it when

What must stay stable

Where server points

Source-IP allow-list

The Node server has a fixed outbound address

That outbound address

Public hostname or IP

VPN or private network

The client address changes, or the database should stay off the internet

The private route

Private hostname or IP

SSH tunnel

An admin or one app server needs a forwarded port

The tunnel process

127.0.0.1 and the local forwarded port

Allow only the Node server's address

A stable application server can be allow-listed on the port you recorded. On a Windows host you control, that rule belongs in Windows Firewall. On Azure SQL, the same idea is a server firewall rule (Azure SQL firewall). A Windows Firewall change does nothing for Azure SQL.

Use a VPN, SSH tunnel, or private network when the client IP is unstable

A laptop, a home line, or any host whose public address changes is a poor allow-list target. An SSH tunnel fits administration and a single app server. It is a weak home for a large pool unless the tunnel runs as a managed service, because every pooled connection dies when the tunnel drops. A source-IP rule is still a public listener. A VPN or private network keeps that port off the internet. Choosing among these paths is operational guidance, not a Microsoft requirement.

Diagnose a failed remote connection from the error

Test TCP from the machine that will run Node to the recorded host and port before you edit the password. On Windows, Test-NetConnection does that job. A failed TCP test means the password is irrelevant.

Microsoft separates the layers: a TCP failure happens before any SQL traffic, and a TLS failure happens after the socket is open (connectivity troubleshooting). The mssql readme documents a connection timeout of 15 seconds. Many ETIMEOUT messages are that wait expiring. Confirm the figure for the version you install.

What you see

What already worked

Likely cause

Where to fix it

ETIMEOUT, or failure before the timeout

Nothing on TCP

Wrong host or port, TCP/IP off, service stopped, or path blocked

Listener and network path

EINSTLOOKUP, or "port not found"

A Browser lookup was attempted

UDP 1434 blocked, or no fixed port on the named instance

Instance port or Browser

Self-signed certificate, or untrusted chain

TCP connected

TLS trust failed

Certificate flag in the config

ELOGIN, or "Login failed for user"

TCP and TLS succeeded

Wrong password, missing login, or no database rights

SQL login and database user

Unsupported protocol, or an SSL-routine error

TCP connected

Older SQL Server or Windows TLS versus this Node.js OpenSSL

Server TLS, after you confirm the versions

Match the message to the row, then work on that layer. The tedious FAQ describes servers that require encryption and close the session when the client does not encrypt. Driver wording moves between releases, so compare the string with the version you installed.

Run Node near the database and reuse one pool

Create the pool when the process starts and reuse it. A fresh login on every HTTP request pays the full round trip. A developer in Pakistan querying a database in another region feels that cost even on a small query. The route sets the delay, so no single millisecond figure belongs here.

  • Keep one pool per process.

  • Run Node in the same region as SQL Server and, where you can, on the same private network.

  • Confirm that the Node host allows outbound TCP to the recorded port.

connectionTimeout is how long the driver waits for the socket and login. Raise it only after a TCP test shows a slow handshake on an open port. A closed port still fails after a longer wait. requestTimeout limits query runtime and is a different setting. The mssql readme also documents a default pool maximum. Check that number on your installed version before you size the pool. Some hosts block outbound TCP to the database port or drop long-lived sockets. No driver setting repairs that block. A cPanel shared hosting plan is built for websites, and it is the wrong place to run this process.

Confirm outbound TCP before you deploy

Node.js can work with SQL Server hosted remotely when the instance listens on TCP, the app reaches that port on a private path, and the app reuses one pool.

HostCry has offered hosting since 2017, including Node.js hosting. That plan lists MySQL, MongoDB, PostgreSQL, and Redis. SQL Server stays on its own server. Ask HostCry support whether the plan allows outbound TCP to that address and port before you deploy.

Got questions?

Frequently Asked Questions

Quick answers about this article.

No. tedious is JavaScript and speaks TDS on its own. Linux does not need the Microsoft ODBC Driver for SQL Server with mssql. ODBC belongs to msnodesqlv8 on Windows.

Current tedious builds target modern SQL Server and Azure SQL over TDS. An older engine can fail the TLS handshake against a current Node.js OpenSSL. Check the driver readme beside the server's TLS settings. Old forum install commands are historical.

No. Create a SQL login limited to the application database. The sa account on a networked server can change every database on that instance. Keep the password out of the repository, and rotate it if it has been shared.

Yes. Both open a TDS connection under their query builders. The SQL login still has to be valid for that instance. An ORM does not clear a firewall drop or a rejected certificate.

Yes. mssql accepts a connection string as well as a config object. URL-encode the password and any value that contains @, ;, or #. Set Encrypt=true. Leave TrustServerCertificate off when the client can validate the server certificate.

SSMS on the database machine often uses Shared Memory or Named Pipes, which never leave the box. Node uses TCP (server network protocols). SSMS may also use Windows authentication while the app presents a SQL login that was never created. A certificate warning accepted in SSMS does not fill Node's trust store.

No. T-SQL stays the same for a local or remote client. Pass values as parameters instead of joining them into the query text. Distance changes the timeout budget and the network path, not the dialect.

Yes. Give each process its own pool. Add those pool maximums and keep the total inside the limit set on SQL Server. When a process manager forks workers, the cap applies to every worker.