首页 文章

使用MySQL RDS在弹性beanstalk上部署Django应用程序 - 永远不会创建表

提问于
浏览
0

我已经按照每个指南和教程,为我的Django应用程序设置RDS MySQL . 一切都在当地很好 . 部署时,我没有错误 . 我的网站运行 . 但是当我尝试访问我设置的restful框架,或者我尝试登录管理页面时,我得到一个不存在该表的ProgrammingError(没有表存在) .

访问数据库似乎不是问题,我的所有RDS环境变量都已设置,安全组已设置等 . 我甚至可以从我的本地mysql客户端访问数据库,我可以看到数据库没有表格 .

我在配置文件中设置了命令来运行'django-admin.py makemigrations'和'django-admin.py migrate',我甚至尝试将其更改为'python manage.py ...'等等,看起来像命令永远不会奏效 .

当我进入弹性beanstalk环境时,我可以使用'mysql -u my_username'进入mysql . 但是,如果我导航到我的应用程序所在的文件夹,并尝试手动运行'python manage.py makemigrations',我会得到:django.db.utils.OperationalError:(1045,“访问被拒绝用户'my_username'@ 'localhost'(使用密码:YES)“)

这是我的配置......

requirements.txt

virtualenv==15.0.1
Django==1.9.5
django-compressor==2.0
django-model-utils==2.4
djangorestframework==3.3.2
dj-database-url==0.4.0
gunicorn==19.4.5
MySQL-python==1.2.5
jsonfield==1.0.3

.ebextentions / 01_packages.config

packages:
  yum:
    git: []
    gcc: []
    mysql: []
    mysql-devel: []
    python-devel: []

.ebextentions / project.config

container_commands:
  01_makemigrations:
    command: "source /opt/python/run/venv/bin/activate && python manage.py makemigrations --noinput"
    leader_only: true
  02_migrate:
    command: "source /opt/python/run/venv/bin/activate && python manage.py migrate --noinput"
    leader_only: true
  03_createsu:
    command: "source /opt/python/run/venv/bin/activate && python manage.py createsu"
    leader_only: true
  04_collectstatic:
    command: "source /opt/python/run/venv/bin/activate && python manage.py collectstatic --noinput"

settings.py

DATABASES = {
    'default': {
        'ENGINE': 'django.db.backends.mysql',
        'NAME': os.environ['RDS_DB_NAME'],
        'USER': os.environ['RDS_USERNAME'],
        'PASSWORD': os.environ['RDS_PASSWORD'],
        'HOST': os.environ['RDS_HOSTNAME'],
        'PORT': os.environ['RDS_PORT'],
    }
}

请帮忙 . 谢谢!

2 回答

  • 0

    查看是否在EC2安全组中为弹性beanstalk配置了RDS安全组 . 它应该如下所示:

    EC2 security groups :your-rds-security-group-name,your-Elastic-Beanstalk-Security-Group

  • 0

    Make sure that the same settings are used when migrating and running!

    我有类似的问题,但我发现我在迁移时使用了本地设置(来自 manage.py ) . 但是为了运行我使用的 生产环境 设置(在 wsgi.py 中定义) . 这意味着迁移了本地数据库,但使用了 生产环境 数据库但是 never migrated ,因此即使迁移命令返回成功,也永远不会创建表 . 我不得不改变我的 django.config

    container_commands:
      01_makemigrations:
        command: "source /opt/python/run/venv/bin/activate && python manage.py makemigrations"
        leader_only: true
    
      02_migrate:
        command: "source /opt/python/run/venv/bin/activate && python manage.py migrate"
        leader_only: true
    

    至:

    container_commands:
      01_migrate:
        command: "django-admin migrate"
        leader_only: true
    option_settings:
      aws:elasticbeanstalk:application:environment:
        DJANGO_SETTINGS_MODULE: fund.productionSettings
    

    按照建议here .

相关问题