SQL Server-链接服务器消失

问题描述 投票:0回答:1

六个月来,我已链接服务器在SQL Server中正常工作。一切正常,但是奇怪的事情开始发生。一周前,我被通知同步不起作用。我发现链接的服务器不见了。我再次添加了它们,但是三天后又添加了,但是三天后它们又消失了。

我没有手动删除它们。

是否有可能将它们移除?

如何防止自动删除?

Windows Server 2019,SQL Server 2017

sql-server linked-server sql-server-2017 windows-server-2019
1个回答
0
投票

创建一个表,该表将保留跟踪的操作,例如:

CREATE TABLE EventLogTable (
    EventLogTableID INT IDENTITY PRIMARY KEY,
    EventType NVARCHAR(100),
    PostTime DATETIME,
    SPID INT,
    ServerName NVARCHAR(100),
    LoginName NVARCHAR(100),
    ObjectName NVARCHAR(100),
    ObjectType NVARCHAR(100),
    [Parameters] NVARCHAR(1000),
    TargetObjectName NVARCHAR(100),
    TargetObjectType NVARCHAR(100),
    TSQLCommand NVARCHAR(1000))

然后通过EVENTDATA()函数创建一个DDL服务器触发器以监视链接的服务器事件:

CREATE TRIGGER utrLogLinkedServerOperations ON ALL SERVER FOR 
    CREATE_LINKED_SERVER, 
    DROP_LINKED_SERVER,
    ALTER_LINKED_SERVER
AS
BEGIN

    DECLARE @EventXML XML = EVENTDATA()  

    INSERT INTO EventLogTable (
        EventType,
        PostTime,
        SPID,
        ServerName,
        LoginName,
        ObjectName,
        ObjectType,
        [Parameters],
        TargetObjectName,
        TargetObjectType,
        TSQLCommand)
    SELECT
        EventType = @EventXML.value('(/EVENT_INSTANCE/EventType)[1]', 'nvarchar(100)'),
        PostTime = @EventXML.value('(/EVENT_INSTANCE/PostTime)[1]', 'nvarchar(100)'),
        SPID = @EventXML.value('(/EVENT_INSTANCE/SPID)[1]', 'nvarchar(100)'),
        ServerName = @EventXML.value('(/EVENT_INSTANCE/ServerName)[1]', 'nvarchar(100)'),
        LoginName = @EventXML.value('(/EVENT_INSTANCE/LoginName)[1]', 'nvarchar(100)'),
        ObjectName = @EventXML.value('(/EVENT_INSTANCE/ObjectName)[1]', 'nvarchar(100)'),
        ObjectType = @EventXML.value('(/EVENT_INSTANCE/ObjectType)[1]', 'nvarchar(100)'),
        [Parameters] = @EventXML.value('(/EVENT_INSTANCE/Parameters)[1]', 'NVARCHAR(1000)'),
        TargetObjectName = @EventXML.value('(/EVENT_INSTANCE/TargetObjectName)[1]','nvarchar(100)'),
        TargetObjectType = @EventXML.value('(/EVENT_INSTANCE/TargetObjectType)[1]', 'nvarchar(100)'),
        TSQLCommand = @EventXML.value('(/EVENT_INSTANCE/TSQLCommand)[1]', 'NVARCHAR(1000)')

END

并确保启用它:

ENABLE TRIGGER utrLogLinkedServerOperations ON ALL SERVER

现在尝试创建,修改和删除一些链接的服务器,以检查触发器是否在表中正确创建了日志。然后等待恐怖分子袭击。

您还可以回滚触发器内部的操作,但是要注意,您可能最终甚至不允许所需的进程来操作链接的服务器。

© www.soinside.com 2019 - 2024. All rights reserved.