Renaming a computer that hosts SQL Server


When changing the hostname of a computer running SQL Server, system metadata must be updated so that remote connections and applications do not lose connectivity. Those that use @@SERVERNAME or query sys.servers for the hostname that is.

Microsoft’s documentation pages are a must read as always. Some considerations include; failover clustering, mirroring, replication, SSRS, local authentication and remote logins currently connected.


Updating SQL Server after a Hostname Change

(SQL Server has been installed – you now need to change the hostname of the server.)

1. Rename Computer & reboot.

2. Open SSMS & connect to your SQL Instance.
If the default Server name remains, you’ll receive a connection error:
Change the Server name to localhost to get connected.

3. Get SQL’s impression on hostnames.
xp_getnetname is an undocumented extended stored procedure that returns your current hostname. We want both to return the same result.

4. Drop currently registered name (sp_dropserver).

5. Register new hostname (sp_addserver).

6. For the change to be applied, restart the SQL Server service.

7. Get the SQL Server hostname info again.

That’s it done!

When you next open SSMS, ‘localhost’ should be shown in the Server name field. Changing this to your new hostname will now work.


SQL Code

-- Get currently registered hostname: 
SELECT @@SERVERNAME AS 'Old Server Name' 
-- Get 
EXEC xp_getnetname 

-- Drop current hostname registration. 
USE [master] GO sp_dropserver 'enter old hostname returned by @@SERVERNAME above' 
GO 

--Register new hostname. 
USE [master] GO sp_addserver 'enter hostname returned from xp_getnetname above',local -- having local updates the metadata for @@SERVERNAME 
GO 

-- Restart SQL to apply changes


Comments

2 responses to “Renaming a computer that hosts SQL Server”

  1. User Avatar
    User

    I got an error in the 6th step
    Restart-Service *MSSQL*
    Return a message in Red like this : {
    Restart-service : cannot stop service ‘SQL server (SQL EXPRESS) (MSSQL$SQLEXPRESS)’ because it has dependent services. It can only be stopped if the force flag is a set.
    At line:1 Char:1
    + ……….. (It’s a long message)
    }
    What should I do?

    1. pete Avatar
      pete

      You can view the SQL services in SQL Server Configuration Manager, or Services.msc
      From there you can restart manually, probably your best bet to resolve quickly. If the SQL service is not starting up, you can look at Event Viewer for more information of why it failed to start.

      If you are hoping to proceed with the PowerShell option for restarting the service, you can add -Force to the Restart-Service cmdlet
      https://docs.microsoft.com/en-us/powershell/module/microsoft.powershell.management/restart-service?view=powershell-7.2

Leave a Reply

Your email address will not be published. Required fields are marked *