简体   繁体   English

Adding and removing Azure database to failover group in elastic pool with TSQL or Azure API C#

[英]Adding and removing Azure database to failover group in elastic pool with TSQL or Azure API C#

Creating database and adding to failover group创建数据库并添加到故障转移组

I have an Azure elastic pool and I have created a failover group with another elastic pool (with the same name) on a different Azure region, during the creation of the failover group I selected the elastic pool and it went through and added all the databases in the elastic pool to the elastic pool on the secondary server.我有一个 Azure 弹性池,我在不同的 Azure 区域上创建了一个带有另一个弹性池(同名)的故障转移组,在创建故障转移组期间,我选择了弹性池,它通过并添加了所有数据库将弹性池中的弹性池复制到辅助服务器上的弹性池。

I have a script setup to automatically create new databases using the following TSQL:我有一个脚本设置来使用以下 TSQL 自动创建新数据库:

CREATE DATABASE databaseName ( SERVICE_OBJECTIVE = ELASTIC_POOL ( name = "Elastic pool name" ) );

However, the above script does not add the database to the failover group and therefore it does not get added to the other SQL server in my other Azure region.但是,上述脚本不会将数据库添加到故障转移组,因此它不会添加到我的其他 Azure 区域中的其他 SQL 服务器。 I don't want to have to manually add any new databases to the failover group via the Azure portal each time so is there a TSQL script I can use at the point of creation to add the database to the failover group?我不想每次都通过 Azure 门户手动将任何新数据库添加到故障转移组,所以是否有一个 TSQL 脚本可以在创建时用于将数据库添加到故障转移组?

Deleting database, removing from failover group and from secondary server删除数据库,从故障转移组和辅助服务器中删除

Following on from the above I also have the following TQL which deletes a database:继上述之后,我还有以下删除数据库的 TQL:

DROP DATABASE databaseName;

Running the above deletes the database from the primary server and it removes it from the failover group but the database still exists on the secondary server.运行上述命令会从主服务器中删除数据库,并将其从故障转移组中删除,但数据库仍存在于辅助服务器上。 Is there a way to remove it from the secondary server using TSQL, is it as simple as running the above script again but pointing to the secondary server or is there a better way of doing this?有没有办法使用 TSQL 从辅助服务器中删除它,是否像再次运行上述脚本但指向辅助服务器一样简单,还是有更好的方法?

EDIT编辑

As it seems there is no way to do this with TSQL then is this possible in C# using the Azure API with something like the following?因为似乎没有办法用 TSQL 做到这一点,那么在 C# 中使用 Azure ZDB974238714CA8DE4D74Z 与以下类似的东西有可能吗?

var dbResponse = client.FailoverGroups.Update("resourceGroupName", 
            "serverName", 
            "failoverGroupName", 
            new Microsoft.Azure.Management.Sql.Models.FailoverGroupUpdate() { 
                    Databases = new List<string>() { "databaseName" } 
                }
            );

I'm afraid no, there isn't a TSQL script which you can use at the point of creation to add the database to the failover group.恐怕不,没有 TSQL 脚本可用于在创建时将数据库添加到故障转移组。

Only the two ways Azure provides for us can manage the failover group: Portal and Powershell .只有 Azure 为我们提供的两种方式可以管理故障转移组: PortalPowershell

If you want to delete the secondary database, remove it from the failover group before deleting it .如果要删除辅助数据库,请先将其从故障转移组中删除,然后再将其删除 Ref here :参考这里 在此处输入图像描述

We still need do these database adding or deleting options with Portal or PowerShell.我们仍然需要使用 Portal 或 PowerShell 来添加或删除这些数据库选项。

Running some tests it seems you can use the Azure Rest API to do this and I have written the following method to test this, although it looks like you have to always pass through all the database names because this does not append what is already there but actually replaces the list of databases in the failover group. Running some tests it seems you can use the Azure Rest API to do this and I have written the following method to test this, although it looks like you have to always pass through all the database names because this does not append what is already there but实际上替换了故障转移组中的数据库列表。

I have not tested this in production to see if this would cause any issues such as effectively removing and re-adding the databases each time.我没有在生产中对此进行测试,看看这是否会导致任何问题,例如每次都有效地删除和重新添加数据库。

Hopefully someone else might be able to provide a better solution, my ideal solution would still be to use TSQL if possible because it means not having to setup a user account to access the REST API.希望其他人能够提供更好的解决方案,我理想的解决方案仍然是尽可能使用 TSQL,因为这意味着不必设置用户帐户来访问 REST API。

 public async Task AddDatabasesToFailoverGroupAsync(List<string> databaseNames)
    {
        // Variables
        string domainName = "XXXX"; /* Tenant ID or AAD domain */
        string username = "XXXX";
        string password = "XXXX";
        string resourceGroupName = "XXXXX";
        string serverName = "XXXX";
        string failoverGroupName = "XXXXX";
        string clientId = "XXXXX"; /* Active Directory APP Client ID */
        string subscriptionId = "XXXXXX";
        string databasePrefix = $"/subscriptions/{subscriptionId}/resourceGroups/{resourceGroupName}/providers/Microsoft.Sql/servers/{serverName}/databases/";
        List<string> databases = new List<string>();

        // Add prefix to database names
        foreach (var dbName in databaseNames)
            databases.Add($"{databasePrefix}{dbName}");

        // Login to Azure
        var credentials = await UserTokenProvider.LoginSilentAsync(clientId, domainName, username, password);

        // Create client
        SqlManagementClient client = new SqlManagementClient(credentials)
        {
            SubscriptionId = subscriptionId
        };

        // Set parameters
        var parameters = new FailoverGroupUpdate()
        {
            Databases = databases
        };

        // Update failover group
        client.FailoverGroups.Update(resourceGroupName, serverName, failoverGroupName, parameters);
    }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM