无法使用pyspark连接到mysql,但可以使用mysql包

nvbavucw  于 2023-03-22  发布在  Mysql
关注(0)|答案(1)|浏览(185)

我尝试使用spark连接到mysql db,使用以下命令:

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'@'<ip>' (using password: NO) 
Current charset is US-ASCII. If password has been set using other charset, consider using option 'passwordCharacterEncoding'

所以我试着写了这段python代码,一切都没有问题:

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

我尝试将authenticationPlugins: mysql_clear_password添加到属性中,但得到相同的错误

7nbnzgx9

7nbnzgx91#

试试这个:

df = spark \
    .read() \
    .format("jdbc") \
    .option("url", "jdbc:mysql://127.0.0.1:3306/test?useUnicode=true&characterEncoding=UTF-8&autoReconnect=true") \
    .option("driver", "com.mysql.jdbc.Driver") \
    .option("user", "root") \
    .option("password", "password") \
    .option("dbtable", <table_name>) \
    .load();

参考:how-to-integrate-apache-spark-with-mysql-for-reading-database-tables-as-a-spark

相关问题