70

I've just started working with Cassandra (datastax), version 2.1.3 and cqlsh version 5.0.1.

Cassandra starts up fine and the cluster is operational instantly.

Cqlsh is not working (on any of the nodes) and emits the following error:

Connection error: ('Unable to connect to any servers', {'127.0.0.1': error(111, "Tried connecting to [('127.0.0.1', 9042)]. Last error: Connection refused")})

I have tried starting cqlsh up with the hosts own ip, other hosts ip, different ports yet the result remains the same - always connection refused.

PiotrWolkowski
  • 8,408
  • 6
  • 48
  • 68
Chros
  • 701
  • 1
  • 5
  • 4
  • Considering I, and with the help offered here, couldn't solve the issue with 2.1.3 (which admittedly is still in development) I reconfigured the servers with 2.0.13 (stable). Cqlsh still gives a localhost:9160 error (despite the rpc_address being specified as the hosts) but it works by manually specifying the ip when starting it up `cqlsh 1.1.2.2 9160` – Chros Mar 20 '15 at 15:42
  • Based on the fact that you downgraded to 2.0.13 and it works, I'm guessing that you had some kind of incorrect setting between `listen_address`, `rpc_address` and `broadcast_rpc_address`...the latter setting being new with 2.1.x. I would be interested to know the `listen_address` and `rpc_address` settings (not necessarily the values, but how they relate to the 1.1.2.2 IP used with cqlsh). If you try 2.1.x again, set `listen_address` to your local IP, `rpc_address` to 0.0.0.0, and `broadcast_rpc_address` to your external IP. – Aaron Mar 20 '15 at 19:04
  • In the case of this setup with `1.1.2.2` being the host IP (host only has an internal IP) the values were: `listen_address: 1.1.2.2` `rpc_address: 0.0.0.0` `broadcast_rpc_address: 1.1.2.2`. The broadcast address was required (cassandra logs). Tried most combinations yet none proved fruitful. Ended up having to remove/comment it out for 2.0.13 so it could very well be the `broadcast_rpc_address` creating the problems. Not sure which other value I could have given it. Wouldn't it have thrown an error anywhere else? – Chros Mar 21 '15 at 19:15

22 Answers22

27

You need to edit cassandra.yaml on the node you are trying to connect to and set the node ip address for rpc_address and listen_address and restart Cassandra.

rpc_address is the address on which Cassandra listens to the client calls.

listen_address is the address on which Cassandra listens to the other Cassandra nodes.

jny
  • 8,007
  • 3
  • 37
  • 56
  • They are defined within `cassandra.yaml`. `rpc_address` is set to 0.0.0.0 `listen_address` is set to the node's own ip. Also have `broadcast_rpc_address`on the same ip as the listen address. Could it be the rpc_address needs to be set to something else? Thanks – Chros Mar 18 '15 at 15:25
  • Try setting rpc_address to the node's ip – jny Mar 18 '15 at 19:39
  • Tried it. The exact same connection refused error on `127.0.0.1` is produced. – Chros Mar 19 '15 at 09:08
  • 2
    let's say the ip of the server Cassandra is on is `1.2.3.4`. Set rpc_address to `1.2.3.4`. Restart Cassandra. Then try `cqlsh 1.2.3.4` – jny Mar 19 '15 at 13:16
  • Getting the same error but instead of `127.0.0.1` it's the servers ip in the error (so `1.2.3.4`going by your example). No mention of the port for cqlsh connects it to 9042 and gives the error. If `cqlsh 1.2.3.4 9160` it throws an AsyncoreConnection error. – Chros Mar 19 '15 at 13:25
  • Have you see this? http://stackoverflow.com/questions/27004773/cassandra-cqlsh-unable-to-connect-to-any-servers-127-0-0-19160-closed-is-a – jny Mar 19 '15 at 13:35
  • Yeah. Tried to remove everything but still get the same error. Tried other ports and, I may be wrong here, but it seems the Asyncore error is thrown whenever it's used on an active port. – Chros Mar 19 '15 at 14:16
  • 1
    Thanks for the help - I reconfigured with 2.0.13 (which is stable) and it ended up working – Chros Mar 20 '15 at 15:42
  • `rpc_address` is optional. Leaving rpc_address blank has the same effect as on listen_address. – Devy Jan 31 '20 at 04:22
