SqlServer教程

How to Fix Login Failed to SQL Server - Error 18456/18452/4064/4062

本文主要是介绍How to Fix Login Failed to SQL Server - Error 18456/18452/4064/4062,对大家解决编程问题具有一定的参考价值,需要的程序猿们随着小编来一起学习吧!

 

After you install Microsoft SQL Server 2016/2014/2012/2008/2005/2000, and you try to connect to server that is running SQL Server, probably you receive one of following error messages. 

  • Login failed for user '<user_name>'. The user is not associated with a trusted SQL Server connection. (Microsoft SQL Server, Error: 18452)
  • Login failed for user '<user_name>'. (Microsoft SQL Server, Error: 18456)
  • Cannot open user default database. (Microsoft SQL Server, Error: 4064/4062)

However, what lead to these errors occurring? Now let's learn to check these login failed error details and fix them in this article.

  1. Section 1: Check the Error State or Find Reason to This Error 
  2. Section 2: Fix SQL Server Login Failed Error

Section 1: Check the Error State or Find Reason to This Error 

While you receive error 18456/18452 message, please check error state in SQL Server error log.

Msg 18456, Level 14, State 1, Server <server name>, Line 1
Login failed for user '<user name>'

We should know that the 'State' will always be shown to be '1' regardless of the nature of the problem. To determine the true reason for the SQL Server login failure, the administrator can look in server's error log where a corresponding entry will be written. 

View the entry and just need to pay attention to the "State", which the server will accurately set to reflect the source of the problem. Refer to common error states and their descriptions in following table, you would find the real reason for failed logining SQL Server. 

ERROR STATE ERROR DESCRIPTION
2 and 5 Invalid userid
6 Attempt to use a Windows login name
7 Login disabled and password mismatch
8 Password mismatch
9 Invalid password
11 and 12 Valid login but server access failure
13 SQL Server service paused
18 Change password required

While you receive SQL Server failed login error 4062/4064, you should look for reason why cannot open default database. 

As is known, each user has a default database. And it is used automatically when you connect to computer that is running Microsoft SQL Server, and you do not specify a login database. However, if it is unavailable when connected, you may not be able to connect. Instead, you receive error 4062/4064 message. 

Default database becomes unavailable probably because the database: 

  • 1. No longer exists, offline or has been detached. 
  • 2. Has been set to the RESTRICTED_USER state or is set to emergency status. 
  • 3. Is in suspect mode, or is in single user mode but the only available connection is already being used by someone else or by something else. 

Section 2: Fix SQL Server Login Failed Error

  • Case 1: Fix SQL Server login failed error 18456/18452
  • Case 2: Fix SQL Server login failed error 4062/4064

Case 1: Fix SQL Server login failed error 18456/18452

"Login failed for user" would occur if the user tries to login with credentials without being validated. Now we will introduce some situations when there is no user credentials for SQL Server logon and how to solve SQL Server login problem. 

Situation 1: The login may be a SQL Server login but the server only accepts Windows Authentication. 

To login SQL Server successfully, you should change to login SQL Server with Windows Authentication mode and enable Mixed Authentication mode or SQL Server Authentication mode.

Situation 2: You are trying to connect by using SQL Server Authentication but the login used does not exist on SQL Server. 

In order to make SQL Server login with user, please confirm that you have a login user. If not, create new login in SQL Server Books Online.

Situation 3: The login may use Windows Authentication but the login is an unrecognized Windows principal.

An unrecognized Windows principal means that Windows can't verify the login. This might be because the Windows login is from an untrusted domain. To resolve this issue, verify that you are logged in to the correct domain.

Situation 4: The login user password is valid or incorrect, or the password is changed.

If it is just because of user password incorrect or invalid, you can reset user password with SQL Password Genius, and then login SQL Server with new password again. 

Situation 5: The login is disabled.

You should need to enable SQL Server login user or enable SQL Server authentication mode after connecting to server with Windows authentication mode. 

Situation 6: The login is valid but server access is failed.

Probably you have no admin privileges to server, or Windows Firewall has not allowed SQL Server access. So you can try to get administrator rights or configure Windows Firewall to allow SQL Server access. 

Case 2: Fix SQL Server login failed error 4062/4064

To fix SQL Server login error 4062/4064, please specify an available, valid database as user's default database. So while default database is connected automatically, successful connection can be guaranteed. 

Step 1: Run Command Prompt as administrator. 

Step 2: Set default database for login user by one of following methods. 

2-1 If SQL Server login uses Windows authentication mode, run command below and press Enter.
sqlcmd –E -S InstanceName –d master

2-2 If SQL Server login uses SQL Server authentication to connect to instance, run command below. 
sqlcmd -S InstanceName -d master -U SQLLogin -P Password

Step 3: At the sqlcmd prompt, type the following, and then press ENTER.

ALTER LOGIN SQLLogin WITH DEFAULT_DATABASE = AvailDBName

Step 4: At the sqlcmd prompt, type GO, and then press ENTER.

 

这篇关于How to Fix Login Failed to SQL Server - Error 18456/18452/4064/4062的文章就介绍到这儿,希望我们推荐的文章对大家有所帮助,也希望大家多多支持为之网!