It is impossible to connect an app in Flask / App Engine to Google Cloud MySQL.
The settings seems correct.
Here are the libraries:
python 38
Flask-SQLAlchemy==2.5.1
pymysql==1.0.2
Here is the mydatabase.py:
unix_socket = "/cloudsql/{conn_name}".format(conn_name=DBCONNECTION)
SQLALCHEMY_DATABASE_URI = ("mysql+pymysql://{user}:{password}@/{database}?unix_socket={socket}").format(user=USER, password=PWD,database=SQLDB, socket=unix_socket)
db = SQLAlchemy()
Here is the main.py:
from mydatabase import db,SQLALCHEMY_DATABASE_URI
db.init_app(app)
The error is strange, it seems that the driver isn't working correctly:
"POST /signin HTTP/1.1" 500
2022-12-06 14:41:34 default[2022120025] [2022-12-06 14:41:34,165] ERROR in app: Exception on /signin [POST]
2022-12-06 14:41:34 default[2022120025] Traceback (most recent call last): File "/layers/google.python.pip/pip/lib/python3.8/site-packages/pymysql/connections.py", line 602, in connect sock.connect(self.unix_socket) ConnectionRefusedError: [Errno 111] Connection refused
2022-12-06 14:41:34 default[2022120025] During handling of the above exception, another exception occurred:
2022-12-06 14:41:34 default[2022120025] Traceback (most recent call last): File "/layers/google.python.pip/pip/lib/python3.8/site-packages/sqlalchemy/engine/base.py", line 3361, in _wrap_pool_connect return fn() File "/layers/google.python.pip/pip/lib/python3.8/site-packages/sqlalchemy/pool/base.py", line 325, in connect return _ConnectionFairy._checkout(self) File "/layers/google.python.pip/pip/lib/python3.8/site-packages/sqlalchemy/pool/base.py", line 888, in _checkout fairy = _ConnectionRecord.checkout(pool) File "/layers/google.python.pip/pip/lib/python3.8/site-packages/sqlalchemy/pool/base.py", line 491, in checkout rec = pool._do_get() File "/layers/google.python.pip/pip/lib/python3.8/site-packages/sqlalchemy/pool/impl.py", line 146, in _do_get self._dec_overflow() File "/layers/google.python.pip/pip/lib/python3.8/site-packages/sqlalchemy/util/langhelpers.py", line 70, in __exit__ compat.raise_( File "/layers/google.python.pip/pip/lib/python3.8/site-packages/sqlalchemy/util/compat.py", line 210, in raise_ raise exception File "/layers/google.python.pip/pip/lib/python3.8/site-packages/sqlalchemy/pool/impl.py", line 143, in _do_get return self._create_connection() File "/layers/google.python.pip/pip/lib/python3.8/site-packages/sqlalchemy/pool/base.py", line 271, in _create_connection return _ConnectionRecord(self) File "/layers/google.python.pip/pip/lib/python3.8/site-packages/sqlalchemy/pool/base.py", line 386, in __init__ self.__connect() File "/layers/google.python.pip/pip/lib/python3.8/site-packages/sqlalchemy/pool/base.py", line 685, in __connect pool.logger.debug("Error on connect(): %s", e) File "/layers/google.python.pip/pip/lib/python3.8/site-packages/sqlalchemy/util/langhelpers.py", line 70, in __exit__ compat.raise_( File "/layers/google.python.pip/pip/lib/python3.8/site-packages/sqlalchemy/util/compat.py", line 210, in raise_ raise exception File "/layers/google.python.pip/pip/lib/python3.8/site-packages/sqlalchemy/pool/base.py", line 680, in __connect self.dbapi_connection = connection = pool._invoke_creator(self) File "/layers/google.python.pip/pip/lib/python3.8/site-packages/sqlalchemy/engine/create.py", line 578, in connect return dialect.connect(*cargs, **cparams) File "/layers/google.python.pip/pip/lib/python3.8/site-packages/sqlalchemy/engine/default.py", line 598, in connect return self.dbapi.connect(*cargs, **cparams) File "/layers/google.python.pip/pip/lib/python3.8/site-packages/pymysql/connections.py", line 353, in __init__ self.connect() File "/layers/google.python.pip/pip/lib/python3.8/site-packages/pymysql/connections.py", line 664, in connect raise exc pymysql.err.OperationalError: (2003, "Can't connect to MySQL server on 'localhost' ([Errno 111] Connection refused)")
I've added additional Cloud SQL admin rights to the Compute Engine default service account and the App Engine default service account, but it doesn't solve the problem.
I've also tried to use the method with the public IP adding the IP into the the authorized list using curl api.ipify.org, but it doesn't work.
In fact, there was initially some rights and install issue, so I've checked a function and installed again the main components.
I've solved the issue by using the shell command gcloud sql instances describe instance_name
that activates some admin rights and installing gcloud components install app-engine-python
that probably install everything correctly.
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.