django无法连接到RDS postgresql

时间:2016-01-25 16:21:48

标签: python django apache postgresql amazon-ec2

我的Django应用程序在部署到EC2时无法连接到RDS PostgreSQL。但奇怪的是,它在桌面上运行时工作正常。

EC2服务器和桌面配置了python3,django1.9,apache2和mod_wsgi_py3

这是我的settings.py数据库设置:

DATABASES = {
    'default': {
        'ENGINE': 'django.db.backends.postgresql',
        'NAME': 'postgres',
        'USER': 'some_name',
        'PASSWORD': 'some_password',
        'HOST': 'myhostname.cltlezrr85xn.ap-northeast-1.rds.amazonaws.com',
        'PORT': 5432,
    }
}
来自apache2的

和error.log:

Traceback (most recent call last):
  File "/usr/local/lib/python3.4/dist-packages/django/db/backends/base/base.py", line 199, in ensure_connection
    self.connect()
  File "/usr/local/lib/python3.4/dist-packages/django/db/backends/base/base.py", line 171, in connect
    self.connection = self.get_new_connection(conn_params)
  File "/usr/local/lib/python3.4/dist-packages/django/db/backends/postgresql/base.py", line 175, in get_new_connection
    connection = Database.connect(**conn_params)
  File "/usr/local/lib/python3.4/dist-packages/psycopg2/__init__.py", line 164, in connect
    conn = _connect(dsn, connection_factory=connection_factory, async=async)
psycopg2.OperationalError: could not connect to server: Connection timed out
    Is the server running on host "myhostname.cltlezrr85xn.ap-northeast-1.rds.amazonaws.com" (172.--.--.---) 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 10, in <module>
    execute_from_command_line(sys.argv)
  File "/usr/local/lib/python3.4/dist-packages/django/core/management/__init__.py", line 353, in execute_from_command_line
    utility.execute()
  File "/usr/local/lib/python3.4/dist-packages/django/core/management/__init__.py", line 345, in execute
    self.fetch_command(subcommand).run_from_argv(self.argv)
  File "/usr/local/lib/python3.4/dist-packages/django/core/management/base.py", line 348, in run_from_argv
    self.execute(*args, **cmd_options)
  File "/usr/local/lib/python3.4/dist-packages/django/contrib/auth/management/commands/createsuperuser.py", line 52, in execute
    return super(Command, self).execute(*args, **options)
  File "/usr/local/lib/python3.4/dist-packages/django/core/management/base.py", line 399, in execute
    output = self.handle(*args, **options)
  File "/usr/local/lib/python3.4/dist-packages/django/contrib/auth/management/commands/createsuperuser.py", line 86, in handle
    default_username = get_default_username()
  File "/usr/local/lib/python3.4/dist-packages/django/contrib/auth/management/__init__.py", line 189, in get_default_username
    auth_app.User._default_manager.get(username=default_username)
  File "/usr/local/lib/python3.4/dist-packages/django/db/models/manager.py", line 122, in manager_method
    return getattr(self.get_queryset(), name)(*args, **kwargs)
  File "/usr/local/lib/python3.4/dist-packages/django/db/models/query.py", line 381, in get
    num = len(clone)
  File "/usr/local/lib/python3.4/dist-packages/django/db/models/query.py", line 240, in __len__
    self._fetch_all()
  File "/usr/local/lib/python3.4/dist-packages/django/db/models/query.py", line 1074, in _fetch_all
    self._result_cache = list(self.iterator())
  File "/usr/local/lib/python3.4/dist-packages/django/db/models/query.py", line 52, in __iter__
    results = compiler.execute_sql()
  File "/usr/local/lib/python3.4/dist-packages/django/db/models/sql/compiler.py", line 846, in execute_sql
    cursor = self.connection.cursor()
  File "/usr/local/lib/python3.4/dist-packages/django/db/backends/base/base.py", line 231, in cursor
    cursor = self.make_debug_cursor(self._cursor())
  File "/usr/local/lib/python3.4/dist-packages/django/db/backends/base/base.py", line 204, in _cursor
    self.ensure_connection()
  File "/usr/local/lib/python3.4/dist-packages/django/db/backends/base/base.py", line 199, in ensure_connection
    self.connect()
  File "/usr/local/lib/python3.4/dist-packages/django/db/utils.py", line 95, in __exit__
    six.reraise(dj_exc_type, dj_exc_value, traceback)
  File "/usr/local/lib/python3.4/dist-packages/django/utils/six.py", line 685, in reraise
    raise value.with_traceback(tb)
  File "/usr/local/lib/python3.4/dist-packages/django/db/backends/base/base.py", line 199, in ensure_connection
    self.connect()
  File "/usr/local/lib/python3.4/dist-packages/django/db/backends/base/base.py", line 171, in connect
    self.connection = self.get_new_connection(conn_params)
  File "/usr/local/lib/python3.4/dist-packages/django/db/backends/postgresql/base.py", line 175, in get_new_connection
    connection = Database.connect(**conn_params)
  File "/usr/local/lib/python3.4/dist-packages/psycopg2/__init__.py", line 164, in connect
    conn = _connect(dsn, connection_factory=connection_factory, async=async)
