Connect to HCD with the CQL shell (cqlsh)
The CQL shell (cqlsh) is a Python-based command-line shell you can use to issue Cassandra Query Language (CQL) statements to your Hyper-Converged Database (HCD) databases.
The CQL shell also provides unique cqlsh commands, like DESCRIBE KEYSPACE, that aren’t available through the Cassandra drivers or other CQL-only tools.
These steps are for running the CQL shell on the command line with direct cluster connections or Mission Control gateway connections. If you are using the embedded CQL shell in Mission Control, see Use the CQL console to interact with the databases in your datacenter.
Prerequisites
-
For Mission Control-managed clusters, configure a CQL gateway.
Start the CQL shell
To start the CQL shell, run cqlsh directly on the command line if it is in your system’s PATH.
Otherwise, run bin/cqlsh from the directory where you installed HCD or a standalone cqlsh binary (without HCD).
With the exception of --version and --help, most cqlsh operations require connection details, credentials, and other options.
CQL and cqlsh statements that interact with your database are passed with --file option, --execute option, or at the interactive cqlsh prompt.
The following sections provide more information about options and ways of interacting with cqlsh.
- CQL shell in PATH
-
cqlsh [ OPTIONS ] [ HOST[:PORT] ] - CQL shell not in PATH
-
INSTALL_DIRECTORY/bin/cqlsh [ OPTIONS ] [ HOST[:PORT] ]
Connect to a node
|
Credentials passed on the command line can be stored in your shell history and the
|
cqlsh connects to a running database instance using the Apache Cassandra Python driver and the Cassandra native protocol.
Starting cqlsh with no arguments attempts to connect to the local host at 127.0.0.1:9042 without authentication if no credentials or connection details are provided in a .cqlshrc file.
To connect to a remote node, provide the host name or IP address and port (optional) of the target node.
If authentication is enabled, provide the required credentials.
The following example uses internal username and password authentication.
The --password option is omitted so that the user is prompted for the password instead of storing it in the shell history.
cqlsh IP_ADDRESS:PORT -u DATABASE_ROLE_NAME
If you use a Mission Control CQL gateway, the gateway Secure Connect Bundle (SCB) sets the host and port automatically.
Specify the SCB zip file with the -b option, and don’t set a host name or port on the command line.
bin/cqlsh -b PATH/TO/SCB_ZIP_FILE -u DATABASE_ROLE_NAME
For more connection options, see CQL shell options.
Run commands with the CQL shell
You can execute CQL and CQL shell commands in the interactive cqlsh shell environment, or non-interactively with the --execute and --file options.
For --execute and --file usage, see General session options.
If you don’t use --execute or --file, cqlsh starts in interactive mode.
A successful connection shows the connection details and the cqlsh> prompt.
Issue CQL or cqlsh commands directly at the cqlsh> prompt.
The results output to the terminal.
Connected to hcd-cluster at 192.0.2.200:9042
[cqlsh 6.1.0 | Cassandra 4.1.x | CQL spec 3.4.6 | Native protocol v5]
Use HELP for help.
cqlsh>
If you start cqlsh with the --keyspace option, the shell prompt includes the keyspace name, such as cqlsh:cycling>.
If you run the USE command to select a keyspace after starting cqlsh, the shell prompt changes accordingly.
For example:
cqlsh> USE cycling;
cqlsh:cycling> SELECT * FROM calendar WHERE race_id = 201;
After you start a cqlsh session, try these basic commands:
-
Create a keyspace named
storethat usesNetworkTopologyStrategywith a replication factor of1.Replace
datacenter1with a valid datacenter name for your cluster.CREATE KEYSPACE IF NOT EXISTS store WITH REPLICATION = { 'class' : 'NetworkTopologyStrategy', 'datacenter1' : '1' }; -
In the
storekeyspace, create a table namedshopping_cartwith three columns:userid,item_count, andlast_update.CREATE TABLE IF NOT EXISTS store.shopping_cart ( userid text PRIMARY KEY, item_count int, last_update timestamp ); -
Insert rows into the
shopping_carttable.The value of the
last_updatecolumn is generated using CQL native functions. Thenow()function gets the current date and time, and thetoTimeStamp()function converts the date and time to a timestamp.INSERT INTO store.shopping_cart (userid, item_count, last_update) VALUES ('4729', 2, toTimeStamp(now())); INSERT INTO store.shopping_cart (userid, item_count, last_update) VALUES ('1836', 5, toTimeStamp(now())); -
Query the
shopping_carttable.This example uses an unbounded
SELECT *query to retrieve all rows from the table. This isn’t recommended in production due to performance impacts. It is acceptable in this example because the amount of data is very small.SELECT * FROM store.shopping_cart;userid | item_count | last_update --------+------------+--------------------------------- 4729 | 2 | 2024-10-12 04:23:03.636000+0000 1836 | 5 | 2024-10-12 04:23:04.327000+0000 (2 rows)
CQL shell options
You can set cqlsh options on the command line, as environment variables, or in a .cqlshrc file.
cqlsh uses the following hierarchy to determine the value of the options:
-
Command line options override environment variables and
.cqlshrc. -
Environment variables override values set in
.cqlshrc. -
.cqlshrcoverrides the default values. -
If not set elsewhere,
cqlshuses the default values.
Connection details
| Command line | Environment variable | .cqlshrc section and option |
Description |
|---|---|---|---|
|
File path to a Mission Control CQL gateway Secure Connect Bundle (SCB).
Use |
||
|
|
Cannot be set in |
Path to a directory containing a
|
|
|
|
Connection timeout in seconds.
The default is |
|
|
|
Host name or IP address of the database node to connect to.
Port is optional if using the default native protocol port.
The default is |
|
|
|
Password for |
|
|
|
Whether to use SSL for the connection.
The default is To enable client-to-node SSL encryption, additional configuration is required. See Use a cqlshrc file. When using a Mission Control CQL gateway, SSL is configured automatically with the SCB.
|
|
|
|
CQL role to use for authentication. |
General session options
| Command line | Environment variable | .cqlshrc section and option |
Description | ||
|---|---|---|---|---|---|
|
|
|
Open CQL |
||
|
|
|
Whether to use color output.
The default is |
||
|
|
Specify the initial consistency level for the session, depending on the transaction type. Lightweight transactions (LWTs) use After starting the session, the consistency level can be changed with the |
|||
|
|
|
Set a specific CQL version to use for the session. Not useful unless connecting to an earlier version of Cassandra or DSE that doesn’t support the default CQL version. The CQL version is printed with the connection details after starting |
||
|
Not set as an environment variable. |
Not set in |
Show additional debugging information. |
||
|
|
|
Whether to disable command history. By default ( If
To change the default history file location, set the When
|
||
|
|
|
Enforce a DSE protocol version for the connection to the server. If not specified, the client requests the highest version it supports, negotiating and downgrading as necessary. Typically used when the default version is undesirable or lower versions are incompatible, such as with ZDM Proxy. Mutually exclusive with |
||
|
|
|
Encoding to use for output.
The default is |
||
|
Not set as an environment variable. |
Not set in |
Runs the specified CQL or The entire value of To execute multiple statements:
By default, results output to the terminal.
Use the redirection operator To run statements without exiting, start |
||
|
Not set as an environment variable. |
Not set in |
Runs one or more CQL or By default, results are displayed in the terminal.
You can use the redirection operator To run statements from a file without exiting, start |
||
|
|
|
Sets the initial keyspace for the The authenticating role must have permission to access the keyspace. When using the interactive |
||
|
|
|
Enforce a Cassandra Native Protocol version for the connection to the server. If not specified, the client requests the highest version it supports, negotiating and downgrading as necessary. Typically used when the default version is undesirable or lower versions are incompatible, such as with ZDM Proxy. Mutually exclusive with |
||
|
|
|
CQL request timeout in seconds. The default is 10 seconds. |
||
|
|
Not set in |
Force TTY command prompt mode. |
||
|
Not set as an environment variable. |
Not set in |
Prints the version of CQL shell that is installed on the local host, and exits. Credentials aren’t required because this command doesn’t start a session or connect to the cluster. |
|
For more |
Troubleshoot the CQL shell
If cqlsh cannot connect, and it mentions a different port than 9042, make sure your ~/.cassandra/cqlshrc file and CQLSH_* environment variables aren’t modifying the default port.
For command help, run cqlsh --help or cqlsh -h with no other arguments to show general help, or run --help with a command or subcommand for context-specific help.
HTML help documentation opens in the system’s default web browser unless --browser is set.
This command exits immediately without starting a session or connecting to the cluster.
For detailed logs, start cqlsh or run any cqlsh command with the --debug option.