Windows Server  ·  high  ·  Roles & shared services

SQL Server error 18456 — login failed for user

The client message deliberately says nothing useful. The SQL error log records a state number that says exactly what was wrong.

What you see

Applications fail to connect with "Login failed for user". The client always shows State 1, which means nothing — the real state is only in the server's log.

What is actually wrong

Depends on the state: 5 is an unknown login, 8 is a wrong password, 38 is a missing or inaccessible database, 40 cannot open the default database, 11 and 12 are a valid Windows login with no SQL access.

Codes and articles

18456Error 18456Login failed for userState 8State 38State 5

Fixes (2)

Read the state, then fix the login
SQL Server Management Studio or sqlcmd30 minutesmedium riskreversible

Any 18456. Always read the state first — it turns this from guesswork into one action.

  1. Read the server's own error log, which is the only place the real state appears.

    Command Prompt
    EXEC xp_readerrorlog 0, 1, N'Login failed';

    The client is told State 1 on purpose, so an attacker cannot tell 'no such user' from 'wrong password'. The server log has no such restriction.

  2. Confirm the server accepts SQL logins at all. Mode 1 is Windows-only, 2 is mixed.

    Command Prompt
    SELECT SERVERPROPERTY('IsIntegratedSecurityOnly') AS WindowsAuthOnly;
  3. If it is Windows-only and you need a SQL login, switch to mixed mode — this needs a service restart.

    Command Prompt
    EXEC xp_instance_regwrite N'HKEY_LOCAL_MACHINE', N'Software\Microsoft\MSSQLServer\MSSQLServer', N'LoginMode', REG_DWORD, 2;
  4. Check the login exists and is not disabled or locked out.

    Command Prompt
    SELECT name, is_disabled, LOGINPROPERTY(name,'IsLocked') AS IsLocked, LOGINPROPERTY(name,'IsExpired') AS IsExpired FROM sys.server_principals WHERE type IN ('S','U','G');
  5. Enable and unlock as needed.

    Command Prompt
    ALTER LOGIN [appuser] ENABLE;ALTER LOGIN [appuser] WITH PASSWORD = 'NewStrongPassword' UNLOCK;
  6. For state 11 or 12, the Windows account authenticated but has no SQL login — create one.

    Command Prompt
    CREATE LOGIN [EXAMPLE\AppService] FROM WINDOWS;
Confirm it workedThe application connects and the log records a successful login.
Command Prompt
EXEC xp_readerrorlog 0, 1, N'Login succeeded';
If you need to undo itALTER LOGIN ... DISABLE, or DROP LOGIN, reverses anything created here.
Fix the default database problem
SQL Server Management Studio25 minuteslow riskreversible

State 38 or 40 — the login is fine but the database it wants is not.

  1. Find which database is being asked for.

    Command Prompt
    EXEC xp_readerrorlog 0, 1, N'Login failed', N'38';
  2. Check the database exists and is online.

    Command Prompt
    SELECT name, state_desc, user_access_desc, is_read_only FROM sys.databases ORDER BY name;
  3. Bring it online if it is not.

    Command Prompt
    ALTER DATABASE [AppDb] SET ONLINE;ALTER DATABASE [AppDb] SET MULTI_USER;
  4. Check the login's default database — a login pointing at a database that has been dropped fails before it can connect anywhere.

    Command Prompt
    SELECT name, default_database_name FROM sys.server_principals WHERE type IN ('S','U');
  5. Point it somewhere that exists.

    Command Prompt
    ALTER LOGIN [appuser] WITH DEFAULT_DATABASE = [master];
  6. Map the login to a user in the database if it is not already.

    Command Prompt
    USE [AppDb];CREATE USER [appuser] FOR LOGIN [appuser];ALTER ROLE db_datareader ADD MEMBER [appuser];
Confirm it workedThe application connects to the intended database.
Command Prompt
SELECT DB_NAME() AS CurrentDb, SUSER_NAME() AS LoginName;
If you need to undo itALTER LOGIN to the previous default database; DROP USER to remove a mapping.

Where this stops. This write-up was written and checked by hand. It says what each step changes, how to confirm it worked and how to reverse it, and anything destructive is flagged before you reach it. If it does not match what your machine is doing, search the Support Centre for the exact code or message — and when something needs a person, get in touch.