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