Sunteți pe pagina 1din 19

What is Database Mirroring?

Database mirroring was introduced with Microsoft SQL Server 2005 technology that
can be used to design high-availability and high-performance solutions for database
redundancy.

In database mirroring, transaction log records are sent directly from the principal
database to the mirror database. This helps to keep the mirror database up to date with
the principal database, with no loss of committed data. If the principal server fails, the
mirror server automatically becomes the new principal server and recovers the
principal database using a witness server under high-availability mode. We will
discuss these modes later. Fundamentally to summarize there are three jargon to
understand Principal database is the active live database that supports all the
commands, Mirror is the hot standby and witness which allows for a quorum in case
of automatic switch over.

2. How does Database Mirroring works?

In database mirroring, the transaction log records for a database are directly
transferred from one server to another, thereby maintaining a hot standby server. As
the principal server writes the databases log buffer to disk, it simultaneously sends
that block of log records to the mirror instance. The mirror server continuously applies
the log records to its copy of the database. Mirroring is implemented on a per-database
basis, and the scope of protection that it provides is restricted to a single-user
database. Database mirroring works only with databases that use the full recovery
model.

3. What are the benefits of that Database Mirroring?


Database mirroring architecture is more robust and efficient than Database Log
Shipping. It can be configured to replicate the changes synchronously to minimized
data loss.
It has automatic server failover mechanism.
Configuration is simpler than log shipping and replication, and has built-in network
encryption support (AES algorithm).
Because propagation can be done asynchronously, it requires less bandwidth than
synchronous method (e.g. host-based replication, clustering) and is not limited by
geographical distance with current technology.
Database mirroring supports full-text catalogs.
Does not require special hardware (such as shared storage, heart-beat connection) and
cluster ware, thus potentially has lower infrastructure cost

4. What are the Disadvantages of Database Mirroring?

Potential data lost is possible in asynchronous operation mode. RTO will vary and
depend on several factors, such as propagation interval time and bandwidth speed.
It only works at database level and not at server level. It only propagates changes at
database level, no server level objects, such as logins and fixed server role
membership, can be propagated.
Automatic server failover may not be suitable for application using multiple
databases.

5. What are the minimum requirements for Database Mirroring?

Database base recovery model should be full


Database name should be same on both SQL Server instances
Server should be in the same domain name
Mirror database should be initialized with principle server

6. What are the Restrictions for Database Mirroring?

A mirrored database cannot be renamed during a database mirroring session.


Only user databases can be mirrored. You cannot mirror the master, msdb, tempdb, or
model databases.
Database mirroring does not support FILESTREAM. A FILESTREAM filegroup
cannot be created on the principal server. Database mirroring cannot be configured for
a database that contains FILESTREAM filegroups.
On a 32-bit system, database mirroring can support a maximum of about 10 databases
per server instance.
Database mirroring is not supported with either cross-database transactions or
distributed transactions.

7. What is a Principal server?

Principal server is the server which serves the databases requests to the Application.

8. What is a Mirror?

This is the Hot standby server which has a copy of the database.

9. What is a Witness Server?

This is an optional server. If the Principal server goes down then Witness server
controls the fail over process.

10. What is Synchronous and Asynchronous mode of Database Mirroring?

In synchronous mode, committed transactions are guaranteed to be recorded on the


mirror server. Should a failure occur on the primary server, no committed transactions
are lost when the mirror server takes over. Using synchronous mode provides
transaction safety because the operational servers are in a synchronized state, and
changes sent to the mirror must be acknowledged before the primary can proceed

In asynchronous mode, committed transactions are not guaranteed to be recorded on


the mirror server. In this mode, the primary server sends transaction log pages to the
mirror when a transaction is committed. It does not wait for an acknowledgement
from the mirror before replying to the application that the COMMIT has completed.
Should a failure occur on the primary server, it is possible that some committed
transactions may be lost when the mirror server takes over.

11. What are the operating modes of Database Mirroring?

SQL Server provides 3 operating modes for database mirroring.

High Availability Mode


High Protection Mode
High Performance Mode

12. What is High Availability operating mode?

It consist of the Principal, Witness and Mirror in synchronous communication. In this


mode SQL server ensures that each transaction that is committed on the Principal is
also committed in the Mirror prior to continuing with next transactional operation in
the principal. The cost of this configuration is high as Witness is required. If the
network does not have the bandwidth, a bottleneck could form causing performance
issue in the Principal. If Principal is lost Mirror can automatically take over.

