SQL Server Database Mirroring: Part 2



Back to Part 1

Morroring2.1.gif

  1. The database must be using the FULL RECOVERY or BULK LOGGED recovery models.
  2. On the secondary server, we must have already restored a full database backup with the NORECOVERY or STANDBY options, so that transaction logs may be applied.

Morroring2.2.gif
  1. FILESTREAM is not supported by database mirroring. As a FILESTREAM filegroup cannot be created on the principal server. So, we can say that database mirroring cannot be configured for a database that contains FILESTREAM filegroups.
  2. Database mirroring can support a maximum of about 10 databases per server instance, on a 32-bit system.
  3. Database mirroring is not supported with either cross-database transactions or distributed transactions.

Morroring2.3.gif
  1. Endpoints must be correctly configured: Make sure that each server instance (the principal server, mirror server, and witness, if any) has a database mirroring endpoint.
  2. Check that the port numbers are correct. To identify the port currently associated with database mirroring endpoint of a server instance, use the following Transact-SQL statement.

    SELECT type_desc, port FROM sys.tcp_endpoints;
    GO

    Note: Inspect each server instance to determine whether it is listening on the correct ports.
     
  3. Make sure that the endpoints are started (STATE=STARTED). On each server instance, use the following Transact-SQL statement.

    SELECT state_desc FROM sys.database_mirroring_endpoints
     
  4. To start an endpoint, use the following Transact-SQL statement.

    ALTER ENDPOINT Endpoint_Mirroring
    STATE = STARTED
    AS TCP (LISTENER_PORT = <port_number>)
    FOR database_mirroring (ROLE = ALL);
    GO
     
  5. Check that the ROLE is correct. On each server instance use the following Transact-SQL statement.

    SELECT role FROM sys.database_mirroring_endpoints;
    GO
     
  6. Make sure that the login from the other server has CONNECT permission. To determine who has CONNECT permission for an endpoint, on each server instance use the following Transact-SQL statement.

    SELECT 'Metadata Check';
    SELECT EP.name, SP.STATE,
    CONVERT(nvarchar(38), suser_name(SP.grantor_principal_id))
    AS GRANTOR,
    SP.TYPE AS PERMISSION,
    CONVERT(nvarchar(46),suser_name(SP.grantee_principal_id))
    AS GRANTEE
    FROM sys.server_permissions SP , sys.endpoints EP
    WHERE SP.major_id = EP.endpoint_id
    ORDER BY Permission,grantor, grantee;
    GO
     
  7. Endpoints must be correctly configured: Make sure that each server instance (the principal server, mirror server, and witness, if any) has a database mirroring endpoint.
     
  8. Check that the port numbers are correct. To identify the port currently associated with database mirroring endpoint of a server instance, use the following Transact-SQL statement.

    SELECT type_desc, port FROM sys.tcp_endpoints;
    GO

    Note: Inspect each server instance to determine whether it is listening on the correct ports.
     
  9. Make sure that the endpoints are started (STATE=STARTED). On each server instance, use the following Transact-SQL statement.

    SELECT state_desc FROM sys.database_mirroring_endpoints
     
  10. To start an endpoint, use the following Transact-SQL statement.

    ALTER ENDPOINT Endpoint_Mirroring
    STATE = STARTED
    AS TCP (LISTENER_PORT = <port_number>)
    FOR database_mirroring (ROLE = ALL);
    GO
     
  11. Check that the ROLE is correct. On each server instance use the following Transact-SQL statement.

    SELECT role FROM sys.database_mirroring_endpoints;
    GO
     
  12. Make sure that the login from the other server has CONNECT permission. To determine who has CONNECT permission for an endpoint, on each server instance use the following Transact-SQL statement.

    SELECT 'Metadata Check';
    SELECT EP.name, SP.STATE,
    CONVERT(nvarchar(38), suser_name(SP.grantor_principal_id)) |
    AS GRANTOR,
    SP.TYPE AS PERMISSION,
    CONVERT(nvarchar(46),suser_name(SP.grantee_principal_id))
    AS GRANTEE
    FROM sys.server_permissions SP , sys.endpoints EP
    WHERE SP.major_id = EP.endpoint_id
    ORDER BY Permission,grantor, grantee;
    GO

