Connect to Apache Cassandra with Cqlsh

Cqlsh is a utility for running simple CQL (Cassandra Query Language) commands on a local or remote Cassandra cluster. This article describes how cqlsh can be used to connect to clusters in Instaclustr. In this article, we assume that your cluster has been set up and provisioned properly as shown in our previous tutorial “Creating a Cluster”.

Table of Contents

Prerequisites

  1. Python needs to be installed.
  2. You also need Cassandra binaries which can be downloaded from https://archive.apache.org/dist/cassandra/. We recommend Cassandra 2.1 or later. You don’t have to install Cassandra after downloading, if you only want to use cqlsh.
  3. The public IP address of your machine must be added to the Cassandra Allowed Addresses in the Firewall Rules tab of your cluster in the Instaclustr console (Refer this support article).
  4. Username, password and certificate file can be found on the Connection Info page of your cluster. Certificate files are required to connect to your cluster with SSL.

supporting screenshot

Connecting to Instaclustr without SSL

If encryption is not enabled in your cluster, you can connect to it using cqlsh without SSL.

a) For Mac/Linux

Open your terminal and use the following command to connect to your cluster. Note: if authentication is not enabled in your cluster, you don’t need the options “-u” and “-p”.

For tarball installation:

For package installation:

For binary/source download:

b) For Windows

Run cmd.exe as administrator and enter the user home directory where your Cassandra is downloaded.

For tarball installation and binary/source download:

For package installation:

Connecting to Instaclustr with SSL

If encryption is enabled in your cluster, SSL is needed for connecting to the cluster and cqlshrc file is used to configure SSL encryption.

a) For Mac/Linux

Open your terminal and using the following command, create a “.cassandra/cqlshrc” file in your user home directory.

Open the empty cqlshrc file using the following command.

Copy the following content and paste it into the empty cqlshrc file. Then save the file.

Now you can start cqlsh with the –ssl option.

For tarball installation and binary download:

For package installation:

For source download:

Please note: If you are using running cqlsh from Mac OS X and it returns an error message that the TLS protocol version is not supported, you may need to update your system Python (E.g. install an updated Python 2.7 via Homebrew).

b) For Windows

Open notepad, create a new file and name it to “cqlshrc”. Copy the following content and paste it into the file. Then save the file into the “.cassandra” directory under your user home directory.

Now you can start cqlsh with the –ssl option.

For tarball installation or binary download:

For package installation:

For source download:

Troubleshooting

If you encounter a cql version error like: “cql_version ‘3.3.1’ is not supported by remote (w/ native protocol). Supported versions: [u’3.2.1′]”, run cqlsh with an extra option ‐‐cqlversion=’3.2.1’.  First check that you have not specified an incorrect version in your cqlshrc file, you can either remove this or update it to the required version.

Otherwise, run cqlsh with an extra option ‐‐cqlversion=’3.2.1’. You can follow the example below:

For tarball installation and binary/source download:

For package installation:

Use the same option for connecting to ssl enabled clusters:

If neither of these solutions work, you may be required to update your cqlsh version to the client bundled with the version of cassandra you are trying to connect to.  You can download the required binaries from https://cassandra.apache.org/download/.

If you encounter a protocol version error like “This version of the driver does not support protocol version N”, then you will need to update your cqlsh version to the client bundled with the version of cassandra you are trying to connect to.  Again, you can download the required binaries from https://cassandra.apache.org/download/.

Additional Resources

For additional information on CQL, refer the following resources: