Python 2.7 在ElasticBeanstalk上使用SQLAlchemy时创建引擎失败

Python 2.7 在ElasticBeanstalk上使用SQLAlchemy时创建引擎失败,python-2.7,sqlalchemy,amazon-elastic-beanstalk,flask-sqlalchemy,Python 2.7,Sqlalchemy,Amazon Elastic Beanstalk,Flask Sqlalchemy,我正在尝试将使用SQLAlchemy的Flask应用程序部署到AWS ElasticBeanstalk环境中。我可以部署并查看正在运行的应用程序,但当我尝试使用create_引擎连接到数据库时,整个过程都崩溃了。我可以在linux虚拟机上本地运行整个应用程序(包括使用ElasticBeanstalk环境中的数据库)。我可以ssh到应用程序服务器并打印到终端 我认为,当从浏览器(通过应用程序url)访问应用程序时,应用程序如何运行是导致问题的原因。如果有人能给我指出正确的方向,我将不胜感激。我已经

我正在尝试将使用SQLAlchemy的Flask应用程序部署到AWS ElasticBeanstalk环境中。我可以部署并查看正在运行的应用程序,但当我尝试使用create_引擎连接到数据库时,整个过程都崩溃了。我可以在linux虚拟机上本地运行整个应用程序(包括使用ElasticBeanstalk环境中的数据库)。我可以ssh到应用程序服务器并打印到终端

我认为,当从浏览器(通过应用程序url)访问应用程序时,应用程序如何运行是导致问题的原因。如果有人能给我指出正确的方向,我将不胜感激。我已经研究这个问题三天了,我开始发疯了

这是我用来创建会话的代码:

from sqlalchemy import create_engine, Column, ForeignKey, Integer, String
from sqlalchemy.ext.declarative import declarative_base
from sqlalchemy.orm import relationship, sessionmaker
from sqlalchemy.engine.url import URL

import settings

Base = declarative_base()

# establish connection to database
def db_connect():
    """
    Performs database connection using database settings from settings.py.
    Returns sqlalchemy engine instance
    """
    print ".............................................."
    print "...Connecting to Database at URL : "
    print "...attempting URL(**settings.DATABASE)"
    print "... ", URL(**settings.DATABASE)
    print "...above should read: "
    print "... postgresql://ebroot:42Snails@aa12jlddfw2awrj.cc6p0ojvcx3g.us-east-1.rds.amazonaws.com:5432/ebdb"
    print ".............................................."
    print "...attempting engine = create_engine(URL(**settings.DATABASE))..."
    try:
        engine = create_engine(URL(**settings.DATABASE))
        print "...Succeeded in creating engine.................."
        return engine
    except:
        print "..................................................."
        print "...Failed to create engine in database_setup.py...."
        print "..................................................."
        raise   

def makeSession():
    # Establishes a session called session which allows you to work with the DB
    try:
        print "....trying to create session.................."
        engine = db_connect()
        print "....succeeded in db_connect().................."
        Base.metadata.bind = engine
        print "....succeeded in Base.metadata.bind = engine..."
    # create a configured "Session" class
        Session = sessionmaker(bind=engine)
        print "....succeeded in Session = sessionmaker(bind=engine)..."
    # create a Session
        session = Session()
        print ".............................................."
        print "...Succeeded in creating session.............."
        print ".............................................."
        return session
    except:
        print "................................................."
        print "...Failed to create session in application.py...."
        print "................................................."
        raise   
        print "finally.... session.close()"
        session.close()
这是/var/log/httpd/error\u log中的一个片段。带“…”的行是我用来隔离问题的打印语句。我改变了我用来访问数据库的url,以挫败机器人。我使用的地址在我的本地机器上以及在我ssh到应用服务器时从终端上都能正常工作