13. What is High Protection operating mode?

It is pretty similar to High Availability mode except that Witness is not available, as a
result failover is manual. It also has transactional safety FULL i.e. synchronous
communication between principal and mirror. Even in this mode if the network is poor
it might cause performance bottleneck.

14. What is High Performance operating mode?


It consists of only the Principal and the Mirror in asynchronous communication. Since
the safety is OFF, automatic failover is not possible, because of possible data loss;
therefore, a witness server is not recommended to be configured for this scenario.
Manual failover is not enabled for the High Performance mode. The only type of
failover allowed is forced service failover, which is also a manual operation.

15. What are Recovery models support Database Mirroring?

Database Mirroring is supported with Full Recovery model.

16. What are End Points and its usages?

An endpoint is a network protocol which is used to communicate Principal, Mirror


and Witness servers over the network.

17. How can we create an end point using SQL script?

CREATE ENDPOINT Endpoint1

STATE = STARTED

AS TCP ( LISTENER_PORT = 5022 )

FOR DATABASE_MIRRORING (ROLE=PARTNER);

GO
18. What is the default of end points (port numbers) of principal, mirror and
witness servers?

The default port numbers of principal, mirror and Witness servers are 5022, 5023 and
5024

19. What is Log Hardening?


Log hardening is the process of writing the log buffer to the transaction log on disk, a
process called.

20. Is it possible to perform read only operation at mirrored database in mirror


server?

Yes, using database snapshots.

21. What is Role-switching?

Inter changing of roles like principal and mirror are called role switching.

22. How to Set a Witness Server to Database Mirroring?

ALTER DATABASE AdventureWorks SET WITNESS =


'TCP://SQLWITN.local:5024'
23. How to Remove a Witness Server from Database Mirroring?

ALTER DATABASE AdventureWorks SET WITNESS OFF


24. What are the Database Mirroring states?

SYNCHRONIZING
SYNCHRONIZED
SUSPENDED
PENDING_FAILOVER
DISCONNECTED

25. What does SYNCHRONIZING state means in Database Mirroring?

The contents of the mirror database are lagging behind the contents of the principal
database. The principal server is sending log records to the mirror server, which is
applying the changes to the mirror database to roll it forward. At the start of a
database mirroring session, the database is in the SYNCHRONIZING state. The
principal server is serving the database, and the mirror is trying to catch up.

26. What does SYNCHRONIZED state means in Database Mirroring?

When the mirror server becomes sufficiently caught up to the principal server, the
mirroring state changes to SYNCHRONIZED. The database remains in this state as
long as the principal server continues to send changes to the mirror server and the
mirror server continues to apply changes to the mirror database.

If transaction safety is set to FULL, automatic failover and manual failover are both
supported in the SYNCHRONIZED state, there is no data loss after a failover.
If transaction safety is off, some data loss is always possible, even in the
SYNCHRONIZED state.

27. What does SUSPENDED state means in Database Mirroring?

The mirror copy of the database is not available. The principal database is running
without sending any logs to the mirror server, a condition known as running exposed.
This is the state after a failover. A session can also become SUSPENDED as a result
of redo errors or if the administrator pauses the session. SUSPENDED is a persistent
state that survives partner shutdowns and startups.

28. What does PENDING_FAILOVER state means in Database Mirroring?

This state is found only on the principal server after a failover has begun, but the
server has not transitioned into the mirror role.
When the failover is initiated, the principal database goes into the
PENDING_FAILOVER state, quickly terminates any user connections, and takes
over the mirror role soon thereafter.

29. What does DISCONNECTED state means in Database Mirroring?

The partner has lost communication with the other partner

30. Why we get the below error message while configuring database mirroring?

Msg 1416, Level 16, State 31, Line 3. Database is not


configured for database mirroring
We need to restore the Full backup from principal server using With NoRecovery
option and also one transactional log backup from principal server using With
NoRecovery option and then start configuring mirroring.

31. Can we configure a single database to be mirrored to more than one server.
i.e) One source & many destination like logshipping?

No, It is not possible.

What is Database Snapshot?


A database snapshot is a view of what the source database looked like at the time at
which the snapshot was created. This means that all the objects will be the same as
what it was when the snapshot was taken and all of the data will be exactly as it was
then. To use database snapshots to recover from an unwanted DML statement, you
need to have a suitable snapshot in place. Snapshots can only be created by using a T-
SQL statement.

