简体   繁体   English

本地 MSSQL 服务器与 pymssql 的数据库连接失败

[英]Database connection failed for local MSSQL server with pymssql

I had been working with pyodbc for database connection in windows envirnment and it is working fine but now I want to switch to pymssql so that it is easier to be deployed to Linux machine as well.我一直在使用pyodbc在 windows 环境中进行数据库连接,它工作正常,但现在我想切换到pymssql以便它也更容易部署到 Linux 机器上。 But I am getting this error:但我收到此错误:

(20009, b'DB-Lib error message 20009, severity 9:\\nUnable to connect: Adaptive Server is unavailable or does not exist (localhost:1433)\\nNet-Lib error during Unknown error (10060)\\n') (20009, b'DB-Lib 错误消息 20009, 严重性 9:\\n无法连接: Adaptive Server 不可用或不存在 (localhost:1433)\\n在未知错误 (10060) 期间出现 Net-Lib 错误 (10060)\\n')

My connection code for using both pyodbc and pymssql is:我使用pyodbcpymssql连接代码是:

import pyodbc
    import pymssql

    def connectODSDB_1():
        conn_str = (
            r"Driver={SQL Server};"
            r"Server=(local);"
            r"Database=populatedSandbox;"
            r"Trusted_Connection=yes;"
        )
        return pyodbc.connect(conn_str)

    def connectODSDB_2():
        server = '(local)'
        database = 'populatedSandbox'
        conn = pymssql.connect(server=server, database=database)
        return conn

What could be the problem?可能是什么问题呢? And solution?和解决方案?

Well after browsing internet for a while, it seems pymssql needs TCP/IP be enabled for communication.好吧,在浏览了一段时间后, pymssql似乎需要启用 TCP/IP 才能进行通信。

  1. Open Sql Server Configuration Manager打开 SQL Server 配置管理器
  2. Expand SQL Server Network Configuration展开 SQL Server 网络配置
  3. Click on Protocols for instance_name单击instance_name协议
  4. Enable TCP/IP启用TCP/IP

I have faced the same issue while using RDS(AWS database instance).我在使用 RDS(AWS 数据库实例)时遇到了同样的问题。 We should configured the inbound outbound rules.我们应该配置入站出站规则。 Do following steps to configure.请按照以下步骤进行配置。

Services->RDS->DB Instances -> Select DB-> Connectivity&Security

Under Security Section在安全科下

VPC security groups -> click on security group    

Change the inbound rules.更改入站规则。

Check the source IP and change into anywhere or specific IP

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

相关问题 pymssql和Adaptive Server连接失败 - pymssql and Adaptive Server connection failed 由于pymssql的未知原因,与数据库的连接失败 - Connection to the database failed for an unknown reason for pymssql Python pymssql:Adaptive Server连接失败 - Python pymssql: Adaptive Server connection failed pymssql“ InterfaceError:由于未知原因,数据库连接失败。” - pymssql “InterfaceError: Connection to the database failed for an unknown reason.” 带有pymssql的Python / Flask / sqlAlchemy环境中的Adaptive Server连接失败错误 - Adaptive Server connection failed error in Python/Flask/sqlAlchemy environment with pymssql 如何使用mssql和pymssql在Python中通过IP远程连接到MS SQL Server数据库 - How to connect to MS SQL Server database remotely by IP in Python using mssql and pymssql 在pythonanywhere上对Linux云上的Azure mssql进行pymssql连接故障排除 - Troubleshooting pymssql connection to Azure mssql on linux cloud at pythonanywhere 与 Pymssql/Adaptive Server 连接失败(Windows.net vs Azure.com) - Connect with Pymssql / Adaptive Server connection failed (Windows.net vs Azure.com) pymssql:有时只能连接数据库 - pymssql: Connection to the database only works sometimes pymssql-服务器连接列表,而不是一个连接 - pymssql - list of server connections instead of one connection
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM