如何使用Pyspark从Google Colab读写本地MySQL Server 8?

问题描述 投票:0回答:3

我一直在尝试,但是未能使用来自Google colab的pyspark从Windows 10本地主机上的MySQL Server 8.0.19写入/读取表。还有很多类似的问题,并给出了一些建议的答案,但似乎所有解决方案都无法在此工作。这是我的代码:

    <...installations  ...>

        from pyspark.sql import SparkSession

        spark = SparkSession\
        .builder\
        .appName("Word Count")\
        .config("spark.driver.extraClassPath", "/content/spark-2.4.5-bin-hadoop2.7/jars/mysql-connector-java-8.0.19.jar")\
        .getOrCreate()

这里是连接字符串:

MyjdbcDF = spark.read.format("jdbc")\
                    .option("url", "jdbc:mysql://127.0.0.1:3306/mydb?user=testuser&password=pwtest")\
                    .option("dbtable", "collisions")\
                    .option("driver","com.mysql.cj.jdbc.Driver")\
                    .load()

我也已经使用过.option("driver","com.mysql.jdbc.Driver"),但仍然会收到此错误:

Py4JJavaError: An error occurred while calling o154.load.
com.mysql.cj.jdbc.exceptions.CommunicationsException: Communications link failure

The last packet sent successfully to the server was 0 milliseconds ago. The driver has not received any packets from the server.

...
...
...
Caused by: java.net.ConnectException: Connection refused (Connection refused)

据此,我猜想MySQL Sever无法实现。我已远程登录到端口3306,它确认MySQL服务器正在接受来自客户端计算机的连接。我已经读到运行该命令:netsh advfirewall firewall add rule name="MySQL Server" action=allow protocol=TCP dir=in localport=3306将允许MySQL服务器使用防火墙规则,以防它被阻止但尚未更改。

有人可以帮忙解决吗?

python mysql pyspark google-colaboratory
3个回答
0
投票

您正在尝试连接安装在本地计算机上的mysql数据库,即来自Google colab的Windows 10作为localhost实例

这是不可能的,因为google colab会自行启动实例来执行代码,如果要访问本地mysql,则需要将其托管在服务器上,以便可以通过Internet进行访问。

否则,您可以在colab上安装mysql,然后使用它来运行代码进行测试。

!apt-get -y install mysql-server

然后在实例上配置它以使用


0
投票

这是我在Colab上安装和设置MySQL的方法

# install, set connection
!apt-get install mysql-server > /dev/null
!service mysql start
!mysql -e "ALTER USER 'root'@'localhost' IDENTIFIED WITH mysql_native_password BY 'root'"
!pip -q install PyMySQL
%load_ext sql
%config SqlMagic.feedback=False 
%config SqlMagic.autopandas=True
%sql mysql+pymysql://root:root@/
# query using %sql or %%sql
df = %sql SELECT Host, User, authentication_string FROM mysql.user
df

0
投票

经过大约几天的研究,我找到了解决方案,所以我将回答我自己的问题。我能够使用WAMP(感谢@Shubham Jain提出建议)以及不使用WAMP进行连接。该答案没有WAMP。

[[Downloadedngrokfrom https://ngrok.com/未压缩已保存在我的本地窗口中,Authenticate具有:./ngrok authtoken xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx(网站上提供了非常简单明了的说明)

仍然在我的本地Windows上,我在命令行上复制并运行了ngrok tcp 3306

C:\Users\userMe> ngrok tcp 3306

它给了一些东西:

ngrok by @inconshreveable                                                             
Session Status                online
Account                       userMe (Plan: Free)
Version                       2.3.35
Region                        United States (us)
Web Interface                 http://localhost:4041
Forwarding                    tcp://0.tcp.ngrok.io:17992 -> localhost:3306

Connections                   ttl     opn     rt1     rt5     p50     p90
                              0       0       0.00    0.00    0.00    0.00

0.tcp.ngrok.io:17992是我唯一感兴趣的地方,3306是MySQL的唯一感兴趣的端口,也是我有兴趣公开与我的Google Colab链接的唯一端口。

因此,在一天结束时,我的PySpark READ连接看起来像:

jdbcDF = spark.read.format("jdbc")\
                    .option("url", "jdbc:mysql://0.tcp.ngrok.io:17992/mydb?user=testUser&password=pestpw")\
                    .option("dbtable", "pipeLineTable")\
                    .option("driver","com.mysql.cj.jdbc.Driver")\
                    .load(); 

WRITE连接将是:

jdbcDF.write.mode("overwrite")\
    .format("jdbc")\
    .option("url",  f"jdbc:mysql://0.tcp.ngrok.io:17992/mydb")\
    .option("dbtable", "fromGcTable")\
    .option("user", "testUser")\
    .option("password", "testpw")\
    .option("driver","com.mysql.cj.jdbc.Driver")\
    .save()

在两个连接字符串中,请注意替换0.tcp.ngrok.io:17992localhost:3306

© www.soinside.com 2019 - 2024. All rights reserved.