Cyberithub

How to Install and Use Apache Cassandra on Ubuntu 20.04 LTS(Focal Fossa)

Advertisements

In this article, I will take you through the steps to install and use Apache Cassandra on Ubuntu 20.04 LTS (Focal Fossa). If you are looking for a NoSQL database which is both efficient and fast then Cassandra is the ideal solution to use. Cassandra is a free and open-source, distributed, wide-column store, decentralized, elastically scalable, highly available, NoSQL database management system designed to handle large amounts of data across many commodity servers, providing high availability with no single point of failure. It is very frequently used to power the cloud applications across many industries.

How to Install and Use Apache Cassandra on Ubuntu 20.04 LTS(Focal Fossa)

How to Install and Use Apache Cassandra on Ubuntu 20.04 LTS(Focal Fossa)

Also Read: How to Perform RPM Hardening in Linux (RHEL/CentOS/Rocky Linux)

Step 1: Prerequisites

a) You should have a running Ubuntu 20.04 LTS(Focal Fossa) Server.

b) You should have sudo or root access to run privileged commands.

c) You should have apt or apt-getcurl and tee utility available in your Server.

 

Step 2: Update Your Server

Before installing any new packages, it is always advisable to first update your System by using apt update command and if you see any packages requires upgradation then run apt upgrade command as well.

root@cyberithub:~# apt update
Hit:1 http://in.archive.ubuntu.com/ubuntu focal InRelease
Get:2 http://in.archive.ubuntu.com/ubuntu focal-updates InRelease [114 kB]
Get:3 http://in.archive.ubuntu.com/ubuntu focal-backports InRelease [108 kB]
Get:4 https://dl.google.com/linux/chrome/deb stable InRelease [1,811 B]
Get:5 http://security.ubuntu.com/ubuntu focal-security InRelease [114 kB]
Get:6 http://in.archive.ubuntu.com/ubuntu focal-updates/main amd64 Packages [1,572 kB]
Get:7 http://in.archive.ubuntu.com/ubuntu focal-updates/main i386 Packages [602 kB]
Get:8 http://in.archive.ubuntu.com/ubuntu focal-updates/main Translation-en [302 kB]
Get:9 http://in.archive.ubuntu.com/ubuntu focal-updates/main amd64 DEP-11 Metadata [282 kB]
Get:10 http://in.archive.ubuntu.com/ubuntu focal-updates/main amd64 c-n-f Metadata [14.7 kB]
Get:11 http://in.archive.ubuntu.com/ubuntu focal-updates/restricted amd64 Packages [801 kB]

 

Step 3: Install Java

Next step is to install Java if it is not already installed. You can check that by running java -version command. If it does not installed then you will see output like below where it will ask you to use any of the below command to install Java platform. Since we are going to need JDK so we will install OpenJDK from the Ubuntu Repository.

root@cyberithub:~# java --version

Command 'java' not found, but can be installed with:

apt install default-jre # version 2:1.11-72, or
apt install openjdk-11-jre-headless # version 11.0.13+8-0ubuntu1~20.04
apt install openjdk-13-jre-headless # version 13.0.7+5-0ubuntu1~20.04
apt install openjdk-16-jre-headless # version 16.0.1+9-1~20.04
apt install openjdk-17-jre-headless # version 17.0.1+12-1~20.04
apt install openjdk-8-jre-headless # version 8u312-b07-0ubuntu1~20.04

To install OpenJDK, use apt install openjdk-8-jdk command as shown below.

root@cyberithub:~# apt install openjdk-8-jdk
Reading package lists... Done
Building dependency tree
Reading state information... Done
The following packages were automatically installed and are no longer required:
dmeventd libaio1 libdevmapper-event1.02.1 liblvm2cmd2.03 libreadline5 thin-provisioning-tools
Use 'apt autoremove' to remove them.
The following additional packages will be installed:
ca-certificates-java fonts-dejavu-extra java-common libatk-wrapper-java libatk-wrapper-java-jni libice-dev libpthread-stubs0-dev libsm-dev libx11-dev
libxau-dev libxcb1-dev libxdmcp-dev libxt-dev openjdk-8-jdk-headless openjdk-8-jre openjdk-8-jre-headless x11proto-core-dev x11proto-dev
xorg-sgml-doctools xtrans-dev
........................................................

Now if you check the Java version again, it should show like below. This confirms that java is properly installed now.

root@cyberithub:~# java -version
openjdk version "1.8.0_312"
OpenJDK Runtime Environment (build 1.8.0_312-8u312-b07-0ubuntu1~20.04-b07)
OpenJDK 64-Bit Server VM (build 25.312-b07, mixed mode)

 

Step 4: Install Apache Cassandra

Now we can proceed towards the installation of Cassandra DB. Since Apache Cassandra is not available from default repo so we will need to add the respective repo first from where we can directly download and install the packages.

a) Install apt-transport-https

