Connect with legacy drivers

DataStax does not support legacy drivers, which includes the PHP and Ruby drivers. To connect to a legacy driver, collect connection information from the secure connect bundle and set up the required SSL context for your driver to speak with Astra DB.

Legacy drivers were developed for Apache Cassandra™. To connect to Astra DB, specify your Secure Connect Bundle (SCB) details (token, userID, etc).

Alternatively, you can use the SSL-related files (ca.cert, cert, and key) and the port from the cqlshrc file.

For the best experience, use supported native drivers to connect to an Astra DB database. For details about supported versions, see Version Compatibility.

Prerequisites

Working with secure connect bundle

Legacy drivers were developed for Apache Cassandra™. To connect to Astra DB, specify your Secure Connect Bundle (SCB) details (token, userID, etc).

Alternatively, you can use the SSL-related files (ca.cert, cert, and key) and the port from the cqlshrc file.

For new projects on Astra DB, use a Cloud or Native driver. Java and Python are available as Legacy and Native drivers.

Legacy drivers should be used only if migrating an existing Cassandra application developed with a Legacy driver to Astra DB.

If you’ve enabled VPC peering, you can also Download External Secure Connect Bundle for use within your VPC peering.

The secure-connect-database_name.zip file downloads, which contains the security certificates and credentials for your database.

Sharing secure connect bundle

Although teammates can access your Astra DB database, it will not display in their list of available databases under My Databases in Astra Portal.

After you create an Astra DB database, you can grant access to other members of your team by providing them with the database credentials and connection details for your database.

Be careful when sharing connection details. Providing this information to another user grants them access to your Astra DB database and ownership capabilities, such as making modifications to the database.

For security, delete downloaded connection credentials after sending them to your teammate.

Collect Astra DB connection information

  1. From the secure connect bundle, collect the SSL related files:

    • ca.cert

    • cert

    • key

  2. From the cqlshrc file, collect the port.

    Use only the port from cqlshrc. Other ports found in the secure connect bundle will not allow you to connect with this method.

  3. From the config.json file, grab the hostname.

PHP legacy driver

This example can be used to attach to Astra DB from PHP using the DataStax Driver. Before proceeding you might also consider using the REST, GraphQL or Document APIs to connect to Astra DB without the need for a custom driver.

Prerequisites

Procedure

  1. Copy the certificate files from the Astra DB secure connect bundle to /etc/ssl/certs/astra-secure-connect. This folder should contain the ca.cert, cert, and key files.

  2. Replace the <hostname> with your Astra DB database hostname.

  3. Replace the <port> with your Astra DB database port.

  4. Replace <username> and <password> with the Astra DB database Client ID and Client Secret.

<?php

$ssl     = Cassandra::ssl()
               \->withTrustedCerts('/etc/ssl/certs/astra-secure-connect/ca.cert')
               \->withClientCert('/etc/ssl/certs/astra-secure-connect/cert')
               \->withPrivateKey('/etc/ssl/certs/astra-secure-connect/key')
               \->build();


$cluster = Cassandra::cluster()
               \->withSSL($ssl)
               \->withCredentials("dbadmin", "my_awesome_password")
               \->withContactPoints('602c8f3c-ea96-41b0-a7c1-6c6c5bdd1f34-us-east1.db.astra.datastax.com')
               \->withPort(31575)
               \->build();

$session = $cluster\->connect();

$result = $session\->execute('SELECT keyspace_name, table_name FROM system_schema.tables');

