在openshift上使用sqlalchemy创建数据库

时间:2014-03-08 23:06:47

标签: python sqlalchemy openshift

当我刚刚将我的应用程序部署到openshift但是当我运行它时,我得到错误500.在检查日志文件时,我意识到sqlalchemy无法找到数据库。 这是错误日志

 python/logs/error_log-20140308-000000-EST <==
[Sat Mar 08 17:47:31 2014] [error] [client 127.10.252.129]     reraise(type(exception), exception, tb=exc_tb)
[Sat Mar 08 17:47:31 2014] [error] [client 127.10.252.129]   File "/var/lib/openshift/531b3df4500446dfb400007a/python/virtenv/lib/python2.7/site-packages/SQLAlchemy-0.8.4-py2.7-linux-x86_64.egg/sqlalchemy/engine/base.py", line 867, in _execute_context
[Sat Mar 08 17:47:31 2014] [error] [client 127.10.252.129]     context)
[Sat Mar 08 17:47:31 2014] [error] [client 127.10.252.129]   File "/var/lib/openshift/531b3df4500446dfb400007a/python/virtenv/lib/python2.7/site-packages/SQLAlchemy-0.8.4-py2.7-linux-x86_64.egg/sqlalchemy/engine/default.py", line 324, in do_execute
[Sat Mar 08 17:47:31 2014] [error] [client 127.10.252.129]     cursor.execute(statement, parameters)
[Sat Mar 08 17:47:31 2014] [error] [client 127.10.252.129]   File "/opt/rh/python27/root/usr/lib64/python2.7/site-packages/MySQLdb/cursors.py", line 174, in execute
[Sat Mar 08 17:47:31 2014] [error] [client 127.10.252.129]     self.errorhandler(self, exc, value)
[Sat Mar 08 17:47:31 2014] [error] [client 127.10.252.129]   File "/opt/rh/python27/root/usr/lib64/python2.7/site-packages/MySQLdb/connections.py", line 36, in defaulterrorhandler
[Sat Mar 08 17:47:31 2014] [error] [client 127.10.252.129]     raise errorclass, errorvalue
[Sat Mar 08 17:47:31 2014] [error] [client 127.10.252.129] OperationalError: (OperationalError) (1046, 'No database selected') 'DESCRIBE `users`' ()

我迷路了。 openshift文档说明创建的数据库与您创建应用程序后立即使用相同的名称

1 个答案:

答案 0 :(得分:1)

关于flask的openshift教程使用PostgreSQL并将连接字符串作为

SQLALCHEMY_DATABASE_URI = os.environ['OPENSHIFT_POSTGRESQL_DB_URL']

我使用MySQL并将其更改为

SQLALCHEMY_DATABASE_URI = os.environ['OPENSHIFT_MYSQL_DB_URL']

导致错误的原因是您必须将应用程序名称(也是数据库的名称)传递给URI。所以连接字符串变为

SQLALCHEMY_DATABASE_URI = os.environ['OPENSHIFT_MYSQL_DB_URL'] + os.environ['OPENSHIFT_APP_NAME']