2) How to create a Database Snapshot?


CREATE DATABASE AdventureWorks2012_S1 ON (NAME =
[AdventureWorks2012_Data], FILENAME = 'D:\
MSSQL\DATA\AdventureWorks2012_Data_SS_1.ss') AS SNAPSHOT
OF AdventureWorks2012
3) How does a snapshot work?

High level tasks of snapshot involves

When you create a snapshot a sparse file is created for each data file
When data is modified in the source database for the first time, the old value of the
modified data is copied to the sparse file
If the same data is the subsequently changed again, those changes will be ignored and
not copied to the snapshot
When you query the snapshot, it first checks if the data is available in the snapshot. If
its there it reads if from the snapshot. If its not there, I reads through to the source
database and gets the data from there instead, because that means the data has not yet
changed since the time the snapshot was taken

4) What is SPARSE file?

When a database snapshot is created, a sparse file is added for each database file in the
database of which the snapshot was taken. A sparse file is basically an empty file. It
does not contain any data until a change is made to the source database

5) What are important things to remember about sparse files?

The maximum size a sparse file can grow to is the size of the original file at the time
of the database creation
Sparse files are limited to 16 GB on Windows 2008 and 64 GB on Windows 2003
Sparse files grow in increments of 64 KB

6) How can we check the SPARSE files?

SELECT name, is_sparse FROM sys.database_files


7) Can we change the permissions in a database snapshot?
No its not possible.

8)Can we take a backup of a database snapshot?

No.

9) Can we detach a database snapshot?

No.

10) What are the restrictions in Database Snapshots?

Database snapshots depend on the source database. They can only be created on the
same server as where the database resides (or the server to which the database is
mirrored.)
While there are snapshots present, you cannot drop the database or add any new files
to it
Once a database snapshot becomes suspect it cannot be saved. It just has to be deleted.
This can happen if the snapshot runs out of space or reaches the maximum sparse file
size limit
You cannot create indexes on snapshots, they are strictly read only
The user permissions are exactly the same as it was in the source database. You
cannot grant a user access to a snapshot. You have to grant the access in the source
database and then take another snapshot first before the user will be able to access it

11) What are the benefits of Database Snapshots?

Snapshots can be used for reporting purposes.


Using a mirror database that you are maintaining for availability purposes to offload
reporting.
Safeguarding data against administrative and user error

12) Can we configure mirroring between Standard Edition & Enterprise Edition
or Vice Versa?
No its not possible, both principal and mirror should have same edition

13) Is it possible to take backup of mirrored database in mirror server?

No

14) Can I create multiple endpoints for configuring different databases for
mirroring and point each database to unique endpoint?

No

15) Can I configure a single database to be mirrored to more than one server. i.e)
One source & many destination like logshipping?

No

16) How to know the database mirroring connection time out in Mirroring?

SELECT Mirroring_Connection_Timeout FROM


sys.database_mirroring WHERE database_id =
db_id('Database Names')
17) How can I increase Heartbeat time between principal and mirror server? By
default its 10 sec.

ALTER DATABASE AdventureWorks SET PARTNER TIMEOUT 30


18) What status of mirroring has if secondary is down?

If secondary is down principle or Mirror show status disconnected

19) What status of mirroring has if principle is down?

If principle is down mirror will be disconnected with in recovery instead of


synchronized with restoring
20) What status of mirroring has if mirroring is paused?

If mirroring is set to paused from principle then both principle & mirror in suspending

21) How to monitoring Mirroring?

There are six methods are available for monitoring the Database Mirroring
a) Database Mirroring Monitor:- Database Mirroring Monitor is a GUI tool that shows
update status and to configure warning thresholds.
To open DM Monitor:- Right click on Principal Database > Tasks > Select Launch
Database Mirroring Monitor.
b) SQL Server Management Studio:- A green arrow on the mirror server is indicates
running well. A red arrow indicates problems that need to investigate.
c) SQL Server Log:- It provides information of Mirroring establishment and status. If
any errors occur it will be logged to SQL Server log and Windows event log.
d) Performance Monitor:- It can provides real-time information about Database
mirroring. We can use performance counters to get status of the database mirroring
such as Bytes received/sec, Bytes sent/sec, Transaction delay etc.
e) Profiler:- Profiler many events are providing the status of the Database mirroring
f) System Stored Procedures:-
sp_dbmmonitoraddmonitoring
sp_dbmmonitorchangemonitoring
sp_dbmmonitorhelpmonitoring
sp_dbmmonitordropmonitoring