21

Try to change the rpc_address to point to the node's IP instead of 0.0.0.0 and specify the IP while connecting to the cqlsh, as if the IP is 10.0.1.34 and the rpc_port left to the default value 9160 then the following should work:

cqlsh 10.0.1.34 9160 

Or:

cqlsh 10.0.1.34 

Also make sure that start_rpc is set to true in /etc/cassandra/cassandra.yaml configuration file.

user9269906
  • 111
  • 1
  • 6
Ruba
  • 494
  • 3
  • 7
  • 1
    The `rpc_port`is not defined yet it defaults to `9042` , not `9160`it seems. Have tried chaing the `rpc_address` but the error remains the same (instead of `127.0.0.1` the error is on the host ip used). – Chros Mar 19 '15 at 13:29
  • 2
    according to the documentation the default for the rpc_port is 9160 , however it sounds like cqlsh in latest Cassandra versions uses the value of the native_transport_port property in cassandra.yaml which has 9042 default value. Can you specify any free port for native_transport_port and check if the error changes ? – Ruba Mar 19 '15 at 15:17
  • Defined `native_transport_value: 9160` yet error remains the same on port `9042` – Chros Mar 19 '15 at 15:33
  • weird, I thought it would cause conflict if affected as 9160 is already reserved for rpc_port. have you restarted Cassandra after the edit? if not I suggest to select another port and restart. Also, is port 8042 is open in firewall ? – Ruba Mar 19 '15 at 16:13
  • 1
    Yeah, cassandra is restarted after every change. Did you mean port `9042`instead of `8042`? In which case, yes - (as is 9160). – Chros Mar 20 '15 at 14:17
  • 1
    Thanks for the help - I reconfigured with 2.0.13 (which is stable) and it ended up working – Chros Mar 20 '15 at 15:42
12

If you check the system.log file for cassandra in /var/log/cassandra, you will see that this problem occurs because the rpc server has not started.

By default, the start_rpc is set to false in the cassandra.yaml file. Set it to start_rpc: true and then try again.

From Cassandra 3.0 onwards, start_rpc is set to true by default. https://docs.datastax.com/en/cassandra/3.0/cassandra/configuration/configCassandra_yaml.html

sazzad
  • 5,740
  • 6
  • 25
  • 42
Rokin Maharjan
  • 629
  • 7
  • 19
8

This is because of some pre-configurations in the cassandra. And to fix this, there are following commented lines in the cassandra-env.sh file:

#add this if you’re having trouble connecting:
#JVM_OPTS="$JVM_OPTS -Djava.rmi.server.hostname=<public name>

where you have to start fixing the error.

Open the terminal and start fixing using the following easy steps:

Step-1

use

sudo nano /etc/cassandra/cassandra-env.sh

this command and search for JVM_OPTS="$JVM_OPTS -Djava.rmi.server.hostname= uncomment the line by simply removing # attached in the start of the line.

NOTE: If you have never opened this file to fix this error then you will find JVM_OPTS="$JVM_OPTS -Djava.rmi.server.hostname=<public name>

Step-2 Now replace <public name> with 127.0.0.1 or the server IP.

