Yarh
Yarh

Reputation: 1098

Google App Engine and Cloud SQL: Lost connection to MySQL server at 'reading initial communication packet' SQL 2nd Gen

I'm getting an error similar to other posts in this subject. I tried switching from 1st gen to 2nd gen SQL server (both on us-central1), but it still doesn't work.

I copied my CLOUDSQL_PROJECT from the url on the top of my project.
I copied my CLOUDSQL_INSTANCE from the proprieties part in the SQL page.

In my main.py, I'm trying to run Google sample code, and it doesn't work (locally it does, of course):

if os.getenv('SERVER_SOFTWARE', '').startswith('Google App Engine/'):
        db = MySQLdb.connect(
            unix_socket='/cloudsql/{}:{}'.format(
                CLOUDSQL_PROJECT,
                CLOUDSQL_INSTANCE),
            user=user,passwd=password)
    # When running locally, you can either connect to a local running
    # MySQL instance, or connect to your Cloud SQL instance over TCP.
    else:
        db = MySQLdb.connect(host=host,user=user,passwd=password)
cursor = db.cursor()
cursor.execute('SHOW VARIABLES')

for r in cursor.fetchall():
    self.response.write('{}\n'.format(r))

Upvotes: 1

Views: 910

Answers (1)

Tony Tseng
Tony Tseng

Reputation: 479

The documentation is slightly outdated. You should be able to always use the "Instance connection name" property from the SQL properties page to construct the unix socket path; just append that value after the "/cloudsql/" prefix.

For second generation, the connection format is project:region:name. In your example, it maps to "hello-world-123:us-central1:sqlsomething3", and the unix socket path is "/cloudsql/hello-world-123:us-central1:sqlsomething3".

Upvotes: 2

Related Questions