SQL Server provides many options for automating routine administrative tasks. This lesson discusses the benefits of automating administration, the SQL Server components that enable automated administration, and how to prepare your server for automated administration.
After this lesson, you will be able to
- Explain how the components of SQL Server automated administration interact
- Explain how jobs and alerts are processed
- Configure SQL Server Agent to use a MAPI client to send e-mail messages
Estimated lesson time: 75 minutes
Automating routine maintenance tasks on your local server or in a multiserver environment allows you to spend time on other database administrative functions that lack predictable responses. Another benefit of automated administration is the ability to configure your server to recognize and respond to potential problems.
You perform several maintenance and administrative tasks routinely on SQL Server, such as
You can automate these tasks to occur on a regular schedule. For example, you can define index maintenance tasks to occur on the first Sunday of every month.
SQL Server allows you to be proactive and prepare for potential problems by
Before you automate tasks, it is important to consider the fundamentals of preparing for and performing SQL Server automated administration.
For a video demonstration that covers automating administration tasks, run the placeholder.avi file located in the \Auto.htm folder on the Supplemental Course Materials CD-ROM.
Components of SQL Server automated administration include the SQL Server SQLServerAgent and MSSQLServer services and the Microsoft Windows NT EventLog service. As Figure 13.1 illustrates, these services work together to allow automated administration. The term administrative task refers generically to activities that system administrators or database owners perform.
Figure 13.1 Components of SQL Server automated administration
NOTE
SQL Server Agent was called SQL Server Executive in previous versions of SQL Server.
When the SQLServerAgent service starts, it registers with the EventLog service and connects to the MSSQLServer service. This allows the EventLog service to notify the SQL Server Agent when events are written to the Windows NT application log. The SQLServerAgent service then reads the Windows NT application log to determine whether the event was generated by SQL Server and if so to determine whether there is a defined action to be taken in response to the event.
The SQLServerAgent service communicates with the MSSQLServer service to take action when an event occurs. Actions include executing jobs or firing alerts. These actions are defined in the msdb database. SQL Server Agent can also execute other applications.
The MSSQLServer service writes events to the Windows NT application log. An event is anything that happens within the system or application that requires attention. Events are written to the EventLog service by SQL Server when
NOTE
Windows 95 and Windows 98 does not support services. When you run SQL Server on Windows 95 or Windows 98, SQL Server Profiler is used to monitor logged events and forward them to SQL Server Agent.
Jobs and alerts are defined separately and can be executed or fired independently. A job is a maintenance or administrative task that consists of one or more steps. Jobs execute according to their defined schedules or in response to an alert. You can automate the process of recognizing and responding to potential problems by creating alerts. Alerts fire when SQL Server Agent is notified about events by the EventLog service or when SQL Server Agent receives performance data from the MSSQLServer service. Figure 13.2 illustrates how SQL Server processes jobs and alerts.
Figure 13.2 Processing jobs and alerts
NOTE
Jobs were called tasks in previous versions of SQL Server.
You will typically create an alert to notify an operator when an error occurs in a database or to execute a job in response to the fired alert. For example, you can create an alert that fires if a transaction log backup fails due to a tape device error. The alert executes a job that performs the backup to a disk device and notifies an operator.
You can create a job that executes on a regular schedule and notifies an operator when it completes. For example, you can define a job to transfer data from another database into the Northwind database once a month. The job definition may include several steps: backing up the transaction log, transferring the data, and then backing up the database.
Other things you can define a job to do include
Jobs and alerts complement one another because an alert may fire in response to an error that has occurred because a job step failed. The alert can in turn execute a job to correct the problem.
Before you begin creating jobs and defining alerts, you should ensure that SQL Server Agent is running and has been set up properly. If you plan to notify operators by e-mail or pager, you should also configure a SQL Server Agent mail profile.
SQL Server Agent is a Windows NT service that must be running in order to execute jobs automatically and fire defined alerts. You should set up the SQLServerAgent service to start automatically whenever you start Windows NT.
When SQL Server is installed, a user account is specified in the startup properties of the SQLServerAgent service. SQL Server Agent can use either the local System account or a domain user account.
Using the local System account for SQL Server Agent allows the service access to the local computer only. A domain user account is required for SQL Server Agent to have permission to
If you plan to send notifications to operators using e-mail or pagers, you must
SQL Server Agent requires a profile in order to start a mail session and send notification by e-mail or pager. A SQL Server Agent mail session is started every time the SQLServerAgent service is started. You can create the profile with a mail client, such as Microsoft Outlook, that is installed locally on the SQL Server computer.
If Microsoft Exchange Server is used, a mail profile must be configured for the domain user account that SQL Server Agent uses.
For pager notifications, SQL Server Agent sends e-mail to your messaging server. On the messaging server, you must have third-party pager-to-e-mail software and/or hardware that converts the inbound e-mail into pager messages.
SQL Server uses two separate mail sessions:
SQL Server uses this mail session when your database applications execute the xp_sendmail extended stored procedure to send a message or query result set to a recipient or when they execute the sp_processmail system stored procedure to process incoming mail.
If the SQLServerAgent and MSSQLServer services use the same Windows NT domain user account, by default they will use the same mail profile for SQL Server Agent and SQL Mail mail sessions. This allows both services to share a common mailbox.
You can configure separate mailboxes for SQL Server and SQL Server Agent by creating separate mail profiles. There are two ways to accomplish this:
In this practice, you will use Windows Messaging to configure the Microsoft Mail service and create profiles for the SQLService user account to enable SQL Server Agent and SQL Mail to send and receive messages. You will then create a profile for your administrative user account, to send and receive messages with Windows Messaging. Finally, you will use SQL Server Enterprise Manager to configure SQL Server Agent to use the mail profile that is configured for the SQLService user account. You will configure SQL Mail in the next lesson.
In this exercise you will configure a workgroup postoffice and add mailboxes to the newly created postoffice.
In this exercise, you will configure profiles for the user account, SQLService.
You have now added a profile for SQL Server Agent to use. Next you will change the default name of this profile and add a profile for SQL Mail.
In this exercise, you will configure a profile for the Administrator user account.
In this exercise, you will use SQL Server Enterprise Manager to configure SQL Server Agent to start a mail session that uses the SQLServerAgent Profile profile.
A message appears, indicating that the test was successful. Click OK to close the message.
A message appears asking you if you want to restart SQL Server Agent so that the changes you have made will take effect. Click Yes. Click OK when the service has restarted.
Automating administration saves time and reduces errors that occur when a database administrator forgets to perform a task or is unavailable. SQL Server uses the SQLServerAgent service and the Windows NT application log to enable automated administration. If you intend to make full use of automated administration, you must ensure that the SQLServerAgent service is running, configure an account for the SQLServerAgent, and configure a SQL Server Agent mail profile.