How to connect Go API to Cassandra DB

Viewed 133

I'm using a Go API and I need to connect it to my Cassandra DB on a Google Cloud server.

This is my Go connection code:

func SetupDBConnection() {
    cluster := gocql.NewCluster("XX.XXX.XXX.XXX")
    cluster.Keyspace = "afterstr"
    cluster.Consistency = gocql.Quorum

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

But, when I run this code, I have this error:

gocql: unable to create session: control: unable to connect to initial hosts: dial tcp xx.xxx.xxx.xxx:9042: i/o timeout

This is my Google Cloud configuration:

server config

server config

server config

I also modified the cassandra.yaml file in my server to change the listen address and the RPC address to put my IP with the port I use to run my Go program that I opened. (It is opened 3 times because I was just testing something)

opened ports

1 Answers

Try:

cluster.Authenticator = gocql.PasswordAuthenticator{Username: username, Password: password}
Related