How to Install Apache Cassandra NoSQL Database on a Single-Node Ubuntu 22.04 Cluster
This tutorial exists for these OS versions
- Ubuntu 22.04 (Jammy Jellyfish)
- Ubuntu 20.04 (Focal Fossa)
- Ubuntu 16.04 (Xenial Xerus)
On this page
Apache Cassandra is an open-source NoSQL distributed database management system. Cassandra can be scaled horizontally by adding more nodes across which data is replicated automatically. Nodes can be added or removed without any downtime. The nodes can be organized logically as a cluster or a ring and set up across multiple data centers to improve speed and reliability for high-performance applications.
In this tutorial, we will learn how to install Apache Cassandra on a Single-node Ubuntu 22.04 Cluster.
Prerequisites
-
A Server running Ubuntu 22.04 with a minimum of 2GB of RAM.
-
A non-sudo user with root privileges.
-
Everything is updated.
$ sudo apt update && sudo apt upgrade
Step 1 - Install Java
Apache Cassandra requires Java 8 or Java 11 to function. The latest version of Cassandra includes full support for Java 11, which is what we will be using.
$ sudo apt install openjdk-11-jdk
Confirm the Java installation.
$ java -version openjdk 11.0.15 2022-04-19 OpenJDK Runtime Environment (build 11.0.15+10-Ubuntu-0ubuntu0.22.04.1) OpenJDK 64-Bit Server VM (build 11.0.15+10-Ubuntu-0ubuntu0.22.04.1, mixed mode, sharing)
Step 2 - Install Apache Cassandra
The first step is to add Cassandra's official repository.
Run the following command to add the repository to the system's repository list.
$ echo "deb http://www.apache.org/dist/cassandra/debian 40x main" | sudo tee -a /etc/apt/sources.list.d/cassandra.sources.list
At the time of writing this tutorial, the latest available version of Cassandra is 4.0.5. The above command uses 40x
to represent the 4.0 series of Cassandra.
Add the repository key to the system's trusted repository list.
$ wget -q -O - https://www.apache.org/dist/cassandra/KEYS | sudo tee /etc/apt/trusted.gpg.d/cassandra.asc
Update the system repository list.
$ sudo apt update
Install Cassandra.
$ sudo apt install cassandra
Step 3 - Check Cassandra Service
Cassandra automatically creates and starts the service daemon. Check the status of the service.
$ sudo systemctl status cassandra
You should get the following output.
? cassandra.service - LSB: distributed storage system for structured data Loaded: loaded (/etc/init.d/cassandra; generated) Active: active (running) since Mon 2022-07-25 11:40:42 UTC; 12min ago Docs: man:systemd-sysv-generator(8) Tasks: 48 (limit: 2241) Memory: 1.3G CPU: 38.219s CGroup: /system.slice/cassandra.service ??4602 /usr/bin/java -ea -da:net.openhft... -XX:+UseThreadPriorities -XX:+HeapDumpOnOutOfMemoryError -Xss256k -XX:+AlwaysPreTouch -XX:-UseBiasedLocking -XX:+UseTLAB -XX:+ResizeTLAB -XX:+UseNUMA -> Jul 25 11:40:42 cassandra systemd[1]: Starting LSB: distributed storage system for structured data... Jul 25 11:40:42 cassandra systemd[1]: Started LSB: distributed storage system for structured data.
You can also verify the status using the nodetool
command.
$ nodetool status Datacenter: datacenter1 ======================= Status=Up/Down |/ State=Normal/Leaving/Joining/Moving -- Address Load Tokens Owns (effective) Host ID Rack UN 127.0.0.1 69.08 KiB 16 100.0% 6690243a-950d-4d64-9463-aa722f8064d4 rack1
Step 4 - Configure Cassandra
The default location of configuration files for Cassandra is at /etc/cassandra
. The default location for the log and data directories is /var/log/cassandra
and /var/lib/cassandra
.
JVM level settings such as heap size can be set via the /etc/cassandra/cassandra-env.sh
file. You can pass additional JVM command-line arguments to the JVM_OPTS
variable. The arguments are passed to Cassandra when it starts.
4.1 Enable User Authentication
To enable user authentication, the first step is to take a backup of the /etc/cassandra/cassandra.yaml
file.
$ sudo cp /etc/cassandra/cassandra.yaml /etc/cassandra/cassandra.yaml.backup
Open the cassandra.yaml
file for editing.
$ sudo nano /etc/cassandra/cassandra.yaml
Locate the following parameters in this file.
... authenticator: AllowAllAuthenticator ... authorizer: AllowAllAuthorizer ... roles_validity_in_ms: 2000 ... permissions_validity_in_ms: 2000 ...
Change the values of the parameters as given below.
... authenticator: org.apache.cassandra.auth.PasswordAuthenticator ... authorizer: org.apache.cassandra.auth.CassandraAuthorizer ... roles_validity_in_ms: 0 ... permissions_validity_in_ms: 0 . . .
You can configure other settings based on your requirements. If they are commented, then uncomment them out.
Once finished, save the file by pressing Ctrl + X and entering Y when prompted.
Restart Cassandra to enable the changed settings.
$ sudo systemctl restart cassandra
4.1.1 - Add an Administrator Superuser
Now that we have enabled authentication, we need to create a user. To do that, we will use the Cassandra Command shell utility. Log in with the credentials for the default user cassandra
.
$ cqlsh -u cassandra -p cassandra
You will get the following shell.
Connected to Test Cluster at 127.0.0.1:9042 [cqlsh 6.0.0 | Cassandra 4.0.5 | CQL spec 3.4.5 | Native protocol v5] Use HELP for help. cassandra@cqlsh>
Create a new superuser. Replace [username]
and [yourpassword]
with your credentials.
cassandra@cqlsh> CREATE ROLE [username] WITH PASSWORD = '[yourpassword]' AND SUPERUSER = true AND LOGIN = true;
Log out of the shell.
cassandra@cqlsh> exit
Log back in with the new superuser account.
$ cqlsh -u username -p yourpassword
Remove the elevated permissions from the default cassandra
account.
username@cqlsh> ALTER ROLE cassandra WITH PASSWORD = 'cassandra' AND SUPERUSER = false AND LOGIN = false; username@cqlsh> REVOKE ALL PERMISSIONS ON ALL KEYSPACES FROM cassandra;
Grant all permissions to the superuser account.
username@cqlsh> GRANT ALL PERMISSIONS ON ALL KEYSPACES TO '[username]';
Log out of the shell.
username@cqlsh> exit
4.2 - Edit the Console Configuration File
If you want to customize the Cassandra Shell, you can do so by editing the cqlshrc
file. The default location for the file is in the ~/.cassandra
directory. If you want to load it from a different directory, you can pass the argument --cqlshrc /customdirectory
to the cqlsh
tool while running. Cassandra's default install doesn't include the file. Therefore, we will need to create it.
Create the cqlshrc
file in the ~/.cassandra
directory. We don't need to use sudo
to perform any of the functions in the ~/.cassandra
directory. Cassandra requires that the files in this directory are owned by the local account and shouldn't be accessible from other accounts or groups.
$ touch ~/.cassandra/cqlshrc
If the ~/.cassandra
directory is not present, then create it using the following command.
$ mkdir ~/.cassandra
Open the file for editing.
$ nano ~/.cassandra/cqlshrc
Cassandra's Github repository provides a sample cqlshrc
file. You can copy any or all of the sections from there depending on your needs. All the settings are commented using ;;
. Uncomment them by removing double semi-colons and then make the change depending upon your needs.
We will configure the shell to automatically log in with the superuser credentials. Replace the [username]
and [password]
with the credentials created in the previous step.
.... [authentication] ;; If Cassandra has auth enabled, fill out these options username = [username] password = [password] ....
Edit any other settings you want to change. Once finished, save the file by pressing Ctrl + X and entering Y when prompted.
Update the permissions for the cqlshrc
file. This stops any other user group from accessing the file.
$ chmod 600 ~/.cassandra/cqlshrc
Login to the Cassandra shell with your new changes.
$ cqlsh Connected to Test Cluster at 127.0.0.1:9042 [cqlsh 6.0.0 | Cassandra 4.0.5 | CQL spec 3.4.5 | Native protocol v5] Use HELP for help. navjot@cqlsh>
Note: The method of storing username and password in the cqlshrc
file will become deprecated from the 4.1 version of Cassandra. To learn more about it, you can read the entry on Cassandra's site.
4.3 - Rename the Cluster
Finally, we will rename the cluster name from Test Cluster to your chosen name.
Log into the cqlsh
terminal.
$ cqlsh
Replace the [clustername]
with your new cluster name in the command below.
username@cqlsh> UPDATE system.local SET cluster_name = '[new_name]' WHERE KEY = 'local';
Exit the shell.
username@cqlsh> exit
Open the file /etc/cassandra/cassandra.yaml
for editing.
$ sudo nano /etc/cassandra/cassandra.yaml
Replace the value of the variable cluster_name
with the name of your choosing.
... # The name of the cluster. This is mainly used to prevent machines in # one logical cluster from joining another. cluster_name: '[new_name]' ...
Once finished, save the file by pressing Ctrl + X and entering Y when prompted.
Clear the Cassandra system cache.
$ nodetool flush system
Restart Cassandra.
$ sudo systemctl restart cassandra
Log in to the shell to see the new name.
$ cqlsh Connected to howtoforge at 127.0.0.1:9042 [cqlsh 6.0.0 | Cassandra 4.0.5 | CQL spec 3.4.5 | Native protocol v5] Use HELP for help. navjot@cqlsh>
Conclusion
In this tutorial, you learned how to install Apache Cassandra on an AlmaLinux or Rocky Linux server. You also learned how to add user authentication and perform some basic configurations. To learn more, visit the official Cassandra documentation. If you have any questions, post them in the comments below.