10

I have been trying to login using one of my sql server logins but I am getting the error message below:

General Error Message

enter image description here

Copy Message Text

TITLE: Connect to Server
------------------------------

Cannot connect to MY_SERVER.

------------------------------
ADDITIONAL INFORMATION:

Logon failed for login 'cola' due to trigger execution.
Changed database context to 'master'.
Changed language setting to us_english. (Microsoft SQL Server, Error: 17892)

For help, click: http://go.microsoft.com/fwlink?> ProdName=Microsoft%20SQL%20Server&EvtSrc=MSSQLServer&EvtID=17892&LinkId=20476

------------------------------
BUTTONS:

OK
------------------------------

Advanced Information

enter image description here

I am aware of the question below but it is slightly different and I have tried everything that is said there and it did not work for me, that's why I am putting this question here:

“Logon failed for login 'sa' due to trigger execution.” When No SA Login Triggers Defined

This question here is very similar too:

Login failed for user - Error: 18456, Severity: 14, State: 38

And from this one I got the following info:

Login failed for user - Error 18456 - Severity 14, State 38

1      'Account is locked out'
2      'User id is not valid'
3-4    'Undocumented'
5      'User id is not valid'
6      'Undocumented'
7      'The login being used is disabled'
8      'Incorrect password'
9      'Invalid password'
10     'Related to a SQL login being bound to Windows domain password policy enforcement.
        See KB925744.'
11-12  'Login valid but server access failed'
16     'Login valid, but not permissioned to use the target database'
18     'Password expired'
27     'Initial database could not be found'
38     'Login valid but database unavailable (or login not permissioned)'

According to the information above I have the following issue:

Login valid but server access failed

Still no joy.

I tried to drop the login and re-create it, but encountered a Login can not be dropped because it is in use.

I used the script below to find out where it is used, by whom, from where:

USE master
go
SELECT 
        sdes.session_id 
       ,sdes.login_time 
       ,sdes.last_request_start_time
       ,sdes.last_request_end_time
       ,sdes.is_user_process
       ,sdes.host_name
       ,sdes.program_name
       ,sdes.login_name
       ,sdes.status

       ,sdec.num_reads
       ,sdec.num_writes
       ,sdec.last_read
       ,sdec.last_write
       ,sdes.reads
       ,sdes.logical_reads
       ,sdes.writes

       ,sdest.DatabaseName 
       ,sdest.ObjName
    ,sdes.client_interface_name
    ,sdes.nt_domain
    ,sdes.nt_user_name
    ,sdec.client_net_address
    ,sdec.local_net_address
    ,sdest.Query
    ,KillCommand  = 'Kill '+ CAST(sdes.session_id  AS VARCHAR)
FROM sys.dm_exec_sessions AS sdes

INNER JOIN sys.dm_exec_connections AS sdec 
        ON sdec.session_id = sdes.session_id

CROSS APPLY (

                SELECT DB_NAME(dbid) AS DatabaseName
                    ,OBJECT_NAME(objectid) AS ObjName
                    ,COALESCE((
                            SELECT TEXT AS [processing-instruction(definition)]
                            FROM sys.dm_exec_sql_text(sdec.most_recent_sql_handle) 
                            FOR XML PATH('')
                                ,TYPE
                            ), '') AS Query

                FROM sys.dm_exec_sql_text(sdec.most_recent_sql_handle)

    ) sdest
WHERE sdes.session_id <> @@SPID 
--ORDER BY sdes.last_request_start_time DESC

I then killed the 2 sessions, re-created the login, tried again, same error.

What am I missing?

0

4 Answers 4

13

According to the screenshots you posted you are encountering an Error 17892 which translate roughly to a logon trigger error.

You might have previously had a logon trigger that evaluated if you were allowed to log in to the SQL Server or not. This trigger may have been removed or might produce wrong results which now does not allow anybody to log in to the SQL Server instance.

There are three possible workarounds:

1. Open Query Window Available

Search for the trigger via:

SELECT * FROM sys.server_triggers;

If you see a server level trigger that might be responsible for the login issues, then you can drop it with:

DROP TRIGGER triggername ON ALL SERVER;

Instead of dropping the trigger just disable it with:

DISABLE TRIGGER triggername ON ALL SERVER;

2. DAC Allowed; No Open Query Window Available

If you don't have an open Query Window, then you will have to log in to your SQL Server instance with a DAC (ADMIN) connection:

sqlcmd –S 127.0.0.1,1434 

1434 is the default port for the DAC connection

You can then run the above queries from solution 1. to determine the trigger and then drop or disable the trigger.

3. No Open Query Window; No DAC Allowed

If you don't have an open Window and DAC has not been turned on, then you are going to have to stop the instance and start again using the following steps:

sqlserver.exe –c -m –f

This will allow you to start the instance in single user mode with minimal configuration and shorter startup time.

Then you can disable the triggers according to step 1. above.

1
  • The trigger locks you out, single user mode lets you back in. The STIG code is meant to be for locking down the server but as a DBA you'll still need access and need to rewrite the trigger slightly to allow your own access. As a startup parameter, we specify "-m" means that the service will start in single-user mode. John's advice here is spot on.
    – Jamie
    Oct 31, 2023 at 13:53
1

Check the user's permission to execute the logon trigger. You'll also get this error if the trigger cannot be executed by the user.

1

I ended up using a batch file as follows: (admin command)

net stop MSSQLSERVER
net start MSSQLSERVER
sqlcmd -E -S <servername>

Make sure no cohort has SQL Server Management studio open and that you have closed all command windows but the one... possibly restart and use another if there are other issues. All session connections must be void.

In my case it was a drop trigger

1> DROP TRIGGER MYTRIGGER ON ALL SERVER;
2> GO

When done running your command line queries make sure you put it back into multi user mode. To do so, use the -T3608 in your SQL Server Configuration Manager properties Startup Parameters. Right click the service and find the Startup Parameters tab. SET it to -T3608 and add it - then restart, then once it runs again, do a final cleanup on the parameters.

1
  • 1
    Did you know that you can start the service in single-user mode and only allow connections from SQLCMD with the command net start mssqlserver /mSQLCMD ? That way it doesn't matter how many other processes are trying to connect. Oct 31, 2023 at 18:51
-2

After struggling with this issue for several hours I found a very simple solution to retore the connextion to my empty local database:

1 - Open a CMD/Terminal on this path (the path may depend on your SQL server installation): C:\Program Files\Microsoft SQL Server\150\Tools\Binn

2- run this command to verify that your local database is there: SqlLocalDB.exe info

3- delete it: SqlLocalDB.exe delete MSSQLLocalDB

4- Now you're ready to connect to your MSSQLLocalDB.

Not the answer you're looking for? Browse other questions tagged or ask your own question.