Introduction to Database Mirroring
Mirroring is mainly implemented for increasing the database availability. Similar to log shipping mirroring is also implemented on per database basis. Database mirroring maintains two copies of a single database that must reside on different instances of SQL Server Database Engine (server instances). Typically, these server instances reside on computers in different locations. One server instance serves the database to clients (the principal server), while the other server instance acts as a hot or warm standby server (the mirror server).
Mirroring provides a hybrid solution i.e
1. Provides a copy of the database like Log Shipping and
2. Rapid failover capabilities like Clustering
Components in Mirroring :
Database mirroring consist of the following components, lets discuss each one in detail.
1. Principal —> The Principal is the originating server i.e it is the source server which contains the database which is configured for mirroring.There can be only one principal database and it has to be in a separate SQL Server instance than the mirror database.
2. Mirror —> The Mirror is the receiving database in a mirror pair i.e it is the destination server which contains the mirrored database.There can be only one mirror for each principal database.The mirror needs to be on its own separate SQL Server instance preferably on separate physical server.
3. Witness —> A Witness is optional and it monitors the Mirrored Pair.It ensures that both principal and mirror are functioning properly.The Witness is also a seperate SQL Server instance preferably on a seperate physical server than principal and mirror.One Witness server can monitor multiple Mirrored Pairs.
4. Quorum —> A Quorum is the relationship between the Witness,Principal and the Mirror.It will be discussed later in this article
5. Endpoint —> Endpoint is the method by which SQL Server Database engine communicates with applications.In the context of Database mirroring endpoint is the method by which the Prinicpal communicates with the Mirror.The mirror listens on a port defined in the endpoint.The default is 5022.Each database mirror pair listens on its own unique port.To list all the database mirror endpoints run,
1 | Select * from sys.database_mirroring_endpoints |
To list all the endpoints
1 | Select * from sys.tcp_endpoints |
Operating modes
SQL Server database mirroring can be set to provide high availability or disaster recovery. Depending on the needs, a DBA can choose among three available modes
- High safety – Data is written and committed on the principal and mirror databases synchronously. Only after committing on both databases, the database application can continue with activity
- Might produce delay and slower operation because transactions must be committed on both databases
- If the principal database goes down, two options are available:
- Do nothing – wait for the principal to become available again. During that time, the SQL Server instance is unavailable. Mirroring will continue where it has stopped
- Force the SQL Server instance on the mirror database – the mirror database becomes the principal. Possible data loss due to committed transactions on the original principal database which are not yet committed on the mirror currently acting as the principal
- High safety with automatic failover – Three servers are necessary. Data is written and must be committed synchronously both on the principal and mirror databases. Only after committing on both databases, the application can continue running
- Might produce delay and slower operation because transactions must be committed on both databases
- If the principal database goes down, only one option is available:
- Let the automatic failover process complete, the mirrored database becomes the principal
- High performance – the asynchronous communication, data is written and committed on the principal server, and later sent and committed to the mirror server. Automatic failover isn’t possible and the witness server can’t be used
- The high performance mode is only available in the Enterprise edition of SQL Server
- If the principal database goes down, three options are available:
- Do nothing – wait for the principal to become available again. The SQL Server is unavailable. Mirroring will continue where it has stopped
- Force the SQL Server instance on the mirror database – the mirror database becomes the principal. Greater possibility for data loss, due to asynchronous communication between databases
- Manual update – to reduce data loss, take the tail of the log backup if the failed server allows, remove mirroring and restore the tail of the log on the previously mirrored database
Prerequisites for Database Mirroring
1. Make sure that the two partners, that is the principal server and mirror server, are running the same edition of Microsoft SQL Server 2005. The partners require either SQL Server 2005 Standard Edition, SQL Server 2005 Enterprise Edition or SQL Server 2005 Developer Edition.
2. If you are using a witness, make sure that SQL Server 2005 is installed on its system. The witness can run on any reliable computer system that supports SQL Server 2005 Standard Edition, Enterprise Edition, Workgroup Edition, SQL Server 2005 Developer Edition or Express Edition.
3. SQL 2005 SP1 or later version is required for Mirroring
4. The principal database must be in the FULL recovery model. Log records that result from bulk-logged operations cannot be sent to the mirror database.
5. Verify that the mirror server has enough disk space for the mirror database.
6. All of the server instances in a mirroring session should use the same master code page and collation. Differences can cause a problem during mirroring setup.
7. The mirror database must have the same name as the principal database.
8. The mirror database must be initialized from a restore of the principal database with NORECOVERY, followed by restores in sequence of principal transaction log backups.Prior to configuring mirroring ensure that atleast 1 tran log is restored in addition to full backup with NORECOVERY mode.
|