Một khía cạch khá quan trọng của DBA, là làm sao nhận biết được các sự kiện xảy ra trong SQL server. Trong bài học này, chúng ta sẽ học cách cấu hình, để SQL server tự động gửi các thông tin về các sự kiện đến cho DBA qua Email, bằng cách sử dụng alerts và notifications.
Thực hiện xong bài lab này, bạn có thể biết cách xem SQL Server Error Log, Cấu hình Database mail, Cấu hình SQL Server Agent Operators, Cấu hình SQL Server Agent Alerts.
Thực Hiện Monitor SQL Server 2014 với Notifications và Alerts
A. Xem SQL Server Error Log:
Tại SSMS, ở cửa sổ Object explorer, Chọn Management, chọn SQL Server Logs, phải chuột vào Current chọn View SQL server Log. Tại cửa sổ Log File Viewer, xem các log hiện tại và thông tin chi tiết của log nằm ở cuối cửa. Bên trái cửa sổ Log File Viewer, check vào SQL server Agent để xem thêm các thông tin log của SQL server agent, đóng cửa sổ Log File Viewer lại.
Vào folder C:\Program Files\Microsoft SQL Server\MSSQL12.MSSQLSERVER \MSSQL\Log, file log hiện tại của SQL server là ERRORLOG và file log hiện tại của SQL server agent là SQLAGENT.1. Trở lại SSMS mở cửa sổ New Query thực thi lệnh sau: EXEC sys.sp_cycle_errorlog;
Sau đó, ở cửa sổ Object explorer, Chọn Management, chọn SQL Server Logs, phải chuột vào Current chọn View SQL server Log lại, sẽ thấy log file đã được khởi tạo lại.
B. Cấu hình Database mail:
1. Tạo database mail: Tại SSMS, ở cửa sổ Object explorer, Chọn Management, phải chuột vào Database Mail chọn Configure Database Mail màn hình Welcome ấn next tại màn hình Select Configuration Task chọn option Set up Database Mail ấn next chọn Yes để enable feature tại màn hình New profile trong phần Profile name: nhập vào SQL Server Agent Profile, sau đó ấn Add tại màn hình New Database Mail Account nhập các thông tin sau:
- Account name: AdventureWorks Administrator
- E-mail address: chungtanloc@msn.com
- Display name: Chung Tan Loc
- Reply e-mail: chungtanloc@msn.com
- Server name: smtp.live.com – port number 25 – check vào this server requires a secure connection (SSL)
- Chọn option Basic authentication , nhập vào user name: chungtanloc@msn.com và password (đây là ví dụ mail của Tôi, bạn phải nhập mail của bạn, có thể dùng mail công ty hay Gmail) —
Trở lại màn hình New profile kiểm tra lại Email address ấn next, tại màn hình Manage Profile Security chọn tab public profile, chọn checkbox public SQL server Agent Profile, trong phần Default Profile chọn Yes ấn next.
Màn hình Configure System Parameters ấn next màn hình Complete the Wizard ấn Finish chờ cấu hình hoàn tất ấn close.
2. Gửi test mail: Tại SSMS, ở cửa sổ Object explorer, Chọn Management, phải chuột vào Database Mail chọn Send Test E-mail, màn hình Send test E-mail trong phần to: nhập vào địa chỉ mail muốn nhận ấn Send Test E-Mail Ok. Kiểm tra mail xem đã nhận được mail từ SQL server không.
3. Kiểm tra Database mail table: Tại SSMS mở cửa sổ New Query, nhập vào lệnh sau kiểm tra xem đã gửi chưa:
SELECT * FROM msdb.dbo.sysmail_event_log;
SELECT * FROM msdb.dbo.sysmail_mailitems;
C. Cấu hình SQL Server Agent Operators:
1. Enable SQL Server Agent mail profile: Tại SSMS, ở cửa sổ Object explorer, phải chuột vào SQL Server Agent chọn properties. Tại cửa sổ SQL Server Agent Properties chọn Alert System, check vào Enable mail profile, trong phần Mail profile chọn SQL Server Agent Profile Ok
Phải chuột vào SQL server Agent chọn Restart
2. Tạo Operator: Tại SSMS, ở cửa sổ Object explorer, chọn SQL Server Agent, phải chuột vào Operators chọn New Operator, tại cửa sổ New operator trong phần name: adminSQL, Email name: adminsql@labs.ctl.vn (có thể là mail công ty, hay Gmail …) Ok
3. Cấu hình job gửi notify đến Operator: Tại SSMS, ở cửa sổ Object explorer, chọn SQL Server Agent, chọn Jobs, chọn local job, phải chuột vào Back Up Database – AdventureWorks chọn properties. Tại cửa sổ Job Properties – Back Up Database – AdventureWorks chọn Notifications, check vào Email, chọn Adminsql và chọn When the job completes Ok
Tại SSMS, ở cửa sổ Object explorer, chọn SQL Server Agent, chọn Oprators, phải chuột vào adminsql chọn properties. Tại cửa sổ adminsql properties chọn Notifications, chọn option Jobs, thấy có job đã được định nghĩa cho operator này Cancel.
Tại SSMS, ở cửa sổ Object explorer, chọn SQL Server Agent, chọn Jobs, chọn local job, phải chuột vào Back Up Database – AdventureWorks chọn Start job at step, chờ cho job thực thi xong ấn close. Check mail của user adminsql xem có mail chưa. Trở lại SSMS, chọn Oprators, phải chuột vào adminsql chọn properties, chọn history thấy có một mail đã được gửi.
D. Cấu hình SQL Server Agent Alerts:
1. Tạo Alert: Tại SSMS, ở cửa sổ Object explorer, chọn SQL Server Agent, phải chuột vào Alerts chọn New Alert. Tại cửa sổ New Alert, trong phần name: nhập vào Log full Alert, trong phần type chọn SQL Server event alert, chọn option Error number nhập vào 9002 (cảnh báo khi transaction log bị đầy)
Cũng tạo cửa sổ new Alert chọn Response, check vào Notify Operators, check vào checkbox E-mail của adminsql.
Cũng tạo cửa sổ new Alert chọn Options, check vào checkbox E-mail Ok
2. Test Alert: (Trước khi làm phần lab này bạn restore database TestAlertDB từ file TestAlertDB_full.bak trong folder Module 13). Tại SSMS, mở file TestAlert.sql trong folder Module 13, ấn F5 và chờ thực thi, khi transaction log bị đầy lỗi 9002 xảy ra .
USE TestAlertDB;
GO
SET NOCOUNT ON;
WHILE 1 = 1
BEGIN
INSERT INTO testtable (col1)
VALUES(‘Test data!’);
END;
GO
Tại SSMS, ở cửa sổ Object explorer, chọn SQL Server Agent, Chọn Alerts, phải chuột vào Log full Alert chọn properties, Chọn History xem giá trị ở 2 dòng Date of last alert và Date of last response.
Mr CTL – CTL.VN