用googlesql连接Python

2024-05-23 16:25:19 发布

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

我需要将Python脚本与googlesql连接起来。 我已经用googlesql创建了数据库,现在想用python脚本连接它。你知道吗

到目前为止,我正在尝试使用这个:

conn = pymysql.connect(host='ip.ip.ipi.ip',user='user', passwd = "passwd", db = 'mysql',
charset = 'utf8')
cur = conn.cursor()
cur.execute("USE Example_Database")

但是得到这个错误:

OperationalError: (2003, "Can't connect to MySQL server on '35.246.235.61' (timed out)")

也尝试使用此功能,但不起作用:

from google.cloud.sql.connector import connector


connector.connect(
    "de-omd-data-and-tech:europe-west3:insight-planning-dus", 
    "mysql-connector",
    host='ip.ip.ipi.ip',
    user="user",
    password="passwd",
    db="Database"

)

Tags: ip脚本数据库hostdbconnectorconnectmysql
2条回答

运行代码的地方似乎存在到googlesql的连接问题。 你在哪里运行你的python代码?你知道吗

你能检查一下它是否能连接到3306端口(或者你正在使用的自定义端口)上的googlesql端点吗?你知道吗

telnet <Google_SQL_IP> 3306

根据python代码的宿主位置,您需要允许应用服务器访问云sql。你知道吗

You can connect to a Cloud SQL instance using the following methods:

  1. By using the proxy (Second Generation instances only)
  2. By configuring access for one or more public IP addresses
  3. By using the JDBC Socket Factory (for the Java programming language, Second Generation instances only)
  4. By using the Cloud SQL Proxy library (for the Go programming language, Second Generation instances only)

请查看以下链接,该链接描述了供您连接的选项: https://cloud.google.com/sql/docs/mysql/connect-external-app

相关问题 更多 >