使用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
相关产品推荐
相关产品推荐

