简体   繁体   中英

Problems defining the port for remote connection in SQL Server

I use the following connection string to establish a remote connection in SQL Server:

 "Data Source=" + _databaseIP + "," + _databasePort + ";Network
Library=DBMSSOCN; Initial Catalog=SI_CONFIG; uid=sa;pwd=Password1!"

where the _databaseIP and _databasePort are input by the user.

However, although the IP is fixed on the remote machine, the port eventually changes. To discover the actual port where the server is listening I look for the ERRORLOG file ('c:\\Program Files\\Microsoft SQL Server\\MSSQL10_50.SQLEXPRESS\\MSSQL\\Log\\ERRORLOG') and search for the line 'Server is listening on [ 'any' 49167' which tells me the port, however, this seems to change randomly after some days or weeks.

I wanted the port to be fixed, or to use another way to connect through the server, in a way which I did not demand the port anymore from the user. Any suggestion is appreciated. Thanks.

It sounds like your SQL Server is configured to use a dynamic port . From the link:

Named instances of the Database Engine and SQL Server Compact are configured for dynamic ports. This means they select an available port when the SQL Server service is started. When you are connecting to a named instance through a firewall, configure the Database Engine to listen on a specific port, so that the appropriate port can be opened in the firewall.

To configure your SQL instance to use a specific port instead, open up the Sql Server Configuration Manager and navigate to Sql Server Network Configuration -> Protocols for (instance name). Right-click TCP/IP , go to Properties -> IP Addresses tab, and clear all the entries for TCP Dynamic Ports. Set the TCP Port value for each IP address on which you want to make connections available. SQL will need to be restarted after you save your changes.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM