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.
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!