Step-3 Save the file and restart the cassandra using systemctl restart cassandra.service (if you haven't the server is alredy running). OR start the cassandra using systemctl start cassandra.service (if the server is not running).

Step-4 Check the status either by using sudo service cassandra status or systemctl status cassandra.service.

Note: Once check the system monitor whether cassandra is running there or not.

Try cqlsh now. It'll work.

Note: nano is editor you can use other editors you are comfortable with.

shaaonee
  • 34
  • 6
Gaurav Paliwal
  • 1,556
  • 16
  • 27
  • 1
    great , my cassandra active now but still cqlsh ('Unable to connect to any servers', {'127.0.0.1': error(111, "Tried connecting to [('127.0.0.1', 9042)]. Last error: Connection refused")}) – MehrAmoon Apr 07 '19 at 15:31
5

Look for native_transport_port in /etc/cassandra/cassandra.yaml The default is 9842.

native_transport_port: 9842

For connecting to localhost with cqlsh, this port worked for me.

cqlsh 127.0.0.1 9842

Amit Teli
  • 875
  • 11
  • 25
3

It is a good idea to check the cassandra log if even the server is running. I was getting exactly the same message and unable to do anything with that and then I found out that there are errors in the log and the system is actually not working.

Silly, I know, but could happen...

TomS
  • 1,159
  • 2
  • 17
  • 35
3

Had same problem recently after downgrade from Cassandra 3.0 to Cassandra 2.2 on ArchLinux.

Unlike above solutions my problem wasn't in .cassandra, but version 3.0 left its configuration in /var/lib/cassandra directory.

Following commands solved my problem:

sudo rm -R /var/lib/cassandra
sudo rm -R /var/log/cassandra
sudo rm -R /usr/share/cassandra

Then i installed cassandra and everything worked again :)

Michal
  • 1,955
  • 5
  • 33
  • 56
3

Try to change the rpc_address to point to the node's IP instead of 0.0.0.0 and specify the IP while connecting to the cqlsh, as if the IP is 10.0.2.64 and the rpc_port left to the default value 9160 then the following should work:

cqlsh 10.0.2.64 9160 

OR

cqlsh 10.0.2.64

Also make sure that start_rpc is set to true in /etc/cassandra/cassandra.yaml configuration file.

Srikant Patra
  • 399
  • 4
  • 5
3

The first and foremost step to diagnose is to check the Cassandra logs.

Find out the process and there will be hints in the command line parameters to tell you where the logs files are.

ps aux | grep cassandra

In my case, it was in default location /etc/cassandra/conf/cassandra.yaml (if you installed the yum package).

Make sure three things are configured and configured the same host/ip.

  1. listen_address - you may leave it blank it will default to hostname/first IP binding with eth0 network card
  2. rpc_address - this is optional, leave blank if you want it to be same as listen_address
  3. seeds - this is a double quoted string of comma-separated list of IP addresses or hostnames. This was default to a hardcoded "127.0.0.1", changing it to the same as list_address and restart of Cassandra service made it work for me.

References:

Devy
  • 9,655
  • 8
  • 61
  • 59
  • 1
    Thanks a lot for this, I think the seeds is what got it all working finally. I also had to set start_rpc to true. It is incredibly unintuitive why I would have to turn on rpc when I just want to connect using the native client that happily works on localhost out of the box. – Researcher Sep 27 '20 at 20:26
2

I was experiencing the same issue with Cassandra 3.11.0, anytime I changed the address of rpc or listen address cqlsh wouldn't work. I had to add the same local ip to seeds

So after much trial and error my working cassandra.yml ended up like this:

class-name: org.apache.cassandra.locator.SimpleSeedProvider
parameters: 
    -seeds: "192.168.0.30"

listen_adress: 192.168.0.30
rpc_address: 192.168.0.30
Eddie Martinez
  • 13,582
  • 13
  • 81
  • 106
2

For me it turned out that the service wasn't running at all. Check with

service cassandra status

If you got the same error as I got, or another type, then messing around with IP addresses won't solve your problem at all.

The error I got:

cassandra dead but pid file exists

Edit: This was the solution for my problem: https://stackoverflow.com/a/46743119/3881406

Andras Kloczl
  • 8,415
  • 2
  • 21
  • 23
1

try changing the native_transport_protocol to port 9160 (if it is set to anything other than 9160; it might be pointing to 9042). Check your logs and see on which port cassandra is listening for CQL clients?

