title | description | author | ms.author | ms.date | ms.service | ms.subservice | ms.topic | helpviewer_keywords | |
---|---|---|---|---|---|---|---|---|---|
Create an availability group using PowerShell |
Learn how to use PowerShell cmdlets to create and configure an Always On availability group by using PowerShell in SQL Server 2019 (15.x). |
MashaMSFT |
mathoma |
05/17/2016 |
sql |
availability-groups |
how-to |
|
[!INCLUDE SQL Server] This topic describes how to use PowerShell cmdlets to create and configure an Always On availability group by using PowerShell in [!INCLUDEssnoversion]. An availability group defines a set of user databases that will fail over as a single unit and a set of failover partners, known as availability replicas, which support failover.
Note
For an introduction to availability groups, see Overview of Always On Availability Groups (SQL Server).
Note
As an alternative to using PowerShell cmdlets, you can use the Create Availability Group wizard or [!INCLUDEtsql]. For more information, see Use the New Availability Group Dialog Box (SQL Server Management Studio) or Create an Availability Group (Transact-SQL).
- Before creating an availability group, verify that the host instances of [!INCLUDEssNoVersion] each resides on a different Windows Server Failover Clustering (WSFC) node of a single WSFC failover cluster. Also, verify that your server instances met the other server-instance prerequisites and that all of the other [!INCLUDEssHADR] requirements are meet and that you are aware of the recommendations. For more information, we strongly recommend that you read Prerequisites, Restrictions, and Recommendations for Always On Availability Groups (SQL Server).
Requires membership in the sysadmin fixed server role and either CREATE AVAILABILITY GROUP server permission, ALTER ANY AVAILABILITY GROUP permission, or CONTROL SERVER permission.
The following table lists the basic tasks involved in configuring an availability group and indicates those that are supported by PowerShell cmdlets. The [!INCLUDEssHADR] tasks must be performed in the sequence in which they are presented in the table.
Task | PowerShell Cmdlets (if Available) or Transact-SQL Statement | Where to Perform Task |
---|---|---|
Create database mirroring endpoint (once per [!INCLUDEssNoVersion] instance) | New-SqlHadrEndPoint | Execute on each server instance that lacks database mirroring endpoint. To alter an existing database mirroring endpoint, use Set-SqlHadrEndpoint. |
Create availability group | First, use the New-SqlAvailabilityReplica cmdlet with the -AsTemplate parameter to create an in-memory availability-replica object for each of the two availability replicas that you plan to include in the availability group. Then, create the availability group by using the New-SqlAvailabilityGroup cmdlet and referencing your availability-replica objects. |
Execute on the server instance that is to host the initial primary replica. |
Join secondary replica to availability group | Join-SqlAvailabilityGroup | Execute on each server instance that is hosts a secondary replica. |
Prepare the secondary database | Backup-SqlDatabase and Restore-SqlDatabase | Create backups on the server instance that hosts the primary replica. Restore backups on each server instance that hosts a secondary replica, using the NoRecovery restore parameter. If the file paths differ between the computers that host the primary replica and the target secondary replica, also use the RelocateFile restore parameter. |
Start data synchronization by joining each secondary database to availability group | Add-SqlAvailabilityDatabase | Execute on each server instance that hosts a secondary replica. |
Note
To perform the given tasks, change directory (cd) to the indicated server instance or instances.
Set up and use the SQL Server PowerShell Provider.
Note
To view the syntax and an example of a given cmdlet, use the Get-Help cmdlet in the [!INCLUDEssNoVersion] PowerShell environment. For more information, see Get Help SQL Server PowerShell.
-
Change directory (cd) to the server instance that is to host the primary replica.
-
Create an in-memory availability-replica object for the primary replica.
-
Create an in-memory availability-replica object for each of the secondary replicas.
-
Create the availability group.
[!NOTE]
The maximum length for an availability group name is 128 characters. -
Join the new secondary replica to the availability group, see Join a Secondary Replica to an Availability Group (SQL Server).
-
For each database in the availability group, create a secondary database by restoring recent backups of the primary database, using RESTORE WITH NORECOVERY.
-
Join every new secondary database to the availability group, see Join a Secondary Replica to an Availability Group (SQL Server).
-
(optional) Use the Windows dir command to verify the contents of the new availability group.
Note
If the [!INCLUDEssNoVersion] service accounts of the server instances run under different domain user accounts, on each server instance, create a login for the other server instance and grant this login CONNECT permission to the local database mirroring endpoint.
The following PowerShell example creates and configures a simple availability group named <myAvailabilityGroup>
with two availability replicas and one availability database. The example:
-
Backs up
<myDatabase>
and its transaction log. -
Restores
<myDatabase>
and its transaction log, using the -NoRecovery option. -
Creates an in-memory representation of the primary replica, which will be hosted by the local instance of [!INCLUDEssNoVersion] (named
PrimaryComputer\Instance
). -
Creates an in-memory representation of the secondary replica, which will be hosted by an instance of [!INCLUDEssNoVersion] (named
SecondaryComputer\Instance
). -
Creates an availability group named
<myAvailabilityGroup>
. -
Joins the secondary replica to the availability group.
-
Joins the secondary database to the availability group.
# Backup my database and its log on the primary
Backup-SqlDatabase `
-Database "<myDatabase>" `
-BackupFile "\\share\backups\<myDatabase>.bak" `
-ServerInstance "PrimaryComputer\Instance"
Backup-SqlDatabase `
-Database "<myDatabase>" `
-BackupFile "\\share\backups\<myDatabase>.log" `
-ServerInstance "PrimaryComputer\Instance" `
-BackupAction Log
# Restore the database and log on the secondary (using NO RECOVERY)
Restore-SqlDatabase `
-Database "<myDatabase>" `
-BackupFile "\\share\backups\<myDatabase>.bak" `
-ServerInstance "SecondaryComputer\Instance" `
-NoRecovery
Restore-SqlDatabase `
-Database "<myDatabase>" `
-BackupFile "\\share\backups\<myDatabase>.log" `
-ServerInstance "SecondaryComputer\Instance" `
-RestoreAction Log `
-NoRecovery
# Create an in-memory representation of the primary replica.
$primaryReplica = New-SqlAvailabilityReplica `
-Name "PrimaryComputer\Instance" `
-EndpointURL "TCP://PrimaryComputer.domain.com:5022" `
-AvailabilityMode "SynchronousCommit" `
-FailoverMode "Automatic" `
-Version 12 `
-AsTemplate
# Create an in-memory representation of the secondary replica.
$secondaryReplica = New-SqlAvailabilityReplica `
-Name "SecondaryComputer\Instance" `
-EndpointURL "TCP://SecondaryComputer.domain.com:5022" `
-AvailabilityMode "SynchronousCommit" `
-FailoverMode "Automatic" `
-Version 12 `
-AsTemplate
# Create the availability group
New-SqlAvailabilityGroup `
-Name "<myAvailabilityGroup>" `
-Path "SQLSERVER:\SQL\PrimaryComputer\Instance" `
-AvailabilityReplica @($primaryReplica,$secondaryReplica) `
-Database "<myDatabase>"
# Join the secondary replica to the availability group.
Join-SqlAvailabilityGroup -Path "SQLSERVER:\SQL\SecondaryComputer\Instance" -Name "<myAvailabilityGroup>"
# Join the secondary database to the availability group.
Add-SqlAvailabilityDatabase -Path "SQLSERVER:\SQL\SecondaryComputer\Instance\AvailabilityGroups\<myAvailabilityGroup>" -Database "<myDatabase>"
To configure a server instance for Always On Availability Groups
-
Enable and Disable Always On Availability Groups (SQL Server)
-
Create a Database Mirroring Endpoint for Always On Availability Groups (SQL Server PowerShell)
To configure availability group and replica properties
-
Change the Availability Mode of an Availability Replica (SQL Server)
-
Change the Failover Mode of an Availability Replica (SQL Server)
-
Create or Configure an Availability Group Listener (SQL Server)
-
Specify the Endpoint URL When Adding or Modifying an Availability Replica (SQL Server)
-
Configure Read-Only Access on an Availability Replica (SQL Server)
-
Configure Read-Only Routing for an Availability Group (SQL Server)
-
Change the Session-Timeout Period for an Availability Replica (SQL Server)
To complete availability group configuration
-
Join a Secondary Replica to an Availability Group (SQL Server)
-
Manually Prepare a Secondary Database for an Availability Group (SQL Server)
-
Join a Secondary Database to an Availability Group (SQL Server)
-
Create or Configure an Availability Group Listener (SQL Server)
Alternative ways to create an availability group
-
Use the Availability Group Wizard (SQL Server Management Studio)
-
Use the New Availability Group Dialog Box (SQL Server Management Studio)
To troubleshoot Always On Availability Groups configuration
-
Troubleshoot Always On Availability Groups Configuration (SQL Server)
-
Troubleshoot a Failed Add-File Operation (Always On Availability Groups)
-
Blogs:
Always On - HADRON Learning Series: Worker Pool Usage for HADRON Enabled Databases
Configuring Always On with SQL Server PowerShell
SQL Server Always On Team Blogs: The official SQL Server Always On Team Blog
-
Whitepapers:
Microsoft SQL Server Always On Solutions Guide for High Availability and Disaster Recovery
The Database Mirroring Endpoint (SQL Server)
Overview of Always On Availability Groups (SQL Server)