You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

使用SQLAlchemy连接MySQL 8.31失败及正确连接方式咨询

Python连接MySQL 8.31的问题与正确连接方法

最初的错误尝试

我一开始用下面的代码连接MySQL 8.31:

connection_string = "DRIVER={ODBC Driver 17 for SQL Server};SERVER=localhost;DATABASE=%s;UID=root;PWD=password" % db_name
connection_url = URL.create("mssql+pyodbc", query={"odbc_connect": connection_string})
engine = create_engine(connection_url)
pd.read_sql('select * from apps_list', engine)

运行后直接抛出异常:

sqlalchemy.exc.OperationalError: (pyodbc.OperationalError) ('08001',
'[08001] [Microsoft][ODBC Driver 17 for SQL Server]Named Pipes
Provider: Could not open a connection to SQL Server. (2)
(SQLDriverConnect); [08001] [Microsoft][ODBC Driver 17 for SQL
Server]Login timeout expired (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)')

我排查了这些点:

  • 系统里没有SQL Server Configuration Manager
  • 确认Named Pipes已启用
  • 端口3306和1433都开放了
    但问题还是没解决。

当前可用但有警告的连接方式

后来我换了下面的代码,能成功连接:

mydb = pymysql.connect(
    host="localhost",
    database=db_name,
    user="root",
    password="****",
    autocommit=True
)
mycursor = mydb.cursor()
engine = create_engine('mysql+pymysql://****@localhost/%s' % db_name ) 

但运行时pandas会抛出警告:

userwarning: pandas only support SQLAlchemy connectable(engine/connection) ordatabase string URI or sqlite3 DBAPI2 connectionother DBAPI2 objects are not tested, please consider using SQLAlchemy warnings.warn(

我搞不懂的是,明明已经用SQLAlchemy的create_engine创建了连接,为什么还会有这个警告?想问问现在这个连接方式能不能用,以及正确的连接方法到底是什么。


当前连接方式的可行性

你当前的代码里其实存在两个独立的连接:

  1. mydb是直接用pymysql原生驱动创建的DBAPI2连接对象,如果把这个对象传给pd.read_sql,就会触发警告——因为pandas明确只保证SQLAlchemy连接/URI或者sqlite3原生连接的兼容性,其他DBAPI2对象不在测试范围内。
  2. 你创建的engine是SQLAlchemy的连接引擎,只要用这个engine来和pandas交互(比如pd.read_sql(..., engine)),就不会触发警告,这种用法是完全可行的。

所以如果要保留当前代码,只需要确保用engine执行数据库操作,不要用mydb这个原生连接对象。

正确的MySQL连接方法

你最初的错误核心是用了SQL Server的驱动去连接MySQL——mssql+pyodbc是SQL Server的SQLAlchemy方言,对应的ODBC驱动也是给SQL Server用的,和MySQL完全不兼容,所以不管怎么排查SQL Server的配置都没用。

连接MySQL的正确方式是使用MySQL对应的SQLAlchemy方言,比如mysql+pymysql(依赖pymysql库),推荐的写法有两种:

方式1:直接构造连接字符串

from sqlalchemy import create_engine
import pandas as pd

# 替换为你的实际密码和数据库名
engine = create_engine(f'mysql+pymysql://root:你的密码@localhost/{db_name}?autocommit=true')

# 用engine执行查询
df = pd.read_sql('select * from apps_list', engine)

方式2:用URL.create构造(更规范,适合处理特殊字符)

from sqlalchemy import create_engine
from sqlalchemy.engine import URL
import pandas as pd

connection_url = URL.create(
    "mysql+pymysql",
    username="root",
    password="你的密码",
    host="localhost",
    database=db_name,
    query={"autocommit": "true"}  # 传递额外参数
)
engine = create_engine(connection_url)

df = pd.read_sql('select * from apps_list', engine)

这两种方式都能正常连接MySQL,且不会触发pandas的警告,是标准的生产环境用法。

内容的提问来源于stack exchange,提问作者Paul Holmes

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.14 12:45:36