Morroring2.4.gif

The mirror database must exist before a database mirroring session can begin.

Steps to do this are as under:
  • Make sure that Microsoft SQL Server 2008 is installed on both the principal and mirror server instances.
     
  • Then Make it verify that the database uses the full recovery model.
     
  • Back up the principal database to a full database backup.
     
  • Confirm it that the system on which we are planning to create the mirror database has a disk drive with sufficient space to hold the database.
     
  • Until the backup is on a network drive it is accessible from both systems, copy the database backup to that system.
     
  • Restore the full database backup onto the mirror server instance to create the mirror database. RESTORE DATABASE (database name statement must specify) WITH NORECOVERY.

    Note: If we restore the database filegroup by filegroup, be sure to restore the whole database.
     
  • Typically, at least one log backup must be taken on the principal database, copied to the mirror server, and restored on the mirror database (using WITH NORECOVERY). So that, a log backup might be unnecessary, if the database has just been created and no log backup has been taken yet, or if the recovery model has just been changed from SIMPLE to FULL.

Example:
  1. Let's take an example to apply database mirroring with one of the database named "PatientInfo", Open SQL Server Management Studio 2008 and make sure that the database is in the Full Recovery mode. To check the recovery mode, right click the database name, select Properties and then select Options, or by using the following T-SQL:

    USE master;
    GO
    ALTER
    DATABASE PatientInfo
    SET RECOVERY FULL;
    GO

    Morroring2.5.gif

    Note: If it is not in the FULL recovery Mode then, modify it to use the full recovery mode:
     
  2. Once we modify the recovery model of our database to FULL, it's time to create a full
    Backup on the principal server instance, create a full backup of the principal database as follows:

    BACKUP DATABASE PatientInfo
    TO DISK = 'D:\PatientInfo.bak'
    WITH FORMAT
    GO

    Morroring2.6.gif

    Morroring2.7.gif
     
  3. Now, copy the full backup to the mirror server.
  4. Restore the full backup WITH NORECOVERY onto the mirror server instance. The restore command depends on whether the paths of principal and mirror databases are identical. Following T-SQL is used to do this:

    RESTORE DATABASE PatientInfo
    FROM DISK = 'D:\PatientInfo.bak'
    WITH NORECOVERY
    GO

    Morroring2.8.gif

    Morroring2.9.gif

    Note: If the path names of the principal and mirror databases differ, we cannot add a file, because on receiving the log for the add file operation, the mirror server instance attempts to place the new file in the location used by the principal database.
     
  5. After creating a full backup, it is important to create a log backup on the principal database. The following Transact-SQL statement backs up the log to the same file used by the preceding full backup:

    BACKUP LOG PatientInfo
    TO DISK = 'D:\PatientInfo.bak'
    GO

    Morroring2.10.gif

    Morroring2.11.gif

    Note: Also the database name on the mirror must be the exact same name as the principal.
     
  6. Before we can start mirroring, we must apply the required log backup (and any subsequent log backups). For example, the following Transact-SQL statement restores the first log from D:\PatientInfo.bak:

    RESTORE LOG PatientInfo
    FROM DISK = 'D:\PatientInfo.bak'
    WITH FILE=1, NORECOVERY
    GO
     
  7. If any additional log backups occur before starting mirroring, we also restore all of those log backups, in sequence, to the mirror server using WITH NORECOVERY. For example, the following Transact-SQL statement restores two additional logs from D:\PatientInfo.bak:

    RESTORE LOG PatientInfo
    FROM DISK = 'D:\PatientInfo.bak'
    WITH FILE=2, NORECOVERY
    GO
    RESTORE
    LOG PatientInfo
    FROM DISK = 'D:\PatientInfo2.bak'
              WITH FILE=3, NORECOVERY
    GO

                                                                                                                                                         Continue to Part 3

 

Up Next
    Ebook Download
    View all
    Learn
    View all
    F11Research & Development, LLC