无法从我的Django Web应用程序连接到Amazon RDS Postgres数据库

3
我正在尝试使用弹性Beanstalk部署网站,并将其连接到Postgres数据库。在尝试在Django中进行makemigrations时,我遇到了错误。
我已成功设置了本地Postgres数据库。但是,我一直尝试了很长时间,却没有成功。
以下是错误日志:
(venv) C:\Users\dania\Development\my_website>python manage.py makemigrations
Traceback (most recent call last):
  File "C:\Users\dania\Development\my_website\venv\lib\site-packages\django\db\backends\base\base.py", line 217, in ensure_connection
    self.connect()
  File "C:\Users\dania\Development\my_website\venv\lib\site-packages\django\db\backends\base\base.py", line 195, in connect
    self.connection = self.get_new_connection(conn_params)
  File "C:\Users\dania\Development\my_website\venv\lib\site-packages\django\db\backends\postgresql\base.py", line 178, in get_new_connection
    connection = Database.connect(**conn_params)
  File "C:\Users\dania\Development\my_website\venv\lib\site-packages\psycopg2\__init__.py", line 127, in connect
    conn = _connect(dsn, connection_factory=connection_factory, **kwasync)
psycopg2.OperationalError: could not connect to server: Connection timed out (0x0000274C/10060)     
        Is the server running on host "my_website.c8lfjtthzko9.ap-southeast-2.rds.amazonaws.com" (13.239.177.165) and accepting
        TCP/IP connections on port 5432?


The above exception was the direct cause of the following exception:

Traceback (most recent call last):
  File "manage.py", line 15, in <module>
    execute_from_command_line(sys.argv)
  File "C:\Users\dania\Development\my_website\venv\lib\site-packages\django\core\management\__init__.py", line 381, in execute_from_command_line
    utility.execute()
  File "C:\Users\dania\Development\my_website\venv\lib\site-packages\django\core\management\__init__.py", line 375, in execute
    self.fetch_command(subcommand).run_from_argv(self.argv)
  File "C:\Users\dania\Development\my_website\venv\lib\site-packages\django\core\management\base.py", line 323, in run_from_argv
    self.execute(*args, **cmd_options)
  File "C:\Users\dania\Development\my_website\venv\lib\site-packages\django\core\management\base.py", line 364, in execute
    output = self.handle(*args, **options)
  File "C:\Users\dania\Development\my_website\venv\lib\site-packages\django\core\management\base.py", line 83, in wrapped
    res = handle_func(*args, **kwargs)
  File "C:\Users\dania\Development\my_website\venv\lib\site-packages\django\core\management\commands\makemigrations.py", line 101, in handle
    loader.check_consistent_history(connection)
  File "C:\Users\dania\Development\my_website\venv\lib\site-packages\django\db\migrations\loader.py", line 283, in check_consistent_history
    applied = recorder.applied_migrations()
  File "C:\Users\dania\Development\my_website\venv\lib\site-packages\django\db\migrations\recorder.py", line 73, in applied_migrations
    if self.has_table():
  File "C:\Users\dania\Development\my_website\venv\lib\site-packages\django\db\migrations\recorder.py", line 56, in has_table
    return self.Migration._meta.db_table in self.connection.introspection.table_names(self.connection.cursor())
  File "C:\Users\dania\Development\my_website\venv\lib\site-packages\django\db\backends\base\base.py", line 256, in cursor
    return self._cursor()
  File "C:\Users\dania\Development\my_website\venv\lib\site-packages\django\db\backends\base\base.py", line 233, in _cursor
    self.ensure_connection()
  File "C:\Users\dania\Development\my_website\venv\lib\site-packages\django\db\backends\base\base.py", line 217, in ensure_connection
    self.connect()
  File "C:\Users\dania\Development\my_website\venv\lib\site-packages\django\db\utils.py", line 89, in __exit__
    raise dj_exc_value.with_traceback(traceback) from exc_value
  File "C:\Users\dania\Development\my_website\venv\lib\site-packages\django\db\backends\base\base.py", line 217, in ensure_connection
    self.connect()
  File "C:\Users\dania\Development\my_website\venv\lib\site-packages\django\db\backends\base\base.py", line 195, in connect
    self.connection = self.get_new_connection(conn_params)
  File "C:\Users\dania\Development\my_website\venv\lib\site-packages\django\db\backends\postgresql\base.py", line 178, in get_new_connection
    connection = Database.connect(**conn_params)
  File "C:\Users\dania\Development\my_website\venv\lib\site-packages\psycopg2\__init__.py", line 127, in connect
    conn = _connect(dsn, connection_factory=connection_factory, **kwasync)
