SQL Server error 18456: "Login failed for user" and how to fix it
Error 18456 means SQL Server rejected the login, but the message on screen hides the real reason on purpose. The reason is in the SQL Server error log as a State number, and that number tells you which fix to use. The most common ones are a wrong password (state 8), a login that does not exist (states 2 and 5) and SQL authentication on a Windows-only server (state 58).
On this page
What it means
"Login failed for user" with error 18456 is an umbrella error. SQL Server returns the same short message for many different problems, and it keeps the client message vague deliberately so an attacker cannot learn which part of a login was wrong. Guessing from the message alone usually wastes time.
The real answer sits in the server's error log. Each failed login is written there as Error: 18456, Severity: 14, State: 8., followed by a line such as Login failed for user 'x'. Reason: ... [CLIENT: ip]. The State number and the Reason text point to the cause. This article is organised around those states and covers SQL Server on Windows and Linux. Azure SQL and Entra ID (Azure AD) logins also surface as 18456 with their own states (102 to 111 and 132 to 133), but they are only touched on briefly here.
Symptoms and causes
You are likely dealing with this problem if:
- SSMS, an application or a connection string returns: Login failed for user 'X'. (Microsoft SQL Server, Error: 18456).
- The user name in the message may be a Windows account (
DOMAIN\user),NT AUTHORITY\ANONYMOUS LOGON, an empty name or'(null)'. - A related message, error 4064 "Cannot open user default database. Login failed", appears when the default database is the problem.
The usual causes, most common first:
- Wrong password (state 8)
- The password does not match, and case matters. State 7 means the login is disabled and the password is also wrong, because the password is checked first.
- The login does not exist (states 2 and 5)
- Both states mean the user ID is not valid. A typical trigger is an app deployed to production with a DEV or QA connection string. The log says "Could not find a login matching the name provided."
- SQL authentication on a Windows-only server (state 58)
- The server is set to Windows authentication only, so SQL logins are refused. The same happens with
saafter switching to mixed mode, becausesastarts out disabled (state 7). State 58 can also be caused by a SID mismatch. - Default or requested database is missing or inaccessible (states 38 and 46)
- The database is offline, deleted, misspelled, or the login has no access to it. It can also point to an orphaned login after a failover.
- Domain\User format used with SQL authentication (state 6)
- A Windows-style name was sent as if it were a SQL login.
- Password must be changed (state 18)
- The login must set a new password. Sometimes this is reported as error 18488 instead.
- Windows account on a contained database with a SID mismatch (state 62)
- A Windows account is trying to reach a contained database and its SID does not match.
- ANONYMOUS LOGON, empty or null user name
- Usually Kerberos or NTLM trouble: NTLM loopback check, double-hop delegation, duplicate or misplaced SPNs, an availability group listener on the primary node, or an unreachable domain controller (SSPI errors 0x80090311 and 0x80090304).
- Other states
- States 122 to 124 mean a blank user name or password. State 126 is reported as a missing database, though the wording is unclear.
How to fix it
Work through these in order. They start with the quickest, safest checks and move to deeper repairs; stop as soon as the problem is gone.
1.Read the State and Reason in the error log
Everything else depends on this. The state tells you which of the fixes below applies.
- Open SSMS and go to Management > SQL Server Logs, then open the current log. Or open the
ERRORLOGfile directly (its location varies by installation). If you cannot connect with the failing account, use another login or local administrator access. - Find the line
Error: 18456, Severity: 14, State: N.and the Reason line after it, then use the matching fix below. Third-party blogs disagree on a few state meanings (6, 1, 10, 126), so trust the Reason text first. Note that state 1 only means error details are not available to you, so it is not a cause by itself.
2.Check the connection string
Many 18456 errors come from a typo or from an app pointing at the wrong server.
- Confirm the user name, password, server name (with the instance name for a named instance) and database name are all correct.
- Make sure the string targets the intended server. A DEV or QA string left in a production deployment is a common mistake.
- Test with a UDL file or a hard-coded connection string to rule out the application's configuration. In SSIS, check whether a configuration file overrides the connection manager.
3.Fix a wrong password (state 8)
The server received a password that does not match the login.
Before you start: Resetting a password breaks every application that still uses the old one. Update those connection strings afterwards.
- Retype the password carefully, paying attention to case. If it is still refused, ask an administrator to reset it.
4.Create the missing login (states 2 and 5)
The server has no login with that name.
Before you start: Only create logins that you intend to give access, and grant the minimum permissions needed.
- Create the login, then map a user in the target database:
CREATE LOGIN ...; CREATE USER [u] FOR LOGIN [u]; - For Windows authentication, add the user or a group that contains them as a login.
5.Enable mixed mode or use Windows authentication (state 58, or state 7 for sa)
A Windows-only server refuses every SQL login.
Before you start: Mixed mode widens the attack surface, because SQL logins with passwords can now be tried against the server. Prefer Windows authentication where you can, and if you need a SQL login, prefer a named login over sa. Never enable sa with an unknown or weak password: set a strong one first. Changing the mode also needs a restart of the SQL Server service, which disconnects users, and may create further work such as creating or enabling SQL logins.
- First consider the safer option: connect with Windows authentication, using
Integrated Security=SSPIfor OLE DB orTrusted_Connection=Yesfor ODBC. If that works, you do not need to change the mode. - Otherwise, in SSMS, right-click the server and choose Properties > Security. Under server authentication, select SQL Server and Windows Authentication mode.
- Restart the SQL Server service so the change takes effect.
- Create or enable only the SQL login you actually need, with a strong password. If it has to be
sa, set the password at the same time as enabling it, sincesastarts out disabled.
6.Fix database problems (state 38)
The login works but its default or requested database cannot be opened.
Before you start: Do not restore or re-create a database without checking that you will not overwrite data you need.
- Bring the database online or restore it, and check the database name spelling (case matters on case-sensitive collations).
- Change the login's default database to one that is always available:
ALTER LOGIN [x] WITH DEFAULT_DATABASE = master; - Or name an available database in the connection string, or in SSMS under Options > Connection Properties. The
sp_defaultdbprocedure is deprecated, so useALTER LOGIN. - If the login has no user in the database, create one with
CREATE USER ... FOR LOGIN .... After a failover, an orphaned login may need to be re-created or re-mapped.
7.Fix a Windows login with no server access (state 11)
The Windows user cannot be matched to a login, directly or through a group.
- If access comes only through the local Administrators group, start the application with Run as administrator, then add the Windows user as its own login.
- Check the Security > Logins folder, and look for a DENY applied through a group.
- See how the account gets access:
EXEC xp_logininfo 'DOMAIN\user'; - An error means the name cannot be resolved (an Active Directory or domain controller problem). For cross-domain access, the group must be in the SQL Server's own domain. No rows means no group grants access.
8.Fix ANONYMOUS LOGON, empty or null user names
These point to Kerberos or NTLM problems rather than a bad password.
Before you start: Registry and security policy changes can affect authentication across the machine. Create a restore point and note the original values first.
- For connections from the same machine over NTLM, set the
BackConnectionHostNamesregistry value (preferred) orDisableLoopbackCheck. For an availability group listener, add the listener's NetBIOS and FQDN names on all nodes. - Look for duplicate or misplaced SPNs:
SETSPN -X SETSPN -Q MSSQLSvc/yourserver.domain.com - For double-hop scenarios, use constrained delegation. If failures start after about 10 hours, switch from "Use Kerberos only" to "Use any protocol".
- Enable the Local Security Policy "Network security: Allow Local System to use computer identity for NTLM", and check whether the account is marked "Sensitive" in Active Directory.
- For an empty user name (LSASS or domain controller unreachable), fix the network or domain connection, then check and reset the secure channel:
nltest /sc_query:DOMAIN nltest /sc_reset:DOMAIN\DC - For
(null), an SPN is usually on the wrong account. Fix the SPNs, then check DNS, name resolution, and that no firewall blocks the domain controller.
9.ODBC says Windows authentication only on a mixed-mode server
A rare case where the ODBC client does not request SQL authentication.
- Add
TRUSTED_CONNECTION=TRUEto the connection string.
Check that it worked
- Connect again from SSMS or the application with the same credentials and confirm the login succeeds.
- Check the SQL Server error log: no new
Error: 18456entries should appear for that user.
If it still fails
- Re-read the exact State and Reason line in the error log and match it again; a different state than you assumed sends you to a different fix.
- If you cannot read the log, ask the SQL Server administrator or someone with local administrator access on the server to read it for you.
- For Kerberos problems, review the Microsoft Customer Support Services troubleshooting guides linked from the Microsoft Learn page, and run the SQLCheck tool to inspect SPNs.
- For Azure SQL or Entra ID logins, use the Microsoft Learn error page for the Azure-specific states.
Questions
Why does SQL Server not just say what is wrong?
The message sent to the client is vague on purpose, so it does not reveal whether the user name or the password was wrong. The detail is only written to the server's error log.
I changed the authentication mode and it still fails. Why?
The change only applies after the SQL Server service is restarted. Also check that the SQL login you use is enabled, since sa starts out disabled.
Sources
- MSSQLSERVER_18456 - SQL ServerOfficial · Microsoft Learn
- Troubleshooting Error 18456 · sqlblog.org (Aaron Bertrand)