Home > OS >  Cassandra hosted on docker connection to NodeJS returns NoHostAvailableError or ECONNREFUSED
Cassandra hosted on docker connection to NodeJS returns NoHostAvailableError or ECONNREFUSED

Time:12-25

I have a Cassandra db hosted on a docker container, created using the following command: docker run -d --name cassandra-docker -p 9842:9842 cassandra

I created keyspaces, tables, uploaded data, etc. And all's been working fine in the bash and cqlsh.

The problem is, I've been trying to connect it to my node application for a while, and I get either the NoHostAvailableError or the ECONNREFUSED error. I'm fairly new to using docker and cassandra, so I'm not sure where the problem is.

So far, my index.js has looked like this:

const cassandra = require('cassandra-driver');

var client = new cassandra.Client({
    contactPoints: ['127.0.0.1:9842'],
    keyspace: 'hospital'
});

const query = 'SELECT * from rooms;'

client.execute(query)
    .then(result => console.log('RES: ', result.rows[0]));

where I get the NoHostAvailableError:

NoHostAvailableError: All host(s) tried for query failed. First host tried, 127.0.0.1:9842: OperationTimedOutError: The host 127.0.0.1:9842 did not reply before timeout 12000 ms

and like this:

const cassandra = require('cassandra-driver');

var client = new cassandra.Client({
    contactPoints: ['127.0.0.1'],
    keyspace: 'hospital'
});

const query = 'SELECT * from hospital.rooms;'

client.execute(query)
    .then(result => console.log('RES: ', result.rows[0]));

where I get the ECONNREFUSED error:

NoHostAvailableError: All host(s) tried for query failed. First host tried, 127.0.0.1:9042: Error: connect ECONNREFUSED 127.0.0.1:9042 at TCPConnectWrap.afterConnect [as oncomplete]

The only different thing between them is the port definition on the first.

Also, there's another question on this site with the exact same title. I've followed the advice in there, but it still doesn't work. What am I doing wrong?

CodePudding user response:

Cassandra listens on Port 9042 per default. This is configured in the .yml files.

I would suggest using defaults for testing.

Change your docker command to:

docker run -d --name cassandra-docker -p 9042:9042 cassandra

  • Related