Hướng dẫn học Microsoft SQL Server 2008 part 106 pot

10 155 0
Hướng dẫn học Microsoft SQL Server 2008 part 106 pot

Đang tải... (xem toàn văn)

Thông tin tài liệu

Nielsen c43.tex V4 - 07/21/2009 2:32pm Page 1012 Part VI Enterprise Data Management install SQL Server, so you’ll always need to set it to start automatically after an installation.) This is an elementary step, but it is occasionally overlooked (and then, after someone restarts the server, none of the scheduled jobs run and, perhaps even worse, critical alerts go undetected). Best Practice T he best way to avoid problems with services not restarting is to set them to start automatically. Here’s how to do that: 1. Open the Services console found in the Administrative Tools folder of the Control Panel. 2. Right-click the SQL Server Agent (Instance) service and choose Properties from the context menu. 3. Select Automatic in the Startup Type field and click OK. The SQL Server Agent service is displayed as SQL Server Agent, with the instance name appearing in parentheses. The instance name is MSSQLSERVER for default instance of SQL Server. If more than one SQL Server instance is installed on the server, a SQL Server Agent service will exist for each instance. As with any service, the SQL Server Agent startup mode can be changed through the Services console in the Control Panel. However, another way to accomplish the same goal is to use the SQL Server Con- figuration Manager found by selecting Start ➪ Programs ➪ Microsoft SQL Server 2008 ➪ Configuration Tools ➪ SQL Server Configuration Manager. The SQL Server Configuration Manager appears in Figure 43-1. In this figure, you can see three instances of the SQL Server Agent service. SQL Server Agent (MSSQLSERVER) is the SQL Server Agent for the default SQL Server (MSSQLSERVER) instance. The services named SQL Server Agent (INST1) and SQL Server Agent (INST2) are the SQL Server Agent instances for the SQL Server named instances SQL Server (INST1) and SQL Server (INST2), respectively. Follow these steps to ensure that the startup mode of the SQL Server Agent service is set to automatic: 1. Open the SQL Server Configuration Manager. 2. Highlight the SQL Server Services folder. 3. Right-click the service you want to change and choose Properties from the context menu. The service’s Properties dialog will appear. 4. Select the Service tab. 5. Highlight the Start Mode property and choose the new starting mode from the drop-down list box. 6. Click OK to make the change complete. 1012 www.getcoolebook.com Nielsen c43.tex V4 - 07/21/2009 2:32pm Page 1013 Automating Database Maintenance with SQL Server Agent 43 FIGURE 43-1 The SQL Server Configuration Manager dialog enables you to change the startup mode of the SQL Server Agent service. It is a good idea to take one extra step to ensure that SQL Server Agent (and SQL Server, for that mat- ter) restarts if it stops unexpectedly: 1. Start SQL Server Management Studio (its default location is Start ➪ Programs ➪ Microsoft SQL Server 2008 ➪ SQL Server Management Studio). 2. Expand the folders until you find the SQL Server you are configuring. If no SQL Servers are configured, connect to the one you wish to manage. 3. Expand to see the folders below the SQL Server. One of these folders is entitled SQL Server Agent. Right-click the folder and choose Properties from the context menu. Choose the General folder, which offers the options shown in Figure 43-2. 4. Check both of the Auto restart options (refer to Figure 43-2). This ensures that SQL Server and SQL Server Agent restart when they unexpectedly stop. 1013 www.getcoolebook.com Nielsen c43.tex V4 - 07/21/2009 2:32pm Page 1014 Part VI Enterprise Data Management FIGURE 43-2 The General tab of the SQL Server Agent Properties dialog enables you to configure how the service runs. SQL Server Agent Setup Pitfalls F or a SQL Server 2008 Failover Clustering instance, do not set the startup mode of the SQL Server or the SQL Server Agent service to automatic. The startup mode of these services should be left to manual (default), as the cluster service controls the starting and stopping of these services. The second pitfall to be aware of when setting up SQL Server Agent has to do with security. It’s crucial to determine which account will be used to run this service. By default, the system account has access to only local resources. You must use a domain account if you want to access network resources in any of the scheduled jobs (backing up a database to a different server, for example). Typically, a domain account is continued 1014 www.getcoolebook.com Nielsen c43.tex V4 - 07/21/2009 2:32pm Page 1015 Automating Database Maintenance with SQL Server Agent 43 continued also needed to enable SQL Server to send e-mail and pager notifications (the steps to do this are outlined later in this section). You must also use a domain account in order for replication to work. Typically, SQL Server Agent is configured to use a Windows domain account that is a member of the sysadmin role, so that the necessary permission is in place to run jobs or send notifications. To change the account used to run the SQL S erver Agent service, follow these steps: 1. Open the SQL Server Configuration Manager. 2. Highlight the SQL Server Services folder. 3. Right-click the SQL Server Agent entry and choose Properties from the context menu. The Properties dialog will appear. 4. Select the Log On tab. As shown in Figure 43-3, you can choose between one of the built-in accounts or use a specific account. Choose ‘‘This account’’ and either type in a name or browse a domain account on production SQL Servers where you want to access network resources, and send e-mail and pager notifications. 5. Click OK. A dialog will confirm that the change in account requires a service restart. 6. Click Yes. The account change will appear as soon as the service restarts. FIGURE 43-3 Choose a built-in or other account to use for log on purposes. 1015 www.getcoolebook.com Nielsen c43.tex V4 - 07/21/2009 2:32pm Page 1016 Part VI Enterprise Data Management The final step is to set up the SQL Server Agent mail profile so that the service can send e-mail and pager notifications when alerts occur. This requires setting up and configuring a mail service and letting SQL Server Agent know how to access the mail service. SQL Server Agent mail can use either Database Mail or SQL Mail to send e-mails. The section ‘‘Database Mail’’ in this chapter discusses how to configure Database Mail to send e-mails. SQL Mail is not discussed here, as this feature exists only for backward compatibility and will be removed i n future versions of SQL Server. Understanding Alerts, Operators, and Jobs An alert defines a specific action that is carried ou t when a certain condition is met. Such a condition can be set up for a variety of performance counters, including number of connections, database file size, and number of deadlocks per second. A condition can also be tied to an error number or degree of error severity. When acting upon an alert condition, SQL Server Agent can notify one or more operators, run a job, or both. Operators are the people responsible for handling critical conditions on the database server. As pointed out in the previous section, one of the neat things SQL Server Agent does is send messages to operators to report job status or make them aware of server conditions. Operators can be set up to receive mes- sages via e-mail, pager, or Net Send. You can specify at which times an operator is available to receive messages via pager (e.g., 9:00 A.M. to 5:00 P.M., Monday to Friday). You can also suspend notification for a specified operator, such as when the operator is taking time off. A job is a database task or group of database tasks. Examples of typical jobs are backing up a database, reorganizing the indexes, or executing a SQL Server Integration Services (SSIS) package. SQL Server Agent jobs are also used behind the scenes to implement and schedule a maintenance plan using the Maintenance Plan Wizard in SQL Server Management Studio. Managing Operators Just as you need to create logins for the users who will be accessing a SQL Server database, you need to create operators in SQL Server to be able to send alerts to these support people. Creating operators in SQL Server is straightforward. Here’s how it works: 1. Start SQL Server Management Studio and find the SQL Server Agent folder below the server you are configuring. 2. Right-click the SQL Server Agent\Operators folder and select New Operator from the context menu. This brings up a dialog similar to the one shown in Figure 43-4. Choose the General folder as shown in the figure. 3. Type in the name of the operator as well as his or her e-mail address, pager e-mail address, and/or Net Send address, depending on how you want the notification to be sent. Filling out a pager address enables you to specify when the operator is available to be paged. 4. Select the Notifications folder and choose the notification method for each alert. 5. Click the Jobs option and set the notification method for each job. Be sure to revisit the operator configuration every time a new alert or job is created and assign operators to new alerts and jobs as needed. 1016 www.getcoolebook.com Nielsen c43.tex V4 - 07/21/2009 2:32pm Page 1017 Automating Database Maintenance with SQL Server Agent 43 FIGURE 43-4 The New Operator dialog enables you to specify when an operator is available to receive pager notifications. If an operator is unavailable to respond to notification, temporarily disable this operator by clearing the Enabled check box in the General folder. Just remember to ensure that another operator will be notified. Rather than disable a n operator, change the e-mail, pager, and Net Send addresses until the operator becomes available again. Managing Alerts Depending on how SQL Server is installed, there may be no alerts created by SQL Server. However, SQL Server does create some default alerts when specific product features such as replication are installed. The names of the fourteen replication alerts start with ‘‘Replication.’’ By default, SQL Server disables all of the replication alerts except for the warnings, and the alerts have no operators assigned to them. When deciding to use the replication features, be sure to provide operators to receive the alerts so someone is aware if something goes wrong. The ‘‘Managing Operators’’ section of this c hapter details one method to add operators to an alert. The following sections describe how to create your own errors and alerts. 1017 www.getcoolebook.com Nielsen c43.tex V4 - 07/21/2009 2:32pm Page 1018 Part VI Enterprise Data Management Creating user-defined errors When deploying custom-written applications that use SQL Server as their data store, the application programmers may define their own set of errors. Use the sp_addmessage system stored proce- dure to perform this task. As a minimum, supply the error number (any value between 50,001 and 2,147,483,647), the severity (a number between 1 and 25), and the message text. The stored procedure also accepts a l anguage identifier that consists of the name of any language installed on the server, such as English. The default setting is the currently selected language. You must also determine whether you want to write the error message to the Windows event log. The default setting is FALSE in this case. Finally, provide an argument to replace an existing message with a new message. If an attempt is made to create a new message with the same number as an existing number without providing the replace value, the system displays error message 15043, stating ‘‘You must specify ‘REPLACE’ to overwrite an existing message .’’ To list the error messages supported by SQL Server, query the sys.sysmessages catalog view filtering for error > 50000 when you want to see just the custom error messages. You can also filter the messages by severity and language ID. The mysterious-looking dlevel field has a value of 128 when the error message writes to the Windows event log. Here’s an example of the sp_addmessage command: sp_addmessage 50001, 1, ‘This is a test message.’; In this case, the command adds a new message with a message identifier of 50001. The severity level of this message is 1 and the human-readable text is ‘‘This is a test message.’’ The system won’t write this message to the event log and it will use the current server language. However, suppose you decide to change this message so that it does write to the event log. In this case, change the command to the following: sp_addmessage 50001, 1, ‘This is a test message.’, ‘English’, TRUE, REPLACE; Notice that the command now has a l anguage specified. The value of TRUE indicates that the system will write the message to the event log. Finally, because you’re replacing this message with one that already exists, the command includes the word REPLACE. As far as alerts are concerned, user-defined and native SQL Server messages are handled uniformly. Specify the error number or severity level, and when an error is raised that matches the alert condition, SQL Server Agent will initiate the specified response. The following section covers how to set up these kinds of alerts. Creating an alert You can create three kinds of alerts. The first is triggered by an error number or by an error of a spec- ified severity. The second is triggered by a SQL Server performance counter. The third is triggered by a Windows Management Instrumentation (WMI) event. Here is how to set up alerts: 1. Start SQL Server Management Studio. 2. Right-click the SQL Server Agent\Alerts folder and choose New Alert from the context menu. You’ll see the New Alert dialog shown in Figure 43-5. 3. Type a name for the alert. Because an alert can affect one or all databases and define any num- ber of events or conditions, name the alert carefully. Use keywords so that the system orders the alert automatically for you — for example, ‘‘Event: AdventureWorks: Severity Level 16.’’ 1018 www.getcoolebook.com Nielsen c43.tex V4 - 07/21/2009 2:32pm Page 1019 Automating Database Maintenance with SQL Server Agent 43 FIGURE 43-5 An error condition is one of the three events that can trigger an alert. 4. Select the type of alert. The Type list box enables you to specify which kind of alert you want to create: a SQL Server event alert (triggered by an error number or level of severity), a SQL Server performance-condition alert, or a WMI event alert. Figure 43-6 shows the changes to make to the New Alert dialog to create a SQL Server performance-condition alert. In this case, the alert will trigger when the size of the AdventureWorks2008 database log exceeds 4,000 KB (or 4 MB). Use this technique to access any of the SQL Server performance counters. 5. Configure the alert (what to do in this step depends on the choice made in previous step): a. When creating a SQL Server event alert, select the severity and enter the error number to monitor. It is possible to monitor either all databases on the server or a specific database. Finally, to restrict alerts to messages c ontaining a specific text string, c heck the ‘‘Raise alert when message contains’’ check box and enter the text in the ‘‘Message text’’ text box. b. When creating a SQL Server performance-condition test alert, select the object and counter to monitor. Then set the threshold for that counter. You can specify that the alert occur when the counter falls below, equals, or exceeds, the specified value. For some counters, you can specify the instance to which the counter is to be applied. For example, you can monitor the data-file size for either all databases on the server or just one specific database. 1019 www.getcoolebook.com Nielsen c43.tex V4 - 07/21/2009 2:32pm Page 1020 Part VI Enterprise Data Management When creating a WMI event alert, you must provide the namespace for the event, such as \\.\root\Microsoft\SqlServer\ServerEvents\MSSQLSERVER. In addition, you must provide a query for that namespace. All WMI queries r ely on the Windows Management Instrumentation Query Language (WQL). FIGURE 43-6 A performance condition is one of the three events that can trigger an alert. 6. Select the Response tab. Determine the kind of response the alert will request. In the Response tab, specify one or more operators to be notified, or which job to run, or both. The next section, ‘‘Managing Jobs,’’ covers how to set up jobs. O f course, the New Operator button brings up the New Operator dialog discussed in the previous section, ‘‘Managing Operators.’’ Typically, you choose to send the error text in an e-mail or a Net Send, but not when pag- ing an operator. Three check boxes beside the list of operators to be notified control the notification method. 7. Select the Options tab. Choose to send the alert error text in Email, Pager, or Net Send noti- fications. In addition to the standard error text, it is also possible to provide special text for 1020 www.getcoolebook.com Nielsen c43.tex V4 - 07/21/2009 2:32pm Page 1021 Automating Database Maintenance with SQL Server Agent 43 this alert. Finally, for recurring alerts, specify the delay between responses in minutes and seconds. This is especially important for SQL Server performance-condition alerts, because these conditions tend to exist for a long time and you don’t want to flood the operators with multiple alerts for the same condition. Managing Jobs A job is defined as a series of steps with a specific work flow. You can, for example, specify that step 1 will execute step 2 if it succeeds, but will execute step 3 if it fails. Steps come in two basic types. The first type involves replication. The second can execute Transact-SQL script, ActiveX script (Visual Basic script or JScript), or any operating-system command. The latter are the most frequently used. After each step, you can specify the next action depending on whether the step succeeds or fails. There are four options: ■ Go to the next step. ■ Go to step x,wherex is the number of any step defined in the job. ■ Quit the job, reporting success. ■ Quit the job, reporting failure. You can also set the number of times a step will be attempted in case of failure. It is possible to asso- ciate one or more schedules with a job. This enables a job to be automatically run at a specified time. A schedule can specify that a job should run once at a specific time or on a recurring basis. You can also schedule a job to run whenever SQL Server Agent starts or whenever the CPU becomes idle. Specify when you c onsider the CPU to be i dle in the Advanced tab of the SQL Server Agent Properties dialog (see Figure 43-7). This involves selecting the level of average CPU usage that the CPU must fall below for a specified time in seconds. The default idle CPU condition is when average CPU usage falls below 10% and remains below this level for 10 minutes. Finally, you can also set notifications for com- pletion, success, or failure of a job. Best Practice I t’s important to realize that your server is never completely idle. Even a well-tuned setup will usually have 2 percent or 3 percent activity. Consequently, setting the ‘‘Average CPU Usage Falls Below’’ setting to 0 percent means the job will never run. However, nor do you want to set this value so high that it interferes with jobs that must run in the foreground in near real time. Consequently, using a setting between 5 percent and 12 percent normally works best. Likewise, the amount of idle time is important. Using a setting between 360 and 600 seconds works well. However, an administrator for a large system might want to set this value higher to ensure that the server is truly idle, while an administrator for a smaller system might want to use a lower value to maximize use of idle time. 1021 www.getcoolebook.com . the SQL Server Agent service. SQL Server Agent (MSSQLSERVER) is the SQL Server Agent for the default SQL Server (MSSQLSERVER) instance. The services named SQL Server Agent (INST1) and SQL Server. are the SQL Server Agent instances for the SQL Server named instances SQL Server (INST1) and SQL Server (INST2), respectively. Follow these steps to ensure that the startup mode of the SQL Server. in parentheses. The instance name is MSSQLSERVER for default instance of SQL Server. If more than one SQL Server instance is installed on the server, a SQL Server Agent service will exist for each

Ngày đăng: 04/07/2014, 09:20

Tài liệu cùng người dùng

Tài liệu liên quan