But even before that we need to first install the apt-transport-https package by using apt install apt-transport-https command as shown below.

root@cyberithub:~# apt install apt-transport-https
Reading package lists... Done
Building dependency tree
Reading state information... Done
The following packages were automatically installed and are no longer required:
dmeventd libaio1 libdevmapper-event1.02.1 liblvm2cmd2.03 libreadline5 thin-provisioning-tools
Use 'apt autoremove' to remove them.
The following NEW packages will be installed:
apt-transport-https
0 upgraded, 1 newly installed, 0 to remove and 170 not upgraded.
Need to get 4,680 B of archives.
After this operation, 162 kB of additional disk space will be used.
Get:1 http://in.archive.ubuntu.com/ubuntu focal-updates/universe amd64 apt-transport-https all 2.0.6 [4,680 B]
Fetched 4,680 B in 0s (12.3 kB/s)
Selecting previously unselected package apt-transport-https.
(Reading database ... 182193 files and directories currently installed.)
Preparing to unpack .../apt-transport-https_2.0.6_all.deb ...
Unpacking apt-transport-https (2.0.6) ...
Setting up apt-transport-https (2.0.6) ...

b) Import GPG Key

You can go to Cassandra Download Page and get the steps to add the GPG Key. This is required to verify the integrity of the package.

root@cyberithub:~# wget -q -O - https://www.apache.org/dist/cassandra/KEYS | sudo apt-key add -
OK

c) Add Repo

Next, you need to add the repo by using below command.

root@cyberithub:~# echo "deb http://www.apache.org/dist/cassandra/debian 40x main" | sudo tee -a /etc/apt/sources.list.d/cassandra.sources.list
deb http://www.apache.org/dist/cassandra/debian 40x main

d) Update Package Cache

Now that repo is added, you can update the package cache with all the package information from the new repository by using apt update command as shown below.

root@cyberithub:~# apt update
Hit:1 http://in.archive.ubuntu.com/ubuntu focal InRelease
Hit:2 http://in.archive.ubuntu.com/ubuntu focal-updates InRelease
Hit:4 http://in.archive.ubuntu.com/ubuntu focal-backports InRelease
Hit:5 https://dl.google.com/linux/chrome/deb stable InRelease
Hit:6 http://security.ubuntu.com/ubuntu focal-security InRelease
Get:3 https://downloads.apache.org/cassandra/debian 40x InRelease [3,902 B]
Get:7 https://downloads.apache.org/cassandra/debian 40x/main amd64 Packages [684 B]
Get:8 https://downloads.apache.org/cassandra/debian 40x/main i386 Packages [684 B]
Fetched 5,270 B in 3s (2,003 B/s)
Reading package lists... Done
Building dependency tree
Reading state information... Done

e) Install Cassandra DB

Now we are ready to install cassandra package and all its required dependencies by using apt install cassandra command as shown below.

root@cyberithub:~# apt install cassandra
Reading package lists... Done
Building dependency tree
Reading state information... Done
The following packages were automatically installed and are no longer required:
dmeventd libaio1 libdevmapper-event1.02.1 liblvm2cmd2.03 libreadline5 thin-provisioning-tools
Use 'apt autoremove' to remove them.
Suggested packages:
cassandra-tools
The following NEW packages will be installed:
cassandra
0 upgraded, 1 newly installed, 0 to remove and 170 not upgraded.
Need to get 47.4 MB of archives.
After this operation, 58.7 MB of additional disk space will be used.

 

Step 5: Check Cassandra Service

After successful installation, you can check the status of the service by using systemctl status cassandra command as shown below.

root@cyberithub:~# systemctl status cassandra
● cassandra.service - LSB: distributed storage system for structured data
Loaded: loaded (/etc/init.d/cassandra; generated)
Active: active (running) since Tue 2022-02-15 11:54:24 IST; 4min 20s ago
Docs: man:systemd-sysv-generator(8)
Tasks: 41 (limit: 2293)
Memory: 1.2G
CGroup: /system.slice/cassandra.service
└─10774 /usr/bin/java -ea -da:net.openhft... -XX:+UseThreadPriorities -XX:+HeapDumpOnOutOfMemoryError -Xss256k -XX:+AlwaysPreTouch -XX:-UseBias>

Feb 15 11:54:23 cyberithub systemd[1]: Starting LSB: distributed storage system for structured data...
Feb 15 11:54:24 cyberithub systemd[1]: Started LSB: distributed storage system for structured data.

 

Step 6: Check Nodetool Status

It is also important to check the node status by running nodetool status command as shown below. If you are running a single node cluster then the output should look like below.

root@cyberithub:~# 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.07 KiB  16      100.0%           5bb92ac6-cc87-4a06-aed6-1817d5cb577f  rack1

 

Step 7: Login to Cassandra

Once Cassandra is up and running, you can login to the DB by using cqlsh command as shown below.

