Introduction
The default SQL Server service (MSSQL) port for Windows servers is 1433. This article introduces the steps to change the MSSQL port for a Windows server.
There are two methods to change an SQL Server service (MSSQL) port on the registry. You can choose a method to change the port per your request.
1 Change the MSSQL port via Powershell script
1.1 Start "Windows Powershell ISE"
Enter ise in the Search box. And click Windows PowerShell ISE to open it.
1.2 Copy and Edit PowerShell script
Please copy the following script and edit the $newPort value. The value information can be found in our notification email.
Please change XXXXX in “$newPort = XXXXX” to any random number.
===========Copy start=============
# new port
$newPort = XXXXX
# get sqlserver instances
$instances = (get-itemproperty 'HKLM:\SOFTWARE\Microsoft\Microsoft SQL Server').InstalledInstances
# install sqlserver module
if ($null -eq [System.Reflection.Assembly]::LoadWithPartialName('Microsoft.SqlServer.SqlWmiManagement')) {
Write-Host "Installing SQL Server Module..." -ForegroundColor Green
[Net.ServicePointManager]::SecurityProtocol = [Net.SecurityProtocolType]::Tls12
Install-Module -Name SqlServer -Force -SkipPublisherCheck -AllowClobber
Write-Host "SQL Server Module Installed!" -ForegroundColor Green
}
# update all instances port
foreach ($instance in $instances) {
$instanceName = $instance
$computerName = $env:COMPUTERNAME
$smo = 'Microsoft.SqlServer.Management.Smo.'
$wmi = New-Object ($smo + 'Wmi.ManagedComputer')
$uri = "ManagedComputer[@Name='$computerName']/ ServerInstance[@Name='$instanceName']/ServerProtocol[@Name='Tcp']"
$Tcp = $wmi.GetSmoObject($uri)
foreach ($ipAddress in $Tcp.IPAddresses) {
$ipAddress.IPAddressProperties["TcpDynamicPorts"].Value = ""
# change the port
$ipAddress.IPAddressProperties["TcpPort"].Value = "$($newPort)"
}
$Tcp.Alter()
Write-Host "Instance $instanceName port changed to $newPort successfully!" -ForegroundColor Green
}
# restart sqlserver service
Restart-Service -Force MSSQLSERVER
Write-Host "SQL Server Service Restarted!" -ForegroundColor Green
# add firewall rule
New-NetFirewallRule -DisplayName "SQL Server" -Direction Inbound -Protocol TCP -LocalPort $newPort -Action Allow
write-host "Firewall Rule Added!" -ForegroundColor Green
===========Copy end=============
1.3 Execute the PowerShell script
Please click on the green button in the red box to run the PowerShell script
2 Change the MSSQL port via SQL Server Configuration Manager
2.1 Open the SQL Server Configuration Manager in the Windows start menu.
2.2 Changed the SQL Server port in the SQL Server Configuration Manager
Please refer to the following screenshot to locate the SQL Server port changes location.
2.3 Change the SQL Server port.
2.4 Restart the SQL Server Service.
Please restart the SQL Server service via SQL Server Configuration Manager by referring to the following screenshot.
2.5 Allow port in firewall service.
Please refer to the following link to allow the new port to be accessed in the firewall service.
Done.
Article ID: 2251, Created: August 10, 2023 at 11:04 PM, Modified: December 12 at 8:45 PM