如何使用Pyspark从googlecolab读写本地MySQL服务器8?

2024-04-25 05:27:57 发布

您现在位置:Python中文网/ 问答频道 /正文

我一直在尝试从MySQL服务器8.0.19在Windows10的localhost上用GoogleColab的pyspark写/读表,但失败了。也有很多类似的问题和一些建议的答案,但没有一个解决方案在这里起作用。这是我的密码:

    <...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服务器是不可访问的。 我已通过Telnet连接到3306端口&;它确认MySQL服务器正在接受来自客户端计算机的连接。我已经读到运行:netsh advfirewall firewall add rule name="MySQL Server" action=allow protocol=TCP dir=in localport=3306将允许MySQL服务器的防火墙规则,以防它被阻止,但没有任何更改

有人能帮我吗


Tags: from服务器comdrivermysqlloadjavaspark
3条回答

下面是我如何在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

经过几天的试验,我发现了一个解决方案,这就是为什么我要回答我自己的问题。我可以使用WAMP服务器连接(感谢@Shubham Jain的建议),也可以不用WAMP服务器。这个答案没有WAMP服务器

https://ngrok.com/下载ngrok
解压它,
将其保存在本地窗口上,
用以下内容编写./ngrok authtoken xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
(网站上提供了非常直接的说明)

仍然在本地窗口上,我复制并在命令行上运行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链接的端口

因此,在一天结束时,我的Pypark 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(); 

写入连接将是:

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()

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

您正在尝试将安装在本地计算机(即windows 10)上的mysql数据库从google colab连接为本地主机实例

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

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

!apt-get -y install mysql-server

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

相关问题 更多 >