Connect with legacy drivers
DataStax does not support legacy drivers, which includes the PHP and Ruby drivers. For the best experience, use supported native drivers to connect to an Astra DB database. For details about supported versions, see Version Compatibility. Legacy drivers should be used only if migrating an existing Apache Cassandra® application developed with a Legacy driver to Astra DB. |
Prerequisites
Legacy drivers were developed for Apache Cassandra®.
To connect to Astra DB with a legacy driver, you must collect connection information from the database’s Secure Connect Bundle (SCB) and set up the required SSL context for your driver.
If you use VPC peering, you can download an external SCB for use within your VPC peering.
Alternatively, you can use the SSL-related files (ca.cert, cert, and key) and the port from the cqlshrc
file.
PHP legacy driver
Use these steps to connect to Astra DB with the DataStax PHP driver:
-
Install the DataStax PHP Driver for Cassandra.
-
Get a Client ID and Client Secret by creating an application token.
-
Copy the certificate files from the Astra DB SCB to
/etc/ssl/certs/astra-secure-connect
. This folder should contain theca.cert
,cert
, andkey
files. -
Create a PHP script file, and then enter the following code:
<?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("CLIENT_ID", "CLIENT_SECRET") \->withContactPoints('HOSTNAME') \->withPort(PORT) \->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']); }"
Replace the following:
-
HOSTNAME
: Your Astra DB database hostname, such asDB_ID-REGION_NAME.db.astra.datastax.com
. -
PORT
: Your Astra DB database port. -
CLIENT_ID
andCLIENT_SECRET**
: The Client ID and Client Secret from your application token.
-
Ruby legacy driver
Use these steps to connect to Astra DB with the DataStax Ruby driver:
-
Install the DataStax Ruby Driver for Cassandra.
-
Copy the certificate files from the Astra DB SCB to
/etc/ssl/certs/astra-secure-connect
. This folder should contain theca.cert
,cert
, andkey
files. -
Create a Ruby script file, and then enter the following code:
require 'cassandra' cluster = Cassandra.cluster( username: 'CLIENT_ID', password: 'CLIENT_SECRET', 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
Replace the following:
-
HOSTNAME
: Your Astra DB database hostname, such asDB_ID-REGION_NAME.db.astra.datastax.com
. -
PORT
: Your Astra DB database port. -
CLIENT_ID
andCLIENT_SECRET**
: The Client ID and Client Secret from your application token.
-