python - 无法使用 python 代码连接到 Microsoft MySQL 服务器出现错误
问题描述
我无法使用 Python 代码连接到 Microsoft MySQL 服务器。我收到如下错误
代码
import pyodbc
server = 'xxx'
database = 'xxx'
username = 'xxx'
password = 'xx'
driver='{ODBC Driver 17 for SQL Server}'
with pyodbc.connect('DRIVER='+driver+';SERVER='+server+';PORT=portnumber;DATABASE='+database+';UID='+username+';PWD='+ password) as conn:
错误
Traceback (most recent call last):
File "sqlCode.py", line 12, in <module>
with pyodbc.connect('DRIVER='+driver+';SERVER='+server+';PORT=2078;DATABASE='+database+';UID='+username+';PWD='+ password) as conn:
pyodbc.OperationalError: ('08001', '[08001] [Microsoft][ODBC Driver 17 for SQL Server]Named Pipes Provider: Could not open a connection to SQL Server [2]. (2) (SQLDriverConnect); [08001] [Microsoft][ODBC Driver 17 for SQL Server]Login timeout expired (0); [08001] [Microsoft][ODBC Driver 17 for SQL Server]Invalid connection string attribute (0); [08001] [Microsoft][ODBC Driver 17 for SQL Server]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. (2)')
解决方案
分享我的经验,也许是关于SSMS permission
问题。
您可以通过IP>Security>Login>Account>property来确认该账号是否有权限连接数据库。
我有和你一样的代码,在我确认权限之前,它对我有用。
推荐阅读
- tslint - Nock - 无效的“扩展”配置值 tslint 错误
- uwp - 如何在 UWP 应用程序中读取/写入环境变量?
- rstudio - OsX 的 Rstudio 中缺少 rJava 图像
- projection - Basemap 的 robin 投影和轮廓数据问题
- java - 在 Eclipse 导出中创建 jar 文件时出错
- node.js - Puppeteer 在每个函数调用上创建一个新的浏览器实例
- mysql - 在 MySql 上创建执行查询的日期明智日志
- node.js - Excel 文件未在 Noe JS 中下载
- java - Hibernate JPAModelGen 5.4.6.Final 在 JDK11 上失败
- python - 无法使用elasticsearch建立连接错误