简体   繁体   English

尝试从 Azure VM 连接到 Azure 托管实例中的 sql 时超时

[英]Timing out while trying to connect to sql in Azure Managed Instance from Azure VM

I am trying to access tables in SQL database in Azure Managed Instance (with IP: xxxx.database.windows.net) from a python script in Azure VM machine but I am getting the Operational Error below. I am trying to access tables in SQL database in Azure Managed Instance (with IP: xxxx.database.windows.net) from a python script in Azure VM machine but I am getting the Operational Error below. I have tried with 2 different ways below.我尝试了以下两种不同的方法。

Error:错误:

OperationalError: ('08001', '[08001] [Microsoft][SQL Server Native Client 11.0]TCP Provider: A connection attempt failed because the connected party did not properly respond after a period of time, or established connection failed because connected host has failed to respond.\r\n (10060) (SQLDriverConnect); [08001] [Microsoft][SQL Server Native Client 11.0]Login timeout expired (0); [08001] [Microsoft][SQL Server Native Client 11.0]A network-related or instance-specific error has occurred while establishing a connection to SQL Server. Server is not found or not accessible. Check if instance name is correct and if SQL Server is configured to allow remote connections. For more information see SQL Server Books Online. (10060)') OperationalError: ('08001', '[08001] [Microsoft][SQL Server Native Client 11.0]TCP Provider: 连接尝试失败,因为连接方在一段时间后没有正确响应,或者建立连接失败,因为连接的主机有未能响应。\r\n (10060) (SQLDriverConnect);[08001] [Microsoft][SQL Server Native Client 11.0]登录超时已过期 (0);[08001] [Microsoft][SQL Server Native Client 11.0]网络- 与 SQL 服务器建立连接时发生了相关或特定于实例的错误。找不到或无法访问服务器。检查实例名称是否正确以及 SQL 服务器是否配置为允许远程连接。有关更多信息,请参阅 Z9778828Z 服务器在线图书. (10060)')

1st way with connectionString:连接字符串的第一种方式:

import pandas as pd
from sqlalchemy import create_engine

engine = create_engine("mssql+pyodbc://<username>:<password>@<server>/<database>?driver=SQL+Server+Native+Client+11.0")
query = "select * from table"
df=pd.read_sql(query,engine)

2nd way with connectionString:连接字符串的第二种方式:

import pyodbc

server = 'xxx.database.windows.net'
database = 'database'
username = 'username'
password = 'password'   
driver= '{SQL Server Native Client 11.0}'
with pyodbc.connect('DRIVER='+driver+';SERVER='+server+';PORT=1433;DATABASE='+database+';UID='+username+';PWD='+ password) as conn:
    with conn.cursor() as cursor:
        cursor.execute("SELECT TOP 3 name, collation_name FROM sys.databases")
        row = cursor.fetchone()
        while row:
            print (str(row[0]) + " " + str(row[1]))
            row = cursor.fetchone()

Besides, I have also tried to change the driver to drivers below, still no luck.此外,我也尝试将驱动程序更改为下面的驱动程序,但仍然没有运气。

{ODBC Driver 11 for SQL Server}
{ODBC Driver 13 for SQL Server}
{ODBC Driver 17 for SQL Server}
{SQL Server Native Client 11.0}

Interesting part is, if I try the connect with the same connection string from on-premise machine which is not Azure VM (ex: my local machine or other servers I can RDP to), I can access the database.有趣的部分是,如果我尝试使用非 Azure VM 的本地机器(例如:我的本地机器或我可以 RDP 连接到的其他服务器)使用相同的连接字符串进行连接,我可以访问数据库。 But when I try on a Azure VM machine, it is timing out.但是当我在 Azure VM 机器上尝试时,它正在超时。 Do you have any ideas how to fix this problem?您对如何解决此问题有任何想法吗?

Thank you for inputs.感谢您的意见。

So in the end we foud out that firewall caused this problem.所以最后我们发现是防火墙导致了这个问题。 We need to check firewall rules first.我们需要先检查防火墙规则。

暂无
暂无

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

相关问题 无法通过Python连接到在Azure Linux VM上运行的Redis实例 - Unable to connect to Redis instance running on Azure Linux VM from Python 如何从Linux VM上的Django应用程序连接到Azure SQL数据库 - How to connect to Azure SQL database from Django app on Linux VM 使用托管标识 python 连接到 azure sql - Connect to azure sql with managed identity python 如何从 Python 中的 Azure Functions 连接到 Azure 托管 MySQL 数据库? - How to connect to Azure Managed MySQL Database from Azure Functions in Python? 使用 azure-mgmt-sql 从 SQL 托管实例获取数据库 - Get databases from SQL managed instance using azure-mgmt-sql Trying to connect Azure SQL database from Azure ML Service using MSI authentication (Without username and passowrd connect the Azure database) - Trying to connect Azure SQL database from Azure ML Service using MSI authentication (Without username and passowrd connect the Azure database) 如何在我的本地笔记本电脑中使用来自 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 尝试通过Azure REST API创建VM时出现运行时错误 - Runtime error while trying to create VM through Azure REST API 使用 Azure 托管服务标识 (MSI) 连接 Azure SQL Server 数据库 - Azure SQL Server Database connect using Azure Managed Service Identity (MSI)
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM