cassandraazure-cosmosdbgocql

unable to create session: control: unable to connect to initial hosts: Invalid Cosmos DB account or key


I have been trying to connect to cosmos cassandra db using gocql.

func GetSession(cosmosCassandraContactPoint, cosmosCassandraPort, cosmosCassandraUser, cosmosCassandraPassword string) *gocql.Session {
    clusterConfig := gocql.NewCluster(cosmosCassandraContactPoint)
    port, err := strconv.Atoi(cosmosCassandraPort)
    if err != nil {
        log.Fatal(err)
    }
    clusterConfig.Port = port
    clusterConfig.ProtoVersion = 4
    clusterConfig.Authenticator = gocql.PasswordAuthenticator{Username: cosmosCassandraUser, Password: cosmosCassandraPassword}
    clusterConfig.SslOpts = &gocql.SslOptions{Config: &tls.Config{MinVersion: tls.VersionTLS12}}

    clusterConfig.ConnectTimeout = 10 * time.Second
    clusterConfig.Timeout = 10 * time.Second
    clusterConfig.DisableInitialHostLookup = true

    // uncomment if you want to track time taken for individual queries
    //clusterConfig.QueryObserver = timer{}

    // uncomment if you want to track time taken for each connection to Cassandra
    //clusterConfig.ConnectObserver = timer{}

    session, err := clusterConfig.CreateSession()
    if err != nil {
        log.Fatal("Failed to connect to Azure Cosmos DB", err)
    }

    return session
}

I have been getting the following error :

unable to create session: control: unable to connect to initial hosts: Invalid Cosmos DB account or key

Not sure what the issue here is.


Solution

  • It seems your account or key is wrong.

    First,please make sure your API is CASSANDRA API.You can check at here. enter image description here

    Second,please make sure your account or key is right.

    COSMOSDB_CASSANDRA_CONTACT_POINT=<value for "CONTACT POINT">
    COSMOSDB_CASSANDRA_PORT=<value for "PORT">
    COSMOSDB_CASSANDRA_USER=<value for "USERNAME">
    COSMOSDB_CASSANDRA_PASSWORD=<value for "PRIMARY PASSWORD">
    

    You can find them here: enter image description here

    More details,you can refer to this documentation. Hope this can help you.