我正在尝试部署使用SQLAlchemy到AWS ElasticBeanstalk环境的Flask应用程序。我可以部署并查看正在运行的应用程序,但是当我尝试使用create_engine连接到我的数据库时,整个事情就崩溃了。我可以在linux vm上本地运行整个应用程序(包括在ElasticBeanstalk环境中使用数据库。)我可以进入应用程序服务器并打印到终端。
我认为应用程序在从浏览器(通过应用程序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的片段。以“...”开头的行是我用来隔离问题的打印语句。我已经改变了我用来访问数据库以阻止机器人的网址。当我进入应用服务器时,我使用的地址可以从我的本地机器和终端完美运行。
[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
答案 0 :(得分:0)
我从来没有能够让这个工作。我最后学习了本教程: https://medium.com/@rodkey/deploying-a-flask-application-on-aws-a72daba6bb80#.t2lbbv5q3
本教程使用Flask-SQLAlchemy模块,而不是单独导入Flask和SQLAlchemy。一旦我使用这种方法,我没有问题。祝你好运!