重建索引时,Django的草垛与Elasticsearch找不到数据库(Django haystac

2019-10-24 08:58发布

我添加草堆到已经成功地部署到AWS ElasticBeanstalk实例的Django项目。 当我运行草堆在本地,但在AWS环境中工作rebuild_index 。 我得到这个错误:

Failed to clear Elasticsearch index: ConnectionError(('Connection aborted.', error(111, 'Connection refused'))) caused by: ProtocolError(('Connection aborted.', error(111, 'Connection refused')))
All documents removed.
ERROR:root:Error updating api using default 
Traceback (most recent call last):
  File "/opt/python/run/venv/local/lib/python2.7/site-packages/haystack/management/commands/update_index.py", line 188, in handle_label
    self.update_backend(label, using)
  File "/opt/python/run/venv/local/lib/python2.7/site-packages/haystack/management/commands/update_index.py", line 219, in update_backend
    total = qs.count()
  File "/opt/python/run/venv/local/lib/python2.7/site-packages/django/db/models/query.py", line 318, in count
    return self.query.get_count(using=self.db)
  File "/opt/python/run/venv/local/lib/python2.7/site-packages/django/db/models/sql/query.py", line 464, in get_count
    number = obj.get_aggregation(using, ['__count'])['__count']
  File "/opt/python/run/venv/local/lib/python2.7/site-packages/django/db/models/sql/query.py", line 445, in get_aggregation
    result = compiler.execute_sql(SINGLE)
  File "/opt/python/run/venv/local/lib/python2.7/site-packages/django/db/models/sql/compiler.py", line 838, in execute_sql
    cursor = self.connection.cursor()
  File "/opt/python/run/venv/local/lib/python2.7/site-packages/django/db/backends/base/base.py", line 162, in cursor
    cursor = self.make_debug_cursor(self._cursor())
  File "/opt/python/run/venv/local/lib/python2.7/site-packages/django/db/backends/base/base.py", line 135, in _cursor
    self.ensure_connection()
  File "/opt/python/run/venv/local/lib/python2.7/site-packages/django/db/backends/base/base.py", line 130, in ensure_connection
    self.connect()
  File "/opt/python/run/venv/local/lib/python2.7/site-packages/django/db/utils.py", line 97, in __exit__
    six.reraise(dj_exc_type, dj_exc_value, traceback)
  File "/opt/python/run/venv/local/lib/python2.7/site-packages/django/db/backends/base/base.py", line 130, in ensure_connection
    self.connect()
  File "/opt/python/run/venv/local/lib/python2.7/site-packages/django/db/backends/base/base.py", line 119, in connect
    self.connection = self.get_new_connection(conn_params)
  File "/opt/python/run/venv/local/lib/python2.7/site-packages/django/db/backends/postgresql_psycopg2/base.py", line 176, in get_new_connection
    connection = Database.connect(**conn_params)
  File "/opt/python/run/venv/local/lib/python2.7/site-packages/psycopg2/__init__.py", line 164, in connect
    conn = _connect(dsn, connection_factory=connection_factory, async=async)
OperationalError: could not connect to server: Connection refused
        Is the server running on host "localhost" (127.0.0.1) and accepting
        TCP/IP connections on port 5432?

看来,草垛正试图连接到我的本地设置中指定的数据库,而不是Postgres的RDS我已经为我的AWS ElasticBeanstalk环境特地指定即使“数据库”设置的AWS作品./manage.py loaddata

    if 'RDS_DB_NAME' in os.environ:
        DATABASES = {
            'default': {
                'ENGINE': 'django.db.backends.postgresql_psycopg2',
                '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'],
            }
        }
    else:
        DATABASES = {
            'default': {
                'ENGINE': 'django.db.backends.postgresql_psycopg2',
                'NAME': 'hhwc',
                'HOST': 'localhost',
                'PORT': '5432',
            }
        }

有什么不对这个“数据库”的设置,还是草堆看别的地方找到的数据库应该连接到用于生成索引的位置?

任何故障排除这种帮助是值得欢迎。 提前致谢。

Answer 1:

从回答另一个问题,SO :

该环境变量没有设置的virtualenv内,而是由另一个脚本。 首先,你必须激活virualenv。

源的/ opt /蟒蛇/运行/ VENV /斌/启动

然后,你需要在“当前”目录激活ENV脚本加载的变量。

源极/选择/蟒/电流/ env的

魔豆RDS变量现在设置,并准备通过您的SSH执行任意脚本使用“。



Answer 2:

错误是不相关的数据库,但是你的草垛配置。 检查你使用那里的URL。 请确保您使用:80的主机名之后,如草垛默认端口9200,如果你不明确给出一个与AWS将它在80端口。



Answer 3:

该错误是基本,Django项目是无法连接到亚马逊弹性搜索实例。 下面是与AWS elasticsearch连接方式

首先,你需要使用安装requests_aws4auth

 sudo pip install requests_aws4auth

现在,您需要与亚马逊elasticsearch实例连接

 from requests_aws4auth import AWS4Auth
 from elasticsearch import Elasticsearch, RequestsHttpConnection
 import elasticsearch
 host = 'YOUR_HOST without putting port number'
 awsauth = AWS4Auth('YOUT_ACCESS_KEY', 'YOUR_SECRET_KEY', 'REGION', 'es')

 HAYSTACK_CONNECTIONS = {
    'default': {
     'ENGINE': 'haystack.backends.elasticsearch_backend.ElasticsearchSearchEngine',
     'URL': host,
     'INDEX_NAME': 'haystack',
     'KWARGS': {
         'port':443,
         'http_auth': awsauth,
         'use_ssl': True,
         'verify_certs': True,
         'connection_class': elasticsearch.RequestsHttpConnection,
     }
 },

}

对于那些谁仍然会面临一些问题,你需要创建索引使用以下

 curl -XPUT 'Your_AWS_ELASTICSEARCH_URL/haystack/'


文章来源: Django haystack with Elasticsearch cannot find database when rebuilding index