SQLServerCentral is supported by Red Gate Software Ltd.
 
Log in  ::  Register  ::  Not logged in
Search:  
 
 

SQL Server Soldier :: News from the frontlines of the database wars

Add to Technorati Favorites Add to Google
February 2009 - Posts

Database Mirroring FAQ: What would be the best practice to configure Failover Clustering and Database Mirroring?

Rating: |  Discuss | 6,545 Reads | 894 Reads in Last 30 Days |no comments

Question: What would be the best practice to configure Failover Clustering and Database Mirroring?

This question is frequently asked in one of the technical discussion groups I frequent. My reply follows.

I have a customer that wants to combine Failover Clustering with Database Mirroring in which the last one will be for disaster recovery. In order that the mirroring will act as primary only if the cluster fails, does the mirroring should be configured as manual? How do we configure mirroring in automatic when both nodes in the cluster fail?

My answer:

If you want automatic failover in case all clustering nodes fail, then you should definitely not set it to manual failover only. The best practice is to set the partner timeout period for mirroring to a number large enough to not be triggered by a cluster failover. The default timeout is 10 seconds.

The following command would set it to a timeout of 30 seconds:

Alter Database <Database Name> Set Partner Timeout 30;

Note: this can be set on the principal server only.


Database Mirroring FAQ: Will the Mirror automatically fail back to the original Principal when it comes back online?

Rating: |  Discuss | 6,171 Reads | 847 Reads in Last 30 Days |no comments

Question: Will the Mirror automatically fail back to the original Principal when it comes back online?

This question doesn't usually come up until someone has experienced an unplanned failover and something that they forgot about has been failing as a result. Hopefully this question will start coming up while planning for database mirroring.

My answer:

Automatic failover only occurs as a result of the current Principal being offline. Database Mirroring does not have any mechanisms built in to set one server as the default active server. When the Mirror partner takes over as the active Principal partner, it will continue to serve as Principal until a failure triggers another automatic failover or a manual failover is performed.

You can set up this functionality on your own, however. It's a simple process. Create a procedure to check the state and role of your mirror partners and manually fail them back to the original Principal if it is connected and in a synchronized state. Then set up a job to run this procedure every minute. Once the original Principal is back online and fully synchronized, the job will fail the server back within one minute.

The procedure:

Create Procedure dbo.dbm_FailoverMirrorToOriginalPrincipal
     @DBName sysname
As
Declare
@SQL nvarchar(200)

Set NoCount On;

/*
     If database is in the principal role and is in a synchronized state
     then fail database back to original principal
*/

If Exists (Select 1 From sys.database_mirroring
          Where database_id = db_id(@DBName)
          And mirroring_role = 1 -- Principal partner
          And mirroring_state = 4) -- Synchronized
  Begin
     Set @SQL = 'Alter Database ' + quotename(@DBName) + ' Set Partner Failover;'

     Exec sp_executesql @SQL;
  End

Set NoCount Off;

Please Note:

You may wonder why I use dynamic SQL for this procedure rather than just hard coding the database name. If you hard code the database name and try to create the procedure while the database is in the mirror role, it will fail because it will be referencing a database that is offline.


How do I ... Determine Database Growth If I Am Not Tracking It? -- Error Logs Part III

Rating: |  Discuss | 6,678 Reads | 669 Reads in Last 30 Days |7 comment(s)

How do I ... Determine Database Growth If I Am Not Tracking It?

If your database has grown considerably and you're not sure when or why it happened, you'll be hard pressed to figure out the answer unless you're tracking changes in database size. Luckily, if you are performing regular full backups, you can get this information from the SQL logs.

When you create a full backup, SQL creates an entry in the SQL Log that looks like the following:

Database backed up. Database: PSP, creation date(time): 2008/12/22(18:25:10), pages dumped: 171, first LSN: 39:41:37, last LSN: 39:59:1, number of dump devices: 1, device information: (FILE=1, TYPE=DISK: {'c:\Bak\PSP.bak'}). This is an informational message only. No user action is required.

So we can look for these statements in the SQL logs, parse out the pages dumped, and calculate the size of the used portion of the database.

The Script:

Declare @OldestLog datetime,
     
@FirstLog int,
     
@SearchText nvarchar(50),
     
@DBName sysname
Declare
@ErrorLog Table (LogID int identity(1, 1) not null primary key,
                              
LogDate datetime null,
                              
ProcessInfo nvarchar(100) null,
                              
LogText nvarchar(max) null)
Declare
@EnumLogs Table (ArchiveNum int not null primary key,
                              
ArcDate Datetime not null,
                              
LogFileSize bigint not null)

Set
nocount On
Set
@OldestLog = '2/1/2009'
Set
@SearchText = N'pages dumped: '
Set
@DBName = '<database name>'

Insert
Into @EnumLogs
Exec
master..xp_enumerrorlogs

