与 PostgresSQL 数据库的连接在 cPanel Flask 应用程序中关闭

问题描述 投票:0回答:1

我开发了一个简单的 Flask 网站,它使用远程 PostgreSQL 数据库。我使用

sqlalchemy
来定义数据库,并使用
pyscopg
来连接到数据库。我将数据库 URL、用户名和密码存储在 .env 文件中,然后将其加载到我的
app.py
文件中。当我在笔记本电脑上本地运行此程序时,一切正常并且可以连接到数据库。

本地

app.py
文件:

from web import create_app
from web.forms import csrf
from database.config import DevConfig, ProdConfig
from database import init_db

config = DevConfig

application = create_app(config)
csrf.init_app(application)
init_db(application, config)

if __name__ == "__main__":
  application.run(debug=True, use_debugger=False, use_reloader=False)

init_db
是创建与数据库的连接的函数。

from sqlalchemy import create_engine
from sqlalchemy.orm import sessionmaker, scoped_session, Session

def init_db(app, config):
    engine = create_engine(
        config.DATABASE_URI,
        echo=False,
    )
    app.db_session = scoped_session(sessionmaker(engine))

我现在正在尝试在使用 cPanel 的虚拟主机服务器上设置

flask
网站,请按照此处的说明操作:https://docs.cloudlinux.com/shared/lve_manager/#python-selector-client-插件

对于不连接到工作正常的数据库的简单烧瓶应用程序。但是我无法让我的设置正常工作。

我已从 app.py 中删除了

if __name__ == "__main__":
块,并使用启动应用程序时默认创建的以下
passenger_wsgi.py

import imp
import os
import sys


sys.path.insert(0, os.path.dirname(__file__))

wsgi = imp.load_source('wsgi', 'app.py')
application = wsgi.application

这最初似乎有效,网站出现在 URL 上,页面呈现,一切看起来都很棒。但是,当我转到查询数据库以返回数据表的网站页面之一时,应用程序崩溃并终止,并显示以下消息“从应用程序收到的响应不完整”。

检查 cPanel 中的

passenger.log
文件,我得到了错误的堆栈跟踪,该错误相当长,但本质上表明连接已关闭。

