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

使用PySpark连接MySQL失败但mysql包可正常连接的问题排查

PySpark连接RDS MySQL认证异常问题

我尝试用以下PySpark代码连接MySQL数据库:

os.environ['LIBMYSQL_ENABLE_CLEARTEXT_PLUGIN'] = '1'
df = spark.read.jdbc("jdbc:mysql://<host>:<port>/<db_name>", "<table>", properties={"user": "spark", "password": password})

但执行后抛出错误:

Access denied for user 'spark'@'' (using password: NO)
Current charset is US-ASCII. If password has been set using other charset, consider using option 'passwordCharacterEncoding'

然而用Python的mysql.connector包连接时却能正常运行:

os.environ['LIBMYSQL_ENABLE_CLEARTEXT_PLUGIN'] = '1'
mysql.connector.connect(user=user, password=password, host=host, database=db_name,
                                       raise_on_warnings=True)

我尝试在PySpark的jdbc连接properties中添加authenticationPlugins: mysql_clear_password,但仍出现相同的认证错误;添加"driver": "com.mysql.jdbc.Driver"后,又出现新错误:

py4j.protocol.Py4JJavaError: An error occurred while calling o84.jdbc.
: java.lang.ClassNotFoundException: com.mysql.jdbc.Driver

奇怪的是,不指定驱动时程序似乎能找到驱动,但连接认证有问题,我想知道此时PySpark使用的默认驱动是什么。

补充说明:连接密码是通过boto3生成的RDS数据库认证令牌,代码如下:

boto3_client = boto3.client('rds', region_name=region)
password = boto3_client.generate_db_auth_token(host, port, user)

内容的提问来源于stack exchange,提问作者Ema Il

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.27 04:57:14