skip to Main Content
`db = MySQLdb.connect(
    host = '12.34.567.891', 
    user = 'root', 
    passwd = '', 
    db = 'testdb', 
    port = "something-that-works")`

Very Simple Can I somehow make it so that it connects only to the ip ‘12.34.567.891’. Google is forwarding the port to 80 but you can’t request port 80 or it ends up in an endless loop.

port=null or port = none will cause and error.

I have no issues connecting from my cli mysql client

Thank you,

I expected to be able to connect to the server no issues if I am able to do so from my cli – I need some way to send the connecting request to the raw IP no port. It may be possible python-mysql can’t do this

2

Answers


  1. 3306 is the default MySQL port and it seems that you are using MySQL, so that should work. https://cloud.google.com/sql/docs/mysql/connect-overview

    Login or Signup to reply.
  2. You will have an easier time connecting with the Cloud SQL Python Connector a library built purely for connecting to Cloud SQL with Python.

    Looks like this:

    from google.cloud.sql.connector import Connector
    
    # build connection
    def getconn() -> pymysql.connections.Connection:
        with Connector() as connector:
            conn = connector.connect(
                "project:region:instance",  # Cloud SQL instance connection name
                "pymysql",
                user="my-user",
                password="my-password",
                db="my-db-name"
            )
        return conn
    
    # create connection pool
    pool = sqlalchemy.create_engine(
        "mysql+pymysql://",
        creator=getconn,
    )
    
    # insert statement
    insert_stmt = sqlalchemy.text(
        "INSERT INTO my_table (id, title) VALUES (:id, :title)",
    )
    
    # interact with Cloud SQL database using connection pool
    with pool.connect() as db_conn:
        # insert into database
        db_conn.execute(insert_stmt, id="book1", title="Book One")
    
        # query database
        result = db_conn.execute("SELECT * from my_table").fetchall()
    
        # Do something with the results
        for row in result:
            print(row)
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search