Unable to connect to Cosmos DB through Cassandra API(gocql)

Velvaluri, Vivek 41 Reputation points

I have provisioned a Cosmos DB (Cassandra API) instance on Azure.
I'm able to connect to it through cqlsh. But, getting different errors when trying to connect to it through gocql.

The following code generates this error:

gocql: unable to dial control conn read tcp> read: connection reset by peer

Here's the code snippet:

cluster := gocql.NewCluster("<host here>")  
cluster.Port = 10350  
cluster.Timeout = 10000  
cluster.SocketKeepalive = 60 * time.Second  
cluster.RetryPolicy = &gocql.SimpleRetryPolicy{NumRetries: 3}  
cluster.Consistency = gocql.LocalOne  
cluster.ProtoVersion = 1  
cluster.ReconnectionPolicy = &gocql.ConstantReconnectionPolicy{MaxRetries: 10, Interval: 8 * time.Second}  
cluster.Keyspace = "<keyspace here>"  
cluster.DisableInitialHostLookup = true  
cluster.IgnorePeerAddr = true  
cluster.Authenticator = gocql.PasswordAuthenticator{Username: "<username here>", Password: "<password here>"}  
_, err := cluster.CreateSession()  
if err != nil{  
// handle the error  

Just to add to this, gocql is listed as an officially supported driver under this page:


Azure Cosmos DB
Azure Cosmos DB
An Azure NoSQL database service for app development.
1,528 questions
0 comments No comments
{count} votes

Accepted answer
  1. Theo van Kraay 81 Reputation points Microsoft Employee

    Hello. One aspect missing is SSL. You either need to enable client-side SSL server cert verification, or set sslOptions.EnableHostVerification to false. Also, ensure the values you are passing for the Cosmos DB Cassandra API account are correct. Here is a snippet which hopefully works better for you:

    // connect to the cluster
    cluster := gocql.NewCluster("<ACCOUNTNAME>.cassandra.cosmos.azure.com")
    cluster.Port = 10350
    var sslOptions = new(gocql.SslOptions)
    // If you want to enable client-side SSL server cert verification do this:
    // sslOptions.EnableHostVerification = true
    // sslOptions.CaPath = "<path/to/cert.pem>"
    // sslOptions.Config = &tls.Config{}
    // sslOptions.ServerName = `ACCOUNTNAME.cassandra.cosmos.azure.com`
    // cluster.SslOpts = sslOptions
    //If you do NOT want to enable client-side SSL, set sslOptions.EnableHostVerification to false and ignore the other options.
    sslOptions.EnableHostVerification = false
    cluster.SslOpts = sslOptions
    cluster.ProtoVersion = 4
    cluster.Authenticator = gocql.PasswordAuthenticator{Username: "<ACCOUNTNAME>", Password: "<PASSWORD>"}
    session, _ := cluster.CreateSession()
    defer session.Close()
    var pk int32
    var ck int32
    var col1 string
    var col2 int64
    if err := session.Query(`SELECT * FROM kspc1.tbl1;`).Scan(&pk, &ck, &col1, &col2); err != nil {
    fmt.Println("Row:", pk, ck, col1, col2)
    1 person found this answer helpful.

0 additional answers

Sort by: Most helpful