简体   繁体   English

尝试使用 pyodbc 使用 python 连接到 sql server 时出错

[英]Error while trying to connect to sql server with python using pyodbc

I'm trying to connect to sql server from a different computer.我正在尝试从另一台计算机连接到 sql server。 I'm using the following code.我正在使用以下代码。 The odbc driver is installed and also pyodbc is installed. odbc 驱动程序已安装,pyodbc 也已安装。

import pyodbc 

server = 'SERVER'
database = 'DBNAME' 
username = 'SA' 
password = 'SQLPWD' 
conn = pyodbc.connect('DRIVER={ODBC Driver 17 for SQL Server};SERVER='+server+';DATABASE='+database+';UID='+username+';PWD='+ password)

However I'm receiving the following error.但是我收到以下错误。

Traceback (most recent call last):
File "test.py", line 7, in <module>
conn = pyodbc.connect('DRIVER={ODBC Driver 17 for SQL 
Server};SERVER='+server+';DATABASE='+database+';UID='+username+';PWD='+ password)
pyodbc.InterfaceError: ('28000', "[28000] [Microsoft][ODBC Driver 17 for SQL Server][SQL 
Server]Login failed for user 'SA'. (18456) (SQLDriverConnect); [28000] [Microsoft][ODBC 
Driver 17 for SQL Server][SQL Server]Login failed for user 'SA'. (18456)")

I'm not sure where I'm going wrong and the older questions have not been much helpful.我不确定我哪里出错了,旧的问题也没有多大帮助。 I'm new to Python.我是 Python 的新手。

Probably the problem is related to:问题可能与以下有关:

  • SA access disabled ( REFERENCE ) SA 访问已禁用( 参考
  • Wrong Server Authentication Selection ( REFERENCE )错误的服务器身份验证选择( 参考
  • SQL Server is not open for remote access ( REFERENCE ) SQL Server 未打开远程访问 ( REFERENCE )

You can use SSMS to manage your server configuration.您可以使用SSMS来管理您的服务器配置。

IMPORTANT : As suggested by @Larnu is not a good idea use the sa user for remote access, create new user with limited access to the database & table is a better way to manage the server and secure it.重要提示:正如@Larnu 所建议的,使用sa用户进行远程访问不是一个好主意,创建对数据库和表具有有限访问权限的新用户是管理服务器并保护它的更好方法。 Take a look here to better understand.这里以更好地理解。

SA Access disabled SA 访问已禁用

To enable it you have to do:要启用它,您必须执行以下操作:

  • Connect to your server with SSMS (Using the Windows Authentication )使用 SSMS 连接到您的服务器(使用Windows 身份验证

    联系

  • Open the Security --> Logins Section and double click on the user sa打开Security --> Logins Section 并双击用户sa

    配置文件

  • Open the page General type a Password and Confirm Password , go in the page Status and set the Login on Enabled .打开页面General输入密码确认密码,进入页面状态并将登录设置为启用

    一般状态变化

Wrong Server Authentication Selection错误的服务器身份验证选择

  • In the Object Explorer right-click on the server instance and open the Properties在对象资源管理器中右键单击服务器实例并打开属性

    服务器道具

  • Open the page Security and check the option SQL Server and Windows Authentication mode打开页面安全并检查选项SQL Server 和 Windows 身份验证模式

    安全类型

SQL Server is not open for remote access SQL Server 未打开远程访问

  • In the Object Explorer right-click on the server instance and open the Properties在对象资源管理器中右键单击服务器实例并打开属性

  • Open the page Connections and check Allow remote connections to this server打开页面连接并选中允许远程连接到此服务器

    远程检查

  • Open your Start Menu and search for SQL Server Configuration Manager打开开始菜单并搜索SQL Server 配置管理器

  • Under the SQL Server Network Configuration select Protocols forSQL Server 网络配置下选择协议

  • Double-Click on TCP/IP and croll to the buttom, set the TCP Port to 1433双击TCP/IP并滚动到底部,将TCP 端口设置为1433

  • In the left pane of SQL Server Configuration Manager click SQL Server Services , right-click SQL Server<instance_name> , and click Restart.SQL Server 配置管理器的左窗格中,单击SQL Server Services ,右键单击SQL Server<instance_name> ,然后单击重新启动。

Now the issues should be solved, if not take a look on how to add a rule to windows firewall in the section Configure a Windows Firewall for Database Engine Access of the reference现在问题应该解决了,如果没有,请查看参考的为数据库引擎访问配置Windows防火墙部分中的如何向Windows防火墙添加规则

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM