Dec 28, 2020

7 min read

Configure Database Mirroring in SQL Server

Database mirroring is a SQL Server high availability solution provided by Microsoft, with the following components.

  1. Principal database server: a source database that you configure for the mirroring.
  2. Mirror database server: a destination database that you must restore with NORECOVERY. Ideally, a mirrored database should be on a separate server.
  3. Witness Server: an optional component that you can set up to configure the mirror with automatic failover.
  4. Endpoint: the communication medium used by the principal and mirror server. The mirror listens on port number 5022; however, you can change it during the configuration.

The Prerequisites to configure the database mirroring are:

  1. Principal and mirror instance must be running on SQL Server 2005 SP1 and later edition.
  2. SQL Server version and edition must be the same on the principal and mirror instance.
  3. Principal and mirror databases must have the same name and database collation. Both databases must be running in the FULL recovery model.
  4. You need to restore a full backup and at least one transaction log backup on the mirrored database. For the mirrored database restore, you must use NORECOVARY.
  5. If you want to configure the automatic failover, then you must set up a witness server. A witness server can be any version of SQL Server. For cost savings, you can use SQL Server Express edition as a witness server.

Now, let’s see step by step how to configure the database mirror.

Configure the database mirror

To understand the configuration, I have set up a virtual lab on my workstation. The lab has three virtual machines, and I have installed Windows Server 2016 and SQL Server 2019 on them all:

Execute the following command on SQL01 to generate the backup:

Execute the following command on SQL02 to restore the database with NORECOVARY:

Now, connect to the SQL01 server and open SQL Server Management Studio.

Expand SQL Server instance > Expand databases > Right-click on AdventureWorks2017 > Hover on Tasks > Select Mirror.

On the Database Properties dialog box, click on Configure Security:

The Configure Database Mirroring Security Wizard opens.

On the first screen, you can view the details and the list of tasks performed by the wizard.

Click Next.

On the Include witness Server screen, you can choose to include the witness server in the mirroring configuration.

We are going to use the witness server in the configuration. Therefore, choose Yes.

On Choose Servers to Configure screen, you can define the server to save the security configurations on. We are using the Witness server instance.

Select the Witness server instance and click Next.

On the Principal Server Instance screen, specify the principal server’s hostname, Listener port, and Endpoint name.

You can choose to encrypt the data sent through the configured endpoints by clicking on Encrypt data send through this endpoint option.

Click on Next.

On the Mirror server instance screen, choose the mirrored instance. Select SQL02 from the Mirror server instance drop-down box, provide the Listener port and Endpoint name. Click Next.

On the Witness Server Instance screen, choose the witness instance to support automatic failover.

From the Witness server instance drop-down box, select iSCSI, provide the Listener port, and Endpoint name. Click Next.

On the Service Accounts screen, provide the SQL login accounts to connect Principal, witness, and mirrored instances.

We have created a domain account and given the sysadmin permission to those domain accounts.

Enter the services accounts in Principal, Witness, and Mirror text boxes and click Next.

On the Complete the Wizard screen, review the list of actions that the wizard will perform on Principal, Witness, and Mirrored instance.

Review the list of actions. If it requires any changes, you can go back to previous screens and adjust the settings.

Once the configuration is complete, click Finish.

The wizard starts the configuration on all servers.

After the successful completion of the Endpoint configuration, close the dialog box.

You can review and export the wizard log by clicking on the Report button.

Return to the Database Properties screen. A message box opens there.

In the message box, you can see the network address of the Principal, Witness, and Mirrored instances. Along with that, you can see the Operating mode of the mirror.

To start mirroring between the principal and mirror database server, click Start mirroring.

If you’ve configured the entire setup correctly, the database mirroring will start at once.

You can view the database mirror synchronization status from the SQL Server Management Studio:

View the status by executing the following T-SQL Query:


Test the failover

I have created an employee table and inserted some data by executing the following script:

Now, we perform the following steps:

  1. Stop the SQL Server services on SQL01.dc.local.
  2. Insert demo records in employee tables.
  3. Start the SQL Server services on SQL01.dc.local.

To stop the services on SQL Server, RDP to SQL01 > Open SQL Server configuration manager > right-click on SQL Server services > Click Stop.

Now, let’s see the status of the principal and mirrored instance:


Execute the following query to insert more data in the table:

Start the SQL Server services on SQL01 from the SQL Server configuration manager, and execute the following T-SQL query to make it principal:

Run the following query to verify that data has been inserted on the principal database:


The query has returned 100 records that indicate the correct mirror setup.

This way, you can see how to configure a SQL Server database mirroring.

Originally published at 👉 https://codingsight.com/configure-database-mirroring-in-sql-server/.

Join a community of database specialists. Weekly CodingSight tips right in your inbox. ✔ No spam ✔ 100% great content, always.

Subscribe here to get more industry insights👋