连接 MySQL 时出错:2003:使用 colab 时无法连接到 '127.0.0.1:3310' 上的 MySQL 服务器(111 连接被拒绝),VSCODE 工作正常

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

我使用 VS Code 来处理我的代码,但是当我使用 colab 时,它无法连接到 mysql,但它与 VS Code 的连接和执行良好。

def process_row():
    try:
        connection = mysql.connector.connect(host="127.0.0.1", user="root", password="", database="assignment1", port="3310")
        cursor = connection.cursor()

        query = "SELECT * FROM temp_card_new;"
        cursor.execute(query)

        rows = cursor.fetchall()
        connection.close()

        return rows

    except mysql.connector.Error as error:
        print("Error while connecting to MySQL:", error)
        return []

错误:连接到 MySQL 时出错:2003:无法连接到“127.0.0.1:3310”上的 MySQL 服务器(111 连接被拒绝)

我已经尝试了所有可能的方法,但我无法理解为什么会发生这种情况。

python mysql database-connection
1个回答
0
投票

Google Colab 不在您的计算机上运行,而是在 Google 服务器上运行。当您尝试连接到 127.0.0.1 又名“localhost”时,您的代码会尝试连接到 *在执行 colab 代码的同一服务器上运行的 mysql”,并且由于它不在该计算机上,因此会失败。

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