Paweł Gościcki
  • 9,066
  • 5
  • 70
  • 81
  • 1
    I dont know how it would make any difference but one correction it's `native_transport_port` not `native_transport_protocol` – Kuldeep Dangi Jun 06 '17 at 07:03
1

Got into this issue for [cqlsh 5.0.1 | Cassandra 3.11.4 | CQL spec 3.4.4 | Native protocol v4] had to set start_native_transport: true in cassandra.yaml file.

For verification,

  • try opening tailf /var/log/cassandra/system.log file in one-tab
  • update cassandra.yaml
  • restart cassandra sudo service cassandra restart

In logfile is shows.

INFO  [main] 2019-03-15 19:53:06,156 Server.java:156 - Starting listening for CQL clients on /10.139.45.34:9042 (unencrypted)...
Sumit Murari
  • 1,597
  • 3
  • 28
  • 43
0

I was trying to run it on a network drive. So I increased the client_timeout in cqlsh and it worked.

spenibus
  • 4,339
  • 11
  • 26
  • 35
0

Make sure that you also set "seeds" to the address which you provided at "listen_address"

Erhard Dinhobl
  • 1,646
  • 18
  • 34
0

Try to telnet to the given address. In my case, there was a firewall in place blocking me.

Jason White
  • 4,462
  • 4
  • 23
  • 23
0

When I installed Cassandra 3.11.1, I came across this problem. I checked the /var/log/cassandra/cassandra.log and found this error Exception encountered during startup....It is a bug and already reported. The original post link https://issues.apache.org/jira/browse/CASSANDRA-14173.

The solution is to downgrade Cassandra to 3.0

  1. download Cassandra rpm

curl -O https://www.apache.org/dist/cassandra/redhat/30x/cassandra-3.0.15-1.noarch.rpm

or

wget https://www.apache.org/dist/cassandra/redhat/30x/cassandra-3.0.15-1.noarch.rpm

  1. rpm -ivh cassandra-3.0.15-1.noarch.rpm
  2. service cassandra start
  3. service cassandra status # check cassandra status

cassandra (pid 2322) is running...

  1. cqlsh # start cassandra
Hunter
  • 21
  • 3
0

cqlsh --cqlversion="3.4.0" Connected to Test Cluster at 127.0.0.1:9042. [cqlsh 5.0.1 | Cassandra 3.0.9 | CQL spec 3.4.0 | Native protocol v4]

try the above command. It works for me.

  • cqlsh --cqlversion="3.4.0" Connection error: ('Unable to connect to any servers', {'127.0.0.1:9042': error(61, "Tried connecting to [('127.0.0.1', 9042)]. Last error: Connection refused")}) – Joe Jan 06 '21 at 01:38
0

First, you need to check the port 9042 whether this open or not if open then check below things like rpc_address and listen_address. you should set the server own IP address instead 0.0.0.0 or 127.0.0.1.

If all okay then please run below command to connect cqlsh.

cqlsh IP address 9042

If you enable authentication and authorization you have to put user/pass with this command.

LetsNoSQL
  • 1,478
  • 1
  • 11
  • 23
0

Check for correct IP address in the cassandra.yaml file. Majority of times the error is due to incorrect IP address of your system also the username and password too.

After doing so initiate cqlsh by the command :-

cqlsh 10.31.79.1 -u cassandra -p cassandra

0

check what the CASSANDRA_HOME environment variable points to. I had a old version of cassandra to which it was set. I had reset to the 3.11.6 installation that I upgraded and it started working.

kiran
  • 21
  • 3
0

Another case that wasn't mentioned for receiving the Unable to connect to any servers error is were the Client-to-Node Encryption is enabled in the client_encryption_options block in the cassandra.yaml file.

In that case you need to add the --ssl flag in order to connect:

cqlsh --ssl.

Rot-man
  • 18,045
  • 12
  • 118
  • 124