尝试使用 Heroku 应用程序配置 AWS RDS。我以前使用过 postgres,但这是我第一次使用 RDS。我在这里遵循了他们的文档(https://devcenter.heroku.com/articles/amazon_rds),但是当我尝试运行syncdb或从Heroku访问dbshell时,我收到以下错误:
$ heroku run ./manage.py syncdb
Running `./manage.py syncdb` attached to terminal... up, run.8947
Traceback (most recent call last):
File "./manage.py", line 10, in <module>
execute_from_command_line(sys.argv)
File "/app/.heroku/python/lib/python2.7/site-packages/django/core/management/__init__.py", line 453, in execute_from_command_line
utility.execute()
File "/app/.heroku/python/lib/python2.7/site-packages/django/core/management/__init__.py", line 392, in execute
self.fetch_command(subcommand).run_from_argv(self.argv)
File "/app/.heroku/python/lib/python2.7/site-packages/django/core/management/__init__.py", line 263, in fetch_command
app_name = get_commands()[subcommand]
File "/app/.heroku/python/lib/python2.7/site-packages/django/core/management/__init__.py", line 109, in get_commands
apps = settings.INSTALLED_APPS
File "/app/.heroku/python/lib/python2.7/site-packages/django/conf/__init__.py", line 53, in __getattr__
self._setup(name)
File "/app/.heroku/python/lib/python2.7/site-packages/django/conf/__init__.py", line 48, in _setup
self._wrapped = Settings(settings_module)
File "/app/.heroku/python/lib/python2.7/site-packages/django/conf/__init__.py", line 132, in __init__
mod = importlib.import_module(self.SETTINGS_MODULE)
File "/app/.heroku/python/lib/python2.7/site-packages/django/utils/importlib.py", line 35, in import_module
__import__(name)
File "/app/rfp/settings.py", line 33, in <module>
'amazon-rds-ca-cert.pem')}
NameError: name 'DATABASES' is not defined
好的,看来是数据库配置设置问题。这是我的相关设置.py
import os
import sys
import dj_database_url
if DEBUG:
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.sqlite3',
'NAME': os.path.join(PROJECT_ROOT, 'db.sqlite3'),
}
}
else:
dbconfig = dj_database_url.config()
if dbconfig:
DATABASES['default']['OPTIONS'] = {
'ssl': {'ca': os.path.join(os.path.dirname(__file__), 'etc', 'amazon-rds-ca-cert.pem')}
}
然后在heroku中我为DATABASE_URL和DEBUG设置了配置变量:
DATABASE_URL: mysql2://db_user:db_pw@db_server/db?sslca=config/amazon-rds-ca-cert.pem
DEBUG: False
.pem 文件位于应用程序根目录的“etc”文件夹中。有人看到我在设置过程中遗漏或配置错误的步骤吗?谢谢
最佳答案
您的主要问题是当DEBUG = False
时您从未设置DATABASES
。您将变量dbconfig
设置为数据库设置,但从未将其设为默认
数据库。你需要这样的东西:
else:
dbconfig = dj_database_url.config()
if dbconfig:
DATABASES = {'default': dj_database_url.config()} # Add this
DATABASES['default']['OPTIONS'] = {
'ssl': {'ca': os.path.join(os.path.dirname(__file__), 'etc', 'amazon-rds-ca-cert.pem')}
}
设置默认数据库后,您可以编辑其OPTIONS
。
编辑:
此外,无论其值(value)如何,this answer说你不能使用python来构建SSL路径。您需要对其进行硬编码。
关于mysql - 名称错误 : name 'DATABASES' is not defined with AWS RDS,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23229731/