Azure SQL Database 和 Azure SQL 托管实例的 Azure PowerShell 示例Azure PowerShell samples for Azure SQL Database and Azure SQL Managed Instance

适用于:是 Azure SQL 数据库 是Azure SQL 托管实例 APPLIES TO: yesAzure SQL Database yesAzure SQL Managed Instance

Azure SQL 数据库和 Azure SQL 托管实例允许使用 Azure PowerShell 配置数据库、实例和池。Azure SQL Database and Azure SQL Managed Instance enable you to configure your databases, instances, and pools using Azure PowerShell.

如果没有 Azure 订阅,可在开始前创建一个试用帐户If you don't have an Azure subscription, create a trial account before you begin.

可以在本地安装并使用 PowerShell,本教程需要 AZ PowerShell 1.4.0 或更高版本。You can install and use the PowerShell locally, this tutorial requires AZ PowerShell 1.4.0 or later. 如果需要进行升级,请参阅 Install Azure PowerShell module(安装 Azure PowerShell 模块)。If you need to upgrade, see Install Azure PowerShell module. 如果在本地运行 PowerShell,则还需运行 Connect-AzAccount -Environment AzureChinaCloud 来创建与 Azure 的连接。If you are running PowerShell locally, you also need to run Connect-AzAccount -Environment AzureChinaCloud to create a connection with Azure.

下表包括了适用于 Azure SQL 数据库的示例 Azure PowerShell 脚本的链接。The following table includes links to sample Azure PowerShell scripts for Azure SQL Database.

链接Link 说明Description
创建并配置单一数据库和弹性池Create and configure single databases and elastic pools
创建单一数据库并配置服务器级防火墙规则Create a single database and configure a server-level firewall rule 此 PowerShell 脚本创建单一数据库,并配置服务器级 IP 防火墙规则。This PowerShell script creates a single database and configures a server-level IP firewall rule.
创建弹性池并移动共用数据库Create elastic pools and move pooled databases 此 PowerShell 脚本创建弹性池,移动共用数据库并更改计算大小。This PowerShell script creates elastic pools, moves pooled databases, and changes compute sizes.
配置异地复制和故障转移Configure geo-replication and failover
配置单一数据库并使用活动异地复制对其进行故障转移Configure and fail over a single database using active geo-replication 这个 PowerShell 脚本用于为单一数据库配置活动异地复制,并将其故障转移到辅助副本。This PowerShell script configures active geo-replication for a single database and fails it over to the secondary replica.
配置共用数据库并使用活动异地复制对其进行故障转移Configure and fail over a pooled database using active geo-replication 此 PowerShell 脚本为弹性池中的数据库配置活动异地复制,并将其故障转移到次要副本。This PowerShell script configures active geo-replication for a database in an elastic pool and fails it over to the secondary replica.
配置故障转移组Configure a failover group
为单一数据库配置故障转移组Configure a failover group for a single database 此 PowerShell 脚本创建一个数据库和一个故障转移组,将该数据库添加到该故障转移组,并测试到辅助服务器的故障转移。This PowerShell script creates a database and a failover group, adds the database to the failover group, and tests failover to the secondary server.
为弹性池配置故障转移组Configure a failover group for an elastic pool 此 PowerShell 脚本创建一个数据库,将其添加到弹性池,将弹性池添加到故障转移组,并测试到辅助服务器的故障转移。This PowerShell script creates a database, adds it to an elastic pool, adds the elastic pool to the failover group, and tests failover to the secondary server.
缩放单一数据库和弹性池Scale a single database and an elastic pool
缩放单一数据库Scale a single database 此 PowerShell 脚本监视单一数据库的性能指标,将其缩放到更高的计算大小,并根据某个性能指标创建预警规则。This PowerShell script monitors the performance metrics of a single database, scales it to a higher compute size, and creates an alert rule on one of the performance metrics.
缩放弹性池Scale an elastic pool 这个 PowerShell 脚本用于监视弹性池的性能指标,将其扩展到更高的计算大小,并基于性能指标之一创建警报规则。This PowerShell script monitors the performance metrics of an elastic pool, scales it to a higher compute size, and creates an alert rule on one of the performance metrics.
审核和威胁检测Auditing and threat detection
配置审核和威胁检测Configure auditing and threat-detection 此 PowerShell 脚本为数据库配置审核和威胁检测策略。This PowerShell script configures auditing and threat-detection policies for a database.
还原、复制和导入数据库Restore, copy, and import a database
还原数据库Restore a database 这个 PowerShell 脚本用于从异地冗余备份还原数据库,并将已删除的数据库还原到最新备份。This PowerShell script restores a database from a geo-redundant backup and restores a deleted database to the latest backup.
将数据库复制到新服务器Copy a database to a new server 这个 PowerShell 脚本用于在新服务器中创建现有数据库的副本。This PowerShell script creates a copy of an existing database in a new server.
从 bacpac 文件导入数据库Import a database from a bacpac file 这个 PowerShell 脚本用于将数据库从 bacpac 文件导入 Azure SQL 数据库。This PowerShell script imports a database into Azure SQL Database from a bacpac file.
在数据库之间同步数据Sync data between databases
在数据库之间同步数据Sync data between databases 这个 PowerShell 脚本用于将数据同步配置为在 Azure SQL 数据库中的多个数据库间进行同步。This PowerShell script configures Data Sync to sync between multiple databases in Azure SQL Database.
在 SQL 数据库和本地 SQL Server 之间同步数据Sync data between SQL Database and SQL Server on-premises 此 PowerShell 脚本将数据同步配置为在 Azure SQL 数据库中的数据库和 SQL Server 本地数据库之间进行同步。This PowerShell script configures Data Sync to sync between a database in Azure SQL Database and a SQL Server on-premises database.
更新 SQL 数据同步的同步架构Update the SQL Data Sync sync schema 此 PowerShell 脚本将在数据同步的同步架构中添加或删除项。This PowerShell script adds or removes items from the Data Sync sync schema.

详细了解单一数据库 Azure PowerShell APILearn more about the Single-database Azure PowerShell API.

其他资源Additional resources

此页上列出的示例使用 PowerShell cmdlet 创建和管理 Azure SQL 资源。The examples listed on this page use the PowerShell cmdlets for creating and managing Azure SQL resources. 用于运行查询和执行多个数据库任务的其他 cmdlet 位于 sqlserver 模块中。Additional cmdlets for running queries and performing many database tasks are located in the sqlserver module. 有关详细信息,请参阅 SQL Server PowerShellFor more information, see SQL Server PowerShell.