App 131002 output: /opt/cpanel/ea-ruby27/root/usr/share/passenger/helper-scripts/wsgi-loader.py:26: DeprecationWarning: the imp module is deprecated in favour of importlib and slated for removal in Python 3.12; see the module's documentation for alternative uses
App 131002 output:   import sys, os, re, imp, threading, signal, traceback, socket, select, struct, logging, errno
App 131002 output: [ pid=131002, time=2023-11-02 17:07:56,254 ]: WSGI application raised an exception!
App 131002 output: Traceback (most recent call last):
App 131002 output:   File "/home/db/virtualenv/dbsite/3.11/lib64/python3.11/site-packages/sqlalchemy/engine/base.py", line 145, in __init__
App 131002 output:     self._dbapi_connection = engine.raw_connection()
App 131002 output:                              ^^^^^^^^^^^^^^^^^^^^^^^
App 131002 output:   File "/home/db/virtualenv/dbsite/3.11/lib64/python3.11/site-packages/sqlalchemy/engine/base.py", line 3293, in raw_connection
App 131002 output:     return self.pool.connect()
App 131002 output:            ^^^^^^^^^^^^^^^^^^^
App 131002 output:   File "/home/db/virtualenv/dbsite/3.11/lib64/python3.11/site-packages/sqlalchemy/pool/base.py", line 452, in connect
App 131002 output:     return _ConnectionFairy._checkout(self)
App 131002 output:            ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
App 131002 output:   File "/home/db/virtualenv/dbsite/3.11/lib64/python3.11/site-packages/sqlalchemy/pool/base.py", line 1268, in _checkout
App 131002 output:     fairy = _ConnectionRecord.checkout(pool)
App 131002 output:             ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
App 131002 output:   File "/home/db/virtualenv/dbsite/3.11/lib64/python3.11/site-packages/sqlalchemy/pool/base.py", line 716, in checkout
App 131002 output:     rec = pool._do_get()
App 131002 output:           ^^^^^^^^^^^^^^
App 131002 output:   File "/home/db/virtualenv/dbsite/3.11/lib64/python3.11/site-packages/sqlalchemy/pool/impl.py", line 168, in _do_get
App 131002 output:     with util.safe_reraise():
App 131002 output:   File "/home/db/virtualenv/dbsite/3.11/lib64/python3.11/site-packages/sqlalchemy/util/langhelpers.py", line 147, in __exit__
App 131002 output:     raise exc_value.with_traceback(exc_tb)
App 131002 output:   File "/home/db/virtualenv/dbsite/3.11/lib64/python3.11/site-packages/sqlalchemy/pool/impl.py", line 166, in _do_get
App 131002 output:     return self._create_connection()
App 131002 output:            ^^^^^^^^^^^^^^^^^^^^^^^^^
App 131002 output:   File "/home/db/virtualenv/dbsite/3.11/lib64/python3.11/site-packages/sqlalchemy/pool/base.py", line 393, in _create_connection
App 131002 output:     return _ConnectionRecord(self)
App 131002 output:            ^^^^^^^^^^^^^^^^^^^^^^^
App 131002 output:   File "/home/db/virtualenv/dbsite/3.11/lib64/python3.11/site-packages/sqlalchemy/pool/base.py", line 678, in __init__
App 131002 output:     self.__connect()
App 131002 output:   File "/home/db/virtualenv/dbsite/3.11/lib64/python3.11/site-packages/sqlalchemy/pool/base.py", line 902, in __connect
App 131002 output:     with util.safe_reraise():
App 131002 output:   File "/home/db/virtualenv/dbsite/3.11/lib64/python3.11/site-packages/sqlalchemy/util/langhelpers.py", line 147, in __exit__
App 131002 output:     raise exc_value.with_traceback(exc_tb)
App 131002 output:   File "/home/db/virtualenv/dbsite/3.11/lib64/python3.11/site-packages/sqlalchemy/pool/base.py", line 898, in __connect
App 131002 output:     self.dbapi_connection = connection = pool._invoke_creator(self)
App 131002 output:                                          ^^^^^^^^^^^^^^^^^^^^^^^^^^
App 131002 output:   File "/home/db/virtualenv/dbsite/3.11/lib64/python3.11/site-packages/sqlalchemy/engine/create.py", line 637, in connect
App 131002 output:     return dialect.connect(*cargs, **cparams)
App 131002 output:            ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
App 131002 output:   File "/home/db/virtualenv/dbsite/3.11/lib64/python3.11/site-packages/sqlalchemy/engine/default.py", line 616, in connect
App 131002 output:     return self.loaded_dbapi.connect(*cargs, **cparams)
App 131002 output:            ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
App 131002 output:   File "/home/db/virtualenv/dbsite/3.11/lib/python3.11/site-packages/psycopg/connection.py", line 728, in connect
App 131002 output:     raise ex.with_traceback(None)
App 131002 output: psycopg.OperationalError: connection failed: server closed the connection unexpectedly
App 131002 output:  This probably means the server terminated abnormally
App 131002 output:  before or while processing the request.
App 131002 output: The above exception was the direct cause of the following exception:
App 131002 output: Traceback (most recent call last):
App 131002 output:   File "/opt/cpanel/ea-ruby27/root/usr/share/passenger/helper-scripts/wsgi-loader.py", line 199, in main_loop
App 131002 output:     socket_hijacked = self.process_request(env, input_stream, client)
App 131002 output:                       ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
App 131002 output:   File "/opt/cpanel/ea-ruby27/root/usr/share/passenger/helper-scripts/wsgi-loader.py", line 333, in process_request
App 131002 output:     result = self.app(env, start_response)
App 131002 output:              ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
App 131002 output:   File "/home/db/virtualenv/dbsite/3.11/lib/python3.11/site-packages/flask/app.py", line 2548, in __call__
App 131002 output:     return self.wsgi_app(environ, start_response)
App 131002 output:            ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
App 131002 output:   File "/home/db/virtualenv/dbsite/3.11/lib/python3.11/site-packages/flask/app.py", line 2528, in wsgi_app
App 131002 output:     response = self.handle_exception(e)
App 131002 output:                ^^^^^^^^^^^^^^^^^^^^^^^^
App 131002 output:   File "/home/db/virtualenv/dbsite/3.11/lib/python3.11/site-packages/flask/app.py", line 2525, in wsgi_app
App 131002 output:     response = self.full_dispatch_request()
App 131002 output:                ^^^^^^^^^^^^^^^^^^^^^^^^^^^^
App 131002 output:   File "/home/db/virtualenv/dbsite/3.11/lib/python3.11/site-packages/flask/app.py", line 1822, in full_dispatch_request
App 131002 output:     rv = self.handle_user_exception(e)
App 131002 output:          ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
App 131002 output:   File "/home/db/virtualenv/dbsite/3.11/lib/python3.11/site-packages/flask/app.py", line 1820, in full_dispatch_request
App 131002 output:     rv = self.dispatch_request()
App 131002 output:          ^^^^^^^^^^^^^^^^^^^^^^^
App 131002 output:   File "/home/db/virtualenv/dbsite/3.11/lib/python3.11/site-packages/flask/app.py", line 1796, in dispatch_request
App 131002 output:     return self.ensure_sync(self.view_functions[rule.endpoint])(**view_args)
App 131002 output:            ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
App 131002 output:   File "/home/db/dbsite/web/templates/materials/bl_materials.py", line 23, in materials
App 131002 output:     materials = get_materials()
App 131002 output:                 ^^^^^^^^^^^^^^^
App 131002 output:   File "/home/db/dbsite/database/em_db.py", line 36, in get_materials
App 131002 output:     materials_view_query = conn.scalars(select(Material)).all()
App 131002 output:                            ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
App 131002 output:   File "/home/db/virtualenv/dbsite/3.11/lib64/python3.11/site-packages/sqlalchemy/orm/session.py", line 2344, in scalars
App 131002 output:     return self._execute_internal(
App 131002 output:            ^^^^^^^^^^^^^^^^^^^^^^^
App 131002 output:   File "/home/db/virtualenv/dbsite/3.11/lib64/python3.11/site-packages/sqlalchemy/orm/session.py", line 2117, in _execute_internal
App 131002 output:     conn = self._connection_for_bind(bind)
App 131002 output:            ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
App 131002 output:   File "/home/db/virtualenv/dbsite/3.11/lib64/python3.11/site-packages/sqlalchemy/orm/session.py", line 1984, in _connection_for_bind
App 131002 output:     return trans._connection_for_bind(engine, execution_options)
App 131002 output:            ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
App 131002 output:   File "<string>", line 2, in _connection_for_bind
App 131002 output:   File "/home/db/virtualenv/dbsite/3.11/lib64/python3.11/site-packages/sqlalchemy/orm/state_changes.py", line 137, in _go
App 131002 output:     ret_value = fn(self, *arg, **kw)
App 131002 output:                 ^^^^^^^^^^^^^^^^^^^^
App 131002 output:   File "/home/db/virtualenv/dbsite/3.11/lib64/python3.11/site-packages/sqlalchemy/orm/session.py", line 1111, in _connection_for_bind
App 131002 output:     conn = bind.connect()
App 131002 output:            ^^^^^^^^^^^^^^
App 131002 output:   File "/home/db/virtualenv/dbsite/3.11/lib64/python3.11/site-packages/sqlalchemy/engine/base.py", line 3269, in connect
App 131002 output:     return self._connection_cls(self)
App 131002 output:            ^^^^^^^^^^^^^^^^^^^^^^^^^^
App 131002 output:   File "/home/db/virtualenv/dbsite/3.11/lib64/python3.11/site-packages/sqlalchemy/engine/base.py", line 147, in __init__
App 131002 output:     Connection._handle_dbapi_exception_noconnection(
App 131002 output:   File "/home/db/virtualenv/dbsite/3.11/lib64/python3.11/site-packages/sqlalchemy/engine/base.py", line 2431, in _handle_dbapi_exception_noconnection
App 131002 output:     raise sqlalchemy_exception.with_traceback(exc_info[2]) from e
App 131002 output:   File "/home/db/virtualenv/dbsite/3.11/lib64/python3.11/site-packages/sqlalchemy/engine/base.py", line 145, in __init__
App 131002 output:     self._dbapi_connection = engine.raw_connection()
App 131002 output:                              ^^^^^^^^^^^^^^^^^^^^^^^
App 131002 output:   File "/home/db/virtualenv/dbsite/3.11/lib64/python3.11/site-packages/sqlalchemy/engine/base.py", line 3293, in raw_connection
App 131002 output:     return self.pool.connect()
App 131002 output:            ^^^^^^^^^^^^^^^^^^^
App 131002 output:   File "/home/db/virtualenv/dbsite/3.11/lib64/python3.11/site-packages/sqlalchemy/pool/base.py", line 452, in connect
App 131002 output:     return _ConnectionFairy._checkout(self)
App 131002 output:            ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
App 131002 output:   File "/home/db/virtualenv/dbsite/3.11/lib64/pyt
App 131002 output: hon3.11/site-packages/sqlalchemy/pool/base.py", line 1268, in _checkout
App 131002 output:     fairy = _ConnectionRecord.checkout(pool)
App 131002 output:             ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
App 131002 output:   File "/home/db/virtualenv/dbsite/3.11/lib64/python3.11/site-packages/sqlalchemy/pool/base.py", line 716, in checkout
App 131002 output:     rec = pool._do_get()
App 131002 output:           ^^^^^^^^^^^^^^
App 131002 output:   File "/home/db/virtualenv/dbsite/3.11/lib64/python3.11/site-packages/sqlalchemy/pool/impl.py", line 168, in _do_get
App 131002 output:     with util.safe_reraise():
App 131002 output:   File "/home/db/virtualenv/dbsite/3.11/lib64/python3.11/site-packages/sqlalchemy/util/langhelpers.py", line 147, in __exit__
App 131002 output:     raise exc_value.with_traceback(exc_tb)
App 131002 output:   File "/home/db/virtualenv/dbsite/3.11/lib64/python3.11/site-packages/sqlalchemy/pool/impl.py", line 166, in _do_get
App 131002 output:     return self._create_connection()
App 131002 output:            ^^^^^^^^^^^^^^^^^^^^^^^^^
App 131002 output:   File "/home/db/virtualenv/dbsite/3.11/lib64/python3.11/site-packages/sqlalchemy/pool/base.py", line 393, in _create_connection
App 131002 output:     return _ConnectionRecord(self)
App 131002 output:            ^^^^^^^^^^^^^^^^^^^^^^^
App 131002 output:   File "/home/db/virtualenv/dbsite/3.11/lib64/python3.11/site-packages/sqlalchemy/pool/base.py", line 678, in __init__
App 131002 output:     self.__connect()
App 131002 output:   File "/home/db/virtualenv/dbsite/3.11/lib64/python3.11/site-packages/sqlalchemy/pool/base.py", line 902, in __connect
App 131002 output:     with util.safe_reraise():
App 131002 output:   File "/home/db/virtualenv/dbsite/3.11/lib64/python3.11/site-packages/sqlalchemy/util/langhelpers.py", line 147, in __exit__
App 131002 output:     raise exc_value.with_traceback(exc_tb)
App 131002 output:   File "/home/db/virtualenv/dbsite/3.11/lib64/python3.11/site-packages/sqlalchemy/pool/base.py", line 898, in __connect
App 131002 output:     self.dbapi_connection = connection = pool._invoke_creator(self)
App 131002 output:                                          ^^^^^^^^^^^^^^^^^^^^^^^^^^
App 131002 output:   File "/home/db/virtualenv/dbsite/3.11/lib64/python3.11/site-packages/sqlalchemy/engine/create.py", line 637, in connect
App 131002 output:     return dialect.connect(*cargs, **cparams)
App 131002 output:            ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
App 131002 output:   File "/home/db/virtualenv/dbsite/3.11/lib64/python3.11/site-packages/sqlalchemy/engine/default.py", line 616, in connect
App 131002 output:     return self.loaded_dbapi.connect(*cargs, **cparams)
App 131002 output:            ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
App 131002 output:   File "/home/db/virtualenv/dbsite/3.11/lib/python3.11/site-packages/psycopg/connection.py", line 728, in connect
App 131002 output:     raise ex.with_traceback(None)
App 131002 output: sqlalchemy.exc.OperationalError: (psycopg.OperationalError) connection failed: server closed the connection unexpectedly
App 131002 output:  This probably means the server terminated abnormally
App 131002 output:  before or while processing the request.
App 131002 output: (Background on this error at: https://sqlalche.me/e/20/e3q8)

我不是开发人员,甚至不太熟悉 cPanel 或托管,但我认为如果 Flask 应用程序在我的桌面计算机上运行并且我重新创建 python 环境,并上传相同的代码,那么一切都会正常!

我认为该问题与网络主机未加载

init_db
函数有关,但并没有真正完全理解问题。

如果有人能指出我正确的方向来解决此数据库连接问题,我将不胜感激。

python postgresql flask cpanel psycopg2
1个回答
0
投票

您找到解决这个问题的方法了吗?我也有同样的情况,无法让我的 cPanel Flask 应用程序与外部托管的 postgreSQL 数据库连接,并且错误日志几乎相同。

© www.soinside.com 2019 - 2024. All rights reserved.