Database Mirroring Automating Failover for Multiple SQL Server Databases
2009-04-29 18:06
597 查看
Written By: Edgewood Solutions Engineers -- 8/18/2008 -- 6 comments
Win SQL Server Books - click here |
Database Mirroring was released with SQL Server 2005 and is becoming a popular choice for a failover solution. One of the big issues with Database Mirroring 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 drawback 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 mirroring failure occurs. Unfortunately this event notification for Database Mirroring is not as straight forward as you would think, but it can be done.
For database mirroring 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 Mirroring 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 |
ALTER DATABASE msdb SET ENABLE_BROKER |
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 is the Namespace that is being queried. By default SQL Server will select the correct namespace based on the instance of SQL Server you are working with.
For the query we are using the following:
SELECT * FROM DATABASE_MIRRORING_STATE_CHANGE WHERE State = 7 OR State = 8 |
In addition, you can further define the query for a particular database such as:
SELECT * FROM DATABASE_MIRRORING_STATE_CHANGE WHERE State = 8 AND DatabaseName = 'Test' |
Below is a list of the different state changes that can be monitored. Additional information can be found here Database Mirroring 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 = Mirroring Suspended
10 = No Quorum
11 = Synchronizing Mirror
12 = Principal Running Exposed
13 = Synchronizing Principal
On the Response screen 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 mirroring setup includes a Witness server so if a failure occurs the failover is automatic.
Here is how this could 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 would look something like the following:
IF EXISTS (SELECT 1 FROM sys.database_mirroring WHERE db_name(database_id) = N'Customer' AND mirroring_role_desc = 'PRINCIPAL') ALTER DATABASE Customer SET PARTNER FAILOVER GO IF EXISTS (SELECT 1 FROM sys.database_mirroring WHERE db_name(database_id) = N'Orders' AND mirroring_role_desc = 'PRINCIPAL') ALTER DATABASE Orders SET PARTNER FAILOVER GO IF EXISTS (SELECT 1 FROM sys.database_mirroring WHERE db_name(database_id) = N'Log' AND mirroring_role_desc = 'PRINCIPAL') ALTER DATABASE Log SET PARTNER FAILOVER GO |
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 would need to think about how you would want this to run. You may want an automated 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 mirroring is still at the database level there is no guarantee that all or your transactions will stay in synch across 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
相关文章推荐
- SQL Server How to Perform Database Mirroring
- sqljdbc4和sqljdbc.jar或No suitable driver found for jdbc:sqlserver......DatabaseName=sql_database01
- VS2010不能正确加载 'VSTS for Database Professionals Sql Server Data-tier Application'
- vs2010未能正确加载vsts for database professional sql server data-tier application这个错误
- The SQL Server Service Broker for the current database is not enabled
- SAP SQL 压缩 Database compression for SQL Server
- Restore SQL Server 2005 error : "The tail of the log for the database "TEST1" has not been backed up."
- vs2010不能正确加载 'VSTS for Database Professionals Sql Server Data-tier Application'包
- [转]Database Mirroring Log Compression in SQL Server 2008 Improves Throughput
- 【推荐】[SQL优化工具]Quest.Central.For.Databases——SQL Tuning for SQL Server
- WIN8安装VS2010'VSTS for Database Professionals Sql Server Data-tier Application‘ didn't load correctly
- VS2010不能正确加载 'VSTS for Database Professionals Sql Server Data-tier Application'
- 未能正确加载“VSTS for Database Professionals Sql Server Data-tier Application”包。(转)
- SQLWave. GUI Client for MySQL Database Server
- SQL Server script to rebuild all indexes for all tables and all databases
- [SQL优化工具]Quest.Central.For.Databases——SQL Tuning for SQL Server
- vs2010不能正确加载 'VSTS for Database Professionals Sql Server Data-tier Application'包
- Configure SQL Server Database Mirroring Using SSMS
- DB太大?一键帮你收缩所有DB文件大小(Shrink Files for All Databases in SQL Server)
- vs2010安装失败后,VS2010不能正确加载 'VSTS for Database Professionals Sql Server Data-tier Application'包的解决办法