22) What will be the impact if we add data files to the Primary database in
Database Mirroring?

Data files will be added to the Mirror database if the identical location is present at the
mirrored server without any issues.
23) What will be the impact if we add data files to the Primary database with a
location which is not present on the Mirrored server in Database Mirroring?

Database mirroring will not work in this case. We have to break the Mirroring and
take a Log backup of the Primary database and restore it on the Mirror database using
WITH MOVE and NORECOVERY option. Once its done then Reestablish the
mirroring.

24) If we add any users to the Primary database, will it be copied to the Mirror
databases or not?

Yes, It will be copied to the Mirror database with the permissions. However you have
to create the login manually with the same SID.s

25) How to disable mirroring by scripts?

ALTER DATABASE <sdatabase_name> SET PARTNER OFF


26) How to do manual failover to Mirror when principle is working fine?

ALTER DATABASE <DB Name> SET PARTNER FAILOVER


27) What is Transparent Client Redirection?

Database mirroring provides a feature of automatically redirection of the Application


connection to Mirror database in case of Primary database failures. Database
connections configured as below to enable this functionality.

.NET Example

Data Source=SQLA;Failover Partner=SQLB;Initial Catalog =


AdventureWorks2008R2 ; Integrated Security=True

28) What is Log buffer?


Log buffer is a small contiguous memory structure, only 60k maximum, to host in
memory transaction logs per database. When the transaction is committed, entire
buffer will be written to transaction log file before commit finishes.

29) What is Automatic Page Repairing?

Automatic page repair is supported by database mirroring. After certain types of errors
corrupt a page, making it unreadable, a database mirroring partner (principal or
mirror) attempts to automatically recover the page. The partner/replica that cannot
read the page requests a fresh copy of the page from its partner. If this request
succeeds, the unreadable page is replaced by the readable copy, and this usually
resolves the error, this whole process is called Automatic Page Repair.

30) What are the error types which cause and Automatic Page Repair attempt?

Error Instances that cause automatic


Description
number page-repair attempt

Action is taken only if the operating system


ERROR_CRC. The operating-
823 performed a cyclic redundancy check (CRC) that
system value for this error is 23.
failed on the data.

Logical data errors, such as torn


824 Logical errors.
write or bad page checksum.

829 A page has been marked as restore pending. All.

31) What are the page types which are not repairable using Automatic Page
repair option?

Automatic page repair cannot repair the following control page types:
File header page (page ID 0).
Page 9 (the database boot page).
Allocation pages: Global Allocation Map (GAM) pages, Shared Global Allocation
Map (SGAM) pages, and Page Free Space (PFS) pages.

What is an asynchronous operation mode?

This is high-performance mode.

The mirror server tries to keep up with the log records sent by the principal
server. The mirror database might lag somewhat behind the principal
database. However, typically, the gap between the databases is small.
However, the gap can become significant if the principal server is under a
heavy work load or the system of the mirror server is overloaded.

In high-performance mode, as soon as the principal server sends a log record


to the mirror server, the principal server sends a confirmation to the client. It
does not wait for an acknowledgement from the mirror server. This means
that transactions commit without waiting for the mirror server to write the log
to disk.
What is a synchronous operation mode?

This is high-safety mode.

When a session starts, the mirror server synchronizes the mirror database
together with the principal database as quickly as possible. As soon as the
databases are synchronized, a transaction is committed on both partners, at
the cost of increased transaction latency.

Can you mirror the master, msdb, tempdb, or model databases?

Only user databases can be mirrored. You cannot mirror the master, msdb,
tempdb, or model databases.
When Is High-Performance Mode Appropriate?
The High-performance mode can be useful in a disaster-recovery scenario in
which the principal and mirror servers are separated by a significant distance
and where you do not want small errors to impact the principal server.
Are you using a witness server on High-Performance Mode?

No, it is not recommended because of asynchronous operation mode.

A witness can coexist with high-performance mode, but the witness provides
no benefit and introduces risk.