Select
Top 1 @FirstLog = ArchiveNum
From
@EnumLogs
Where
ArcDate < @OldestLog
Order
By ArcDate DESC

If
@FirstLog Is Null
  
Begin
     
Select Top 1 @FirstLog = ArchiveNum
     
From @EnumLogs
     
Order By ArchiveNum DESC
  
End

While
@FirstLog >= 0
  
Begin
     
Insert Into @ErrorLog (LogDate, ProcessInfo, LogText)
     
Exec master..xp_readerrorlog @FirstLog
     
Set @FirstLog = @FirstLog - 1
  
End

Select
Convert(varchar, LogDate, 101) As BUPDate,
Cast
(Cast((Cast(RTrim(LTrim(SubString(LogText, CharIndex(@SearchText, LogText) + Len(@SearchText), CharIndex(',', LogText, CharIndex(@SearchText, LogText)) - CharIndex(@SearchText, LogText) - Len(@SearchText)))) as BigInt) * 8.0)/1024 As Decimal(9, 2)) As varchar) + ' MB' As BUPSize
From
@ErrorLog
Where
CharIndex('Backup', ProcessInfo) > 0
And
CharIndex('Database backed up. Database: ' + @DBName, LogText) > 0
Order
By LogDate Asc

Set
nocount Off


Database Mirroring FAQ: Why does Database Mirroring Monitor report that my mirror is disconnected?

Rating: (not yet rated) Rate this |  Discuss | 6,106 Reads | 600 Reads in Last 30 Days |no comments

Question: Why does Database Mirroring Monitor report that my mirror is disconnected?

This question was sent to a discussion list via email. My reply follows.

Sometimes, I have seen a behavior where the Principal was unable to connect to the Mirror. What is the reason for this behavior? Could it be network? I could not find any information that explain this behavior.

Any help here would be appreciated.

My answer:

This does not mean that the principal is disconnected from the mirror.

This simply means that the Database Mirroring Monitor has not connected to the mirror yet. This is not an unusual behavior to see when first looking at a mirrored database in the monitor. The monitor connects to the principal and then to the mirror. Usually, this will clear up within a few seconds on its own as it eventually makes the connection.

If it does not make the connection on its own within a short time, then the problem exists between the monitor and the mirror.


Database Mirroring FAQ: Can a 2008 SQL instance be used as the witness for a 2005 database mirroring setup?

Rating: |  Discuss | 4,914 Reads | 518 Reads in Last 30 Days |no comments

Question: Can a 2008 SQL instance be used as the witness for a 2005 database mirroring setup?

This question was sent to me via email. My reply follows.

Can a 2008 SQL instance be used as the witness for a 2005 database mirroring setup?

Databases to be mirrored are currently running on 2005 SQL instances but will be upgraded to 2008 SQL in the near future. I have not tried this, need mirroring setup and was hoping to get a jump on the future upgrade.

My answer:

Yes, a SQL 2008 machine can act as witness for 2 SQL 2005 machines. When upgrading the systems from SQL Server 2005 to SQL Server 2008, the witness can be upgraded at any time (first, last, or in the middle).


Database Mirroring FAQ: If the principal fails while running in high performance mode, what options do I have for bringing the mirror online?

Rating: (not yet rated) Rate this |  Discuss | 5,743 Reads | 579 Reads in Last 30 Days |2 comment(s)
Question: If the principal fails while running in high performance mode, what options do I have for bringing the mirror online?

This question was sent to me via email. My reply follows.

Quick question on DB mirroring client redirection after Principal fails ...

Consider a simple scenario with Asynchronous mirroring:

  1. We are implementing asynchronous [high performance] mirroring without witness.
  2. The client application connects to the mirroring session using below connection string:

    Source={PrincipalServerName};Initial Catalog={DBName};Integrated Security=SSPI;Failover Partner= {MirrorServerName}
  3. Now due to some disaster, the Principal Server goes offline and will be offline for let’s say next 48 hours.
  4. We notice that client connections fail since primary is offline and a failover has not yet be done.
  5. What steps should be taken now to connect the client application to the mirrored database?

I can force the service or break the mirroring session (assuming with possible data loss) and re-setup later when Principal comes online.

  1. Is above statement correct?
  2. If yes, what changes do I need do in the connection strings for force service and breaking the mirror?
  3. Would connection string in step#2 works with failover partner parameter?

My answer:

  1. Yes, those are your only options for bringing the mirror online at this point. Forcing the service is preferable.
  2. If you force the service, no changes will need to be made to the connection strings because the clients will be able to connect to the failover partner.

    If you break mirroring, the Failover Partner parameter of the connection string will not be honored because the database is not currently participating in mirroring. You would have to alter the connection string to point at the mirror as the principal. One caveat to this is if you were already running on the original mirror as the current principal and the server you force service on is listed as the principal in the connection string already, no changes will need to be made.
  3. As described above. Always yes for forced service, usually no but sometimes yes for breaking the mirror.