0

Help, I am quite new to AstraDB, and while connecting it via goCQL, I got an error that says "Error creating cassandra session: gocql: unable to create session: unable to discover protocol version: dial tcp 34.93.79.117:34567: i/o timeout"

I want to connect my code with server using GoCQL, and I was following this tutorial :- https://community.datastax.com/questions/3753/has-anyone-managed-to-connect-to-astra-from-gocql.html

I did not make much changes in my code, just a few different things from this tutorial.

In my code, Cassandra is a struct

type Cassandra struct {
    Host     string `yaml:"host"`
    Port     string `yaml:"port"`
    Username string `yaml:"username"`
    Password string `yaml:"password"`
    Keyspace string `yaml:"keyspace"`
}

type cassandra struct {
    Session *gocqlx.Session
}

func NewCassandraConfig() *Cassandra {
    return &Cassandra{
        Host:     "",     //databaseid-db_region.db.astra.datastax.com
        Port:     "34567",     //port
        Username: "",     //token id
        Password: "",     //token password
        Keyspace: "test",     //keyspace created at 
    }
}


func New(conf *Cassandra) (*cassandra, error) {
    cluster := gocql.NewCluster(conf.Host)
    cluster.Keyspace = conf.Keyspace
    cluster.Consistency = gocql.Quorum
    cluster.Authenticator = gocql.PasswordAuthenticator{
        Username: conf.Username,
        Password: conf.Password,
    }
    cluster.Hosts = []string{conf.Host + ":" + conf.Port}

    certPath, _ := filepath.Abs("absolute path to //cert")  //gotten from bundle
    keyPath, _ := filepath.Abs("absolute path to //key")
    caPath, _ := filepath.Abs("absolute path to //ca.crt")
    cert, _ := tls.LoadX509KeyPair(certPath, keyPath)
    caCert, _ := ioutil.ReadFile(caPath)
    caCertPool := x509.NewCertPool()
    caCertPool.AppendCertsFromPEM(caCert)
    
    tlsConfig := &tls.Config{
        Certificates: []tls.Certificate{cert},
        RootCAs:      caCertPool,
    }
    cluster.SslOpts = &gocql.SslOptions{
        Config:                 tlsConfig,
        EnableHostVerification: false,
    }

    session, err := gocqlx.WrapSession(cluster.CreateSession())
    if err != nil {
        // log via logrus
        log.Errorf("Error creating cassandra session: %v", err)
        return nil, err
    }
    return &cassandra{
        Session: &session,
    }, nil
}

func main() {
    CassandraConfig := NewCassandraConfig()

    CassandraSession, err := New(CassandraConfig)

    if err != nil {
        log.Println("Error")
    }

    query := "SELECT id, name from test.testdata"

    result := &testdata{}
    iter := CassandraSession.Session.Query(query, nil).Iter()
    for iter.Scan(&result.id, &result.name) {
        log.Println(result.id, "     ", result.name)
}
}

Can anyone help me find out what mistakes I made, cause I am unable to find that.

Ansh Joshi
  • 41
  • 1
  • 7

2 Answers2

1

The error you posted indicates that you have configured something incorrectly so the driver is unable to connect to your database. It could be the wrong password, wrong CQL port, or incorrect certificate credentials.

I wrote those instructions all the way back to 2020 and they are a bit obsolete.

My suggestion is that you have a look at Nathan Bak's https://github.com/NathanBak/easy-cass-go package which makes it very easy to connect to your Astra DB. Cheers!

Erick Ramirez
  • 13,964
  • 1
  • 18
  • 23
0

If you are receiving the error message, while trying to create a session with your Cassandra cluster, there might be an issue with the SSL/TLS configuration.

One possible solution is to specify the cql_port instead of the port property in the gocql.NewCluster function call. The cql_port is used for the Cassandra Query Language (CQL) and defaults to 9042, while the port property is used for the Thrift protocol and defaults to 9160. Here is an example of how to modify the code:

cluster := gocql.NewCluster("hjsbjhdd-region.db.astra.datastax.com)

cluster.Authenticator = gocql.PasswordAuthenticator{
    Username: "username",
    Password: "password",
}
certPath, _ := filepath.Abs("./secure-connect-way/cert") //extracted bundle
caPath, _ := filepath.Abs("./secure-connect-way/ca.crt")
keyPath, _ := filepath.Abs("./secure-connect-way/key")

cluster.Keyspace = "keyspace"
cluster.Port = 29042 // used the cql_port of the config.json

cluster.ProtoVersion = 4
cluster.CQLVersion = "3.4.5"
cluster.ConnectTimeout = time.Second * 6
cluster.SslOpts = &gocql.SslOptions{
    CertPath: certPath,
    CaPath:   caPath,
    KeyPath:  keyPath,
}

session, err := cluster.CreateSession()