19

I know there are several threads about the NoHostAvailableException but they simply don't provide a solution to my problem.

I can't connect to Cassandra with the Datastax Java Cassandra Driver. I get the Error:

com.datastax.driver.core.exceptions.NoHostAvailableException: All host(s) tried for query failed (tried: [/54.221.241.107])

I am sure that the configuration is correct. I've set the configuration in cassandra.yaml:

start_native_transport: true 
# port for the CQL native transport to listen for clients on 
native_transport_port: 9042 

My Cassandra installation is a standard installation on a EC2 instance on AWS. I've configured AWS to allow port 9042.

Cassandra is running on Windows Server 2008 R2 and I also configured the firewall to inbound and outbound connection on 9042.

My code looks like the following:

cluster = Cluster.builder()
  .withPort(9042)
  .addContactPoint("54.221.241.107").build();

I don't know what to do anymore since I always get this error. Any suggestions?

ssuperczynski
  • 3,190
  • 3
  • 44
  • 61
user2280013
  • 191
  • 1
  • 1
  • 4
  • 2
    are you sure it's port issue? it could be an IP resolution too. can you ping or somehow reach this ip? – Alex Sep 10 '13 at 18:42
  • 3
    Are you able to connect using cqlsh? Every time I've seen this question it was related to either the IP not being accessible from the client machine or the port not being open. – Alex Popescu Sep 10 '13 at 22:28
  • is it working for localhost? – abhi Sep 11 '13 at 05:22
  • The answer that you have accepted is not a perfect solution. Refer my answer below. – Jaya Ananthram Jan 04 '15 at 08:05
  • I have this same problem. I know it's now the cassandra instance issue. I can connect from datagrip and can connect from python code. But using the java driver, I am getting this issue. I can't find a solution. Did you find solution? – alltej Sep 27 '20 at 14:13

5 Answers5

31

Check your casssandra.yaml file. The native_transport uses the same address binding as the rpc_address. If it is bound to another address than "54.221.241.107" you would get this problem. Try setting it to

rpc_address: 0.0.0.0 

or to

rpc_address: 54.221.241.107  

and see if it helps. Keep in mind that ec2-ips might change on restarts.

My guess is that is is bound to the internal ip of the ec-2. And remember to add some security if you are opening up your database to the public this way :-)

polve
  • 2,789
  • 2
  • 18
  • 20
  • 2
    I had the same issue on EC2, setting rpc address to 0.0.0.0 fixed it. – Ali Dec 31 '14 at 11:12
  • According to your solution if I try to stop and start the ec2-instance I need to modify the rpc_address right? Is it correct to do so? Pointing the private IP is the best solution. It wont change if you stop the instance. Refer my answer below. – Jaya Ananthram Jan 04 '15 at 08:03
  • That depends on your setup - of course if the ip changes and you bind through that then you must reconnect. Pointing to a private ip is the best - but 0.0.0.0 for RPC should also be ok. Then I guess the route-setup decides what interface you connect via. – polve Jan 05 '15 at 12:56
  • 1
    **0.0.0.0 for RPC should also be ok** No it is wrong if your are moving to cluster, RPC address with 0.0.0.0 will not allow any node to join in cluster. So pointing the PrivateIP is the best solution. – Jaya Ananthram Jan 06 '15 at 05:02
  • See datastax reference: http://www.datastax.com/documentation/cassandra/2.0/cassandra/configuration/configCassandra_yaml_r.html?scroll=reference_ds_qfg_n1r_1k__rpc_address – polve Jan 07 '15 at 14:56
  • We are using cassandra cluster in ec2-instances, and using 0.0.0.0 for rpc_address. And clustered. – polve Jan 07 '15 at 14:57
  • But any how whether using public IP is perfect solution?? – Jaya Ananthram Jan 09 '15 at 04:26
  • No "perfect" solution. I like 0.0.0.0 because then I can use cqlsh without specifying the private address. If you have control over your routes it should be ok. Our cassandra instances does not have any public ip anyway. – polve Jan 09 '15 at 12:52
  • Isn't this a terrible idea? By default cassandra has no auth enabled. Using 0.0.0.0 and the public address hangs your database out in the wind with no auth. – jorfus Jan 05 '17 at 23:10
  • I am using rpc_address=public-ip and then firewalling 9042 to known clients as below: 1) firewall-cmd --permanent --new-zone=cassandraconsumer 2) firewall-cmd --permanent --zone=cassandraconsumer --add-source=client-ip-address/32 3) firewall-cmd --permanent --zone=cassandraconsumer --add-port=9042/tcp – donlys May 02 '17 at 17:01
  • THANK YOU THANK YOU THANK YOU! :-) – eladyanai Mar 19 '19 at 12:17
8

Change public IP to private IP.

  • If your cassandra is in EC2, you need to configure private IP in yaml configurations rpc_address: PRIVATE_IP.

  • If your client program (java app used to connect cassandra) is also in EC2 then you should add private IP in your code .addContactPoint("PRIVATE_IP").build();.

  • If your cassandra is in EC2 and your client app is in out of EC2 (means client java app in your local network) you need to configure private IP in yaml configurations and public IP in your java app

Then important point is mentioning native_transport_port: 9042, allow access for port 9042 and Firewall configurations. I think these things you did correctly. And also ensure that you have properly configured your endpoint snitch endpoint_snitch: Ec2Snitch in your yaml file. I Hope it will work if you follow these steps....

Jaya Ananthram
  • 3,433
  • 1
  • 22
  • 37
1

I also stumbled on that issue recently. My environment was different and reason was also different. We used SpringBoot 1.5.3.RELEASE as parent library and Datastax cassandra driver version 3.3.0:

        <parent>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-parent</artifactId>
            <version>1.5.3.RELEASE</version>
            <relativePath/> <!-- lookup parent from repository -->
        </parent>
    ............
        <dependencies>
            <dependency>
                <groupId>com.datastax.cassandra</groupId>
                <artifactId>cassandra-driver-core</artifactId>
                <version>3.3.0</version>
                <exclusions>
                    <exclusion>
                        <groupId>io.netty</groupId>
                        <artifactId>netty-handler</artifactId>
                    </exclusion>
                </exclusions>
            </dependency>
.......

The Netty library inherited from SpringBoot's parent POM and Datastax has clashed. After excluding Netty the error went away.

blandger
  • 718
  • 7
  • 17
0

Had the same problem.Later figured out that Driver version and CQL version were not compatible.Get the latest 3.0 Driver according to the compatibility chart

http://www.datastax.com/dev/blog/java-driver-2-1-2-native-protocol-v3

0

Accessing Cassandra using java driver very much depends on the driver version and the dependencies of the driver. The final thing that worked for me was to take the lib(jars) that are bundled on the server download. In my case it was windows. But I am sure the linux tar balls also will have the driver and the dependencies in them. take the jars shipped in the server download and use them in your java client's classpath instead of downloading the drivers separately from the internet

Vahees
  • 11
  • 2