Django无法连接到RDS Postgresql [英] django cannot connect to RDS postgresql

查看:106
本文介绍了Django无法连接到RDS Postgresql的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的Django应用程序部署到EC2后无法连接到RDS PostgreSQL。



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



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

  DATABASES = {
'默认':{
'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登录的日志:

 跟踪(最近一次调用最近):
文件 / usr / local / lib / python3 .4 / dist-packages / django / db / backends / base / base.py,行199,在sure_connection
self.connect()
文件中 /usr/local/lib/python3.4 /dist-packages/django/db/backends/base/base.py,行171,在connect
中self.connection = self.get_n ew_connection(conn_params)
文件 /usr/local/lib/python3.4/dist-packages/django/db/backends/postgresql/base.py,行175,在get_new_connection
连接=数据库中.connect(** conn_params)
文件 /usr/local/lib/python3.4/dist-packages/psycopg2/__init__.py,第164行,在connect
中conn = _connect(dsn, connection_factory = connection_factory,async = async)
psycopg2.OperationalError:无法连接到服务器:连接超时
服务器是否在主机 myhostname.cltlezrr85xn.ap-northeast-1.rds.amazonaws上运行。 com(172 .--.--.--),并在端口5432上接受
TCP / IP连接?


上面的异常是以下异常的直接原因:

追溯(最近一次通话):
文件 manage.py在< module>中的第10行
execute_from_command_line(sys.argv)
文件 /usr/local/lib/python3.4/dist-packages/django/core/management/__init__.py,行353,在execute_from_command_line $ b $中b utility.execute()
文件 /usr/local/lib/python3.4/dist-packages/django/core/management/__init__.py,行345,在执行
self.fetch_command中(子命令).run_from_argv(self.argv)
文件 /usr/local/lib/python3.4/dist-packages/django/core/management/base.py,行348,在run_from_argv $ b $中b self.execute(* args,** cmd_options)
文件 /usr/local/lib/python3.4/dist-packages/django/contrib/auth/management/commands/createsuperuser.py,第52行,在执行
中返回super(Command,self).execute(* args,** options)
文件 /usr/local/lib/python3.4/dist-packages/django/core/management /base.py,行399,在执行
输出= self.handle(* args,** options)
文件 /usr/local/lib/python3.4/dist-packages/django /contrib/auth/management/commands/createsuperuser.py,第8行6,在句柄
default_username = get_default_username()
文件 /usr/local/lib/python3.4/dist-packages/django/contrib/auth/management/__init__.py中,第189行,在get_default_username
auth_app.User._default_manager.get(username = default_username)
文件 /usr/local/lib/python3.4/dist-packages/django/db/models/manager.py中,第122行,在manager_method
中返回getattr(self.get_queryset(),name)(* args,** kwargs)
File /usr/local/lib/python3.4/dist-packages/django /db/models/query.py,第381行,获取
num = len(clone)
文件 /usr/local/lib/python3.4/dist-packages/django/db/ models / query.py,第240行,位于__len__
self._fetch_all()
文件 /usr/local/lib/python3.4/dist-packages/django/db/models/query中。 py,第1074行,位于_fetch_all
self._result_cache = list(self.iterator())
文件 /usr/local/lib/python3.4/dist-packages/django/db/models /query.py,第52行,位于__iter__
结果= editor.execute_sql()
在execute_sql
游标= self.connection.cursor()$ b中的文件 /usr/local/lib/python3.4/dist-packages/django/db/models/sql/compiler.py中,行846 $ b文件 /usr/local/lib/python3.4/dist-packages/django/db/backends/base/base.py,行231,在光标
中,cursor = self.make_debug_cursor(self._cursor ())
文件 /usr/local/lib/python3.4/dist-packages/django/db/backends/base/base.py,第204行,位于_cursor
self.ensure_connection( )
文件 /usr/local/lib/python3.4/dist-packages/django/db/backends/base/base.py,第199行,在sure_connection
self.connect()
文件 /usr/local/lib/python3.4/dist-packages/django/db/utils.py,第95行,位于__exit__
六。reraise(dj_exc_type,dj_exc_value,回溯)
文件 /usr/local/lib/python3.4/dist-packages/django/utils/six.py,第685行,以提高
的价值。with_traceback(tb)
File /usr/local/lib/python3.4/dist-packages/django/db/backends/base/base.py,在199行中,
self.connect()
文件 /usr/local/lib/python3.4/dist-packages/django/db/backends/base/base.py,连接$ b中的第171行$ b self.connection = self.get_new_connection(conn_params)
文件 /usr/local/lib/python3.4/dist-packages/django/db/backends/postgresql/base.py,行175,在get_new_connection
连接= Database.connect(** conn_params)
在连接$ b中,文件 /usr/local/lib/python3.4/dist-packages/psycopg2/__init__.py,第164行$ b conn = _connect(dsn,connection_factory = connection_factory,async = async)
django.db.utils.OperationalError:无法连接至服务器:连接超时
服务器是否在主机 myhostname上运行。 cltlezrr85xn.ap-northeast-1.rds.amazonaws.com(172 .--.--.--),并接受端口5432上的
TCP / IP连接吗?

感谢您的建议。

解决方案

编辑:



只需再次运行一次,并使用更简单的方法即可。



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



1)创建RDS,并将其匹配:

 环境变量-RDS控制台标签

RDS_HOSTNAME-端点(这是主机名)
RDS_PORT-端口
RDS_DB_NAME –数据库名
RDS_USERNAME –用户名
RDS_PASSWORD –您设置的密码为您的数据库

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实例作为源( awseb-z-afsafdsaf-stack-AWSEBSecurityGroup- asfdsadfasdf )。协议和端口范围应自动填充。



6)保存它



