Issue
I am fairly new to programming. Deployed a flask api to heroku, after creating Procfile and requirements.txt. The api connects to a mysql database also running on localhost. The api runs fine on localhost. I've been at it from 2 days. Please help me out.
Procfile:
web: gunicorn app:app
I've checked whether the procfile has extra spaces or not. Also tried the
web: gunicorn --bind 0.0.0.0:$PORT flaskapp:app
Also the requirements.txt file contains gunicorn
Complete logs --tail
2022-10-01T08:46:41.992202+00:00 heroku[web.1]: Starting process with command `gunicorn app:app`
2022-10-01T08:46:44.279446+00:00 app[web.1]: [2022-10-01 08:46:44 +0000] [4] [INFO] Starting gunicorn 20.1.0
2022-10-01T08:46:44.279831+00:00 app[web.1]: [2022-10-01 08:46:44 +0000] [4] [INFO] Listening at: http://0.0.0.0:16331 (4)
2022-10-01T08:46:44.279887+00:00 app[web.1]: [2022-10-01 08:46:44 +0000] [4] [INFO] Using worker: sync
2022-10-01T08:46:44.283663+00:00 app[web.1]: [2022-10-01 08:46:44 +0000] [9] [INFO] Booting worker with pid: 9
2022-10-01T08:46:44.374994+00:00 app[web.1]: [2022-10-01 08:46:44 +0000] [10] [INFO] Booting worker with pid: 10
2022-10-01T08:46:44.381067+00:00 heroku[web.1]: State changed from starting to up
2022-10-01T08:46:46.530064+00:00 app[web.1]: [2022-10-01 08:46:46 +0000] [9] [ERROR] Exception in worker process
2022-10-01T08:46:46.530083+00:00 app[web.1]: Traceback (most recent call last):
2022-10-01T08:46:46.530084+00:00 app[web.1]: File "/app/.heroku/python/lib/python3.10/site-packages/mysql/connector/connection_cext.py", line 263, in _open_connection
2022-10-01T08:46:46.530085+00:00 app[web.1]: self._cmysql.connect(**cnx_kwargs)
2022-10-01T08:46:46.530086+00:00 app[web.1]: _mysql_connector.MySQLInterfaceError: Can't connect to MySQL
server on 'localhost:3306' (111)
2022-10-01T08:46:46.530087+00:00 app[web.1]:
2022-10-01T08:46:46.530088+00:00 app[web.1]: The above exception was the direct cause of the following exception:
2022-10-01T08:46:46.530088+00:00 app[web.1]:
2022-10-01T08:46:46.530088+00:00 app[web.1]: Traceback (most recent call last):
2022-10-01T08:46:46.530089+00:00 app[web.1]: File "/app/.heroku/python/lib/python3.10/site-packages/gunicorn/arbiter.py", line 589, in spawn_worker
2022-10-01T08:46:46.530102+00:00 app[web.1]: worker.init_process()
2022-10-01T08:46:46.530102+00:00 app[web.1]: File "/app/.heroku/python/lib/python3.10/site-packages/gunicorn/workers/base.py", line 134, in init_process
2022-10-01T08:46:46.530103+00:00 app[web.1]: self.load_wsgi()
2022-10-01T08:46:46.530103+00:00 app[web.1]: File "/app/.heroku/python/lib/python3.10/site-packages/gunicorn/workers/base.py", line 146, in load_wsgi
2022-10-01T08:46:46.530103+00:00 app[web.1]: self.wsgi = self.app.wsgi()
2022-10-01T08:46:46.530104+00:00 app[web.1]: File "/app/.heroku/python/lib/python3.10/site-packages/gunicorn/app/base.py", line 67, in wsgi
2022-10-01T08:46:46.530104+00:00 app[web.1]: self.callable = self.load()
2022-10-01T08:46:46.530104+00:00 app[web.1]: File "/app/.heroku/python/lib/python3.10/site-packages/gunicorn/app/wsgiapp.py", line 58, in load
2022-10-01T08:46:46.530104+00:00 app[web.1]: return self.load_wsgiapp()
2022-10-01T08:46:46.530105+00:00 app[web.1]: File "/app/.heroku/python/lib/python3.10/site-packages/gunicorn/app/wsgiapp.py", line 48, in load_wsgiapp
2022-10-01T08:46:46.530105+00:00 app[web.1]: return util.import_app(self.app_uri)
2022-10-01T08:46:46.530105+00:00 app[web.1]: File "/app/.heroku/python/lib/python3.10/site-packages/gunicorn/util.py", line 359, in import_app
2022-10-01T08:46:46.530105+00:00 app[web.1]: mod = importlib.import_module(module)
2022-10-01T08:46:46.530106+00:00 app[web.1]: File "/app/.heroku/python/lib/python3.10/importlib/__init__.py", line 126, in import_module
2022-10-01T08:46:46.530107+00:00 app[web.1]: return _bootstrap._gcd_import(name[level:], package, level)
2022-10-01T08:46:46.530107+00:00 app[web.1]: File "<frozen importlib._bootstrap>", line 1050, in _gcd_import
2022-10-01T08:46:46.530107+00:00 app[web.1]: File "<frozen importlib._bootstrap>", line 1027, in _find_and_load
2022-10-01T08:46:46.530108+00:00 app[web.1]: File "<frozen importlib._bootstrap>", line 1006, in _find_and_load_unlocked
2022-10-01T08:46:46.530108+00:00 app[web.1]: File "<frozen importlib._bootstrap>", line 688, in _load_unlocked
2022-10-01T08:46:46.530108+00:00 app[web.1]: File "<frozen importlib._bootstrap_external>", line 883, in exec_module
2022-10-01T08:46:46.530108+00:00 app[web.1]: File "<frozen importlib._bootstrap>", line 241, in _call_with_frames_removed
2022-10-01T08:46:46.530109+00:00 app[web.1]: File "/app/app.py", line 8, in <module>
2022-10-01T08:46:46.530109+00:00 app[web.1]: cnx = mysql.connector.connect(user='root',
2022-10-01T08:46:46.530109+00:00 app[web.1]: File "/app/.heroku/python/lib/python3.10/site-packages/mysql/connector/pooling.py", line 286, in connect
2022-10-01T08:46:46.530109+00:00 app[web.1]: return CMySQLConnection(*args, **kwargs)
2022-10-01T08:46:46.530110+00:00 app[web.1]: File "/app/.heroku/python/lib/python3.10/site-packages/mysql/connector/connection_cext.py", line 101, in __init__
2022-10-01T08:46:46.530110+00:00 app[web.1]: self.connect(**kwargs)
2022-10-01T08:46:46.530110+00:00 app[web.1]: File "/app/.heroku/python/lib/python3.10/site-packages/mysql/connector/abstracts.py", line 1095, in connect
2022-10-01T08:46:46.530110+00:00 app[web.1]: self._open_connection()
2022-10-01T08:46:46.530111+00:00 app[web.1]: File "/app/.heroku/python/lib/python3.10/site-packages/mysql/connector/connection_cext.py", line 268, in _open_connection
2022-10-01T08:46:46.530111+00:00 app[web.1]: raise get_mysql_exception(
2022-10-01T08:46:46.530111+00:00 app[web.1]: mysql.connector.errors.DatabaseError: 2003 (HY000): Can't connect to MySQL server on 'localhost:3306' (111)
2022-10-01T08:46:46.531937+00:00 app[web.1]: [2022-10-01 08:46:46 +0000] [10] [ERROR] Exception in worker
process
2022-10-01T08:46:46.531938+00:00 app[web.1]: Traceback (most recent call last):
2022-10-01T08:46:46.531939+00:00 app[web.1]: File "/app/.heroku/python/lib/python3.10/site-packages/mysql/connector/connection_cext.py", line 263, in _open_connection
2022-10-01T08:46:46.531939+00:00 app[web.1]: self._cmysql.connect(**cnx_kwargs)
2022-10-01T08:46:46.531940+00:00 app[web.1]: _mysql_connector.MySQLInterfaceError: Can't connect to MySQL
server on 'localhost:3306' (111)
2022-10-01T08:46:46.531940+00:00 app[web.1]:
2022-10-01T08:46:46.531940+00:00 app[web.1]: The above exception was the direct cause of the following exception:
2022-10-01T08:46:46.531941+00:00 app[web.1]:
2022-10-01T08:46:46.531941+00:00 app[web.1]: Traceback (most recent call last):
2022-10-01T08:46:46.531942+00:00 app[web.1]: File "/app/.heroku/python/lib/python3.10/site-packages/gunicorn/arbiter.py", line 589, in spawn_worker
2022-10-01T08:46:46.531943+00:00 app[web.1]: worker.init_process()
2022-10-01T08:46:46.531943+00:00 app[web.1]: File "/app/.heroku/python/lib/python3.10/site-packages/gunicorn/workers/base.py", line 134, in init_process
2022-10-01T08:46:46.531944+00:00 app[web.1]: self.load_wsgi()
2022-10-01T08:46:46.531944+00:00 app[web.1]: File "/app/.heroku/python/lib/python3.10/site-packages/gunicorn/workers/base.py", line 146, in load_wsgi
2022-10-01T08:46:46.531944+00:00 app[web.1]: self.wsgi = self.app.wsgi()
2022-10-01T08:46:46.531944+00:00 app[web.1]: File "/app/.heroku/python/lib/python3.10/site-packages/gunicorn/app/base.py", line 67, in wsgi
2022-10-01T08:46:46.531945+00:00 app[web.1]: self.callable = self.load()
2022-10-01T08:46:46.531945+00:00 app[web.1]: File "/app/.heroku/python/lib/python3.10/site-packages/gunicorn/app/wsgiapp.py", line 58, in load
2022-10-01T08:46:46.531945+00:00 app[web.1]: return self.load_wsgiapp()
2022-10-01T08:46:46.531945+00:00 app[web.1]: File "/app/.heroku/python/lib/python3.10/site-packages/gunicorn/app/wsgiapp.py", line 48, in load_wsgiapp
2022-10-01T08:46:46.531946+00:00 app[web.1]: return util.import_app(self.app_uri)
2022-10-01T08:46:46.531946+00:00 app[web.1]: File "/app/.heroku/python/lib/python3.10/site-packages/gunicorn/util.py", line 359, in import_app
2022-10-01T08:46:46.531946+00:00 app[web.1]: mod = importlib.import_module(module)
2022-10-01T08:46:46.531946+00:00 app[web.1]: File "/app/.heroku/python/lib/python3.10/importlib/__init__.py", line 126, in import_module
2022-10-01T08:46:46.531947+00:00 app[web.1]: return _bootstrap._gcd_import(name[level:], package, level)
2022-10-01T08:46:46.531947+00:00 app[web.1]: File "<frozen importlib._bootstrap>", line 1050, in _gcd_import
2022-10-01T08:46:46.531947+00:00 app[web.1]: File "<frozen importlib._bootstrap>", line 1027, in _find_and_load
2022-10-01T08:46:46.531948+00:00 app[web.1]: File "<frozen importlib._bootstrap>", line 1006, in _find_and_load_unlocked
2022-10-01T08:46:46.531948+00:00 app[web.1]: File "<frozen importlib._bootstrap>", line 688, in _load_unlocked
2022-10-01T08:46:46.531948+00:00 app[web.1]: File "<frozen importlib._bootstrap_external>", line 883, in exec_module
2022-10-01T08:46:46.531949+00:00 app[web.1]: File "<frozen importlib._bootstrap>", line 241, in _call_with_frames_removed
2022-10-01T08:46:46.531949+00:00 app[web.1]: File "/app/app.py", line 8, in <module>
2022-10-01T08:46:46.531949+00:00 app[web.1]: cnx = mysql.connector.connect(user='root',
2022-10-01T08:46:46.531950+00:00 app[web.1]: File "/app/.heroku/python/lib/python3.10/site-packages/mysql/connector/pooling.py", line 286, in connect
2022-10-01T08:46:46.531950+00:00 app[web.1]: return CMySQLConnection(*args, **kwargs)
2022-10-01T08:46:46.531950+00:00 app[web.1]: File "/app/.heroku/python/lib/python3.10/site-packages/mysql/connector/connection_cext.py", line 101, in __init__
2022-10-01T08:46:46.531950+00:00 app[web.1]: self.connect(**kwargs)
2022-10-01T08:46:46.531951+00:00 app[web.1]: File "/app/.heroku/python/lib/python3.10/site-packages/mysql/connector/abstracts.py", line 1095, in connect
2022-10-01T08:46:46.531951+00:00 app[web.1]: self._open_connection()
2022-10-01T08:46:46.531951+00:00 app[web.1]: File "/app/.heroku/python/lib/python3.10/site-packages/mysql/connector/connection_cext.py", line 268, in _open_connection
2022-10-01T08:46:46.531951+00:00 app[web.1]: raise get_mysql_exception(
2022-10-01T08:46:46.531951+00:00 app[web.1]: mysql.connector.errors.DatabaseError: 2003 (HY000): Can't connect to MySQL server on 'localhost:3306' (111)
2022-10-01T08:46:46.532126+00:00 app[web.1]: [2022-10-01 08:46:46 +0000] [10] [INFO] Worker exiting (pid:
10)
2022-10-01T08:46:47.012587+00:00 app[web.1]: [2022-10-01 08:46:47 +0000] [9] [INFO] Worker exiting (pid: 9)
2022-10-01T08:46:47.177705+00:00 app[web.1]: [2022-10-01 08:46:47 +0000] [4] [WARNING] Worker with pid 9 was terminated due to signal 15
2022-10-01T08:46:47.272054+00:00 app[web.1]: [2022-10-01 08:46:47 +0000] [4] [INFO] Shutting down: Master
2022-10-01T08:46:47.272197+00:00 app[web.1]: [2022-10-01 08:46:47 +0000] [4] [INFO] Reason: Worker failed
to boot.
2022-10-01T08:46:47.479778+00:00 heroku[web.1]: Process exited with status 3
2022-10-01T08:46:47.541145+00:00 heroku[web.1]: State changed from up to crashed
2022-10-01T08:51:18.453575+00:00 heroku[router]: at=error code=H10 desc="App crashed" method=GET path="/"
host=irtiza-api4.herokuapp.com request_id=18135c23-adbc-416d-a329-91874b257899 fwd="39.34.142.164" dyno= connect= service= status=503 bytes= protocol=https
2022-10-01T08:51:19.025738+00:00 heroku[router]: at=error code=H10 desc="App crashed" method=GET path="/favicon.ico" host=irtiza-api4.herokuapp.com request_id=a4f1be19-26ca-4397-a7d8-acf3f4ddb7d9 fwd="39.34.142.164" dyno= connect= service= status=503 bytes= protocol=https
Solution
The Flask deployment is fine (you can see in the logs it binds to a port), the error is here
_mysql_connector.MySQLInterfaceError: Can't connect to MySQL
Locally it is fine because it finds your local MySQL database which is not the case on Heroku.
Answered By - Beppe C
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.