Thor Logo dbatools

Set-DbaDbMirror

View Source
Chrissy LeMaire (@cl), netnerds.net
Windows, Linux, macOS

Synopsis

Configures database mirroring partner, witness, safety level, and operational state settings.

Description

Modifies database mirroring configuration by setting the partner server, witness server, safety level, or changing the mirror state. This function lets you reconfigure existing mirrored databases without manually writing ALTER DATABASE statements. Use it to add or change witness servers for automatic failover, adjust safety levels between synchronous and asynchronous modes, or control mirror states like suspend, resume, and failover operations.

Syntax

Set-DbaDbMirror
    [[-SqlInstance] <DbaInstanceParameter[]>]
    [[-SqlCredential] <PSCredential>]
    [[-Database] <String[]>]
    [[-Partner] <String>]
    [[-Witness] <String>]
    [[-SafetyLevel] <String>]
    [[-State] <String>]
    [[-InputObject] <Database[]>]
    [-EnableException]
    [-WhatIf]
    [-Confirm]
    [<CommonParameters>]

 

Examples

 

Example: 1
PS C:\> Set-DbaDbMirror -SqlInstance sql2005 -Database dbatools -Partner TCP://SQL2008.ad.local:5374

Prompts for confirmation then sets the partner to TCP://SQL2008.ad.local:5374 for the database “dbtools”

Example: 2
PS C:\> Set-DbaDbMirror -SqlInstance sql2005 -Database dbatools -Witness TCP://SQL2012.ad.local:5502 -Confirm:$false

Does not prompt for confirmation and sets the witness to TCP://SQL2012.ad.local:5502 for the database “dbtools”

Example: 3
PS C:\> Get-DbaDatabase -SqlInstance sql2005 | Out-GridView -PassThru | Set-DbaDbMirror -SafetyLevel Full -Confirm:$false

Sets the safety level to Full for databases selected from a grid view. Does not prompt for confirmation.

Example: 4
PS C:\> Set-DbaDbMirror -SqlInstance sql2005 -Database dbatools -State Suspend -Confirm:$false

Does not prompt for confirmation and sets the state to suspend for the database “dbtools”

Optional Parameters

-SqlInstance

The target SQL Server instance or instances. This can be a collection and receive pipeline input to allow the function
to be executed against multiple SQL Server instances.

PropertyValue
Alias
RequiredFalse
Pipelinefalse
Default Value
-SqlCredential

Login to the target instance using alternative credentials. Accepts PowerShell credentials (Get-Credential).
Windows Authentication, SQL Server Authentication, Active Directory - Password, and Active Directory - Integrated are all supported.
For MFA support, please use Connect-DbaInstance.

PropertyValue
Alias
RequiredFalse
Pipelinefalse
Default Value
-Database

Specifies the database(s) to configure mirroring settings for. Accepts multiple database names.
Use this to target specific mirrored databases when you need to modify partner, witness, safety level, or state settings.

PropertyValue
Alias
RequiredFalse
Pipelinefalse
Default Value
-Partner

Sets the mirroring partner server endpoint in TCP://servername:port format. This establishes or changes the mirror partnership.
Use this when setting up initial mirroring or changing the partner server after a configuration change or migration.

PropertyValue
Alias
RequiredFalse
Pipelinefalse
Default Value
-Witness

Sets the witness server endpoint in TCP://servername:port format to enable automatic failover in high-safety mode.
Use this to add witness functionality for automatic failover or to change the witness server location.

PropertyValue
Alias
RequiredFalse
Pipelinefalse
Default Value
-SafetyLevel

Controls transaction safety mode: ‘Full’ for synchronous high-safety, ‘Off’ for asynchronous high-performance.
Use ‘Full’ when you need zero data loss with automatic failover, or ‘Off’ for better performance with potential data loss during failover.

PropertyValue
Alias
RequiredFalse
Pipelinefalse
Default Value
Accepted ValuesFull,Off,None
-State

Changes the operational state of the mirroring session. Options include Suspend, Resume, Failover, or RemoveWitness.
Use this to temporarily pause mirroring during maintenance, resume after suspension, perform manual failover, or remove witness functionality.

PropertyValue
Alias
RequiredFalse
Pipelinefalse
Default Value
Accepted ValuesForceFailoverAndAllowDataLoss,Failover,RemoveWitness,Resume,Suspend,Off
-InputObject

Accepts database objects from Get-DbaDatabase pipeline input for batch operations.
Use this to configure mirroring settings across multiple databases efficiently by piping database objects from other dbatools commands.

PropertyValue
Alias
RequiredFalse
Pipelinetrue (ByValue)
Default Value
-EnableException

By default, when something goes wrong we try to catch it, interpret it and give you a friendly warning message.
This avoids overwhelming you with “sea of red” exceptions, but is inconvenient because it basically disables advanced scripting.
Using this switch turns this “nice by default” feature off and enables you to catch exceptions with your own try/catch.

PropertyValue
Alias
RequiredFalse
Pipelinefalse
Default ValueFalse
-WhatIf

Shows what would happen if the command were to run. No actions are actually performed.

PropertyValue
Aliaswi
RequiredFalse
Pipelinefalse
Default Value
-Confirm

Prompts you for confirmation before executing any changing operations within the command.

PropertyValue
Aliascf
RequiredFalse
Pipelinefalse
Default Value