[英]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.