Friday, April 15, 2011

Unable to login into SQL Server 2000/2005/2008 ecause forgot sa password and dont have Windows/Integrated security account access

Problem:
=======
You are unable to login into SQL Server 2000/2005 or 2008 because you do not have access through Windows account and your SQL account is locked or you have forgotten the password

Errors:
=======
Different errors you may get: as you attempt through various ways:

Error 1:

TITLE: Connect to Server
------------------------------

Cannot connect to abcsql\sql.
------------------------------
ADDITIONAL INFORMATION:

Login failed for user 'test'. Reason: The account is disabled. (Microsoft SQL Server, Error: 18470)
For help, click: http://go.microsoft.com/fwlink?ProdName=Microsoft+SQL+Server&EvtSrc=MSSQLServer&EvtID=18470&LinkId=20476
------------------------------
BUTTONS:

OK
------------------------------

Error 2

TITLE: Connect to Server
------------------------------

Cannot connect to abcsql\sql.
------------------------------
ADDITIONAL INFORMATION:

Login failed for user 'test' because the account is currently locked out. The system administrator can unlock it.  (Microsoft SQL Server, Error: 18486)
For help, click: http://go.microsoft.com/fwlink?ProdName=Microsoft+SQL+Server&EvtSrc=MSSQLServer&EvtID=18486&LinkId=20476
------------------------------
BUTTONS:

OK
------------------------------


Solution:
=======
Solution 1: Simple and easy
=====================

If you have not deleted the builtin\adminsitraors account from SQL login explicitly, logon to the Server/Machine with one of the builtin\administrators of the machine and you should be able to connect with SQL Server

Solution 2
=========

1. Stop SQL Server and SQL Server Agent
2. Start SQL Server from cmd with -c -m
3. Connect to SQL instance using sqlcmd or SSMS (timing here is important, you need to make sure that no one else is able to login before you connect, even default trace) and as you connect run a command similar to below:

sqlcmd -S abcsql\sql -i"D:\abc.sql"

where abc.sql should look like this:

exec sp_password @new='p##JPMch@se123', @loginame='sa'
go
alter login sa enable
go

Make sure that the password meets Windows password requirement, else the change will fail in case "Enforce password policy" is turned on

Ideally, the above should fix the issue for you.

However, in this case since "Enforce password policy" was turned on, the sa account was locked, so back to square one, and I had to struggle again with timing to unlock the account using query similar to below:

sqlcmd -S admin:abcsql\sql -i"D:\xyz.sql"

where xyz will look something like below:

alter login sa WITH CHECK_POLICY = OFF
alter login sa WITH CHECK_POLICY = ON
go

You should be able to get in now :)

Don’t forget to add the required accounts and start SQL as a service

During the above you may need to restart sql several times in case someone else gains login before you.

1 comment:

  1. Great post. I am still unable to access though it said it is in single user mode. Its 2000 server. And while using osql command it says I do not have access. I am admin in the box.

    ReplyDelete