通过 .csv 中的 MySql 将大量数据行插入云 sql table

Inserting large number of data rows into cloudsql table via PyMySql from .csv

我是 Cloud 的新手SQL,我正在尝试插入 40 多个不同列和超过 150 万行的记录。但是我无法在 Google CloudSQL 中执行此操作。我已经采取了下面列出的许多措施来解决这个问题,但我得到的主要错误是:

ERROR:
textPayload:  "2019-04-12T06:10:47.348295Z 8554 [Note] Aborted connection 8554 to db: 'xxxxx_xxx' user: 'root' host: 'x.x.x.x' (Got an error reading communication packets)"  

总结:

  1. 我正在使用 Python、PyMySql 将 150 万行数据插入 35 列的 table
  2. 实例、数据库、Table 已在云中创建SQL。
  3. 系统配置:vCPU - 4,内存 - 15 GB,SSD 存储 - 10 GB
  4. 我可以在我的本地系统中完全正常地加载这些数据。
  5. 在GoogleCloudSQl中,部署时间特别长,部署成功。
  6. 但是当我查看我的 table 时,它是空的。
  7. 实例中的 MySql 错误日志显示以上内容。

我已经尝试了以下操作:

def adddata():
    try:
        conn = pymysql.connect(unix_socket='/cloudsql/' + 'karto-235001:asia-east1:karto', user='xxx', password='xxx', db='xxx')
        cur = conn.cursor()
        insert_ = "INSERT INTO data_table(a, b, c) VALUES (%s, %s, %s)"
        with open('info.csv', newline='') as myFile:
            reader = csv.reader(myFile)
            for item in reader:
                cur.execute(insert_, (item[3], item[4], item[5]))
            conn.commit()
            cur.close()
    finally:
        conn.close()

我已在线查看并实施了 CloudSQL 和其他 stack-overflow 用户推荐的解决方案。如果有人能确定我做错了什么,或者我的代码或配置是否有问题?非常感谢。

我看到您想使用 Python 上传包含在 CSV 文件中的信息。您是否尝试过直接导入数据库?您可以按照 link[1].

中的步骤操作

与此同时,我会尝试复制您的案例。您可能还需要检查您的安装和配置是否正确。

验证您的云 SQL 实例和连接 [2] 以及您是否 Python 安装[3]。

[1]https://cloud.google.com/sql/docs/mysql/import-export/importing#csv
[2]https://cloud.google.com/sql/docs/mysql/connect-compute-engine
[3]https://cloud.google.com/python/setup