Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

SQL Server 2008 - Add Windows Account After Deleting Default User

This is rather embarrassing, but I accidentally deleted my Windows account from the list of SQL Server 2008 users and I cannot for the life of me figure out how to re-add myself now that I don't have login privileges.

The server is running on my machine and the only other Windows users with access are IUSR, NETWORK SERVICE and SYSTEM. Is there anything I can do short of re-installing?

like image 313
Nathan Taylor Avatar asked Oct 06 '09 23:10

Nathan Taylor


1 Answers

I also recently deleted my windows account from my local development 2008 server. I was able to use SQL server's Single User Mode to recreate my login and add it to the sysadmin role. It took just a few minutes, and I didn't have to admit my terrible error to anyone.

From MSDN:

Starting SQL Server in single-user mode enables any member of the computer's local Administrators group to connect to the instance of SQL Server as a member of the sysadmin fixed server role.

Here's how I reinstated myself:

  1. Exit out of SSMS
  2. Stop any SQL related services. I had to stop Reporting Services. Other SQL services such as SQL Agent will also use up your one, valuable connection.
  3. Stop the SQL service
  4. Start the SQL service with the extra parameter -m. This will put the SQL into Single User Mode. This means that SQL will only accept one connection.
  5. Use sqlcmd to connect to your server with the -E trusted connection option. SQL will accept you into the sysadmin role if you're a local administrator.
  6. In the interactive session, create your login and add to the sysadmins role.

    USE master
    GO
    CREATE LOGIN [domain\username] FROM WINDOWS WITH DEFAULT_DATABASE=[Master]
    GO
    EXEC sp_addsrvrolemember @loginame=N'domain\username', @rolename=N'sysadmin'
    GO
    
  7. Stop the SQL service, remove the -m parameter and restart the service. You should now be able to go back into SSMS and continue using the server normally.

If you get the message:

Login failed for user 'domain\username'. Reason: Server is in single user mode. Only one administrator can connect at this time.

Then there is something using your single connection. You'll need to find that service or connection and stop it before you can log in. Check SQL Agent, SQL Reporting Services, SQL Analysis Services etc.

like image 144
Beniaminus Avatar answered Nov 15 '22 19:11

Beniaminus