foreach ($result as $row) {
    printf("The keyspace "%s" has a table "%s".
    ", $row['keyspace_name'], $row['table_name']);
}"

Example

<?php

$ssl     = Cassandra::ssl()
               \->withTrustedCerts('/etc/ssl/certs/astra-secure-connect/ca.cert')
               \->withClientCert('/etc/ssl/certs/astra-secure-connect/cert')
               \->withPrivateKey('/etc/ssl/certs/astra-secure-connect/key')
               \->build();

$cluster = Cassandra::cluster()
               \->withSSL($ssl)
               \->withCredentials("john.smith", "jsP@ssw0rd")
               \->withContactPoints('602c8f3c-ea96-41b0-a7c1-6c6c5bdd1f34-us-east1.db.astra.datastax.com')
               \->withPort(31575)
               \->build();
$session = $cluster\->connect();

$result = $session\->execute('SELECT keyspace_name, table_name FROM system_schema.tables');

foreach ($result as $row) {
    printf("The keyspace "%s" has a table "%s".
    ", $row['keyspace_name'], $row['table_name']);
    }"

In the example above, the following variables are used:

  • <hostname> is 602c8f3c-ea96-41b0-a7c1-6c6c5bdd1f34-us-east1.db.astra.datastax.com

  • <port> is 31575

  • <username> is john.smith

  • <password> is jsP@ssw0rd

Ruby legacy driver

This example can be used to attach to DataStax Astra DB from Ruby using the DataStax Driver. Before proceeding you might also consider using the REST, GraphQL or Document APIs to connect to Astra DB without the need for a custom driver.

Prerequisites

Procedure

  1. Copy the certificate files from the Astra DB secure connect bundle to /etc/ssl/certs/astra-secure-connect. This folder should contain the ca.cert, cert, and key files.

  2. Replace the <hostname> with your Astra DB database hostname.

  3. Replace the <port> with your Astra DB database port.

  4. Replace <username> and <password> with the Astra DB database Client ID and Client Secret.

    require 'cassandra'
    
    cluster = Cassandra.cluster(
                username: '<username>',
                password: '<password>',
                hosts: ['<hostname>'],
                port: <port>,
                server_cert: '/etc/ssl/certs/astra-secure-connect/ca.cert',
                client_cert: '/etc/ssl/certs/astra-secure-connect/cert',
                private_key: '/etc/ssl/certs/astra-secure-connect/key'
    )
    
    keyspace = 'system_schema'
    session  = cluster.connect(keyspace) # create session, optionally scoped to a keyspace, to execute queries
    
    future = session.execute_async('SELECT keyspace_name, table_name FROM tables') # fully asynchronous api
    future.on_success do |rows|
      rows.each do |row|
        puts "The keyspace #{row['keyspace_name']} has a table #{row['table_name']}"
      end
    end
    future.join

Example

require 'cassandra'

cluster = Cassandra.cluster(
            username: 'john.smith',
            password: 'jsP@ssw0rd',
            hosts: ['602c8f3c-ea96-41b0-a7c1-6c6c5bdd1f34-us-east1.db.astra.datastax.com'],
            port: 31575,
            server_cert: '/etc/ssl/certs/astra-secure-connect/ca.cert',
            client_cert: '/etc/ssl/certs/astra-secure-connect/cert',
            private_key: '/etc/ssl/certs/astra-secure-connect/key'
)

keyspace = 'system_schema'
session  = cluster.connect(keyspace) # create session, optionally scoped to a keyspace, to execute queries

future = session.execute_async('SELECT keyspace_name, table_name FROM tables') # fully asynchronous api
future.on_success do |rows|
  rows.each do |row|
    puts "The keyspace #{row['keyspace_name']} has a table #{row['table_name']}"
  end
end
future.join

In the example above, the following variables are used:

  • hostname is 602c8f3c-ea96-41b0-a7c1-6c6c5bdd1f34-us-east1.db.astra.datastax.com

  • port is 31575

  • username is john.smith

  • password is jsP@ssw0rd

Was this helpful?

Give Feedback

How can we improve the documentation?

© 2024 DataStax | Privacy policy | Terms of use

Apache, Apache Cassandra, Cassandra, Apache Tomcat, Tomcat, Apache Lucene, Apache Solr, Apache Hadoop, Hadoop, Apache Pulsar, Pulsar, Apache Spark, Spark, Apache TinkerPop, TinkerPop, Apache Kafka and Kafka are either registered trademarks or trademarks of the Apache Software Foundation or its subsidiaries in Canada, the United States and/or other countries. Kubernetes is the registered trademark of the Linux Foundation.

General Inquiries: +1 (650) 389-6000, info@datastax.com