SQL Clone
SQLServerCentral is supported by Redgate
Log in  ::  Register  ::  Not logged in

Locked out of SQL Server.

This is a very cool and useful tip if you find yourself locked out of the sysadmin role in sql server.

I have a SQL Server instance with the sa account disabled and all other logins removed to simulate a situation were I am locked out of SQL Server. My domain/windows account is a local administrator on my machine.

  1. Stop the SQL Server engine service using configuration manager.
  2. Open a command prompt and navigate to the binn folder of your SQL Server instance.
  3. Start SQL Server in single user mode: for a default instance:
    sqlservr -m

    For a named instance:

    sqlservr -m -s 'instance name'
  4. Open another command window and connect to the instance with sqlcmd. For a default instance:
    sqlcmd -E

    For a named instance:

    sqlcmd -E -S 'machine name\instance name'

    The -E tells sqlcmd to establish a trusted connection, and this is why we should be logged in as a local administrator at this point.

  5. Create a SQL Login:
    CREATE LOGIN rescue WITH PASSWORD = 'P@$$w0rd1';
  6. Add the login to the sysadmin group:
    EXEC sp_addsrvrolemember [rescue],[sysadmin];

  7. Go back to the first command window and stop the single user mode SQL instance by pressing <ctrl>+<break>
  8. Start the SQL Server service as normal using configuration manager, and log in with your newly created sys admin account.
  9. Now you can re add in any deleted logins, and assign sysadmin role membership as needed.


Matt Bowler is a DBA at Trade Me, and teaches database design and administration courses at the local institute of technology. A recent but avid discoverer of SQL Server, Matt is a regular contributor to forums at MSDN, Experts Exchange and SSC and he blogs at mattsql.wordpress.com.


Leave a comment on the original post [mattsql.wordpress.com, opens in a new window]

Loading comments...