就是这样。






原文:



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



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



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

 环境变量-RDS控制台标签

RDS_HOSTNAME-端点(这是主机名)
RDS_PORT-端口
RDS_DB_NAME –数据库名称
RDS_USERNAME –用户名
RDS_PASSWORD –您为数据库设置的密码

2)使用例如 eb setenv



3)转到您的EC2 / EB实例并获取该安全组,例如 awseb-z-afsafdsaf-stack-AWSEBSecurityGroup-asfdsadfasdf ,对于负载均衡器: awseb-e-adsfadsf-stack-AWSEBLoadBalancerSecurityGroup-asdfadsf



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



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



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



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



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



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

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



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


My Django application cannot connect to RDS PostgreSQL when it deployed to EC2. but oddly, it works fine when it running in my desktop.

EC2 server and desktop were configured with python3, django1.9, apache2, and mod_wsgi_py3

here is my settings.py database settings:

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,
    }
}

and error.log from apache2:

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?

thanks for suggestions..

解决方案

Edit:

Just ran through this again, and have an easier way.

When you're trying to use an existing RDS database with Django on EC2 or EB, you'll have to adjust the security groups, and then get the proper parameters and set them as environment variables (RDS_*)

1) Create RDS, and match these up:

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) Set those using, for example, eb setenv

3) Go to your EC2/ EB instance and get the security group for that e.g. awseb-z-afsafdsaf-stack-AWSEBSecurityGroup-asfdsadfasdf

4) Go to the panel for your RDS instance, scroll down to Security Groups and take note of which security group it has. e.g. rds-launch-wizard-1 (ab-sdjfalkajsdf39)

5) Select the RDS security group, and add an Inbound rule with type: PostgreSQL (or whatever db you're using), and using the EC2 or EB instance that you got in step 2 as the source (awseb-z-afsafdsaf-stack-AWSEBSecurityGroup-asfdsadfasdf). Protocol and Port range should autopopulate.

6) Save it

That's it.


Original:

For anybody who stumbles upon this question:

When you're trying to use an existing RDS database with Django on EC2 or EB, you'll have to adjust the security groups, and then get the proper parameters and set them as environment variables (RDS_*)

1) Create RDS, and match these up:

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) Set those using, for example, eb setenv

3) Go to your EC2/ EB instance and get the security group for that e.g. awseb-z-afsafdsaf-stack-AWSEBSecurityGroup-asfdsadfasdf and for the load balancer: awseb-e-adsfadsf-stack-AWSEBLoadBalancerSecurityGroup-asdfadsf

4) Go to the panel for your RDS instance, scroll down to Security Groups and take note of which security group it has. e.g. rds-launch-wizard-1 (ab-sdjfalkajsdf39)

5) Click modify for the RDS instance, and in the Security Groups setting in the middle, add the load balancer security group you found above. It should have suggestions.

6) Go to the EC2 Dashboard and choose security groups from the menu on the left.

7) Select the load balancer security group, and add an Outbound rule. Type should be your RDS type (PostgreSQL) and the destination should be custom & the RDS instance's security group. Save.

8) Do the same for Inbound, use the same RDS type & Destination

9) Select the RDS security group, and add an Inbound rule, similar to 8, but using the EC2 or EB instance that you got in step 2.

10) Save, you're done. They should be able to work together now.

I'm not 100% certain all these steps are necessary, one or two might not be, but this got the job done for me.

这篇关于Django无法连接到RDS Postgresql的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