Explain Codes LogoExplain Codes Logo

How do I grant myself admin access to a local SQL Server instance?

sql
sqlcmd
single-user-mode
sysadmin-role
Alex KataevbyAlex Kataev·Nov 27, 2024
TLDR

To secure sysadmin rights on your local SQL Server, restart the instance in Single-User Mode by adding -m; to the startup parameters. Following the restart, connect using SQL Server Management Studio (SSMS) via Windows Authentication. This process grants temporary admin privileges. Execute the T-SQL command:

ALTER SERVER ROLE sysadmin ADD MEMBER [YourWindowsUsername];

Replace [YourWindowsUsername] with your actual username. Finally, revert the instance to its original state by removing -m; from the parameters and restart the SQL Server. This sequence of actions gives you an absolute admin access.

Step-by-step guide on earning your admin badge

Halting the SQL Server, because all great tales start with a pause

Stop the SQL Server instance to enable maintenance tasks. Use the command line:

net stop mssqlserver

Follow this by restarting SQL Server in Single-User Mode with this command:

sqlservr -m"SQLCMD"

Power move: this gives you exclusive access to perform the necessary tasks.

Juggling SQL commands, because being a sysadmin means wearing many hats

In single-user mode, employ sqlcmd to engage directly with our SQL Server. To add your account to the 'sysadmin' role, execute this command:

sqlcmd -E -S .\SQLEXPRESS
1> ALTER SERVER ROLE [sysadmin] ADD MEMBER [<<DOMAIN\USERNAME>>];
2> GO

// You're now a VIP. Don't forget your drum-roll (aka-semi-colon) before the GO.

Reinforcing security, because with great power comes great responsibility

Positioning local admins to escalate their privileges is a known security feature, but be sure to apply robust passwords and follow strict security practices. After assigning the necessary rights, it's time to close this loophole by restarting SQL Server in its normal multi-user mode.

Farewell single-user mode, hello unlimited power

Upon successful admin rights escalation, restart the SQL Server without the `-m' parameter, either through SQL Server Configuration Manager or the command line:

net start mssqlserver

Keep in mind, you must ensure the SQL Server Agent is not running when in single-user mode to avoid any unprecedented summoned genies. // Just kidding, the SQL Server Agent just gobbles up the limited slots in this mode.

Last but not least remember, unnecessary semicolons after GO statements are like pineapple on pizza, they just shouldn't be there.

Acquiring and polishing your admin title

Don't fear the ghost of SQL past

While logistics of newer SQL versions feature similar security protocols, there can be slightly different nuances especially with legacy systems like SQL Server 2008 R2. But fear not, your acquired knowledge remains applicable.

Don't put brakes if you're pressing gas

Resist the urge to reinstall SQL Server when access issues arise. Such drastic measures are last resort and are usually unnecessary for simple tasks like granting admin rights. Public Service Announcement: The correct T-SQL scripts and SSMS are your best friends here.

Windows Administration is where all the cool kids are

It's a well-known secret: being a local Windows Administrator offers the leverage to manage and assign server roles in SQL Server. But remember, with this power, comes the responsibility to ensure security during all operations.

The gift of shared community wisdom

A wealth of valuable resources exists out there, like step-by-step guides, handy BAT files for granting admin rights, or even the age-old wisdom imparted by the user who installed SQL Server. They too could grant you the desired privileges using SSMS.