Flask deployment

195 views
Skip to first unread message

Samuel Tremblay

unread,
Aug 29, 2021, 4:13:41 PM8/29/21
to Google App Engine
Hi everyone, 

I am creating a website using Flask. It is currently working on my local server. Locally, my cloud SQL mysql database is connected to my project and there is no problem. (I authorized my IP to get it to work). However, I am getting a 500 internal server error when I deploy my app. 

Looking at the logs, I can see that I am getting errors connecting to the database. I am probably not doing it correctly for production

The logged error is the following : 

"Traceback (most recent call last): File "/layers/google.python.pip/pip/lib/python3.9/site-packages/flask/app.py", line 2070, in wsgi_app response = self.full_dispatch_request() File "/layers/google.python.pip/pip/lib/python3.9/site-packages/flask/app.py", line 1515, in full_dispatch_request rv = self.handle_user_exception(e) File "/layers/google.python.pip/pip/lib/python3.9/site-packages/flask/app.py", line 1513, in full_dispatch_request rv = self.dispatch_request() File "/layers/google.python.pip/pip/lib/python3.9/site-packages/flask/app.py", line 1499, in dispatch_request return self.ensure_sync(self.view_functions[rule.endpoint])(**req.view_args) File "/srv/main.py", line 181, in home for item in Blog_Post.query.order_by(Blog_Post.id.desc()).limit(NUMBER_RESULTS): File "/layers/google.python.pip/pip/lib/python3.9/site-packages/sqlalchemy/orm/query.py", line 2840, in __iter__ return self._iter().__iter__() File "/layers/google.python.pip/pip/lib/python3.9/site-packages/sqlalchemy/orm/query.py", line 2847, in _iter result = self.session.execute( File "/layers/google.python.pip/pip/lib/python3.9/site-packages/sqlalchemy/orm/session.py", line 1688, in execute conn = self._connection_for_bind(bind) File "/layers/google.python.pip/pip/lib/python3.9/site-packages/sqlalchemy/orm/session.py", line 1529, in _connection_for_bind return self._transaction._connection_for_bind( File "/layers/google.python.pip/pip/lib/python3.9/site-packages/sqlalchemy/orm/session.py", line 747, in _connection_for_bind conn = bind.connect() File "/layers/google.python.pip/pip/lib/python3.9/site-packages/sqlalchemy/engine/base.py", line 3166, in connect return self._connection_cls(self, close_with_result=close_with_result) File "/layers/google.python.pip/pip/lib/python3.9/site-packages/sqlalchemy/engine/base.py", line 96, in __init__ else engine.raw_connection() File "/layers/google.python.pip/pip/lib/python3.9/site-packages/sqlalchemy/engine/base.py", line 3245, in raw_connection return self._wrap_pool_connect(self.pool.connect, _connection) File "/layers/google.python.pip/pip/lib/python3.9/site-packages/sqlalchemy/engine/base.py", line 3215, in _wrap_pool_connect Connection._handle_dbapi_exception_noconnection( File "/layers/google.python.pip/pip/lib/python3.9/site-packages/sqlalchemy/engine/base.py", line 2069, in _handle_dbapi_exception_noconnection util.raise_( File "/layers/google.python.pip/pip/lib/python3.9/site-packages/sqlalchemy/util/compat.py", line 207, in raise_ raise exception File "/layers/google.python.pip/pip/lib/python3.9/site-packages/sqlalchemy/engine/base.py", line 3212, in _wrap_pool_connect return fn() File "/layers/google.python.pip/pip/lib/python3.9/site-packages/sqlalchemy/pool/base.py", line 307, in connect return _ConnectionFairy._checkout(self) File "/layers/google.python.pip/pip/lib/python3.9/site-packages/sqlalchemy/pool/base.py", line 767, in _checkout fairy = _ConnectionRecord.checkout(pool) File "/layers/google.python.pip/pip/lib/python3.9/site-packages/sqlalchemy/pool/base.py", line 425, in checkout rec = pool._do_get() File "/layers/google.python.pip/pip/lib/python3.9/site-packages/sqlalchemy/pool/impl.py", line 146, in _do_get self._dec_overflow() File "/layers/google.python.pip/pip/lib/python3.9/site-packages/sqlalchemy/util/langhelpers.py", line 70, in __exit__ compat.raise_( File "/layers/google.python.pip/pip/lib/python3.9/site-packages/sqlalchemy/util/compat.py", line 207, in raise_ raise exception File "/layers/google.python.pip/pip/lib/python3.9/site-packages/sqlalchemy/pool/impl.py", line 143, in _do_get return self._create_connection() File "/layers/google.python.pip/pip/lib/python3.9/site-packages/sqlalchemy/pool/base.py", line 253, in _create_connection return _ConnectionRecord(self) File "/layers/google.python.pip/pip/lib/python3.9/site-packages/sqlalchemy/pool/base.py", line 368, in __init__ self.__connect() File "/layers/google.python.pip/pip/lib/python3.9/site-packages/sqlalchemy/pool/base.py", line 611, in __connect pool.logger.debug("Error on connect(): %s", e) File "/layers/google.python.pip/pip/lib/python3.9/site-packages/sqlalchemy/util/langhelpers.py", line 70, in __exit__ compat.raise_( File "/layers/google.python.pip/pip/lib/python3.9/site-packages/sqlalchemy/util/compat.py", line 207, in raise_ raise exception File "/layers/google.python.pip/pip/lib/python3.9/site-packages/sqlalchemy/pool/base.py", line 605, in __connect connection = pool._invoke_creator(self) File "/layers/google.python.pip/pip/lib/python3.9/site-packages/sqlalchemy/engine/create.py", line 578, in connect return dialect.connect(*cargs, **cparams) File "/layers/google.python.pip/pip/lib/python3.9/site-packages/sqlalchemy/engine/default.py", line 584, in connect return self.dbapi.connect(*cargs, **cparams) File "/layers/google.python.pip/pip/lib/python3.9/site-packages/MySQLdb/__init__.py", line 130, in Connect return Connection(*args, **kwargs) File "/layers/google.python.pip/pip/lib/python3.9/site-packages/MySQLdb/connections.py", line 185, in __init__ super().__init__(*args, **kwargs2) sqlalchemy.exc.OperationalError: (MySQLdb._exceptions.OperationalError) (2003, "Can't connect to MySQL server on '34.152.7.115' (110)")"
timestamp: "2021-08-28T18:50:33.109645Z"

