執行登入觸發器(sql server 2016限制IP訪問)後,登入sql server報錯: 由於執行觸發器,登入失敗已將資料庫上下文更改為master。解決方法:用cmd執行刪除登入觸發器
背景:
在cmd中執行osql命令:
osql -S 【資料庫伺服器】 -E -i mysql.sql |
其中mysql.sql內容如下:
CREATE LOGIN test WITH PASSWORD = '輸入密碼' GO ALTER TRIGGER tr_connection_limit ON ALL SERVER FOR LOGON AS BEGIN IF ORIGINAL_LOGIN()= 'test' AND (SELECT EVENTDATA().value('(/EVENT_INSTANCE/ClientHost)[1]', 'NVARCHAR(15)')) NOT IN('<local machine>','192.168.191.22') ROLLBACK; END; |
結果使用SSMS登入Sql Server時失敗,無論是用windows驗證方式登入還是sql server賬號登入都會報錯:由於執行觸發器,登入失敗已將資料庫上下文更改為master
我的解決方法是用sqlcmd刪除登入觸發器,刪除時剛開始報錯:不存在該觸發器或是沒有許可權,重啟一次sql server,重啟cmd,執行如下命令就成功了(我也不知道許可權問題怎麼解決的^_^):
C:\Users\Administrator>sqlcmd -A -S WIN-FI7RNDQ5SB4\MYSQLINSTANCE 1> select * from sys.server_trigger_events (1 行処理されました) 1> select * from sys.server_triggers 2> go name object_id parent_class parent_class_desc parent_id type type_desc create_date modify_date is_ms_shipped is_disabled -------------------------------------------------------------------------------------------------------------------------------- ----------- ------------ ------------------------------------------------------------ ----------- ---- ------------------------------------------------------------ ----------------------- ----------------------- ------------- ----------- tr_connection_limit 2107154552 100 SERVER 0 TR SQL_TRIGGER 2018-10-23 17:40:04.367 2018-10-24 11:29:30.647 0 0 (1 行処理されました) (1 行処理されました) 1> drop trigger tr_connection_limit on all server; 2> go 1> select * from sys.server_triggers 2> go name object_id parent_class parent_class_desc parent_id type type_desc create_date modify_date is_ms_shipped is_disabled -------------------------------------------------------------------------------------------------------------------------------- ----------- ------------ ------------------------------------------------------------ ----------- ---- ------------------------------------------------------------ ----------------------- ----------------------- ------------- ----------- (0 行処理されました) 1> |
接下來登入Sql Server就成功了,無論是用什麼賬戶登入。