【问题标题】:Elasticsearch refusing connection through Python clientElasticsearch拒绝通过Python客户端连接
【发布时间】:2021-09-24 06:05:36
【问题描述】:

在使用 ES 进行测试时,我正在努力让它与它的 Python 客户端一起工作。我按照快速入门指南设置了运行 ES 和 Kibana 的本地 Docker 容器。为了测试安装,我使用 Postman 向 http://localhost:9200/ 发送了一些请求,该请求以正确的数据响应。

但是 Python 客户端无法连接 - 通过 Pip 安装它,导入它并像这样设置它:

from elasticsearch import Elasticsearch

es = Elasticsearch(
    ['localhost'],
    scheme="http",
    port=9200,
)

if not es.ping():
    raise BaseException("Connection failed")

抛出的错误:

Failed to establish a new connection: [Errno 111] Connection refused

这是我的 docker-compose 的摘录:

es01:
    image: docker.elastic.co/elasticsearch/elasticsearch:7.13.3
    container_name: es01
    environment:
      - node.name=es01
      - cluster.name=es-docker-cluster
      - discovery.seed_hosts=es02,es03
      - cluster.initial_master_nodes=es01,es02,es03
      - bootstrap.memory_lock=true
      - "ES_JAVA_OPTS=-Xms512m -Xmx512m"
    ulimits:
      memlock:
        soft: -1
        hard: -1
    volumes:
      - data01:/usr/share/elasticsearch/data
    ports:
      - 9200:9200
    networks:
      - elastic

请注意,我的 python 容器与 ES 共享网络 - 这在这里起到了显着的作用吗?

【问题讨论】:

    标签: python elasticsearch


    【解决方案1】:

    原来它与网络有关。

    我已通过在与我的 Python 应用程序相同的网络中包含 ES 和 Kibana 并将初始化更改为:

    from elasticsearch import Elasticsearch
    
    es = Elasticsearch(
        ['es01'],
        scheme="http",
        port=9200,
    )
    if not es.ping():
        print("Connection failed")
    else:
        print("Connection successful")
    

    es01 引用了运行 Elasticsearch 的 docker 容器的名称。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2020-10-27
      • 2019-02-28
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-04-22
      • 2016-09-15
      • 2023-04-01
      相关资源
      最近更新 更多