s_khan92
s_khan92

Reputation: 979

Connecting Python with Google SQL

I need to connect the Python script with google sql. I have created the database in google sql and now want to connect it with python script.

I am trying to use this so far:

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

but getting this error:

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

Also tried to use this but not working:

from google.cloud.sql.connector import connector


connector.connect(
    "instance:instancename", 
    "mysql-connector",
    host='ip.ip.ipi.ip',
    user="user",
    password="passwd",
    db="Database"

)

Upvotes: 0

Views: 1264

Answers (2)

Parth Mehta
Parth Mehta

Reputation: 1927

Depending on where your python code is hosted you need to allow necessary access for your app server to cloud 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)

Please review the following link which describes options for you to connect: https://cloud.google.com/sql/docs/mysql/connect-external-app

Upvotes: 1

Pradeep Bhadani
Pradeep Bhadani

Reputation: 4741

It seems there is connectivity issue to Google SQL from where you run the code. Where are you running your python code?

Can you check if it can connec to you Google SQL endpoint on port 3306 (or custom port you are using)?

telnet <Google_SQL_IP> 3306

Upvotes: 2

Related Questions