简体   繁体   English

使用 azure-mgmt-sql 从 SQL 托管实例获取数据库

[英]Get databases from SQL managed instance using azure-mgmt-sql

I use the library azure-mgmt-sql to get all the SQL servers and databases with the following code:我使用库 azure-mgmt-sql 通过以下代码获取所有 SQL 服务器和数据库:

resources = sql_client.servers.list_by_resource_group("myresourcegroup")
for r in resources:
    databases = sql_client.databases.list_by_server("myresourcegroup", r.name)
    for d in databases:
        print(d.name)

I also need to get the SQL managed instances and their databases.我还需要获取 SQL 托管实例及其数据库。 I found that using managed_instances instead of servers returns the SQL managed instances, but I didn't find a way to get the databases.我发现使用 managed_instances 而不是服务器返回 SQL 托管实例,但我没有找到获取数据库的方法。

resources = sql_client.managed_instances.list_by_resource_group("myresourcegroup")
    for r in resources:
        databases = sql_client.databases.list_by_server("myresourcegroup", r.name)
        for d in databases: <- ERROR when accessing the iterator
            print(d.name)

The error I am getting is the following:我收到的错误如下:

azure.core.exceptions.ResourceNotFoundError: (ParentResourceNotFound) Can not perform requested operation on nested resource. azure.core.exceptions.ResourceNotFoundError:(ParentResourceNotFound)无法对嵌套资源执行请求的操作。 Parent resource 'mymanagedinstancename>' not found.找不到父资源“mymanagedinstancename>”。

How I can get the databases from the sql managed instance?如何从 sql 托管实例获取数据库?

I tried in my environment and got below results:我在我的环境中尝试并得到以下结果:

Initially i tried the same code and got an same error:最初我尝试了相同的代码并得到了相同的错误:

Console:安慰:

在此处输入图像描述

From your code sql_client.databases.list_by_server("myresourcegroup", r.name) this you were used database.list_by_server .从您的代码sql_client.databases.list_by_server("myresourcegroup", r.name)中,您使用了database.list_by_server

I found that using managed_instances instead of servers returns the SQL managed instances, but I didn't find a way to get the databases.我发现使用 managed_instances 而不是服务器返回 SQL 托管实例,但我没有找到获取数据库的方法。

If you need to get database using managed instance, you can use sql_client.managed_databases.list_by_instance method.如果需要使用托管实例获取数据库,可以使用sql_client.managed_databases.list_by_instance方法。

Code:代码:

from azure.mgmt.sql import SqlManagementClient
from azure.identity import DefaultAzureCredential

credential=DefaultAzureCredential()
subscriptionid="<subscription ID >"
sql_client=SqlManagementClient(credential=credential,subscription_id=subscriptionid)
resources = sql_client.managed_instances.list_by_resource_group("<resource grp>")
for r in resources:
    databases=sql_client.managed_databases.list_by_instance("<resource grp>",r.name)
    for d in databases:
            print(d.name)

Console:安慰:

在此处输入图像描述

Reference:参考:

azure.mgmt.sql.SqlManagementClient class | azure.mgmt.sql.SqlManagementClient class | Microsoft Learn 微软学习

暂无
暂无

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

相关问题 尝试从 Azure VM 连接到 Azure 托管实例中的 sql 时超时 - Timing out while trying to connect to sql in Azure Managed Instance from Azure VM 如何在我的本地笔记本电脑中使用来自 Django 应用程序的托管服务标识连接到 Azure SQL 服务器 - How to connect to Azure SQL Server using Managed Service Identity from Django application in my local laptop 使用托管标识将 Django 与 Azure MS SQL 服务器数据库连接起来 - Connect Django with Azure MS SQL Server DB using managed identity 使用 Python 在 SQL 中使用多个数据库 - Using multiple databases in SQL with Python 使用托管标识 python 连接到 azure sql - Connect to azure sql with managed identity python 使用 Azure 托管服务标识 (MSI) 连接 Azure SQL Server 数据库 - Azure SQL Server Database connect using Azure Managed Service Identity (MSI) 使用 django 在前端显示来自 sql 数据库的数据 - Showing data on frontend from sql databases using django Accessing Azure SQL Server using 1) Python (local script--not going to be Azure function), 2) pyodbc &amp; 3) Azure user-managed identity - Accessing Azure SQL Server using 1) Python (local script--not going to be Azure function), 2) pyodbc & 3) Azure user-managed identity 使用Python从API获取数据并使用Azure Data Factory加载到Azure SQL数据仓库中 - Get data from API using Python and load into Azure SQL Data Warehouse using Azure Data Factory 使用python循环调用多个SQL数据库 - Using python looping to call multiple SQL databases
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM