Database Mirroring in SQL Server 2008

July 7, 2009

What is Database Mirroring?

Database mirroring is the feature in SQL Server 2005 and SQL Server
2008 that provides a high availability solution for Databases. This feature can
be enabled and used only on a database with Full recovery models. The database
can be mirrored from one SQL Server instance to another SQL Server instance.
The source instance is called Principal server; the target instance is called
Mirrored server. We could have one more server called Witness server--we will
talk about that in later part of this article series.

How does database mirroring work?

The principle server sends the active transaction log record to the
mirrored server. The mirrored server applies the transaction log record one by
one in sequence.

Modes of Database Mirroring

Database mirroring can be configured in two different modes, High-Safety
mode also known as synchronous mode and High-Performance mode also known as asynchronously.
The term synchronous and asynchronous says it all.

In the synchronous mode, the principal server sends the transaction
and waits until the transaction is committed on the mirrored server. Then the
transaction is committed on the principal server.

In Asynchronous mode, the principal server sends the transaction to
the mirrored server and does not wait for the transaction on the mirrored
server to commit.

We will discuss transaction safety in detail in a future installment
of this series.

What are the Pre-Requisites of database mirroring?

Edition
of SQL Server should be Standard, Enterprise or Developer
edition

Principal
Database involved in database mirroring should be in full recovery mode

Before
configuring database mirroring, take a full backup, transactional log backup on
the principal server and restored it on the mirrored server with NORECOVERY
option.

Now let's create a database DB1 on the principal server,
PowerPC\SQL2008, using the following transact SQL statement. In this part of article
series, we are going to discuss database mirroring with synchronous mode and
with no witness server.

On the target server, if the database is not in restore mode you will
get the following error. [Refer Fig 1.2]

Fig 1.2

Configure the database DB1 on the principal server for database
mirroring. Using SQL Server management studio, expand the databases and click
on the Database DB1. Right click on the database DB1 and select properties. In
the properties window select the "Mirroring" option as shown below. [Refer
Fig 1.3]

Fig 1.3

Now click on the "Configure Security" button and you will
see the following screen. Since we are not going to setup the witness server,
select the option "No" and click next. [Refer Fig 1.4]

Fig 1.4

Select the default port and the endpoint name chosen by the SQL server
management studio and click Next. [Refer Fig 1.5] If you are choosing some
other port, then make sure that port is open and available.

Fig 1.5

Now select the mirrored server name, click on the "Connect"
button and make sure you can connect to the mirrored server. Select the default
port and the endpoint name chosen by the SQL server management studio and click
Next. [Refer Fig 1.6] If you are choosing some other port, then make sure that
port is open and available.

Fig 1.6

Type the appropriate service account you want to use for the database
mirroring. [Refer Fig 1.7]

On the next screen, click on the button "Start Mirroring". [Refer
Fig 1.11]

Fig 1.11

On the next screen, click on the "Yes" button. [Refer Fig
1.12]

\
Fig 1.12

The following screen shows that database mirroring is configured and
running. [Refer Figure 1.13]

Fig 1.13

Click OK and refresh the databases. You can see the caption of the DB1
database has changed in both principal and mirrored server. [Refer Fig 1.14]

\
Fig 1.14

Conclusion:

Part I of this series explained the basics of the Database mirroring
feature in SQL Server 2008. It also illustrated a step-by-step process on how
to create database mirroring. Part 2 of this series we will discuss how to add
data on the principal server and how data is mirrored to the mirrored server.