Python pymssql.OperationalError:DB-Lib 错误消息 20009,严重性 9

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/19348255/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-19 13:31:51  来源:igfitidea点击:

pymssql.OperationalError: DB-Lib error message 20009, severity 9

pythonmysqlsqlsql-serversql-server-2008

提问by Mona Jalal

I am trying to run this program

我正在尝试运行这个程序

conn = pymssql.connect(host='localhost',  user='notsa', password='notsayly', database='TallicaTweets')

but I am receiving the following errors:

但我收到以下错误:

Traceback (most recent call last):
  File "harvester_of_tweets.py", line 11, in <module>
    conn = pymssql.connect(host='localhost',  user='username', password='password!', database='Database')
  File "/usr/local/lib/python2.7/dist-packages/pymssql.py", line 607, in connect
    raise OperationalError, e[0]
pymssql.OperationalError: DB-Lib error message 20009, severity 9:
Unable to connect: Adaptive Server is unavailable or does not exist
Net-Lib error during Connection refused Error 111 - Connection refused

Searching Google wasn't really helpful and the few tuts out there weren't really thorough. Should I add my credential into a specific configuration file? And how to create credentials in mssql? (I have done these things in psql but not familiar with mssql.

搜索谷歌并没有真正的帮助,而且那里的一些啧啧声也不是很彻底。我应该将我的凭据添加到特定的配置文件中吗?以及如何在 mssql 中创建凭据?(我在psql中做过这些但对mssql不熟悉。

I am using Ubuntu 13.04 ad my python version is 2.7.

我使用的是 Ubuntu 13.04 广告,我的 python 版本是 2.7。

After I created a user login I am receiving this error:

创建用户登录后,我收到此错误:

Traceback (most recent call last):
  File "harvester_of_tweets.py", line 11, in <module>
    conn = pymssql.connect(host='localhost',  user='tweet', password='1234', database='tweet_db')
  File "/usr/local/lib/python2.7/dist-packages/pymssql.py", line 607, in connect
    raise OperationalError, e[0]
pymssql.OperationalError: DB-Lib error message 20009, severity 9:
Unable to connect: Adaptive Server is unavailable or does not exist
Net-Lib error during Connection refused Error 111 - Connection refused

回答by podiluska

You should create a loginin MS SQL, and associate that with a specific userin the relevant database.

您应该login在 MS SQL 中创建一个,并将其与user相关数据库中的特定关联。

See here for an introduction to users and logins : http://blogs.msdn.com/b/lcris/archive/2007/03/23/basic-sql-server-security-concepts-logins-users-and-principals.aspx

有关用户和登录的介绍,请参见此处:http: //blogs.msdn.com/b/lcris/archive/2007/03/23/basic-sql-server-security-concepts-logins-users-and-principals。 aspx

回答by forrestfeng

I had similar issue. pymssql.OperationalError: DB-Lib error message 20009, ....

我有类似的问题。pymssql.OperationalError: DB-Lib 错误信息 20009, ....

Try the following steps.

请尝试以下步骤。

  1. Do not start MSSQLSERVER and SQLEXPRESS at the same time. Stop the one you do not needed.
  2. Be sure the right SQL Server service is started
  3. Be sure the ritht SQL Server Agent is started.
  4. Check the user name and password is correct or not with Microsoft SQL Server Management Studio on the server.
  1. 不要同时启动 MSSQLSERVER 和 SQLEXPRESS。停止你不需要的那个。
  2. 确保启动了正确的 SQL Server 服务
  3. 确保正确的 SQL Server 代理已启动。
  4. 使用服务器上的 Microsoft SQL Server Management Studio 检查用户名和密码是否正确。

Hope that helps

希望有帮助

回答by Hainan.Z

I encountered the same issue. It turns out that I didn't open up TCP/IP access for my local SQL server.

我遇到了同样的问题。事实证明,我没有为本地 SQL 服务器打开 TCP/IP 访问。

Do a quick test to check whether it's caused by this, you can try to connect to a remote server with the same code. If it works, it means something is wrong with your local server.

做一个快速测试以检查是否是由这个引起的,您可以尝试使用相同的代码连接到远程服务器。如果它有效,则意味着您的本地服务器有问题。

So just open the access to your 127.0.0.1:1433 in the SQL server Configuration Manager.

所以只需在 SQL 服务器配置管理器中打开对 127.0.0.1:1433 的访问。

Steps:

脚步:

  1. Start -> All Programs -> Microsoft SQL Server 20XX -> Configuration Tools -> SQL Server Configuration Manager
  2. SQL Server Network Configuration -> Protocols for MSSQLSERVER
  3. TCP/IP -> Properties -> IP Addresses. Find 127.0.0.1 and change the "Enabled" to "Yes". You can do it for all the IPs if you want.
  1. 开始 -> 所有程序 -> Microsoft SQL Server 20XX -> 配置工具 -> SQL Server 配置管理器
  2. SQL Server 网络配置 -> MSSQLSERVER 协议
  3. TCP/IP -> 属性 -> IP 地址。找到 127.0.0.1 并将“已启用”更改为“是”。如果需要,您可以为所有 IP 执行此操作。

回答by Aminah Nuraini

I solved my problem by using a different library instead. I use pyodbc

我通过使用不同的库解决了我的问题。我用pyodbc

import pyodbc
cnxn = pyodbc.connect('DRIVER={ODBC Driver 13 for SQL Server};SERVER=SERVERNAME;DATABASE=testdb;UID=me;PWD=pass')
cursor = cnxn.cursor()
cursor.execute("select user_id, user_name from users")
rows = cursor.fetchall()
for row in rows:
    print row.user_id, row.user_name

回答by Talley Ouro

  1. check installed python version(32 bit or 64 bit).

  2. Download freetds from below based on your OS architecture.
    https://github.com/ramiro/freetds/releases

  3. Unzip it to C:\drive (like c:\freetds)

  4. Add Bin (from c:\freetds\bindirectory) to the environment variables(both).

  5. Make sure port 1433 is open and TCP/IP enabled in SQL Configuration.

  6. Restart your PC and everything should be fine.

  1. 检查已安装的 python 版本(32 位或 64 位)。

  2. 根据您的操作系统架构从下面下载 freetds。
    https://github.com/ramiro/freetds/releases

  3. 将其解压缩到C:\驱动器(如c:\freetds

  4. 将 Bin(来自c:\freetds\bin目录)添加 到环境变量(两者)。

  5. 确保在 SQL 配置中打开端口 1433 并启用 TCP/IP。

  6. 重新启动你的电脑,一切都应该没问题。