【问题标题】:RuntimeError: OperationalError: (2003, Can't connect to MySQL server on 'IPaddress of the instance'RuntimeError: OperationalError: (2003, Can't connect to MySQL server on 'IPaddress of the instance'
【发布时间】:2018-11-30 21:39:38
【问题描述】:

我正在尝试运行 Python(version 2.7.1') 脚本,我正在使用 pymysql 包 从 CSV 文件将表创建到数据库中.

它在我的本地系统中正常运行,但是,在 Google Cloud Dataflow 中将相同的脚本作为管道的一部分运行时会出现问题。

我的 Python 函数如下:

class charge_to_db(beam.DoFn):
    def process(self, element):
        import pymysql

        with open(element, 'r') as f:
            data = f.read().decode("UTF-8")

        datalist = []
        for line in data.split('\n'):
            datalist.append(line.split(','))

        db = pymysql.connect(host='IPaddress', user='root', password='mypassword', database='stack_model')

        cursor = db.cursor()
        cursor.execute("DROP TABLE IF EXISTS stack_convergence")

        # create column names from the first line in fList
        up = "upper_bnd"
        primal = "primal"
        d = "dualit"
        gap = "gap_rel"
        teta = "teta"
        alpha = "alpha"
        imba = "imba_avg"
        price = "price_avg"
       # create STUDENT table // place a comma after each new column except the last
       queryCreateConvergenceTable = """CREATE TABLE stack_convergence(
                                {} float not null,
                                {} float not null,
                                {} float not null,
                                {} float not null,
                                {} float not null,
                                {} float not null,
                                {} float not null,
                                {} float not null )""".format(up, primal, d, gap, teta, alpha, imba, price)

        cursor.execute(queryCreateConvergenceTable)

在云中运行此功能时,我收到以下错误:

  RuntimeError: OperationalError: (2003, 'Can\'t connect to MySQL server on \'35.195.1.40\' (110 "Connection timed out")')

我不知道为什么会发生此错误,因为它在本地系统中正确运行,因此我可以从本地系统访问我的云 SQL 实例,但不能从云中的数据流访问。

为什么会出现这个错误?

【问题讨论】:

    标签: python mysql google-cloud-platform google-cloud-dataflow google-cloud-sql


    【解决方案1】:

    在 Dataflow 上,您无法将 IP 列入白名单以使 Dataflow 能够访问 SQL 实例。如果您使用 Java,最简单的方法是使用 JdbcIO / JDBC 套接字工厂。

    但是由于您使用的是 Python,因此使用 Python 特定的数据库连接工具来模拟 JdbcIO.read() 的实现会有所帮助。在更改一些 Cloud SQL 设置并添加相关的 python 代码后,有this related questionworkaround

    如果这看起来很复杂,您也可以export data from Cloud SQL to Cloud Storage,然后从 Cloud Storage 加载。

    【讨论】:

      猜你喜欢
      • 2013-05-04
      • 2021-12-16
      • 1970-01-01
      • 2021-06-25
      • 1970-01-01
      • 1970-01-01
      • 2019-12-29
      • 2016-04-08
      • 2020-10-23
      相关资源
      最近更新 更多