close

[Solved] NewConnectionError(: Failed to establish a new connection: [Errno 111] Connection refused)

Hello Guys, How are you all? Hope You all Are Fine. Today I get the following error NewConnectionError(: Failed to establish a new connection: [Errno 111] Connection refused) in Python. So Here I am Explain to you all the possible solutions here.

Without wasting your time, Let’s start This Article to Solve This Error.

How NewConnectionError(: Failed to establish a new connection: [Errno 111] Connection refused) Error Occurs?

Today I get the following error NewConnectionError(: Failed to establish a new connection: [Errno 111] Connection refused) in Python.

How To Solve NewConnectionError(: Failed to establish a new connection: [Errno 111] Connection refused) Error ?

  1. How To Solve NewConnectionError(: Failed to establish a new connection: [Errno 111] Connection refused) Error ?

    To Solve NewConnectionError(: Failed to establish a new connection: [Errno 111] Connection refused) Error You can also start this service by using $ sudo systemctl start elasticsearch.service. Another possibility is that maybe you configured elastic variables in a wrong way.

Solution 1


First of all make sure you have installed and configured ElasticSearch service properly (you can use this document).This error is saying that your code could not connect to ElasticSearch service with your current configs. Check if your elastic service is up and running. In Linux based OS you can check it by $ sudo systemctl status elasticsearch.service and in the result page you should see the line Active: active (running). After this step check the url http://localhost:9200/ (if you did not change the default settings) and if everything is OK you should see the following results:

{
  "name" : "some_name-System-Product-Name",
  "cluster_name" : "elasticsearch",
  "cluster_uuid" : "erP_jdhLRvuwqLkx1_tehw",
  "version" : {
    "number" : "7.9.0",
    "build_flavor" : "default",
    "build_type" : "deb",
    "build_hash" : "a479a2a7fce0389512d6a9361301708b92dff667",
    "build_date" : "2020-08-11T21:36:48.204330Z",
    "build_snapshot" : false,
    "lucene_version" : "8.6.0",
    "minimum_wire_compatibility_version" : "6.8.0",
    "minimum_index_compatibility_version" : "6.0.0-beta1"
  },
  "tagline" : "You Know, for Search"
}

You can also start this service by using $ sudo systemctl start elasticsearch.service. Another possibility is that maybe you configured elastic variables in a wrong way. For example instead of default port (i.e. 9200) using wrong port will also raise the same error. In following example you can see an example of Elastic configs which will goes in your project’s settings.py:

# ElasticSearch configs
ELK_BASE_URL = 'elasticsearch://{username}:{password}@{host_ip}:{host_port}'
ELASTIC_SEARCH_URL = ELK_BASE_URL.format(
    username='ELASTICSEARCH_USER',
    password='ELASTICSEARCH_PASS',
    host_ip='ELASTICSEARCH_HOST',
    host_port='ELASTICSEARCH_PORT'
)
ELASTICSEARCH_DSL = {
    'default': {
        'hosts': [ELASTIC_SEARCH_URL]
    },
}

Summery

It’s all About this issue. Hope all solution helped you a lot. Comment below Your thoughts and your queries. Also, Comment below which solution worked for you? Thank You.

Also Read

Leave a Comment