root@cyberithub:~# cqlsh
Connected to Test Cluster at 127.0.0.1:9042
[cqlsh 6.0.0 | Cassandra 4.0.2 | CQL spec 3.4.5 | Native protocol v5]
Use HELP for help.
cqlsh>

 

Step 8: Configure Cassandra

You might have noticed that by default Cassandra cluster name is set to Test Cluster. If you would like to change this name then first you need to run below update query and finally exit the prompt by using exit.

cqlsh> update system.local SET cluster_name = 'CyberITHub Cluster' WHERE KEY = 'local';
cqlsh> exit

Then you need to edit the /etc/cassandra/cassandra.yaml configuration file using an editor such as nano editor and then update the cluster name from Test to CyberITHub as shown below. Once done, Press Ctrl+X to save and close the file.

root@cyberithub:~# nano /etc/cassandra/cassandra.yaml
# Cassandra storage config YAML

# NOTE:
# See https://cassandra.apache.org/doc/latest/configuration/ for
# full explanations of configuration directives
# /NOTE

# The name of the cluster. This is mainly used to prevent machines in
# one logical cluster from joining another.
cluster_name: 'CyberITHub Cluster'

After changing the configuration, you need to restart the service by using systemctl restart cassandra command. Verify the status by using systemctl status cassandra command as shown below.

root@cyberithub:~# systemctl restart cassandra
root@cyberithub:~# systemctl status cassandra
● cassandra.service - LSB: distributed storage system for structured data
Loaded: loaded (/etc/init.d/cassandra; generated)
Active: active (running) since Tue 2022-02-15 13:28:23 IST; 8s ago
Docs: man:systemd-sysv-generator(8)
Process: 16771 ExecStart=/etc/init.d/cassandra start (code=exited, status=0/SUCCESS)
Tasks: 5 (limit: 2293)
Memory: 48.5M
CGroup: /system.slice/cassandra.service
├─16785 /bin/sh /usr/sbin/cassandra -p /var/run/cassandra/cassandra.pid -H /var/lib/cassandra/java_1644911903.hprof -E /var/lib/cassandra/hs_er>
├─16838 /bin/sh /usr/sbin/cassandra -p /var/run/cassandra/cassandra.pid -H /var/lib/cassandra/java_1644911903.hprof -E /var/lib/cassandra/hs_er>
├─16839 /sbin/ldconfig.real -v
├─16840 grep -v ^\s
└─16841 sed s/^\([^:]*\):.*$/\1/

Feb 15 13:28:22 cyberithub systemd[1]: Starting LSB: distributed storage system for structured data...
Feb 15 13:28:23 cyberithub systemd[1]: Started LSB: distributed storage system for structured data.

Then wait for few mins to have the services come up and then test the Cluster name by running cqlsh in the terminal again. You will see that the Cluster name is now updated to CyberITHub.

NOTE:

Please note that if you are receiving Connection Error: Unable to connect to any servers error after running cqlsh command then check the node status by using nodetool status command. Once the node is up then only try to login to Cassandra using cqlsh command.
root@cyberithub:~# cqlsh
Connected to CyberITHub Cluster at 127.0.0.1:9042
[cqlsh 6.0.0 | Cassandra 4.0.2 | CQL spec 3.4.5 | Native protocol v5]
Use HELP for help.
cqlsh>

 

Step 9: List All Keyspaces

If you want to list all the Keyspaces then you need to use describe keyspaces or desc keyspaces query as shown below.

cqlsh> desc keyspaces;

system system_distributed system_traces system_virtual_schema
system_auth system_schema system_views

 

Step 10: List All System Tables

If you want to list all the tables of a keyspace then you need to first go that keyspace by using use keyword and then list the tables by using describe tables or desc tables query as shown below.

cqlsh> use system;
cqlsh:system> desc tables;

available_ranges paxos size_estimates
available_ranges_v2 peer_events sstable_activity
batches peer_events_v2 table_estimates
built_views peers transferred_ranges
compaction_history peers_v2 transferred_ranges_v2
"IndexInfo" prepared_statements view_builds_in_progress
local repairs

 

Step 11: Uninstall Cassandra

If at all you have to remove the Cassandra from your Server then you can do that by using one simple command - apt remove cassandra as shown below.

root@cyberithub:~# apt remove cassandra
Reading package lists... Done
Building dependency tree
Reading state information... Done
The following packages were automatically installed and are no longer required:
dmeventd libaio1 libdevmapper-event1.02.1 liblvm2cmd2.03 libreadline5 thin-provisioning-tools
Use 'apt autoremove' to remove them.
The following packages will be REMOVED:
cassandra
0 upgraded, 0 newly installed, 1 to remove and 170 not upgraded.
After this operation, 58.7 MB disk space will be freed.
Do you want to continue? [Y/n] Y
(Reading database ... 182370 files and directories currently installed.)
Removing cassandra (4.0.2) ...

Leave a Comment