What is Mirroring in SQL Server ?

Database mirroring is an alternative high-availability solution to failover clustering in SQL Server. Database mirroring supports automatic failover, but does not require cluster-capable hardware, and can therefore provide a cost-effective alternative to failover clustering. In this Article, we will implement Mirroring with witness Server and will Automatic Failover too.

What is the Hardware and software requirement for implementing Mirroring in SQL Server ?

There is no any such hardware and software requirement for implementing. Even SQL Server Standard Edition supports Mirroring. For More Details about feature supported by various editions, please check my previous post, “Does SQL Server Standard Edition Supports Mirroring?” To test the Automatic failover, you need three SQL Server Instances.

In case there is a firewall in between Principal, Mirror and Witness Server, please ensure there endpoint ports are Opened in firewall.

How to implement Database Mirroring in SQL Server 2008 R2?

Implementing SQL Server Mirroring is quite simple in SQL Server 2008 R2. For better understanding, Lets take an real business requirement and deploy mirroring with WITNESS Server (Automatic Failover)

I have already posted an Video Post, where a step by step implementation of mirroring is available, but you need to have silver light installed in your system to watch this video and a good bandwidth.

In this Article, I am going to implement a Mirroring using three SQL Server Instances, where

  1. Principal Server
  2. Mirror Server
  3. Witness Server

Principal Server Details

  • SQL Server Instance Name : <SERVER_NAME>SQL1
  • Version : SQL Server 2008 R2
  • Edition : Evaluation Copy
  • Database Name : Test_SQLServer_Mirroring

Mirror Server Details

  • SQL Server Instance Name : <SERVER_NAME>SQL2
  • Version : SQL Server 2008 R2
  • Edition : Evaluation Copy
  • Database Name : Test_SQLServer_Mirroring

Witness Server Details

  • SQL Server Instance Name : <SERVER_NAME>SQL3
  • Version : SQL Server 2008 R2
  • Edition : Evaluation Copy

*Note : In this example, all these instances are named instances hosted on a single operative case. In case you are implementing in your production environment, All these three roles should be on different machines.

We are going to perform following Steps to setup Mirroring and automatic Failover Testing

  1. Backup Primary Database (Principal Server)
  2. Using latest backup files, Restore database on Mirror Server with NORECOVERY Option
  3. Set up Mirroring using Wizard where we will define
    1. Identify of Principle Server, Mirror Server and Witness Server
    2. Create End Points for Principle Server, Mirror Server and Witness Server
    3. Configure Security
  4. Start Mirroring
  5. Test Manual Failover
  6. Test Automatic Failover

STEP 1 –  Backup Primary Database (Principle Server)

On Principal Server Perform a full backup and copy this backup file to mirror server for restore. Backup is quite simple, which can be taken using the following Script on Principal Server (.SQL1)

-- This will Backup Database named Test_SQLServer_Mirroring to C:tempMirroringTEST.bak file
BACKUP DATABASE [Test_SQLServer_Mirroring] TO  DISK = N'C:tempMirroringTEST.bak' WITH NOFORMAT, NOINIT,  NAME = N'Test_SQLServer_Mirroring-Full Database Backup', SKIP, NOREWIND, NOUNLOAD,  STATS = 10
GO

STEP 2 –  Restore Database on Secondary Database (Mirror Server)

On Mirror Server, Restore the database with same name with NORECOVERY option on Server .SQL2Restore is quite simple, which can be taken using the following Script or by SSMS

-- Restore Database name Test_SQLServer_Mirroring] on Mirror Server (.SQL2) with NORECOVERY option
RESTORE DATABASE [Test_SQLServer_Mirroring] FROM  DISK = N'C:tempMirroringTEST.bak' WITH  FILE = 1,  MOVE N'Test_SQLServer_Mirroring' TO N'C:tempMirroringSQL2_Test_SQLServer_Mirroring.mdf',  MOVE N'Test_SQLServer_Mirroring_log' TO N'C:tempMirroringSQL2_Test_SQLServer_Mirroring_1.ldf',  NORECOVERY,  NOUNLOAD,  STATS = 10
GO

STEP 3 Set up Mirroring using Wizard where we will Identify of Principle Server, Mirror Server and Witness Server, Create End Points for Principle Server, Mirror Server and Witness Server and will also configure Security