If the witness is disconnected from the session when either partner goes
down, the database becomes unavailable. This is because, even though high-
performance mode does not require a witness.
How database mirroring works under the synchronous operation mode?
On receiving a transaction from a client, the principal server writes the log for the transaction to the
transaction log.
The principal server writes the transaction to the database and, concurrently, sends the log record to
the mirror server. The principal server waits for an acknowledgement from the mirror server before
confirming either of the following to the client: a transaction commit or a rollback.
The mirror server hardens the log to disk and returns an acknowledgement to the principal server.
On receiving the acknowledgement from the mirror server, the principal server sends a confirmation
message to the client.
T-SQL Script to FULL/OFF Transaction Safety option.

Set FULL:
1 ALTER DATABASE database_name SET PARTNER SAFETY FULL

Set OFF:
1 ALTER DATABASE database_name SET PARTNER SAFETY OFF
Can you use single witness server for multiple database mirroring
sessions?

Yes,

A specific server instance can act as a witness in concurrent database


mirroring sessions, each for a different database. Different sessions can be
with different partners.
During the mirroring session, can you turn off the witness server?
At any time during a database mirroring session, the database owner can turn
off the witness for a database mirroring session.
T-SQL Script to turn off the witness server.
1 ALTER DATABASE Database_Name SET WITNESS OFF;
During the database mirroring session, Can you perform role switching?

Yes,

In role switching, the mirror server acts as the failover partner for the
principal server, taking over the principal role, recovering its copy of the
database and bringing it online as the new principal database. The former
principal server, when available, assumes the mirror role, and its database
becomes the new mirror database.

How Automatic Failover Works?


If the principal server is still running, it changes the state of the principal database to
DISCONNECTED and disconnects all clients from the principal database.
The witness and mirror servers register that the principal server is unavailable.
If any log is waiting in the redo queue, the mirror server finishes rolling forward the mirror database.
The former mirror database moves online as the new principal database, and recovery cleans up all
uncommitted transactions by rolling them back as quickly as possible.
When the former principal server rejoins the session, it recognizes that its failover partner now owns
the principal role. The former principal server takes on the role of mirror, making its database the
mirror database. The new mirror server synchronizes the new mirror database with the principal
database as quickly as possible.
What are the different types of Mirroring states?
SYNCHRONIZING: The contents of the mirror database are lagging behind the contents of the
principal database. The principal server is sending log records to the mirror server, which is applying
the changes to the mirror database to roll it forward.
SYNCHRONIZED: When the mirror server becomes sufficiently caught up to the principal server,
the mirroring state changes to SYNCHRONIZED. The database remains in this state as long as the
principal server continues to send changes to the mirror server and the mirror server continues to
apply changes to the mirror database.
SUSPENDED: SUSPENDED is a persistent state that survives partner shutdowns and startups.
The mirror copy of the database is not available. The principal database is running without sending
any logs to the mirror server, a condition known as running exposed. This is the state after a failover.
PENDING_FAILOVER: This state is found only on the principal server after a failover has begun,
but the server has not transitioned into the mirror role.
DISCONNECTED: The partner has lost communication with the other partner.
Can you set up Database Mirroring session using windows
authentication?
Yes, you can configure database mirroring using windows authentication
mode.
During the mirroring session, Can you add data file into primary
database?

When using Database Mirroring and if you have not placed your database
files for your principal database and mirrored database on an identical path
then adding a database file to the principal database is quite different than the
normal process. In this case, when you add a database file to a database, your
mirroring configuration will be suspended because SQL Server will not be
able to create that file on the mirrored server. This will not allow the
databases to be in sync and will force mirroring to go into a suspended state.

But can add data file during the mirroring session, If the complete path
(including drive letter and folder names) for the database files exists on both
the principal and mirrored server.
What are the steps to add a data file in primary server, when primary
and mirror database path are different?

We will take to add a new database file for a mirrored database that has
different file paths on the principal and mirror. First remove the mirror
partner, then create the database file on the principal server. After that I will
take a log backup and restore it on the mirrored server using the WITH
MOVE option. Once the restore is done, I will re-establish database
mirroring.
What are the steps to add a data file in primary server, when primary
and mirror database path are different?

We will take to add a new database file for a mirrored database that has
different file paths on the principal and mirror. First remove the mirror
partner, then create the database file on the principal server. After that I will
take a log backup and restore it on the mirrored server using the WITH
MOVE option. Once the restore is done, I will re-establish database
mirroring.
If we add any users to the Primary database, will it be copied to the
Mirror databases or not?

Yes, It will be copied to the Mirror database with the permissions. However,
you have to create the login manually with the same user ID.

S-ar putea să vă placă și