简体   繁体   中英

Can't connect to Google SQL in Flask app running locally

I'm running Python3 in Google App Engine Flexible with gunicorn and Flask . Deploying my app leads to a successful connection, however I can't seem to connect locally and given how slow/tedious it is to push new versions, I'd really like to be able to work from the local version.

Here's the error when I run: gunicorn main:application

Traceback (most recent call last):
File "/Users/XXXX/Desktop/Flexible/pymysql/connections.py", line 
571, in connect
sock.connect(self.unix_socket)
FileNotFoundError: [Errno 2] No such file or directory

During handling of the above exception, another exception occurred:

<stacktrace within pymysql/connection.py>

sqlalchemy.exc.OperationalError: (pymysql.err.OperationalError) (2003, 
"Can't connect to MySQL server on 'localhost' ([Errno 2] No such file 
or directory)") (Background on this error at: 
http://sqlalche.me/e/e3q8)
[2019-02-18 09:40:41 +1100] [93536] [INFO] Worker exiting (pid: 93536)
[2019-02-18 09:40:41 +1100] [93533] [INFO] Shutting down: Master
[2019-02-18 09:40:41 +1100] [93533] [INFO] Reason: Worker failed to boot.

I've set up my Google SQL to have a public IP and I've connected it to my own IP address but this doesn't seem to have changed anything.

This is my code:

engine = create_engine('mysql+pymysql://<USER>:<PASSWORD>@/<DBNAME>?unix_socket=/cloudsql/<INSTANCE_NAME>')
connection = engine.connect()

Any help would be great thanks :)

The traceback you've included makes it look like you're trying to connect to a local Unix socket. This will only work if you have your database running on the same machine as your application code.

Instead, you'll need either connect via the public IP address, or use Cloud SQL Proxy (which is likely the easiest method).

See:https://cloud.google.com/python/getting-started/using-cloud-sql

Since your database is hosted on Cloud SQL, setting a Cloud SQL Proxy is an easy method to access it while running your app on your local machine.

This link provides you with some details about Cloud SQL, with specific info about the Proxy in the beginning:https://cloud.google.com/python/getting-started/using-cloud-sql

Here is a useful quickstart on the setup and installation for local testing: https://cloud.google.com/sql/docs/mysql/quickstart-proxy-test

Make sure your Proxy is running and listening the whole time you are testing locally, otherwise you would get a “Connection refused” error.

Hope this helps, let me know if it all works out!

Cheers

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM