SQL SERVER – Fix : Error: 4064 – Cannot open user default database. Login failed. Login failed for user

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)

SQL SERVER - Fix : Error: 4064 - Cannot open user default database. Login failed. Login failed for user error4064

The fix for this problem is very simple.

Fix/Workaround/Solution:

First click on Option>> Button of “Connect to Server” Prompt.

SQL SERVER - Fix : Error: 4064 - Cannot open user default database. Login failed. Login failed for user error4064_1

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

SQL SERVER - Fix : Error: 4064 - Cannot open user default database. Login failed. Login failed for user error4064_2

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

SQL SERVER - Fix : Error: 4064 - Cannot open user default database. Login failed. Login failed for user error4064_3

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)

SQL Error Messages, SQL Scripts
Previous Post
SQLAuthority News – SQL Server Security Whitepapers
Next Post
SQL SERVER – Clear Drop Down List of Recent Connection From SQL Server Management Studio

Related Posts

282 Comments. Leave new

  • This worked for my login on the master database but I cannot get it to work for the actual database I want the login to refer to.

    Reply
  • Imran Mohammed
    July 19, 2009 10:41 pm

    @Kirk

    Ask your SysAdmin to give you access to all other databases. Every login should be mapped to a user in a database, only then a login can enter into that database.

    Exception to above comment would be, if you enable Guest A/c in all databases, then you can access all databases with out actually having a user in any of the database, Which ofcourse is very dangerous, Guest A/c should be disabled all the time in all databases ( Except Master and Tempdb ).

    ~ IM.

    Reply
  • Thanks heaps for this, thought i’d stuffed it!

    Reply
  • thanks,
    it worked :)

    Reply
  • Saved my skin there mate. Cheers!

    Reply
  • Thanks…
    Work!

    Reply
  • muralekarthick
    December 2, 2009 8:23 pm

    Thank you, It helped me a lot, made me to recover from panic ………

    Reply
  • Thank you Ross, I was about ready to commit seppuku there for a bit.

    Reply
  • This helped me! Thanks

    Reply
  • Muchas gracias!!!

    Reply
  • I have error 4064, according to your instructions its doesn’t solve the problem.

    Following error showing while i wanted to open new connection.

    ‘Failed to open a new connection’
    Failed to initialize MSDB database for turning (exit code: -1)(DTAClient)

    Please advise.

    Thanks
    D

    Reply
  • Many thanks for the valuable and its really a very good post

    Reply
  • I used the same solution, and now I can login. But like many others, I get the same error when I try to actually do anything.

    Reply
  • Mohammad Yusuf
    April 20, 2010 7:55 am

    Thank you !!! I stuck with this problem for five days.

    Reply
  • This did not work for me, I ran into the issue after detaching my default database. I ended up just creating a new admin user and re-attaching the database in question. Problem solved.

    Reply
  • Andrew Wright
    May 4, 2010 11:03 pm

    I had same problem but the solution Curvian Vynes mentioned did: Right click on database > Properties > Files > set owner equal to the login of the user.

    Reply
  • thanxa
    it work’s
    i love u

    Reply
  • This solution worked great and saved me loads of time. Big Thanks. However all was working fine before so I wonder what changed to cause the problem?

    Anyway … Thanks.

    Reply
  • Thank you so much! It worked very much.

    Reply
  • Mamunur Rashid
    July 2, 2010 2:13 pm

    Thanks a lot.
    Fantastic solution !!

    Reply

Leave a Reply