0

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"

)

2 Answers 2

2

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
Sign up to request clarification or add additional context in comments.

Comments

1

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

2 Comments

Its simply on my local device.
Assuming your local device is on a home network which does not have a fixed / static IP address then you'll have to open up your cloud sql to public as suggested on this link: cloud.google.com/sql/docs/mysql/configure-ip

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.