The way I tried to configure the connection was using the following code in my main.py file :

database_name = 'edge_test'

app.config['SQLALCHEMY_DATABASE_URI'] = f'mysql://root:password@database_ip/{database_name}'
app.config['SQLALCHEMY_TRACK_MODIFICATIONS'] = False

db = SQLAlchemy(app)

db_config = {
    "pool_size": 5,
    "max_overflow": 2,
    "pool_timeout": 30,
    "pool_recycle": 1800
}

db_socket_dir = '/cloudsql'
db_connection_name = 'edge-321919:northamerica-northeast1:sam-edge-mysql'

pool = create_engine(
    # Equivalent URL:
    # mysql+pymysql://<db_user>:<db_pass>@/<db_name>?unix_socket=<socket_path>/<cloud_sql_instance_name>
    engine.url.URL.create(
        drivername="mysql",
        username='root', 
        password='MSSam20fred18', 
        database=database_name,  
        query={
            "unix_socket": "{}/{}".format(
                db_socket_dir,  # e.g. "/cloudsql"
                db_connection_name)  
        }
    ),
    **db_config
)

Any ideas why I am getting the error ?

Thanks a lot in advance, I am very new to this and there is so much to learn it is pretty overwhelming !

Sam

NoCommandLine

unread,
Oct 5, 2021, 11:37:17 AM10/5/21
to Google App Engine
Hi,

This type of question is better handled on Stackoverflow. Just tag it with Google App Engine and any other relevant tags.

..... NoCommandLine ......
 https://nocommandline.com

A GUI for Google App Engine

wesley chun

unread,
Oct 5, 2021, 9:48:35 PM10/5/21
to google-a...@googlegroups.com
I also 2nd the notion of searching on SO as this question has likely already been asked by at least a dozen other developers. I'd tag google-app-engine , google-cloud-sql and perhaps google-cloud-platform for starters. I also suggest looking at this page in the App Engine docs which was made specifically for Cloud SQL usage, and in particular, if you're not using a public IP, you need to setup Serverless VPC Access.

Cheers,
--Wesley

--
You received this message because you are subscribed to the Google Groups "Google App Engine" group.
To unsubscribe from this group and stop receiving emails from it, send an email to google-appengi...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/google-appengine/003e4aec-c160-4255-823c-6a4fe788917fn%40googlegroups.com.


--
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
"A computer never does what you want... only what you tell it."
    wesley chun :: @wescpy :: Software Architect & Engineer
    Developer Advocate at Google Cloud by day; at night...
    Python training & consulting : http://CyberwebConsulting.com
    "Core Python" books : http://CorePython.com
    Python blog: http://wescpy.blogspot.com
Reply all
Reply to author
Forward
0 new messages