django.db.utils.OperationalError: could not connect to server: Connection timed out
    Is the server running on host "myhostname.cltlezrr85xn.ap-northeast-1.rds.amazonaws.com" (172.--.--.---) and accepting
    TCP/IP connections on port 5432?

感谢您的建议..

1 个答案:

答案 0 :(得分:1)

编辑:

只需再次执行此操作,并找到更简单的方法即可。

当您尝试在EC2或EB上使用现有的RDS数据库和Django时,必须调整安全组,然后获取适当的参数并将其设置为环境变量(RDS _ *)

1)创建RDS,并进行匹配:

Environment variables - RDS console label

RDS_HOSTNAME - Endpoint (this is the hostname)
RDS_PORT - Port
RDS_DB_NAME –  DB Name
RDS_USERNAME –  Username
RDS_PASSWORD – Password you set for your DB

2)使用eb setenv

设置那些

3)转到您的EC2 / EB实例,然后获取该实例的安全组。 awseb-z-afsafdsaf-stack-AWSEBSecurityGroup-asfdsadfasdf

4)转到RDS实例的面板,向下滚动到“安全组”,并记下它具有的安全组。例如rds-launch-wizard-1 (ab-sdjfalkajsdf39)

5)选择RDS安全组,然后添加以下类型的入站规则:PostgreSQL(或您正在使用的任何数据库),并使用在步骤2中获得的EC2或EB实例作为源({{ }}。协议和端口范围应自动填充。

6)保存

就是这样。


原文:

对于偶然发现此问题的任何人:

当您尝试在EC2或EB上使用现有的RDS数据库和Django时,必须调整安全组,然后获取适当的参数并将其设置为环境变量(RDS _ *)

1)创建RDS,并进行匹配:

awseb-z-afsafdsaf-stack-AWSEBSecurityGroup-asfdsadfasdf

2)使用Environment variables - RDS console label RDS_HOSTNAME - Endpoint (this is the hostname) RDS_PORT - Port RDS_DB_NAME – DB Name RDS_USERNAME – Username RDS_PASSWORD – Password you set for your DB

设置那些

3)转到您的EC2 / EB实例,然后获取该实例的安全组。 eb setenv,对于负载均衡器:awseb-z-afsafdsaf-stack-AWSEBSecurityGroup-asfdsadfasdf

4)转到RDS实例的面板,向下滚动到“安全组”,并记下它具有的安全组。例如awseb-e-adsfadsf-stack-AWSEBLoadBalancerSecurityGroup-asdfadsf

5)单击RDS实例的“修改”,然后在中间的“安全组”设置中,添加您在上面找到的负载均衡器安全组。应该有建议。

6)转到EC2仪表板,然后从左侧菜单中选择安全组。

7)选择负载均衡器安全组,然后添加出站规则。 Type应该是您的RDS类型(PostgreSQL),目的地应该是custom和RDS实例的安全组。保存。

8)对“入站”执行相同操作,使用相同的RDS类型和“目标”

9)选择RDS安全组,并添加一个入站规则,类似于8,但使用在步骤2中获得的EC2或EB实例。

10)保存,完成。他们现在应该可以一起工作了。

我不是100%肯定所有这些步骤都是必要的,也许不需要一两个步骤,但这已经为我完成了工作。