【问题标题】:Traceback error when running elasticsearch and kibana on python在 python 上运行 elasticsearch 和 kibana 时出现回溯错误
【发布时间】:2020-06-19 19:20:39
【问题描述】:

Traceback (most recent call last):
  File "/usr/local/lib/python3.7/site-packages/urllib3/connection.py", line 157, in _new_conn
    (self._dns_host, self.port), self.timeout, **extra_kw
  File "/usr/local/lib/python3.7/site-packages/urllib3/util/connection.py", line 84, in create_connection
    raise err
  File "/usr/local/lib/python3.7/site-packages/urllib3/util/connection.py", line 74, in create_connection
    sock.connect(sa)
ConnectionRefusedError: [Errno 111] Connection refused

我在上面有一条错误消息。我的python代码(main.py)如下。我正在使用 docker-compose 来实现 elasticsearch 和 kibana。我应该如何修复我的代码?我的问题是来自 main.py 还是 docker-compose.yml?

我的 main.py 在下面。

from datetime import datetime
from elasticsearch import Elasticsearch


es = Elasticsearch()

doc = {
	'author': 'taqkarim',
	'text': 'Testing ElasticSearch',
	'timestamp': datetime(2012, 9, 16, 0, 0), # in the far past
}

# load a tweet into ES
res = es.index(index="test-index", doc_type='tweet', id=1, body=doc)
print(res['result'])

# query by id
res = es.get(index="test-index", doc_type='tweet', id=1)
print(res['_source'])

es.indices.refresh(index="test-index")

# search
res = es.search(index="test-index", body={"query": {"match_all":{}}})
print("Got %d Hits:" % res['hits']['total'])
for hit in res['hits']['hits']:
	print("%(timestamp)s %(author)s: %(text)s" % hit["_source"])

【问题讨论】:

  • 检查你的 es ip

标签: python docker elasticsearch docker-compose kibana


【解决方案1】:

您应该将 Elasticsearch 主机和端口明确设置为:

es = Elasticsearch([
    {'host': 'my-es-host', 'port': 9200},
])

此外,请检查您的 Elasticsearch 容器是否已打开其 9200 端口,例如

services:
  elasticsearch:
    image: docker.elastic.co/elasticsearch/elasticsearch:7.6.1
    environment:
      .... some env vars
    ports:
      - 9200:9200

【讨论】:

  • 非常感谢。你的回答让我再次思考我的代码。而且,我发现我做了docker-compose down 而没有docker-compose up -d
猜你喜欢
  • 1970-01-01
  • 2018-06-23
  • 2018-02-12
  • 1970-01-01
  • 2015-03-25
  • 1970-01-01
  • 2019-11-04
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多