首页 > 解决方案 > 无法使用 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)')

标签: pythonsql-serverrobotframework

解决方案


分享我的经验,也许是关于SSMS permission问题。

您可以通过IP>Security>Login>Account>property来确认该账号是否有权限连接数据库。

我有和你一样的代码,在我确认权限之前,它对我有用。


推荐阅读