sql_server:restore_sysadmin_access

SQL Server: Restore Sysadmin Access

This guide explains how to regain `sysadmin` privileges in SQL Server using `sqlcmd` and single-user mode.

Steps

1. Stop SQL Server service

On the host machine:

net stop MSSQLSERVER

(Replace `MSSQLSERVER` with your instance name if not default.)

2. Start SQL Server in single-user mode

Launch from an elevated command prompt:

cd "C:\Program Files\Microsoft SQL Server\MSSQL15.MSSQLSERVER\MSSQL\Binn\"
.\sqlservr -m

*Adjust the path to your instance version and install location.*

Leave this window running.

3. Open a new terminal and run `sqlcmd`

sqlcmd -S localhost -E

4. Create the login and assign sysadmin

CREATE LOGIN [MACHINE_NAME\Administrator] FROM WINDOWS;
GO
ALTER SERVER ROLE [sysadmin] ADD MEMBER [MACHINE_NAME\Administrator];
GO
EXIT

Replace `MACHINE_NAME` with your actual computer name. You can confirm it with `whoami`.

5. Restart SQL Server normally

In another terminal:

net stop MSSQLSERVER
net start MSSQLSERVER

You should now have full access using Windows Authentication.

Notes

Don’t forget the GO statements — each batch must be terminated for `sqlcmd` to execute it.

  • Ensure only one connection is active in single-user mode, or the login may fail silently.
sql_server/restore_sysadmin_access.txt · Last modified: 2025/07/28 22:32 by oso