Can't connect to cassandra - NoHostAvailableException

断了今生、忘了曾经 提交于 2019-12-17 12:53:13

问题


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?


回答1:


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 :-)




回答2:


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....




回答3:


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.




回答4:


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




回答5:


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



来源:https://stackoverflow.com/questions/18724334/cant-connect-to-cassandra-nohostavailableexception

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!