django.db.utils.OperationalError: could not connect to server: Connection timed out (0x0000274C/10060)
        Is the server running on host "my_website.c8lfjtthzko9.ap-southeast-2.rds.amazonaws.com" (13.239.177.165) and accepting
        TCP/IP connections on port 5432?

以下是我的项目.settings.py文件中的数据库部分:

from .base import *    
DEBUG = config('DEBUG', cast=bool)
ALLOWED_HOSTS = ['localhost', '127.0.0.1','http://my_website-dev.ap-southeast-2.elasticbeanstalk.com','*******']


AUTH_PASSWORD_VALIDATORS = [
    {'NAME': 'django.contrib.auth.password_validation.UserAttributeSimilarityValidator'},
    {'NAME': 'django.contrib.auth.password_validation.MinimumLengthValidator'},
    {'NAME': 'django.contrib.auth.password_validation.CommonPasswordValidator'},
    {'NAME': 'django.contrib.auth.password_validation.NumericPasswordValidator'}
]



DATABASES = {
    'default': {
        'ENGINE': 'django.db.backends.postgresql',
        'NAME': 'my_website',
        'USER': '******',
        'PASSWORD': '*********',
        'HOST': 'my_website.*********.ap-southeast-2.rds.amazonaws.com',
        'PORT': '5432'
    }
}

这是我的Django配置文件

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 --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"
    leader_only: true

option_settings:
  aws:elasticbeanstalk:application:environment:
    DJANGO_SETTINGS_MODULE: my_website.settings.base
  aws:elasticbeanstalk:container:python:
    WSGIPath: "my_website/wsgi.py"

packages:
    yum:
        httpd24-devel: []

我的 requirements.txt 文件

appdirs==1.4.4
autopep8==1.4.4
awsebcli==3.18.2
botocore==1.15.49
cassandra-driver==3.24.0
cement==2.8.2
certifi==2019.3.9
chardet==3.0.4
click==7.1.2
colorama==0.4.3
defusedxml==0.6.0
distlib==0.3.1
Django==2.2
django-allauth==0.39.1
django-countries==5.3.3
django-crispy-forms==1.7.2
django-debug-toolbar==1.10.1
docutils==0.15.2
filelock==3.0.12
future==0.16.0
geomet==0.2.1.post1
idna==2.7
importlib-metadata==1.7.0
importlib-resources==3.0.0
jmespath==0.10.0
mod-wsgi==4.7.1
oauthlib==3.0.1
pathspec==0.5.9
pep8==1.7.1
Pillow==6.2.2
psycopg2-binary==2.8.5
pycodestyle==2.5.0
python-dateutil==2.8.0
python-decouple==3.1
python-slugify==4.0.1
python3-openid==3.1.0
pytz==2018.5
PyYAML==5.3.1
requests==2.20.1
requests-oauthlib==1.2.0
semantic-version==2.5.0
six==1.11.0
slugify==0.0.1
sqlparse==0.2.4
stripe==2.27.0
termcolor==1.1.0
text-unidecode==1.3
urllib3==1.24.2
virtualenv==20.0.30
wcwidth==0.1.9
zipp==3.1.0

我已经尝试修复数据库凭据并在Amazon RDS上创建了一个新的数据库实例,但不幸的是没有成功。

提前感谢您的帮助!

编辑:为了获取更多信息,我已添加了错误日志,这是当我尝试将此代码部署到弹性Beanstalk时发生的错误。

