SQL Server "Cannot open database requested by the login" (error 4060): what to check
Fix SQL Server error 4060 "Cannot open database requested by the login": check the database name and state, map the login to a user, and fix logins after a restore.
by OhSudo ·
Msg 4060, Level 11, State 1Cannot open database "shop" requested by the login. The login failed.Login failed for user 'app'.Here, the password worked. SQL Server knows you, and it couldn’t, or wouldn’t, open that database for you. So there are three questions: does the database exist, is it online, and does your login have a user in it?
TL;DR
Connect as an admin to master:
1. Is the name right?
It’s the most common reason, and connection strings are the place to look: Initial Catalog=shop or Database=shop. Is it correct for this environment? List what exists:
Names are case-insensitive on most servers, but not on all. A server with a case-sensitive collation treats Shop and shop as different. A typical sign is that a new deploy points at a database that hasn’t been created yet.
2. Is it online?
state_desc |
Means | Do this |
|---|---|---|
ONLINE |
Fine | Section 3 |
RESTORING |
A restore hasn’t finished | RESTORE DATABASE shop WITH RECOVERY; if nothing more is coming |
RECOVERING |
Starting up, after a crash or restart | Wait, and watch the error log |
OFFLINE |
Someone took it offline | ALTER DATABASE shop SET ONLINE; |
SUSPECT |
Damaged, or it’s missing files | Restore from backup: don’t guess |
If user_access_desc is SINGLE_USER, then someone else holds the only slot, usually an old tool or the application. Find them, and open it up again:
3. Does the login have a user?
SQL Server has two levels: a login gets you into the server, and a user gets you into one database. A login with no user in that database gets exactly this error.
No row? Then add one, and give it rights:
4. After a restore or a move
This is the classic: it worked on the old server. A user inside a database points to its login by an ID, and the login on the new server has another ID. The names match, and the link doesn’t, so it’s known as an orphaned user. Find them:
USE shop;SELECT dp.name AS orphaned_userFROM sys.database_principals dpLEFT JOIN sys.server_principals sp ON dp.sid = sp.sidWHERE dp.type = 'S' AND sp.sid IS NULL AND dp.authentication_type_desc = 'INSTANCE';Fix the link, and there’s no need to delete anything:
To avoid this on a move, script the logins from the old server with their SIDs, using Microsoft’s sp_help_revlogin.
5. The login’s default database
Even with no database in your connection string, you can get this. Each login has a default database, and if that’s gone, then logging in fails, even for tools like SSMS:
SELECT name, default_database_name FROM sys.server_principals WHERE name = 'app';ALTER LOGIN app WITH DEFAULT_DATABASE = master;If you’re locked out by this, tell your tool to connect to master explicitly. In SSMS it’s under Options → Connection Properties → Connect to database, and with sqlcmd, use -d master.
6. Azure SQL
On Azure SQL Database, you can’t USE to change databases, and the firewall has a similar message for clients it doesn’t allow. Connect to the database itself, and create a contained user there:
Cannot open server "x" requested by the login is the firewall rule, so add your address under the server’s networking settings.
Warning
Giving the application sysadmin will make this error go away, along with every other limit. Create a user in the one database, with the roles it needs. If a database is SUSPECT, don’t reach for DBCC CHECKDB ... REPAIR_ALLOW_DATA_LOSS as a first step: as the name says, it can lose data, so go to your backups first.
Shell, we have a problem!
Similar, but different: error 18456 is usually the login itself: wrong password, or disabled. Error 40 and 26 mean you never reached the server. Error 9002 means the database is open, and can’t write. The SELECT permission was denied means you’re in, without rights to that table. To compare this with other databases, see the SQL Dialect Reference.
Found this useful? Share it.
Found a mistake? Let us know at contact@ohsudo.com.