Programming, ASP.NET, C#, JavaScript and Computer Life
Ralph Whitbeck
Home
Contact
Subscribe
Sunday, October 12, 2008
Database Mirroring with SQL Server 2005
Database mirroring is a software solution, provided by SQL Server 2005, that gives you the ability to replicate your data in real-time. Mirroring allows for increased database availability and data protection and works at the database level on databases that are set to full recovery mode. You need two instances ofSQL Server to set up mirroring because it maintains separate copies of data of a single database across both servers.
Let's look at a common scenario for database mirroring. Take a company that has their customer database in a data center in New York City. They also have a backup data center in Houston. The database in NY is mirroring the data to the database in Houston. Let's say the data center in NY loses power and they need to shut the server down. With a "flip of a switch" they can move the database to Houston and continue as if nothing happened. When the data center gets power again they can switch to the NY city database as soon as it's resynchronized.
Benefits
One benefit of database mirroring is the increased data protection of your data. As each transaction is being written to your principle database it is sending that exact transaction to the mirror database. This provides complete or almost complete (depending on which mode you set up; I'll get into this later) data redundancy in real time.
High-availability, this is a huge benefit for mission critical data. Hardware failures happen and network issues arise, with a "flip of a switch' you can send the responsibility to the mirrored server to become the principled server at any time. You can also set up automatic failover with the addition of a witness server to monitor the servers.
Another benefit is that you have the flexibility to install updates on your server without data disruption. Because you can manually failover to the mirror server at any time you can failover, install the updates and reboot if necessary. When you are done you can then have the new principle server failover again to restore the principle on to the primary server without any data loss or downtime and upgrade the mirror server.
Finally, you can set up mirroring to sync in synchronous or asynchronous modes. Synchronous or high-safety mode will write a transaction to the principle database and send that exact transaction to the mirror and then wait for it to finish committing that transaction before moving on. With high-safety you are assured that the data that is on the principle is on the mirror at the exact same time. In that you trade some latency waiting for the transaction for the true redundancy of the data. Automatic and manualfailovers, explained more below, require the use of high-safety mode.
Asynchronous or high-performance mode will send the stream of transactions at the mirror server and the mirror will try to keep up. With high-performance there can be a gap where the mirror has a back log of transactions to complete that the principle already has completed. This gap can become pretty big with heavy loads on the principle.
Requirements
Database mirroring requires two servers. With SQL Server 2005 you can set up mirroring with Standard or Enterprise editions. It's very important that both servers are running the same edition. It is also recommended that the hardware for both servers be near identical and have the ability to handle the same load.
Setting up a Database Mirror
Let's start with some simple preparation:
Make sure that you have the proper logins created on the mirror server.
Create the mirror database by restoring with NORECOVERY a recent full backup of the principle database. The principle database must have been set-up with full recovery when the backup was created.
You must also restore all log backups since the full backup was made. Create a backup of the transaction logs and restore on the mirror server. You'll want to start the mirror session as soon as you can after taking the log backup.
Configuring Database mirroring:
Connect to the principle database. Select the database you wish to mirror.
Right-click > Tasks > Mirror; this will bring you to the Mirror tab on the database properties pop-up.
Select the Configure Security button to configure the mirror session through the Configure Database Mirroring Security Wizard (only in this wizard you can add or change the witness server instance).
Upon completion of the security wizard and you are still connected to the principle server click on Start Mirroring.
Client Connections (Connection Strings)
I am going to assume that you as the reader already know how to connect to a database in your application code in order to access it. What I'll cover here are the required key strings needed in the connection string to handle connecting to both database servers in a mirror session depending on which one is acting as the principle at the moment.
Just as you would with a normal connection string you need to provide a initial partner name. This will be the principle server. If you are usingTCP/IP then enter in the IP, if you are using named pipes then enter in the name of your server. Additionally, if your SQL Server is using a Instance Name then you will need to add that as well (example: 10.10.2.1\InstanceName. Now we need to provide the failover partner. This will be the mirror server. Add this information similarly as you would for the initial partner name.
The other item you will need to provide in your connection string is the network attribute. This will specify the network protocol to be used and ensures that the proper network protocol persists between connections to different partners. ForTCP/IP use the following:
Network=dbmssocn;
Named Pipes use:
Network=dbnmpntw;
Putting it all together in a sample connection string, you can get
more info on connection string keywords here
:
Data Source = 10.10.2.10/InstanceName; Failover partner = 10.10.2.11\InstanceName; Initial Catalog = databaseName; User ID = sa; Password = 12345; Network = dbmssocn;
Failover
There are three types of failover: automatic, manual and forced. Automatic requires a witness server and the mirror set-up in high-safety mode. Manual failover does not use a witness server and also requires the mirror set-up to be in high-safety mode. Forced is usually used because the principle server has been disconnected somehow. Using forced service may result in data lose as all transactions may not have made it to the mirrored server. Forced service is supported on high-availability and high-safety mirrored set-up modes.
Automatic Failover
For automatic failover to occur the following conditions are required:
Mirroring sessions must be running high-safety mode and posses a witness server.
The mirror database must be synchronised.
The principle server has lost communication with the witness and mirror servers but the witness and mirror server are still online. Note: If all servers lose connectivity but then the witness and mirror server comes online automatic failover does not occur.
The mirror server detects the loss of the principle server.
How it works
If the principle server is still online and changes it's state to DISCONNECTED and disconnects all clients.
The mirror and witness server both register that the principle server is unavailable.
The mirror server waits for all logs to be written from the redo queue before rolling forward the mirror database.
The former mirror server now moves online to be the principle. Recovery will roll back any uncommitted transactions, locks isolate those transactions.
When the former principle server comes back online and sees that the mirror has moved to principle the former principle server will become the new mirror and start synchronising. Once synchronising is complete failover is possible again.
Manual Failover
With manual failover it's possible to failover to the mirror server so that updates and upgrades can be performed on the principle server. Manual failover requires the mirror set-up to be in high-safety mode (transaction safety set to FULL). The partners need to be connected and synchronised.
How it works:
Principle server disconnects, sends the last log message to the mirror and switches to be the mirror server.
The mirror server records the last log message as the failover log.
The mirror server waits for all logs to be written from the redo queue before rolling forward the mirror database.
The mirror server becomes the principle server and the principle becomes the new mirror.
The new mirror server quickly resynchronizes with the new principle server. Once complete failover is possible again.
Initiate Manual failover:
Connect to the principle server. Choose your database.
Right-click > Tasks > Mirror; this will bring you to the Mirror tab on the database properties pop-up.
Click Failover. Confirm that you want to failover to the mirror server.
Note: The client will need to see that the connection to the primary has failed before it will try to connect to the failover partner.
Forced Service
If the Principle server goes down due to hardware issues or is unreachable you can use forced service to bring the mirror to principle state. Doing this may cause data loss as the mirror may not have received all of the transaction logs from the principle.
Read this Microsoft TechNet article for more information on forced service
.
Initiate Forced Service
Connect to the mirror server.
Run the following T-SQL:
ALTER DATABASE
<database_name>
SET PARTNER FORCE_SERVICE_ALLOW_DATA_LOSS
The mirror will take over as principle and mirroring will be suspended.
Summary and Online Resources
Database mirroring has many options and ways to be effective in maintaining a high-availability database. This article is my collected research as I was learning how to set up mirroring. If you would like more information on setting up database mirroring please look at the following resources:
Database Mirroring Overview
How to: Configure a Database Mirroring Session (SQL Server Management Studio)
How to: Add or Replace a Database Mirroring Witness (SQL Server Management Studio)
Making the Initial Connection to a Database Mirroring Session
Using Connection String Keywords with SQL Server Native Client
Role Switching During a Database Mirroring Session
Automatic Failover
Manual Failover
How to: Manually Fail Over a Database Mirroring Session (SQL Server Management Studio)
Forced Service (with Possible Data Loss)
How to: Force Service in a Database Mirroring Session (Transact-SQL)
Sunday, October 12 2008 at 11:15 PM |
Comments [0]
Tags:
How-to
|
Programming
|
Technology
Name
E-mail
Home page
Remember Me
Comment (Some html is allowed:
a@href@title, b, blockquote@cite, em, i, strike, strong, sub, super, u
)
Enter the code shown (prevents robots):
Blog Posts by:
Recent
Month
Category
Search
Currently Viewable:
My Twitter Updates
View Twitter Page
Contact Ralph
E-Mail
Click icon to send me e-mail.
Instant Messenger
AIM:
RedWolves2
Social Networks
Twitter:
http://twitter.com/redwolves
Last.fm:
http://last.fm/user/RedWolves/
Close
Subscribe
Main Feed
Category Feeds
BrandLogic
Entertainment
Fitness
How-to
Interesting Links
Mussings
Personal Finance
Politics
Programming
Simple Shadow
Sports
Sushi
Technology
Other feeds
Twitter updates: RedWolves
MediaGab Forum posts
Close
Blog Posts
Recent
System.Net.WebException: The operation has timed-out.
Database Mirroring with SQL Server 2005
Gaining Exclusive Access to database in SQL Server 2005 via T-SQL
Light at the End of the Tunnel
jQuery Plugin - Simple Shadow
Adding link separators to a unordered list using jQuery
How to target="_blank" a link while keeping it XHTML compliant with jQuery
Book Review: "Smart & Gets Things Done" by Joel Spolsky
Close
Blog Posts
Month
November, 2008 (1)
October, 2008 (2)
September, 2008 (1)
August, 2008 (1)
July, 2008 (1)
June, 2008 (3)
May, 2008 (4)
April, 2008 (4)
February, 2008 (1)
January, 2008 (2)
November, 2007 (4)
October, 2007 (5)
July, 2007 (2)
May, 2007 (2)
April, 2007 (1)
February, 2007 (5)
January, 2007 (16)
December, 2006 (5)
November, 2006 (3)
October, 2006 (10)
September, 2006 (6)
August, 2006 (3)
July, 2006 (17)
June, 2006 (16)
May, 2006 (13)
April, 2006 (4)
March, 2006 (9)
February, 2006 (10)
January, 2006 (23)
December, 2005 (3)
Close
Blog Posts
Category
BrandLogic
Entertainment
Fitness
How-to
Interesting Links
Mussings
Personal Finance
Politics
Programming
Simple Shadow
Sports
Sushi
Technology
Close