Database connection failed for local MSSQL server with pymssql

时间:2018-09-19 08:17:27

标签: python database-connection pymssql

I had been working with pyodbcfor 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. 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')

My connection code for using both pyodbc and pymssql is:

    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?

2 个答案:

答案 0 :(得分:1)

好一会儿浏览互联网之后,似乎pymssql需要启用TCP / IP进行通信。

  1. 打开Sql Server配置管理器
  2. 扩展SQL Server网络配置
  3. 单击instance_name的协议
  4. 启用TCP/IP

答案 1 :(得分:1)

在使用RDS(AWS数据库实例)时,我遇到了同样的问题。我们应该配置入站出站规则。 请执行以下步骤进行配置。

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

“安全”部分

VPC security groups -> click on security group    

更改入站规则。

Check the source IP and change into anywhere or specific IP