Jesver
Jesver

Reputation: 83

How do I connect to a Cassandra cluster deployed on AWS with gocql?

I have done a setup of a three node Cassandra cluster. This one is on aws, and the server port are open. The three servers are well connected to each other and work perfectly. I have set

authenticator: AllowAllAuthenticator

in my cassandra.yaml file

I would like to make the connection with go cql, here is the connection code

cluster := gocql.NewCluster("x.xxx.xxx.xxx")
cluster.Keyspace = "keyspace_name"
cluster.Consistency = gocql.Quorum
cluster.ProtoVersion = 4
cluster.ConnectTimeout = time.Second * 10

session, err := cluster.CreateSession()
if err != nil {
    fmt.Println(err)
}

but go cql sends me back this message.

2020/09/26 09:53:44 gocql: unable to dial control conn x.xxx.xxx: dial tcp 127.0.0.1:9042: connectex: No connections could be made because the target machine actively refused them.
2020/09/26 09:53:44 gocql: unable to create session: control: unable to connect to initial hosts: dial tcp x.xxx.xxx.xxx:9042: connectex: No connections could be made because the target machine actively refused them.
panic: runtime error: invalid memory address or nil pointer dereference

Upvotes: 1

Views: 375

Answers (1)

Erick Ramirez
Erick Ramirez

Reputation: 16333

There's a good chance that you're using the private IP address in your app to connect to the nodes but those are not accessible remotely because by definition, they are private addresses.

You need to make sure that you've configured your nodes to use:

listen_address: <ec2_private_ip>
rpc_address: <ec2_public_ip>

Nodes communicate with each other using the listen_address whereas clients connect to the cluster using the rpc_address which is why it needs to be configured with the publicly-accessible IP addresses of the EC2 instances.

Once you've got the nodes configured correctly, configure your app to use the public IPs as contact points in:

cluster := gocql.NewCluster("public_ip1,public_ip2,...")

This should allow your app to connect to the cluster. Cheers!

Upvotes: 1

Related Questions