Database initialization ing automating failover for Multiple SQL Server databases

Source: Internet
Author: User
Tags failover sql server books

Written by: Edgewood solutions engineers -- 8/18/2008 -- 6 comments

Win SQL Server books-click here

problem
database partitioning ing was released with SQL Server 2005 and is becoming a popular choice for a Failover solution. one of the big issues with database processing is that the Failover is tied to one database, so if there is a database failure the mirrored server takes over for this one database, but all other databases remain functional on the primary server. the Dra Wback is that more and more applications are being built where multiple databases make up the application, so if only one of the databases fails over the other database will still be functional on the principal server, but the application won't work. how can I be notified when this happens and make all of the databases failover?

Solution
As with just about all functions in SQL Server, there is a way to get alerts or check for events that occur when a database refreshing failure occurs. unfortunately this event notification for database processing ing is not as straight forward as you wowould think, but it can be done.

For database monitoring ing you have the choice of using trace events or you can setup a SQL Server alert to check for a WMI (Windows Management Instrumentation) event for database monitoring ing state changes. before we get started there are a few things you need to do:

Preliminary steps

The mirrored databases and the MSDB database must have service broker enabled. This can be checked with this query

Select name, is_broker_enabled
From SYS. Databases

If the service broker value is not set to 1 You can turn this on by issuing this command for each database.

Alter database MSDB set enable_broker

If SQL Server Agent is running this command will not complete, so you will need to stop SQL Server Agent, run the above command and then start SQL Server Agent again.

Lastly, if SQL Server Agent is not running you will need to start it.

Creating the alert

To setup the alert we are going to do this just like any other alert, but we are going to be using the "WMI event alert" type.

To create the alert expand the SQL Server Agent tree and right click alerts and select "new alert ".

On the new alert screen we are going to change the type to "WMI event alert". Another thing that you will notice isNamespaceThat is being queried. By default SQL server will select the correct namespace Based on the instance of SQL Server you are working.

For the query we are using the following:

Select * From database_mirroring_state_change where State = 7 or state = 8

This data is read from WMI and whenever the database processing ing state changes to either 7 (manual failover) or 8 (automatic failover) the job or notification will be fired.

In addition, you can further define the query for a particle database such:

Select * From database_mirroring_state_change where State = 8 and databasename = 'test'

For additional information you can read about database_mirroring_state_change in books online.

Below is a list of the different State changes that can be monitored. Additional information can be found hereDatabase refreshing ing state change event class.

    • 0 = NULL notification
    • 1 = synchronized principal with witness
    • 2 = synchronized principal Without Witness
    • 3 = synchronized mirror with witness
    • 4 = synchronized mirror Without Witness
    • 5 = connection with principal lost
    • 6 = connection with mirror lost
    • 7 = manual failover
    • 8 = automatic failover
    • 9 = processing ing susponded
    • 10 = no quorum
    • 11 = synchronizing mirror
    • 12 = Principal running exposed
    • 13 = synchronizing principal

OnResponseScreen we can setup how to handle this event when it occurs. You can either setup a job that runs when the alert is fired and/or a notification to an operator that has been setup.

Lastly, you can setup additional options for the alert as shown below.

Example setup

So let's say you have three databases (customer, orders, log) that are part of an application and if one of the databases automatically fails over you want the other two databases to failover as well. in addition, this processing ing setup has des a witness server so if a failure occurs the Failover is automatic.

Here is how this cocould be setup.

First we setup the alert to look at just these three databases.

Then we tell the alert which job to run.

Then we need to create the job "failover databases" which will be run when this alert is fired.

For the SQL Agent job "failover databases" the jobs steps wocould look something like the following:

If exists (select 1 from SYS. database_indexing ing where db_name (database_id) = n 'customer' and fig _ role_desc = 'prinal al ')
Alter database customer set partner failover
Go

If exists (select 1 from SYS. database_sorting ing where db_name (database_id) = n 'Orders 'and fig _ role_desc = 'prinal al ')
Alter database orders set partner failover
Go

If exists (select 1 from SYS. database_indexing ing where db_name (database_id) = n 'log' and mirroring_role_desc = 'prinal al ')
Alter Database Log set partner failover
Go

The alter database command above forces the Failover to the mirrored server for the other databases that did not automatically get flipped. this is the same as if you clicked on the "failover" button in the GUI.

Next steps

    • now that you have an idea how you can use the WMI events to check for mirror state changes do some testing on your servers to see if this is something you could use.
    • each time a database fails over it will kick off the alert, so if there are multiple failovers it will attempt to run the "failover databases" job multiple times.
    • you also wowould need to think about how you wowould want this to run. you may want an automatic failover from the primary server to the secondary server, but you may only want to be notified if the failure goes from the secondary to the primary server.
    • although this may not be an ideal solution, it does allow you to get all of your databases on the same server. since database processing ing is still at the database level there is no guarantee that all or your transactions will stay in synch processing SS multiple databases whether you use this approach or not.

Click to share this tip on Twitter!

Related tips

    • Click here for more articles on similar subjects

Forum posts

    • Discuss This tip: http://blogs.mssqltips.com/forums/t/812.aspx
    • There are 6 comments for this tip, last post by dinga
Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.