Database Mirroring Basics
Whats 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
Advantages of Mirroring:
* Increases data protection ---> Depending on the mode of operation Mirroring provides minimal data loss.
* Increases availability of a database ---> In the event of a disaster, in high-safety mode with automatic failover, failover quickly brings the standby copy of the database online (with no data loss). In the other operating modes, the database administrator has the alternative of forcing service (with possible data loss) to the standby copy of the database.
* Improves the availability of the production database during upgrades ---> During service packs installation or any patch applied on Principal server which requires downtime, the standby comes into effect.
Components in Mirroring:
Database mirroring consist of the following components
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. Mirrored Pair ---> A Principal and Mirror operating together are called a Mirrored Pair. The changes on the principal are reflected in the mirrored database
4. 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 separate SQL Server instance preferably on a separate physical server than principal and mirror. One Witness server can monitor multiple Mirrored Pairs.
5. Quorum ---> A Quorum is the relationship between the Witness, Principal and the Mirror.
6. 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 Principal 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,
---> Select * from sys.database_mirroring_endpoints
To list all the endpoints
---> Select * from sys.tcp_endpoints
**
Database Mirroring can be configured for three different operating modes:**
High Availability Operating Mode
- This provides durable, synchronous transfer of data between principal and mirror, including automatic failure detection and failover. There is performance overhead on this mode because a transaction is not considered committed until SQL Server has successfully committed it to the transaction log on both the principal and the mirror database. And as the distance between the principal and the mirror increases, the performance impact also increases. There is a continuous ping process between all three to detect failover. If the witness server is not visible from the mirror, you must either reconfigure the operating mode for the database mirroring session or turn off the witness.
Alternatively, you can manually fail over a database mirroring session at the mirror in High Availability Mode by issuing the following command at the principal. You can also issue the same command if you have to take principal down for maintenance.
ALTER DATABASE <DBNAME> SET PARTNER FAILOVER
** **
High Performance Operating Mode - In this configuration you dont need a WITNESS Server and the Mirror Server acts as an WARM standby and does not support automatic failure detection or failover. There is any asynchronous data transfer between principal and mirror. This mode provide better performance and you can have geographic dispersion between the principal and the mirror.
** **
High Protection Operating Mode (Recommended Mode) - This mode is the same as High Availability Mode except failover is manual and you have to manually promote the mirror to be the principal. Data transfer is synchronous.
** 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 or 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, 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 at least 1 tran log is restored in addition to full backup with NORECOVERY mode.