Email Database dan pemberitahuan email dengan SQL Server Agent di Linux

Berlaku untuk:SQL Server - Linux

Artikel ini memperlihatkan cara menyiapkan Email Database dan menggunakannya dengan SQL Server Agent (mssql-server-agent) di Linux.

1. Aktifkan Email Database

USE master;
GO

sp_configure 'show advanced options', 1;
GO

RECONFIGURE WITH OVERRIDE
GO

sp_configure 'Database Mail XPs', 1;
GO

RECONFIGURE;
GO

2. Buat akun baru

EXECUTE msdb.dbo.sysmail_add_account_sp @account_name = 'SQLAlerts',
    @description = 'Account for Automated DBA Notifications',
    @email_address = 'sqlagenttest@example.com',
    @replyto_address = 'sqlagenttest@example.com',
    @display_name = 'SQL Agent',
    @mailserver_name = 'smtp.example.com',
    @port = 587,
    @enable_ssl = 1,
    @username = 'sqlagenttest@example.com',
    @password = '<password>';
GO

3. Buat profil default

EXECUTE msdb.dbo.sysmail_add_profile_sp
    @profile_name = 'default',
    @description = 'Profile for sending Automated DBA Notifications';
GO

4. Tambahkan akun Email Database ke profil Email Database

EXECUTE msdb.dbo.sysmail_add_principalprofile_sp
    @profile_name = 'default',
    @principal_name = 'public',
    @is_default = 1;
GO

5. Tambahkan akun ke profil

EXECUTE msdb.dbo.sysmail_add_profileaccount_sp
    @profile_name = 'default',
    @account_name = 'SQLAlerts',
    @sequence_number = 1;
GO

6. Kirim email pengujian

Anda mungkin harus masuk ke klien email Anda dan mengaktifkan opsi izinkan klien yang kurang aman untuk mengirim email . Tidak semua klien mengenali Email Database sebagai daemon email.

EXECUTE msdb.dbo.sp_send_dbmail
    @profile_name = 'default',
    @recipients = 'recipient-email@example.com',
    @subject = 'Testing DBMail',
    @body = 'This message is a test for DBMail'
GO

7. Atur profil Email Database menggunakan mssql-conf atau variabel lingkungan

Anda dapat menggunakan utilitas mssql-conf , atau variabel lingkungan, untuk mendaftarkan profil Email Database Anda. Dalam hal ini, mari kita panggil profil defaultkita .

  • Atur melalui mssql-conf:

    sudo /opt/mssql/bin/mssql-conf set sqlagent.databasemailprofile default
    
  • Atur melalui variabel lingkungan:

    MSSQL_AGENT_EMAIL_PROFILE=default
    

8. Menyiapkan operator untuk pemberitahuan pekerjaan SQL Server Agent

EXEC msdb.dbo.sp_add_operator
    @name = N'JobAdmins',
    @enabled = 1,
    @email_address = N'recipient-email@example.com',
    @category_name = N'[Uncategorized]';
GO

9. Kirim email ketika 'Pekerjaan Uji Agen' berhasil

EXEC msdb.dbo.sp_update_job
    @job_name = 'Agent Test Job',
    @notify_level_email = 1,
    @notify_email_operator_name = N'JobAdmins'
GO