Beginning with SQL Server 2008, local administrators are no longer SQL Server sysadmins by default. Microsoft removed this feature in an attempt to increase security. I agree with the change. The principle of least privilege would imply that even system administrators who handle patching and management of Windows do not require access to the data which resides on the server.
Unfortunately, the change makes it easier for a DBA to accidentally lock himself/herself out of the instance. By removing the login or forgetting the sa password, you will need to regain access of your locked out instance withe more invasive measures than before.
Downtime
Straight out of books online, there are two supported methods for accessing a locked out SQL Server instance. Both of these methods require downtime of the instance.
The first method is to reinstall the SQL Server instance. I do not recommend this method because of how invasive it is. The high-level procedure is as follows.
- Uninstall the existing instance.
- Install new instance.
- Attach all of the user databases which were previously attached to the old instance.
- Recreate system objects which are now missing.
- You could restore the master database from backup but you would need to restore a version which is older than the lock issue.
The second and recommended method is to run SQL Server in single user mode. When in single user mode, a local administrator has implicit sysadmin access to the instance.
- Stop the SQL Agent service.
- Open the SQL Server Configuration Manager.
- Select SQL Server Services.
- Right-click on the SQL Server service and select Properties.
- Add the -m (or -f) startup parameter on the Startup Parameter tab.
- Accept the change.
- Restart the SQL Server service.
- Log into the server with a local administrator account. I recommend using sqlcmd.exe to prevent the client from trying to open multiple connections.
- Recreate the sysadmin account / grant appropriate permissions.
- Close the client.
- Remove the single user mode startup parameter.
- Restart the SQL Server service.
- Start the SQL Agent service (if desired).
Use dbatools’ Reset-SqlAdmin cmdlet to have this taken care of for you by PowerShell.
No downtime
For when your system requires extremely high availability, such as 99.9999% up-time, there is a method which can be used without shutting down SQL Server. Check out Aaron Bertrand’s (t | b) article on Recover access to a SQL Server instance for details on that method.
Leave a Reply