Printing Status:
2020-08-16 10:33:18    INFO    createEnvironment is starting.
2020-08-16 10:33:19    INFO    Using elasticbeanstalk-ap-southeast-2-369458984841 as Amazon S3 storage bucket for environment data.
2020-08-16 10:33:40    INFO    Created security group named: sg-049f4a122d881069e
2020-08-16 10:33:43    INFO    Created load balancer named: awseb-e-f-AWSEBLoa-UWLA7I0LF6WN
2020-08-16 10:33:58    INFO    Created security group named: awseb-e-f9zthb6x58-stack-AWSEBSecurityGroup-UXKL3CPFGSVJ
2020-08-16 10:33:58    INFO    Created Auto Scaling launch configuration named: awseb-e-f9zthb6x58-stack-AWSEBAutoScalingLaunchConfiguration-UB2HYII1KHCR
2020-08-16 10:34:46    INFO    Created Auto Scaling group named: awseb-e-f9zthb6x58-stack-AWSEBAutoScalingGroup-1EGDGJYCTFOFQ
2020-08-16 10:34:46    INFO    Waiting for EC2 instances to launch. This may take a few minutes.
2020-08-16 10:35:01    INFO    Created Auto Scaling group policy named: arn:aws:autoscaling:ap-southeast-2:369458984841:scalingPolicy:5681f42e-9672-4f68-8566-fe465044ea90:autoScalingGroupName/awseb-e-f9zthb6x58-stack-AWSEBAutoScalingGroup-1EGDGJYCTFOFQ:policyName/awseb-e-f9zthb6x58-stack-AWSEBAutoScalingScaleUpPolicy-TLZXCZ8CV6Q2
2020-08-16 10:35:01    INFO    Created Auto Scaling group policy named: arn:aws:autoscaling:ap-southeast-2:369458984841:scalingPolicy:13bc6e9d-fd8f-4a12-a2bd-74f036335481:autoScalingGroupName/awseb-e-f9zthb6x58-stack-AWSEBAutoScalingGroup-1EGDGJYCTFOFQ:policyName/awseb-e-f9zthb6x58-stack-AWSEBAutoScalingScaleDownPolicy-JUL92X7VVS7T
2020-08-16 10:35:01    INFO    Created CloudWatch alarm named: awseb-e-f9zthb6x58-stack-AWSEBCloudwatchAlarmHigh-KVUMT41RNBO5
2020-08-16 10:35:01    INFO    Created CloudWatch alarm named: awseb-e-f9zthb6x58-stack-AWSEBCloudwatchAlarmLow-131ZV7XJVRICW
2020-08-16 10:36:03    ERROR   [Instance: i-0bf7de5ced912896c] Command failed on instance. Return code: 1 Output: (TRUNCATED)... in complain
raise ImproperlyConfigured("settings.DATABASES is improperly configured. "
django.core.exceptions.ImproperlyConfigured: settings.DATABASES is improperly configured. Please supply the ENGINE value. Check settings documentation for more details.
container_command 02_migrate in .ebextensions/django.config failed. For more detail, check /var/log/eb-activity.log using console or EB CLI.
2020-08-16 10:36:03    INFO    Command execution completed on all instances. Summary: [Successful: 0, Failed: 1].
2020-08-16 10:37:06    ERROR   Create environment operation is complete, but with errors. For more information, see troubleshooting documentation.

现在附上安全组的截图: [1]: https://istack.dev59.com/4LEfH.webp

(注意:本文中涉及的IT技术术语已尽可能使用通俗易懂的表述,如有不当之处还请指正。)

1
你应该检查 RDS 实例的安全组,它是否允许 5432 端口的流量通过? - spqa
@spqa,入站和出站规则都设置为允许所有流量。我尝试特别调用端口5432上的Postgres - 但没有成功! - Danie
你能分享RDS安全组吗?另外,你是否已经启用了它的公共访问权限? - Marcin
@Marcin 我在帖子底部添加了 RDS 安全组设置的截图。我已启用公共可访问性。感谢你能提供的任何帮助 :) - Danie
1个回答

4

根据评论和额外信息。

5432端口的入站规则被设置为安全组sg-597。这只允许来自具有相同sg的其他实例的入站流量,而不是来自互联网

要从互联网访问rds,必须使用0.0.0.0/0作为源(即所有源位置),或者更好的方法是使用特定的IP范围或地址(例如1.2.3.4/32)。范围可以是您工作或家庭公共网络的范围,IP可以是您工作站的特定地址。


1
太棒了!问题解决了 - 非常感谢你 - 你是我的英雄。 - Danie

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接