WalzoneInterview Prep
πŸ“ž Interviewing soon? Practice with a realistic AI mock phone interview β€” it calls you, then scores you. First 15 min FREE β†’

SQL Server Β· Advanced Β· question 45 of 100

How do you configure and manage database mirroring in SQL Server for high availability and disaster recovery?

πŸ“• Buy this interview preparation book: 100 SQL Server questions & answers β€” PDF + EPUB for $5

Database mirroring in SQL Server is a high-availability and disaster-recovery solution that provides data redundancy and failover capability in case of a database failure. It involves creating and managing a mirrored copy of a database on another server, known as the mirror server. The process of configuring and managing database mirroring can be divided into the following steps:

1. Prerequisites: Ensure that both the principal and mirror servers are running the same edition of SQL Server, have the same collation, and are connected to the same network. Also, make sure that the databases involved in mirroring are in the Full recovery model.

2. Setting up endpoints: Database mirroring requires setting up endpoints, which are network sockets that allow communication between the principal and mirror servers. The endpoints can be configured using either T-SQL or SQL Server Management Studio. You can create the endpoints using the following T-SQL command:

CREATE ENDPOINT endpoint_name
STATE = STARTED
AS TCP (LISTENER_PORT = endpoint_port, LISTENER_IP = ALL)
FOR DATABASE_MIRRORING (ROLE = PARTNER)

Replace endpoint_name with a name for the endpoint, endpoint_port with the port number for the endpoint, and PARTNER with either PARTNER or WITNESS, depending on the role of the server.

3. Initializing the mirror database: To initialize the mirror database, take a full backup of the principal database and restore it on the mirror server with the NORECOVERY option. Then, take a transaction log backup of the principal database and restore it on the mirror server with the NORECOVERY option. Finally, restore any subsequent transaction log backups with the NORECOVERY option. You can use the following T-SQL command to restore the backups:

RESTORE DATABASE database_name
FROM DISK = 'backup_file_name'
WITH NORECOVERY

Replace database_name with the name of the database, backup_file_name with the name of the backup file, and NORECOVERY with the appropriate option.

4. Starting mirroring: After setting up endpoints and initializing the mirror database, you can start mirroring using T-SQL or SQL Server Management Studio. To start mirroring using T-SQL, run the following command on the principal server:

ALTER DATABASE database_name
SET PARTNER = 'TCP://mirror_server:port'

Replace database_name with the name of the database and mirror_server:port with the endpoint of the mirror server.

5. Testing failover: Once mirroring is set up and running, you should test failover to ensure that it works as expected. To initiate a failover manually, use the following T-SQL command on the principal server:

ALTER DATABASE database_name
SET PARTNER FAILOVER

Replace database_name with the name of the database.

In conclusion, database mirroring in SQL Server is a powerful high-availability and disaster-recovery solution that can help ensure that critical data remains available in case of a failure. The process of configuring and managing database mirroring involves setting up endpoints, initializing the mirror database, starting mirroring, and testing failover. By following these steps, you can create a robust mirroring solution that provides data redundancy and failover capability for your organization.

Reading is step one. Saying it out loud is the interview. Our AI interviewer calls your phone and runs a realistic SQL Server interview β€” then scores it.
πŸ“ž Practice SQL Server β€” free 15 min
πŸ“• Buy this interview preparation book: 100 SQL Server questions & answers β€” PDF + EPUB for $5

All 100 SQL Server questions Β· All topics