Setting Up Database Mirroring on Sql Server 2008 - Part 1
Following are the steps that I follow to set up DB Mirroring between Sql Server 2008 databases on separate servers.
Overview:
- Create Sample Database for Mirroring on Principal Server
- Create some database objects in the database to test i.e. tables, stored procedures, etc
- Create Database Mirroring EndPoints on Principal, Mirror and Witness
- CREATE necessary accounts and GRANT permissions.
- Prepare Mirroring database
- Enable / Set up mirroring.
- Create Snapshot database on Mirror database.
- Failover:
- Test Failover
- Force Failover
- Server Objects.
- Other points.
Server Names:
Initial Instance Role
|
Domain Service Account
|
Server Name
|
Principal
|
AD\sql1
|
SalesDB.AD.com
|
Mirror
|
AD\sql1
|
SalesDBFailOver.AD.com
|
Witness
|
AD\sqlWitness
|
WitnessDB.AD.com
|
Assumptions:
- Principal, Mirror and Witness services run with domain accounts.
- All users and application connections to Sql Server are also domain accounts.
Step 1: Create Sample Database for Mirroring on Principal Server
First let’s create a database on Principal server for mirroring. FULL recovery model is required for databases participating in mirroring.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
| -- * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *-- Step 01: CREATE Principal Database.-- Run on Principal Server-- * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *CREATE DATABASE DBMirONPRIMARY(NAME = N'DBMir_PRIMARY_Data', FILENAME = N'E:\MS SQL\MSSQL_2K8\Play1\MDF\DBMir_Primary_Data.MDF', SIZE = 1024 MB, MAXSIZE = 2 GB, FILEGROWTH = 10)LOG ON(NAME = N'DBMir_Log', FILENAME = N'E:\MS SQL\MSSQL_2K8\Play1\LDF\DBMir_Log.LDF', SIZE = 1024 MB, MAXSIZE = 2048 MB, FILEGROWTH = 10)GO-- SET recovery model to FULLALTER DATABASE [DBMir]SET RECOVERY FULLGO |
Step 02: Create some database objects and server level objects to test after failover i.e. logins, tables, stored procedures, etc.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
| -- sample server level objectUSE MASTERGOCREATE LOGIN [test] WITH PASSWORD = 'test'WITH DEFAULT_DATABASE = [DBMir], DEFAULT_LANGUAGE = [us_english]GOUSE [DBMir]GOCREATE USER [test] FOR LOGIN [test]GO-- Sample tableCREATE TABLE dbo.Test(ID INT NOT NULL PRIMARY KEY IDENTITY(1,1), Name VARCHAR(40) NULL, Age INT NULL, Blob VARCHAR(MAX) NULL)GO-- Insert a few records, before setting up Mirroring.INSERT INTO dbo.Test (Name, Age, Blob) VALUES('Name1', 10, REPLICATE(CONVERT(VARCHAR(MAX), 'ac'),10000))GO 10 |
Step 03: Create Database Mirroring EndPoints on Principal, Mirror and Witness.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
| -- Create endpoint on PrincipalCREATE ENDPOINT DBMirror_PrincipalSTATE = STARTEDASTCP (LISTENER_PORT = 7022) -- Do not use port 7022 on prod. serverFOR DATABASE_MIRRORING (ROLE = PARTNER)GO-- Create endpoint on MirrorCREATE ENDPOINT DBMirror_MirrorSTATE = STARTEDASTCP (LISTENER_PORT = 7023)FOR DATABASE_MIRRORING (ROLE = PARTNER)GO-- Create endpoint on WitnessCREATE ENDPOINT DBMirror_WitnessSTATE = STARTEDASTCP (LISTENER_PORT = 7024)FOR DATABASE_MIRRORING (ROLE = WITNESS)GO |
Step 04: CREATE necessary accounts and GRANT permissions
Run on Principal
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
| -- CREATE LOGIN for Witness (service account) on Principal-- and GRANT CONNECT permission on Principal's EndPointCREATE LOGIN [AD\sqlWitness] FROM WINDOWS;GOGRANT CONNECT ON ENDPOINT::DBMirror_Principal TO [AD\sqlWitness]GO-- CREATE LOGIN for Witness (service account) on Mirror-- and GRANT CONNECT permission on Mirror's EndPointCREATE USER [AD\sqlWitness] FROM WINDOWS;GOGRANT CONNECT ON ENDPOINT::DBMirror_Mirror TO [AD\sqlWitness]GO-- CREATE LOGIN for Principal/Mirror(same service account on both) on Witness-- and GRANT CONNECT permission on Witness's EndPointCREATE LOGIN [AD\sql1] FROM WINDOWS;GOGRANT CONNECT ON ENDPOINT::DBMirror_Witness TO [AD\sql1]GO |
Step 05: Prepare Mirroring database
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
| -- Run on PrincipalBACKUP DATABASE DBMir TO DISK = N'E:\MS SQL\MSSQL_2K8\Play1\Backup\DBMir_Principal.BAK'GOBACKUP LOG DBMir TO DISK = N'E:\MS SQL\MSSQL_2K8\Play1\Backup\DBMir_Principal.TRN'GO-- Copy the database files over to Mirror Instance-- Run on MirrorRESTORE DATABASE DBMir FROM DISK = N'E:\MS SQL\MSSQL_2K8\Play2\Backup\DBMir_Principal.BAK'WITH MOVE 'DBMir_Principal_Data' TO 'E:\MS SQL\MSSQL_2K8\Play2\MDF\DBMir_Mirror.MDF', MOVE 'DBMir_Log' TO 'E:\MS SQL\MSSQL_2K8\Play2\LDF\DBMir_Mirror_Log.LDF', NORECOVERY, REPLACEGORESTORE LOG DBMir FROM DISK = N'E:\MS SQL\MSSQL_2K8\Play2\Backup\DBMir_Principal.TRN'WITH NORECOVERYGO |
Step 06: Enable / Set up mirroring
1
2
3
4
5
6
7
8
9
10
11
12
13
14
| -- Run on Mirror: Inform Mirror about Principal (first step)ALTER DATABASE DBMirSET PARTNER = 'TCP://SalesDB.AD.com:7022'GO-- Run on Principal: Inform Principal about MirrorALTER DATABASE DBMirSET PARTNER = 'TCP://SalesDBFailOver.AD.com:7023'GO-- Run on Principal: Inform Principal about WitnessALTER DATABASE DBMirSET WITNESS = 'TCP://WitnessDB.AD.com:7024'GO |
Step 07: Create Snapshot database on Mirror database.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
| -- Create snapshot on Mirror to run sample queriesUSE MASTERGOCREATE DATABASE DBMir_SnapShotON(NAME = 'DBMir_Primary_Data', FILENAME = 'E:\MS SQL\MSSQL_2K8\Play2\MDF\DBMir_SnapShot2.DS')AS SNAPSHOT OF DBMirGOSELECT COUNT(*) FROM dbo.TestGO |
Step 08: Failover
If you want to test the cussecc of failover in our environment use the following FAILOVER command.
1
2
3
4
5
6
| -- To test FailOver, run the following query on Principal-- Smooth failover happens. If there are any SQL Logins,-- To map orphan users, run sp_change_users_login on MirrorALTER DATABASE DBMirSET PARTNER FAILOVERGO |
But if the Principal goes down and you want to force FAILOVER to
the mirror server, use the following command. This could result in
some data loss.
the mirror server, use the following command. This could result in
some data loss.
1
2
3
4
5
| -- To force failover, when the Principal is unavailable, run this on Mirror-- Pre-requisite: Principal must be unavailableALTER DATABASE DBMirSET PARTNER FORCE_SERVICE_ALLOW_DATA_LOSSGO |
Step 09: Server Objects
A note on logins: Make sure logins are created in the same order on the Mirror server as on the Principal server. This way login SID for will remain the same and will not create any orphans.
Any other server level objects could be transferred using an SSIS package designed specifically for this purpose.
Step 10: Other points
If you want to run any maintenance on one of the servers participating in Mirroring the following commands will come to good use. Lets say, you want to upgrade patches on Principal and Mirror database server.
- First, you want to perform a manual FAILOVER [from Principal to Mirror]
- Suspend DB mirroring [stop any data flow from new Principal to new Mirror]
- Upgrade original Principal Server
- Bring Principal up and running
- Resume DB Mirroring to bring previous Principal (now Mirror) up to date.
- Perfom another manual FAILOVER to the newly upgraded Mirror (original Principal).
- Suspend Mirroring
- Upgrade Mirror (original)
- Bring it back up.
- Resume DB Mirroring.
To perform all the above steps, the following commands will be useful.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
| -- To drop DB MirroringALTER DATABASE DBMirSET PARTNER OFF -- to DROP DB MirroringGO-- To re-establish DB MirroringALTER DATABASE DBMirSET PARTNER ONGO-- To temporarily suspend mirrorringALTER DATABASE DBMirSET PARTNER SUSPENDGO-- To resume, suspended mirroringALTER DATABASE DBMirSET PARTNER RESUMEGO |
Hope this helps,