[Sat Mar 19 20:16:00.159375 2016] [mpm_prefork:notice] [pid 670] AH00169: caught SIGTERM, shutting down
[Sat Mar 19 20:16:01.259575 2016] [suexec:notice] [pid 1077] AH01232: suEXEC mechanism enabled (wrapper: /usr/sbin/suexec)
[Sat Mar 19 20:16:01.270801 2016] [so:warn] [pid 1077] AH01574: module wsgi_module is already loaded, skipping
[Sat Mar 19 20:16:01.273963 2016] [auth_digest:notice] [pid 1077] AH01757: generating secret for digest authentication ...
[Sat Mar 19 20:16:01.274662 2016] [lbmethod_heartbeat:notice] [pid 1077] AH02282: No slotmem from mod_heartmonitor
[Sat Mar 19 20:16:01.274712 2016] [:warn] [pid 1077] mod_wsgi: Compiled for Python/2.7.9.
[Sat Mar 19 20:16:01.274719 2016] [:warn] [pid 1077] mod_wsgi: Runtime using Python/2.7.10.
[Sat Mar 19 20:16:01.276674 2016] [mpm_prefork:notice] [pid 1077] AH00163: Apache/2.4.16 (Amazon) mod_wsgi/3.5 Python/2.7.10 configured -- resuming normal operations
[Sat Mar 19 20:16:01.276690 2016] [core:notice] [pid 1077] AH00094: Command line: '/usr/sbin/httpd -D FOREGROUND'
[Sat Mar 19 20:16:04.069713 2016] [:error] [pid 1080] ...in settings.py.......
[Sat Mar 19 20:19:53.419659 2016] [:error] [pid 1080] ....trying to create session..................
[Sat Mar 19 20:19:53.419702 2016] [:error] [pid 1080] ..............................................
[Sat Mar 19 20:19:53.419707 2016] [:error] [pid 1080] ...Connecting to Database at URL : 
[Sat Mar 19 20:19:53.419711 2016] [:error] [pid 1080] ...attempting URL(**settings.DATABASE)
[Sat Mar 19 20:19:53.419876 2016] [:error] [pid 1080] ...  postgresql://user:password@aa12jlddfw2awrj.cc6p9990ojvcx3g.us-east-1.rds.amazonaws.com:5432/ebdb
[Sat Mar 19 20:19:53.419884 2016] [:error] [pid 1080] ...above should read: 
[Sat Mar 19 20:19:53.419889 2016] [:error] [pid 1080] ... postgresql://user:password@aa12jlddfw2awrj.cc6p9990ojvcx3g.us-east-1.rds.amazonaws.com:5432/ebdb
[Sat Mar 19 20:19:53.419893 2016] [:error] [pid 1080] ..............................................
[Sat Mar 19 20:19:53.419897 2016] [:error] [pid 1080] ...attempting engine = create_engine(URL(**settings.DATABASE))...
[Sat Mar 19 20:19:53.427168 2016] [:error] [pid 1080] ...................................................
[Sat Mar 19 20:19:53.427189 2016] [:error] [pid 1080] ...Failed to create engine in database_setup.py....
[Sat Mar 19 20:19:53.427193 2016] [:error] [pid 1080] ...................................................
[Sat Mar 19 20:19:53.427200 2016] [:error] [pid 1080] .................................................
[Sat Mar 19 20:19:53.427203 2016] [:error] [pid 1080] ...Failed to create session in application.py....
[Sat Mar 19 20:19:53.427206 2016] [:error] [pid 1080] .................................................
以下是运行应用程序的url:

我从来没能让它起作用。我最终学习了本教程:


本教程使用Flask SQLAlchemy模块,而不是分别导入Flask和SQLAlchemy。一旦我使用了这种方法,我就没有问题了。祝你好运

我从来没能让它起作用。我最终学习了本教程:

本教程使用Flask SQLAlchemy模块,而不是分别导入Flask和SQLAlchemy。一旦我使用了这种方法,我就没有问题了。祝你好运