How to connect remotely to Cassandra cluster?
panic: gocql: unable to create session: unable to discover protocol version: dial tcp xxx.xxx.xx.xxx:9042: i/o timeout
This is a quick article on how to set up Cassandra for remote access.
After creating your cluster, keyspace, and table, it’s time now to connect to Cassandra DB.
Locally We can connect to Cassandra using the CQLSH interface. what if any third party application wants to access to our database? that’s when RPC comes in.
RPC stands for Remote Procedure Call.
So let’s configure it.
To configure the Cassandra RPC, navigate and open the cassandra.yaml file with any editor:
> sudo nano /etc/cassandra/cassandra.yaml
find this line
# For security reasons, you should not expose this port to the internet. Firew$
rpc_address: localhost
Replace localhost by the current host IP where Cassandra is installed, for example 192.168.xx.xx
# For security reasons, you should not expose this port to the internet. Firew$
rpc_address: Your_IP_Addr
As mentioned this port should not be exposed to the internet or shared with anyone for security reasons.
Now save the file and restart Cassandra
> sudo service cassandra restart
Try to connect from any remote computer
cqlsh IP_SERVER -u USER -p PASSWORD
Or use any programming language :
With Go
// main.go
package mainimport ("fmt"
"github.com/gocql/gocql")var Session *gocql.Sessionfunc main() {var err errorcluster := gocql.NewCluster("YOUR_IP_ADDR")cluster.Keyspace = "restfulapi"Session, err = cluster.CreateSession()if err != nil {panic(err)}fmt.Println("cassandra well initialized")}> go run main.gocassandra well initialized
Boom, well done!!!!!😁