Open SSMS and connect to Principal Server >>> Database, which you wanted to enabled for mirroring>>> right click >>> and Select Properties

Setting up SQL Server Mirroring

which will open up a Database Properties Box like this and Click on Mirroring

SQL Server Database Properties Mirroring

Once you are on Mirroring Properties page, select “Configure Security…” button, which will open aConfigure Database Mirroring Security Wizard

Configure Database Mirroring Security Wizard

Next screen, will ask you, do you want to include Witness Server or not ? Witness Server is useful to initiate Automatic failover. As per our requirement, we will be opting to include Witness Server, so we will SelectYES

Mirroring include Witness Server or not

Next screen will ask you, where you wanted to save the configuration, In case we opted for Automatic Failover / Witness Server, configuration must be saved at Witness Server. In case, we do not want to include Witness Server and would like to to do Manual failover, then we can have option for saving Configuration either on Principal Server or on Mirror Server.

As per our requirement (Automatic Failover), we will opt for Witness Server Instance.

SQL Server Mirroring where to save the configuration

Next Screen where we need to specify information about the SQL instance where the database was originally located. In out example this is .SQL1

SQL Server Mirroring - specify information about the SQL instance where the database was originally located

Here we have option to specify the TCP / IP port on which endpoint will be listening to requests. In case there is a firewall in between Principal and Mirror Server, please ensure there these ports are Open.

In our example, all these three instances are hosted on a single physical box that the reason we will opt for different port for Mirror and Witness Server End point.

 

Adding to this we have an option to encrypt data, which travel between principal, mirror and witness server. In case we opt for this, we assume that you are already have certificates, otherwise, this will work without Certificates too.

So In Select Principal Option, we specified .SQL1 as Server name and used Windows Authentication to connect to principal server, which has sysadmin privileges. We also checked the Encrypt Data option and 5022 is the Port number which we used for Principal endpoint.

Next Screen where we need to specify information about the SQL instance where the mirror copy of the database will be located, in our example this is ./SQL2

SQL Server Mirroring - specify information about the SQL instance where mirror copy of the database will be located

So In Select Mirror Option, we specified .SQL2 as Server name and used Windows Authentication to connect to principal server, which has sysadmin privileges. We also checked the Encrypt Data option and 5023 is the Port number which we used for Mirror endpoint.

Next Screen where we need to specify information about the SQL instance that monitors the status of the principal and mirror server instances

SQL Server Mirroring - specify information about the SQL instance that monitors the status of the principal and mirror server instances

So In Select Witness Server Option, we specified .SQL3 as Server name and used Windows Authentication to connect to principal server, which has sysadmin privileges. We also checked the Encrypt Data option and 5024 is the Port number which we used for Mirror endpoint.

Next Screen will actually create these endpoint in the respective Server

SQL Server Mirroring - Configure Endpoint in Principal,Mirror and Witness Server

This is Work in progress status, Once this is being configured and running, you will get a confirmation status as shown below.

Configure Endpoint in Principal,Mirror and Witness Server

STEP 4 –  Start Mirroring

Once this is Done, you are ready to start the mirroring, the wizard, will itself ask you to do that, as shown in the following screen shot

Start SQL Server Mirroring after endpoints

Once, you click on YES, this will implement the mirroring and will take some time to revert back to old screen, Please wait, as this wait is depends on changes which has been performed on Principal Server after full backup. This could take 30 minutes too.

Once this check all configuration and synchronized data, you will get the YES button enabled as shown below.

SQL Server Mirroring Established

Once, this is Done, you mirroring setup is completed. We can verify that by checking database status connecting to Principal and Mirror Instance via SSMS. We will get a status like below

Database Status at Principal Server

Database status would be Principal and Synchronized, is (Role,Status) as shown below in the screen shot.

SQL Server Mirroring, Principal Database Staus (Pricipal and Synchronized)

Database Status at Mirror Server

Database status would be Mirror, Synchronized and restoring, is (Role,Status) as shown below in the screen shot.

SQL Server Mirroring, Mirror Database Staus (Mirror, Synchronized and restoring)

This is quite lengthy post, so Post Check Implementation and Failover Testing (Step 5 and Step 6) I will be posting in next article.

Credits:

http://www.sqlserver-training.com/how-to-setup-mirroring-in-sql-server-screen-shots/-