【问题标题】:Failing on create_engine while using SQLAlchemy on ElasticBeanstalk在 ElasticBeanstalk 上使用 SQLAlchemy 时 create_engine 失败
【发布时间】:2016-07-06 12:59:30
【问题描述】:

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

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

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

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_log 的 sn-p。以“...”开头的行是我用来隔离问题的打印语句。我已经更改了用于访问数据库的 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] .................................................

这是正在运行的应用程序的网址:
http://keirseysorterapp.9yk6hymk2z.us-east-1.elasticbeanstalk.com/testApp

【问题讨论】:

    标签: python-2.7 sqlalchemy amazon-elastic-beanstalk flask-sqlalchemy


    【解决方案1】:

    我一直无法让它工作。我最终遵循了本教程: https://medium.com/@rodkey/deploying-a-flask-application-on-aws-a72daba6bb80#.t2lbbv5q3

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

    【讨论】:

      猜你喜欢
      • 2021-03-11
      • 2017-12-09
      • 2019-10-30
      • 2016-06-30
      • 2023-03-22
      • 2021-10-11
      • 2017-09-26
      • 2019-06-25
      相关资源
      最近更新 更多