I have received following question nearly 10 times in last week though emails. Many users have received following error while connecting to the database. This error happens when database is dropped for which is default for some of the database user. When user try to login and their default database is dropped following error shows up.
Cannot open user default database. Login failed.
Login failed for user ‘UserName’. (Microsoft SQL Server, Error: 4064)

The fix for this problem is very simple.
Fix/Workaround/Solution:
First click on Option>> Button of “Connect to Server” Prompt.

Now change the connect to database to any existing database on your server like master or msdb.

Now click on connect and it will successfully log you in. Once you login in the system run following statement for your username and it should fix your problem. Make sure to replace [test] with your username and master with yourdatabase name.
ALTER LOGINÂ [test] WITH DEFAULT_DATABASE = master
GO

Watch SQL in Sixty Seconds video to Resolve Connection Error:
[youtube=http://www.youtube.com/watch?v=1k00z82u4OI]
Reference : Pinal Dave (http://www.SQLAuthority.com)






282 Comments. Leave new
Thank you so much for this!
Many thanks for your help!
Thank you very much. I have not found so quick solution solution.
Hi can you help me with another issue. I am using sql with php. Whnever I am trying to execute stored procedure I am getting below error.
MSSQL error: Changed database context to ‘md_test’.
Saved my life yet again… don’t know why you do it but keep doing what you do do!!!
thank you!!
Great :-)
Thanks:-)
it works,,,thanks
Well done!!
Worked Famously!!! Thank you
Excellent answer.. resolved my problem!!! thank you :-)
thank you very much
Thanks much.
Very good!! Thanks
I’m using, SQL2008, I’m able to login with Master db.
When i’m trying to change the login to my databasse, it throws syntax errors.
Can any body tell me the exact syntax Please?
thanks for your tip, it was very useful!
Pinal Dave, the greatest SQL Authority on the web!
Pinal, thanks for the great tip, which helped me in the real world today.
This happened on my SQL 2012 dev / sandbox server. I was testing disaster recovery by taking a COPY_ONLY backup of our prod database, copying it over to the dev server and timing RTO for various restore strategies. In my case I left the database in NORECOVERY mode and left for a meeting. When I came back I couldn’t connect to the server at all, and wondered what happened to the instance.
5 years on and still a very useful post! I encountered this error because a re-install of an application deleted and re-created a database, leaving the username adrift from it.
Yep, still relevant. Saved my life today.