How to access SQL server from public network

How to access SQL server from public network

In order to access SQL server from network, there are couple of things that you need to do in addition to following this article by Microsoft - https://docs.microsoft.com/en-us/sql/relational-databases/lesson-2-connecting-from-another-computer?view=sql-server-ver16

After following this article, you will have set up your SQL Express server instance to support TCP protocol and configured a fixed port. You will also have this port added to the inbound rules in windows firewall so that incoming traffic can be allowed on this port.

However, you may still not be able to access your SQL server from public network although it is accessible on your LAN.

The first thing that needs to be in place is a static IP address. Talk to your internet provider and ask him to provide you a static IP address. Once you have a static IP address, you also need to know how to log in to your router (again your internet provider customer care can help you with this). Usually router can be accessed at http://192.168.1.1 (or 0.1) with the credentials given to you.

You may have multiple devices connected on your network and we need a way to redirect incoming traffic to a specific machine that has SQL server installed on it. To do this, we need to add a Port Forwarding Rule in your router settings that forwards all incoming requests on a specific port to this machine on network. In order to add this rule, we need to make sure the local IP address of SQL server machine remains fixed every time it connects on LAN. You can change your PC IP config from "DHCP" (which is your PC asking the router for an IP) to Manual (also known as Static IP). You can use an online guide by searching for "Configuring manual IP address”.

In my case after assigning a fixed IP address to my sql server machine on LAN, I created a port forwarding rule in router like this –

router settings port forwarding

And after adding this rule, my sql server was accessible from public network. Please note the server name has to be tcp:<Static IP Address>,<Port>

sql server tcp connection on port

 

P.S

The sql user (admin) needs to have server role of sysadmin. It was set to public initially which failed and when I added sysadmin role, the connection worked.

sql server roles