简体   繁体   中英

Failed to open TCP connection to localhost:9200 (Connection refused - connect(2) for “localhost” port 9200) (Faraday::ConnectionFailed)

I am getting the error: Puma caught this error: Failed to open TCP connection to localhost:9200 (Connection refused - connect(2) for "localhost" port 9200) (Faraday::ConnectionFailed) in my rails application in ubuntu 14.04.

below is the output of my elastic search in terminal:

prashanth@prashanth-OptiPlex-390:~$ sudo service elasticsearch restart
 * Starting Elasticsearch Server                                                                                                               prashanth@prashanth-OptiPlex-390:~$ sudo service elasticsearch start
 * Starting Elasticsearch Server                                                                                                                * Already running.                                                                                                                     [ OK ] 
prashanth@prashanth-OptiPlex-390:~$ curl -X GET 'http://localhost:9200'
curl: (7) Failed to connect to localhost port 9200: Connection refused
prashanth@prashanth-OptiPlex-390:~$ 

here are the my elastic search settings in /etc/elasticsearch/elasticsearch.yml file

network.host: 127.0.0.1
transport.tcp.port: 9200
http.port: 9200

error description:

Puma caught this error: Failed to open TCP connection to localhost:9200 (Connection refused - connect(2) for "localhost" port 9200) (Faraday::ConnectionFailed)
/home/prashanth/.rvm/rubies/ruby-2.3.1/lib/ruby/2.3.0/net/http.rb:882:in `rescue in block in connect'
/home/prashanth/.rvm/rubies/ruby-2.3.1/lib/ruby/2.3.0/net/http.rb:879:in `block in connect'
/home/prashanth/.rvm/rubies/ruby-2.3.1/lib/ruby/2.3.0/timeout.rb:91:in `block in timeout'
/home/prashanth/.rvm/rubies/ruby-2.3.1/lib/ruby/2.3.0/timeout.rb:101:in `timeout'
/home/prashanth/.rvm/rubies/ruby-2.3.1/lib/ruby/2.3.0/net/http.rb:878:in `connect'
/home/prashanth/.rvm/rubies/ruby-2.3.1/lib/ruby/2.3.0/net/http.rb:863:in `do_start'
/home/prashanth/.rvm/rubies/ruby-2.3.1/lib/ruby/2.3.0/net/http.rb:852:in `start'
/home/prashanth/.rvm/rubies/ruby-2.3.1/lib/ruby/2.3.0/net/http.rb:1398:in `request'
/home/prashanth/.rvm/gems/ruby-2.3.1/gems/faraday-0.10.1/lib/faraday/adapter/net_http.rb:83:in `perform_request'
/home/prashanth/.rvm/gems/ruby-2.3.1/gems/faraday-0.10.1/lib/faraday/adapter/net_http.rb:41:in `block in call'
/home/prashanth/.rvm/gems/ruby-2.3.1/gems/faraday-0.10.1/lib/faraday/adapter/net_http.rb:88:in `with_net_http_connection'
/home/prashanth/.rvm/gems/ruby-2.3.1/gems/faraday-0.10.1/lib/faraday/adapter/net_http.rb:33:in `call'
/home/prashanth/.rvm/gems/ruby-2.3.1/gems/faraday-0.10.1/lib/faraday/rack_builder.rb:139:in `build_response'
/home/prashanth/.rvm/gems/ruby-2.3.1/gems/faraday-0.10.1/lib/faraday/connection.rb:377:in `run_request'
/home/prashanth/.rvm/gems/ruby-2.3.1/gems/elasticsearch-transport-5.0.0/lib/elasticsearch/transport/transport/http/faraday.rb:23:in `block in perform_request'
/home/prashanth/.rvm/gems/ruby-2.3.1/gems/elasticsearch-transport-5.0.0/lib/elasticsearch/transport/transport/base.rb:257:in `perform_request'
/home/prashanth/.rvm/gems/ruby-2.3.1/gems/elasticsearch-transport-5.0.0/lib/elasticsearch/transport/transport/http/faraday.rb:20:in `perform_request'
/home/prashanth/.rvm/gems/ruby-2.3.1/gems/elasticsearch-transport-5.0.0/lib/elasticsearch/transport/client.rb:128:in `perform_request'

In the settings you posted, your HTTP and TCP ports have the same port number assigned

network.host: 127.0.0.1
transport.tcp.port: 9200
http.port: 9200

I would suggest changing the transport.tcp.port and trying again.

Try using the defaults unless you already have port 9300 in use:

network.host: 127.0.0.1
transport.tcp.port: 9300
http.port: 9200

If you are still facing issues, you may need to open up the ports on your firewall program. On Ubuntu, this is likely UFW.

If your service script has a status function, this may tell you if your Elasticsearch instance has started up properly or not. Failing this, you can use ps aux to determine if it is running.

$ sudo service elasticsearch status

Or

$ ps aux | grep elasticsearch

As pointed out in similar questions, cURL can give a refused connection error when Elasticsearch isn't running (source: Faraday::ConnectionFailed, Connection refused - connect(2) for “localhost” port 9200 Error Ruby on Rails ).

Do you have Docker? The only solution for me was unninstall Docker completely. Otherwhise, i think the problem is something about the port that docker use.

If you don't use alot like me, just remove it and you're good to go.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM