SQL Server 2016 database mail queue inactive by bug

版权声明:複製請註明來源 https://blog.csdn.net/weixin_39392627/article/details/88561949

env: Windows Server 2016

        SQL Server 2016 SP2

這幾天發現某幾台的SQL Server怎麼都沒有發送一些警示的mail,是管理的太好嗎?感覺不太可能,因為機器的資源很少,難免會出現一些壓力測試的警訊。

所以開始一步一步檢查SQL Server database mail

 

1.檢查database mail queue與status

command:

EXEC msdb.dbo.sysmail_help_queue_sp

database mail queue竟然有383,很多mail沒有發送出來

 

2.檢查database mail是否啟動

command:

EXEC msdb.dbo.sysmail_help_status_sp

database mail確定有啟動

 

3.再次使用查詢指令確認是否有mail尚未發送,與哪些mail已送出

command:

SELECT * FROM msdb.dbo.sysmail_sentitems;

SELECT * FROM msdb.dbo.sysmail_unsentitems;

已sent的mail是0,但是有很多unsent的mail紀錄

 

4.確認msdb的service broker是否啟用

command:

SELECT is_broker_enabled FROM sys.databases WHERE name = 'msdb';

msdb的service broker已經啟用

 

5.因為一直找不到哪裡錯誤,所以往微軟官方找文件看看

結果找到以下訊息

SQL Server 2016 SP2 CU4

https://support.microsoft.com/en-gb/help/4464106/cumulative-update-4-for-sql-server-2016-sp2

When you install Cumulative Update 1(CU1), CU2 or CU3 for Microsoft SQL Server 2016 Service Pack 2, you will notice that the config file DatabaseMail.exe.config may be removed. Additionally, the Database Mail feature may not work correctly and cannot send email messages.

裡面提到DatabaseMail.exe.config這個檔案可能被移除。

在檢查後,可以確認是一定會被移除。

 

SQL Server 2016 SP2 CU5

https://support.microsoft.com/en-us/help/4475776/cumulative-update-5-for-sql-server-2016-sp2

這裡也有修復database mail的錯誤

 

另外一篇

SQL Server 2016 SP1 CU1

https://support.microsoft.com/en-au/help/3186435/sql-server-2016-database-mail-doesn-t-work-when-net-framework-3-5

FIX: SQL Server 2016 Database Mail does not work on a computer that does not have the .NET Framework 3.5 installed or stops working after applying SQL Server update

 

SQL Server 2016 SP1 CU2

https://support.microsoft.com/en-au/help/4013106/cumulative-update-2-for-sql-server-2016-sp1

 

6.確認修復方式與範圍

Database mail bug損害的版本範圍:

SQL Server 2016 SP1 CU1

SQL Server 2016 SP2 CU1, CU2, CU3

現行版本SQL Server 2016 SP2 CU1,可以利用service pack修復,但是要重開機。

 

7.找尋可以修復卻不用重開機的方式

用service pack的方式行不通

所找這篇來看看

SQL Server 2016 SP1 CU1

https://support.microsoft.com/en-au/help/3186435/sql-server-2016-database-mail-doesn-t-work-when-net-framework-3-5

FIX: SQL Server 2016 Database Mail does not work on a computer that does not have the .NET Framework 3.5 installed or stops working after applying SQL Server update

 

裡面有提到做法:

To work around this issue, you can implement any one of the following:

1. Create the DatabaseMail.exe.config and drop it next to the DatabaseMail.exe under the Binn folder. You can use notepad.exe or any other editor to edit it. Just make sure you save it by using UTF-8 encoding (in notepad.exe, select Save As... and in the Encoding combo box, select UTF-8):

         <?xml version="1.0" encoding="utf-8" ?>

         <configuration>

         <startup useLegacyV2RuntimeActivationPolicy="true">

         <supportedRuntime version="v4.0"/>   

         <supportedRuntime version="v2.0.50727"/>

         </startup>

         </configuration>

2.  Run a repair setup action of SQL Server 2016.

3. Manually install .Net Framework 3.5 on the machine.

 

但是我想不透為何與.Net Framework 3.5有關???

 

8.開始進行修復,確認DatabaseMail.exe.config是否存在

檔案目錄:

” C:\Program Files\Microsoft SQL Server\MSSQL13.MSSQLSERVER\MSSQL\Binn”

 

9.修復database mail

因為我覺得這篇文件寫得有點誇張

https://support.microsoft.com/en-au/help/3186435/sql-server-2016-database-mail-doesn-t-work-when-net-framework-3-5

除了建立DatabaseMail.exe.config

還要執行下面步驟

Run a repair setup action of SQL Server 2016.

Manually install .Net Framework 3.5 on the machine.

所以我決定忽略後面兩個步驟。

建立DatabaseMail.exe.config的動作,我也找一台SQL Sserver 2016把檔案複製過來

 

接著執行下面指令:

USE msdb;
EXEC sysmail_stop_sp;
EXEC sysmail_start_sp;

沒錯,重啟database mail

 

10.確認重啟database mail後的狀態

一旦重啟後,database mail就會開始發送mail。

所以在重起錢可以考慮是否要把unsent mail刪除

刪除unsent mail指令:

EXECUTE msdb.dbo.sysmail_delete_mailitems_sp   
    @sent_status = 'unsent' ;  
GO  

 

11.利用指令確認mail是否還卡在queue中

command:

SELECT * FROM msdb.dbo.sysmail_sentitems;

SELECT * FROM msdb.dbo.sysmail_unsentitems;

確認沒有unsent mail

 

手動發送測試mail也有收到。

SQL Server database mail修復大功告成。

 

 

猜你喜欢

转载自blog.csdn.net/weixin_39392627/article/details/88561949