Python 2.7 无法建立新连接:[Errno 111]连接被拒绝(elasticsearch)
我已经使用以下命令安装了elasticsearch:Python 2.7 无法建立新连接:[Errno 111]连接被拒绝(elasticsearch),python-2.7,
elasticsearch,Python 2.7,
elasticsearch,我已经使用以下命令安装了elasticsearch:pip install elasticsearch 安装后,我执行了以下命令: >>> from datetime import datetime >>> from elasticsearch import Elasticsearch # by default we connect to localhost:9200 >>> es = Elasticsearch() # create an
pip install elasticsearch
安装后,我执行了以下命令:
>>> from datetime import datetime
>>> from elasticsearch import Elasticsearch
# by default we connect to localhost:9200
>>> es = Elasticsearch()
# create an index in elasticsearch, ignore status code 400 (index already exists)
# but when I run this instruction:
>>> es.indices.create(index='my-index', ignore=400) // HERE IS THE PROBLEM
我得到这个错误:
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "elasticsearch/client/utils.py", line 69, in _wrapped
return func(*args, params=params, **kwargs)
File "elasticsearch/client/indices.py", line 110, in create
params=params, body=body)
File "elasticsearch/transport.py", line 327, in perform_request
status, headers, data = connection.perform_request(\
method, url, params, body, ignore=ignore, timeout=timeout)
File "elasticsearch/connection/http_urllib3.py", line 105, in perform_request
raise ConnectionError('N/A', str(e), e)
elasticsearch.exceptions.ConnectionError:
ConnectionError(<urllib3.connection.HTTPConnection object at 0xM3M>:
Failed to establish a new connection: [Errno 111] Connection refused) caused by:
NewConnectionError(<urllib3.connection.HTTPConnection object at 0xM3M>:
Failed to establish a new connection: [Errno 111] Connection refused)
回溯(最近一次呼叫最后一次):
文件“”,第1行,在
文件“elasticsearch/client/utils.py”,第69行,包装为
返回函数(*args,params=params,**kwargs)
文件“elasticsearch/client/index.py”,第110行,在create中
参数=参数,主体=主体)
文件“elasticsearch/transport.py”,第327行,在执行请求中
状态、标题、数据=连接。执行请求(\
方法、url、参数、正文、忽略=忽略、超时=超时)
文件“elasticsearch/connection/http_urllib3.py”,第105行,在执行请求中
升起连接器错误('N/A',str(e),e)
elasticsearch.exceptions.ConnectionError:
连接错误(:
无法建立新连接:[Errno 111]连接被拒绝),原因是:
NewConnectionError(:
无法建立新连接:[Errno 111]连接被拒绝)
您安装的是一个Python客户端,用于Python脚本和现有Elasticsearch集群之间的通信
正如您在评论中提到的,在您开始阅读的顶部,它说:
Elasticsearch的官方低级客户端。其目标是提供
Python中所有Elasticsearch相关代码的共同点;因为
它试图不受意见限制,并且非常可扩展
您可以将客户端配置为群集运行的主机和端口,然后连接到该客户端并在该群集上执行命令
在您的代码中,您将客户端配置为使用默认设置,该设置假定集群在localhost
上运行,并使用默认的elasticsearch端口9200
您需要对其进行配置和运行,然后才能将客户端连接到群集并与之通信。接下来的步骤很简单,您应该能够启动并运行elasticsearch
。仅将其分解,使用pip3安装elasticsearch
不会创建到elasticsearch
的连接。按照以下步骤设置、安装和启动连接:
安装必要的软件包
由于Elasticsearch
运行在Java
之上,因此需要安装Java开发工具包(JDK)。通过在终端中运行以下命令,可以检查是否安装了Java
:
$ java -version
如果您没有java
,可以通过以下方式安装默认JDK:
$ sudo apt install openjdk-8-jre-headless
运行java-version
检查是否安装了java
要允许通过HTTPS访问存储库,您需要安装APT传输包:
$ sudo apt install apt-transport-https
下载并安装Elasticsearch(在Ubuntu上)
首先,使用wget
命令更新Elasticsearch存储库的GPG密钥,以提取公钥():
将存储库添加到您的系统:
$ echo "deb https://artifacts.elastic.co/packages/7.x/apt stable main" | sudo tee -a /etc/apt/sources.list.d/elastic-7.x.list
现在,首先更新软件包索引,然后运行安装,安装elasticsearch
:
$ sudo apt update
$ sudo apt install elasticsearch
启动Elasticsearch
Elasticsearch
在启动之前不会运行。此外,当您重新启动计算机时,您需要重新运行elasticsearch
服务,因为它不会自动启动
要重新加载systemd配置,请运行:
$ sudo systemctl daemon-reload
启用elasticsearch
服务:
$ sudo systemctl enable elasticsearch.service
现在,您可以开始elasticsearch
:
$ sudo systemctl start elasticsearch.service
此时,elasticsearch
将在每次重新启动系统时启动
要测试设置,请尝试运行http://localhost:9200/
在浏览器的url栏上。您应该会在屏幕上看到一些JSON数据。更好的是,在您的终端上,尝试:
$ curl localhost:9200
这就完成了设置、安装以及如何启动elasticsearch
服务。现在,您可以尝试在终端上运行命令,一切都会正常工作
>>> from datetime import datetime
>>> from elasticsearch import Elasticsearch
>>> es = Elasticsearch()
>>> es.indices.create(index='my-index', ignore=400)
{'acknowledged': True, 'shards_acknowledged': True, 'index': 'my_index'}
elasticsearch
是否在您的计算机上本地运行?它是通过以下方式安装的:pip install elasticsearch作为python库运行否,您安装的只是一个用于与现有集群通信的python客户端,默认情况下,它假定在localhost:9200
上运行。实际上,我刚刚开始学习如何使用elasticsearch。。。我用这段代码作为起点:看看官方的答案。
>>> from datetime import datetime
>>> from elasticsearch import Elasticsearch
>>> es = Elasticsearch()
>>> es.indices.create(index='my-index', ignore=400)
{'acknowledged': True, 'shards_acknowledged': True, 'index': 'my_index'}