Поделиться через


Add-SqlAvailabilityDatabase

Adds primary databases to an availability group or joins secondary databases to an availability group.

Синтаксис

ByPath (по умолчанию)

Add-SqlAvailabilityDatabase
    [[-Path] <String[]>]
    -Database <String[]>
    [-Script]
    [-WhatIf]
    [-Confirm]
    [<CommonParameters>]

ByObject

Add-SqlAvailabilityDatabase
    [-InputObject] <AvailabilityGroup[]>
    -Database <String[]>
    [-Script]
    [-WhatIf]
    [-Confirm]
    [<CommonParameters>]

Описание

The Add-SqlAvailabilityDatabase cmdlet adds primary databases to an availability group or joins secondary databases to an availability group. The InputObject or Path parameter specifies the availability group. A database can belong to only one availability group.

To add databases to an availability group, run this cmdlet on the server instance that hosts the primary replica. Specify one or more local user databases.

To join a secondary database to the availability group, manually prepare the secondary database on the server instance that hosts the secondary replica. Then run this cmdlet on the server instance that hosts the secondary replica.

Примеры

Example 1: Add a database to an availability group

PS C:\>Add-SqlAvailabilityDatabase -Path "SQLSERVER:\SQL\PrimaryServer\InstanceName\AvailabilityGroups\MainAG" -Database "Database16"

This command adds the database Database16 to the availability group MainAG. Run this command on the primary server instance of the availability group. This command does not prepare secondary databases for data synchronization.

Example 2: Join a database to an availability group

PS C:\>Add-SqlAvailabilityDatabase -Path "SQLSERVER:\SQL\SecondaryServer\InstanceName\AvailabilityGroups\MainAG" -Database "Database16"

This command joins a secondary database named Database16 to the availability group MainAG on one of the server instances that hosts a secondary replica.

Example 3: Add a database and join a secondary database to an availability group

PS C:\> $DatabaseBackupFile = "\\share\backups\Database16.bak"
PS C:\> $LogBackupFile = "\\share\backups\Database16.trn"
PS C:\> $AGPrimaryPath = "SQLSERVER:\SQL\PrimaryServer\InstanceName\AvailabilityGroups\MainAG"
PS C:\> $MyAGSecondaryPath = "SQLSERVER:\SQL\SecondaryServer\InstanceName\AvailabilityGroups\MainAG"
PS C:\> Backup-SqlDatabase -Database "Database16" -BackupFile $DatabaseBackupFile -ServerInstance "PrimaryServer\InstanceName"
PS C:\> Backup-SqlDatabase -Database "Database16" -BackupFile $LogBackupFile -ServerInstance "PrimaryServer\InstanceName" -BackupAction Log
PS C:\> Restore-SqlDatabase -Database "Database16" -BackupFile $DatabaseBackupFile -ServerInstance "SecondaryServer\InstanceName" -NoRecovery
PS C:\> Restore-SqlDatabase -Database "Database16" -BackupFile $LogBackupFile -ServerInstance "SecondaryServer\InstanceName" -RestoreAction Log -NoRecovery
PS C:\> Add-SqlAvailabilityDatabase -Path $AGPrimaryPath -Database 'Database16'
PS C:\> Add-SqlAvailabilityDatabase -Path $AGSecondaryPath -Database "Database16"

This example prepares a secondary database from a database on the server instance that hosts the primary replica of an availability group. It adds the database to an availability group as a primary database. Finally, it joins the secondary database to the availability group.

The first four commands store paths in variables for use later in the example. The commands assign values to the $DatabaseBackupFile, $LogBackupFile, $AGPrimaryPath, and $AGSecondaryPath variables.

The fifth command backs up the database named Database16 that is on the primary server to the location in $DatabaseBackupFile.

The sixth command backs up the log file for Database16 on the primary server to the location in $LogBackupFile.

The seventh command restores the database backup for Database16 on a secondary server.

The eighth command restores the log file for Database16 on a secondary server.

The ninth command adds the database to the availability group on the primary server.

The final command joins the secondary database for that replica to the availability group. If you have more than one secondary replica, restore and join the secondary database for each of them.

Example 4: Create a script to add a database to an availability group

PS C:\>Add-SqlAvailabilityDatabase -Path "SQLSERVER:\SQL\PrimaryServer\InstanceName\AvailabilityGroups\MainAG" -Database "Database16" -Script

This command creates a Transact-SQL script that adds the database Database16 to the availability group MainAG.

Параметры

-Confirm

Prompts you for confirmation before running the cmdlet.

Свойства параметра

Тип:SwitchParameter
Default value:False
Поддерживаются подстановочные знаки:False
DontShow:False
Aliases:cf

Наборы параметров

(All)
Position:Named
Обязательно:False
Значение из конвейера:False
Значение из конвейера по имени свойства:False
Значение из оставшихся аргументов:False

-Database

Specifies an array of user databases. This cmdlet adds or joins the databases that this parameter specifies to the availability group. The databases that you specify must reside on the local instance of SQL Server.

Свойства параметра

Тип:

String[]

Default value:None
Поддерживаются подстановочные знаки:False
DontShow:False

Наборы параметров

(All)
Position:Named
Обязательно:True
Значение из конвейера:True
Значение из конвейера по имени свойства:True
Значение из оставшихся аргументов:False

-InputObject

Specifies availability group, as an AvailabilityGroup object, to which this cmdlet adds or joins databases.

Свойства параметра

Тип:

AvailabilityGroup[]

Default value:None
Поддерживаются подстановочные знаки:False
DontShow:False

Наборы параметров

ByObject
Position:2
Обязательно:True
Значение из конвейера:True
Значение из конвейера по имени свойства:False
Значение из оставшихся аргументов:False

-Path

Specifies the path of an availability group to which this cmdlet adds or joins databases. If you do not specify this parameter, this cmdlet uses current working location.

Свойства параметра

Тип:

String[]

Default value:None
Поддерживаются подстановочные знаки:False
DontShow:False

Наборы параметров

ByPath
Position:2
Обязательно:False
Значение из конвейера:False
Значение из конвейера по имени свойства:False
Значение из оставшихся аргументов:False

-Script

Indicates that this cmdlet returns a Transact-SQL script that performs the task that this cmdlet performs.

Свойства параметра

Тип:SwitchParameter
Default value:None
Поддерживаются подстановочные знаки:False
DontShow:False

Наборы параметров

(All)
Position:Named
Обязательно:False
Значение из конвейера:False
Значение из конвейера по имени свойства:False
Значение из оставшихся аргументов:False

-WhatIf

Shows what would happen if the cmdlet runs. The cmdlet is not run.

Свойства параметра

Тип:SwitchParameter
Default value:False
Поддерживаются подстановочные знаки:False
DontShow:False
Aliases:wi

Наборы параметров

(All)
Position:Named
Обязательно:False
Значение из конвейера:False
Значение из конвейера по имени свойства:False
Значение из оставшихся аргументов:False

CommonParameters

This cmdlet supports the common parameters: -Debug, -ErrorAction, -ErrorVariable, -InformationAction, -InformationVariable, -OutBuffer, -OutVariable, -PipelineVariable, -ProgressAction, -Verbose, -WarningAction, and -WarningVariable. For more information, see about_CommonParameters.

Входные данные

Microsoft.SqlServer.Management.Smo.AvailabilityGroup