Cassandra is a free, open-source, NoSQL database management system designed to handle large amounts of data. Cassandra uses dynamo-style replication. These replicas are stored on several nodes, thus providing high availability and zero points of failure. Cassandra is ideal in IoT and other applications including social media analytics, messaging services, and retail applications where massive data is collected. Many big organizations, including Netflix, Facebook, Cisco, Hulu, Twitter, and more, use Apache Cassandra.

This tutorial will explain how to install Apache Cassandra on Ubuntu 22.04.

Prerequisites

  • A server running Ubuntu 22.04.
  • A root password is configured on the server.

Install Java 8

Apache Cassandra only supports Java version 8. So you will need to install it on your server. You can install it by running the following command:

apt-get install openjdk-8-jdk -y

Once Java is installed. verify the Java installation using the following command:

java -version

You will get the following output:

openjdk version "1.8.0_312"
OpenJDK Runtime Environment (build 1.8.0_312-8u312-b07-0ubuntu1-b07)
OpenJDK 64-Bit Server VM (build 25.312-b07, mixed mode)

Once you are finished, you can proceed to the next step.

Add Apache Cassandra Repository

By default, the Apache Cassandra package is not included in the Ubuntu 22.04 default repository. So you will need to add the Cassandra official repository to APT.

First, install the required dependencies using the following command:

apt-get install apt-transport-https gnupg2 -y

Once all the dependencies are installed, import the Cassandra GPG key with the following command:

wget -q -O - https://www.apache.org/dist/cassandra/KEYS | apt-key add -

Next, add the Cassandra repository to APT using the following command:

sh -c 'echo "deb http://www.apache.org/dist/cassandra/debian 311x main" > /etc/apt/sources.list.d/cassandra.list'

Next, update the repository cache using the following command:

apt-get update -y

Once your system repository is updated, you can proceed to the next step.

Install Cassandra on Ubuntu 22.04

You can now install the Apache Cassandra by just running the following command:

apt-get install cassandra -y

Once the Apache Cassandra is installed, you can check the running status of the Cassandra using the following command:

systemctl status cassandra

You will get the following output:

? cassandra.service - LSB: distributed storage system for structured data
     Loaded: loaded (/etc/init.d/cassandra; generated)
     Active: active (running) since Thu 2022-05-05 11:57:22 UTC; 40s ago
       Docs: man:systemd-sysv-generator(8)
    Process: 24707 ExecStart=/etc/init.d/cassandra start (code=exited, status=0/SUCCESS)
      Tasks: 57 (limit: 4630)
     Memory: 1.2G
        CPU: 15.952s
     CGroup: /system.slice/cassandra.service
             ??24800 /usr/bin/java -Xloggc:/var/log/cassandra/gc.log -ea -XX:+UseThreadPriorities -XX:ThreadPriorityPolicy=42 -XX:+HeapDumpOn>

May 05 11:57:22 ubuntu2204 systemd[1]: Starting LSB: distributed storage system for structured data...
May 05 11:57:22 ubuntu2204 systemd[1]: Started LSB: distributed storage system for structured data.

You can also verify the Cassandra listening port using the following command:

ss -antpl | grep java

You will get the following output:

LISTEN 0      500        127.0.0.1:7000       0.0.0.0:*    users:(("java",pid=24800,fd=86))                                                  
LISTEN 0      50         127.0.0.1:45049      0.0.0.0:*    users:(("java",pid=24800,fd=83))                                                  
LISTEN 0      50         127.0.0.1:7199       0.0.0.0:*    users:(("java",pid=24800,fd=82))                                                  
LISTEN 0      4096       127.0.0.1:9042       0.0.0.0:*    users:(("java",pid=24800,fd=100)) 

How to Connect Apache Cassandra

First, you will need to check the status of the Apache Cassandra, you can check it using the following command:

nodetool status

You will get the following output:

Datacenter: datacenter1
=======================
Status=Up/Down
|/ State=Normal/Leaving/Joining/Moving
--  Address    Load       Tokens       Owns (effective)  Host ID                               Rack
UN  127.0.0.1  70.93 KiB  256          100.0%            eaa94d10-69c6-405d-b7bd-4b3dcbd380be  rack1

You can now use the cqlsh utility to connect to the Cassandra shell:

cqlsh

Once you are connected, you will get the following output:

Connected to Test Cluster at 127.0.0.1:9042.
[cqlsh 5.0.1 | Cassandra 3.11.12 | CQL spec 3.4.4 | Native protocol v4]
Use HELP for help.
cqlsh> 

Now, exit from the Cassandra shell using the following command:

cqlsh> exit

Change Cassandra Cluster Name

By default, Cassandra default cluster name is set to Test Cluster. It is recommended to change it with a meaningful name.

To do so, connect to the Cassandra with the following command:

cqlsh

Once you are connected, change the cluster name to "Cassandra Cluster" as shown below:

cqlsh> UPDATE system.local SET cluster_name = 'Cassandra Cluster' WHERE KEY = 'local';

Next, exit from the Cassandra with the following command:

cqlsh> exit

Next, you will also need to edit the Cassandra configuration file and change your cluster name. You can edit it with the following command:

nano /etc/cassandra/cassandra.yaml

Change the following line:

cluster_name: 'Cassandra Cluster'

Save and close the file when you are finished then flush the system cache using the following command:

nodetool flush system

Next, restart the Cassandra service to apply the changes:

systemctl restart cassandra

You can now verify the Cassandra cluster name using the following command:

cqlsh

If everything is fine, you will get a new cluster name in the following output:

Connected to Cassandra Cluster at 127.0.0.1:9042.
[cqlsh 5.0.1 | Cassandra 3.11.12 | CQL spec 3.4.4 | Native protocol v4]
Use HELP for help.
cqlsh> 

Conclusion

Congratulations! you have successfully installed Apache Cassandra on Ubuntu 22.04. You can now use Cassandra in a cluster environment to handle a large amount of data. Feel free to ask me if you have any questions.

Share this page:

0 Comment(s)