Collectives™ on Stack Overflow
Find centralized, trusted content and collaborate around the technologies you use most.
Learn more about Collectives
Teams
Q&A for work
Connect and share knowledge within a single location that is structured and easy to search.
Learn more about Teams
Ask Question
def main(data, context):
log = logging.getLogger("course_gen")
db = sqlalchemy.create_engine(
sqlalchemy.engine.url.URL(
drivername="mysql+pymysql",
username=os.environ.get("DB_USER"),
password=os.environ.get("DB_PASS"),
host="**.***.**.***", # this is actually the public IP of my cloud mysql instance
port=3306,
database="table_name"
pool_size=5,
max_overflow=2,
pool_timeout=30,
pool_recycle=1800
with db.connect() as cursor:
start_time = perf_counter()
if __name__ == '__main__':
main('data', 'context')
and here is the corresponding overview of my Cloud MySQL instance from which I copied the IP:
the
port
kwarg was a bit confusing but from what I've inferred from posts like
this
, it's always 3306.
Basically when I run my cloud function locally, I expect it to be able to connect to the live GCP MySQL instance I have provisioned but the full error I'm getting is:
sqlalchemy.exc.OperationalError: (pymysql.err.OperationalError) (2003, "Can't connect to MySQL server on (timed out)")
So I actually figured this out while I was doing some research - basically I had to follow this guide:
https://cloud.google.com/sql/docs/mysql/quickstart-proxy-test#windows-64-bit
to set up what's basically some sort of local running proxy on my personal machine
(looks like this)
$ ./cloud_sql_proxy -instances=********:us-east1:********=tcp:3306
2020/05/27 22:36:06 Listening on 127.0.0.1:3306 for ********:us-east1:********
2020/05/27 22:36:06 Ready for new connections
2020/05/27 22:37:05 New connection for "********:us-east1:********"
2020/05/27 22:37:05 Client closed local connection on 127.0.0.1:3306
and set the host to localhost
, or 127.0.0.1
, which through the magic of proxies eventually ends up hitting the real cloud MySQL instance. Voila no more errors.
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.