How to Install Apache Cassandra on AlmaLinux / Rocky Linux 8

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 AlmaLinux and Rocky Linux 8 OS. The commands for both the Operating systems will be identical unless specified otherwise.

Prerequisites

  • A Server running AlmaLinux or Rocky Linux with a minimum of 2GB of RAM.

  • A non-sudo user with root privileges.

  • Everything is updated.

    $ sudo dnf update
    

Step 1 - Install Java

Apache Cassandra requires Java 8 to function. The latest version of Cassandra includes experimental support for Java 11, but for our tutorial, we will stick to using Java 8.

$ sudo dnf install java-1.8.0-openjdk java-1.8.0-openjdk-devel

Confirm the Java installation.

$ java -version
openjdk version "1.8.0_312"
OpenJDK Runtime Environment (build 1.8.0_312-b07)
OpenJDK 64-Bit Server VM (build 25.312-b07, mixed mode)

Step 2 - Install Apache Cassandra

The first step is to add Cassandra's official repository.

Run the following command to create the repository file /etc/yum.repos.d/cassandra.repo and enter the details.

$ sudo tee  /etc/yum.repos.d/cassandra.repo <<EOF
> [cassandra]
> name=Apache Cassandra
> baseurl=https://www.apache.org/dist/cassandra/redhat/40x/
> gpgcheck=1
> repo_gpgcheck=1
> gpgkey=https://www.apache.org/dist/cassandra/KEYS
> EOF

Now that the repo file is created install, Cassandra.

$ sudo dnf install cassandra -y

Step 3 - Install Cqlsh

We will use the CQL Shell (cqlsh) tool to interact with Cassandra. The tool is compatible with Python 2.7 or Python 3.6+. For our tutorial, we will be using Python 3.8. Install Python 3.8.

$ sudo dnf install python38

Set Python 3.8 as the default Python version.

$ sudo update-alternatives --config python
There are 3 programs which provide 'python'.

  Selection    Command
-----------------------------------------------
*+ 1           /usr/libexec/no-python
   2           /usr/bin/python3
   3           /usr/bin/python3.8

Enter to keep the current selection[+], or type selection number: 3

You will be presented with multiple options. We will choose number 3 to set Python 3.8 as the default version in our case.

Confirm the Python installation.

$ python --version
Python 3.8.8

After using the update-alternatives utility, you don't need to use the python3 command.

Install cqlsh using pip Python package manager.

$ pip3 install --user cqlsh

Confirm the cqlsh install.

$ cqlsh --version
cqlsh 6.0.0

Step 4 - Create a Systemd Unit file for Cassandra

Create and open the /etc/systemd/system/cassandra.service for editing.

$ sudo nano /etc/systemd/system/cassandra.service

Paste the following code in it.

[Unit]
Description=Apache Cassandra
After=network.target

[Service]
PIDFile=/var/run/cassandra/cassandra.pid
User=cassandra
Group=cassandra
ExecStart=/usr/sbin/cassandra -f -p /var/run/cassandra/cassandra.pid
Restart=always

[Install]
WantedBy=multi-user.target

Save the file by pressing Ctrl + X and entering Y when prompted.

Reload the service daemon.

$ sudo systemctl daemon-reload

Enable and start the Cassandra service.

$ sudo systemctl enable cassandra --now

Check the status of the service.

$ sudo systemctl status cassandra
? cassandra.service - Apache Cassandra
   Loaded: loaded (/etc/systemd/system/cassandra.service; enabled; vendor preset: disabled)
   Active: active (running) since Thu 2021-12-30 11:07:43 UTC; 12s ago
 Main PID: 4679 (java)
    Tasks: 48 (limit: 23696)
   Memory: 1.3G
   CGroup: /system.slice/cassandra.service
           ??4679 /usr/bin/java -ea -da:net.openhft... -XX:+UseThreadPriorities -XX:+HeapDumpOnOutOfMemoryError -Xss256>

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.09 KiB  16      100.0%            2fe7ccae-2af9-4841-9bff-bffa29f10dc5  rack1

Step 5 - 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/conf/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.

5.1 Enable User Authentication

To enable user authentication, first, take a backup of the /etc/cassandra/conf/cassandra.yaml file.

$ sudo cp /etc/cassandra/conf/cassandra.yaml /etc/cassandra/conf/cassandra.yaml.backup

Open the cassandra.yaml file for editing.

$ sudo nano /etc/cassandra/conf/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

5.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

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.

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.

username@cqlsh> exit

5.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.

You can find a sample file at /etc/cassandra/conf/cqlshrc.sample containing all the settings you can configure regarding the Cassandra Shell.

Copy and rename the example file to the ~/.cassandra directory.

$ sudo cp /etc/cassandra/conf/cqlshrc.sample ~/.cassandra/cqlshrc

Update the cqlshrc file with the required permissions.

$ sudo chmod 600 ~/.cassandra/cqlshrc
$ sudo chown $USER:$USER ~/.cassandra/cqlshrc

Open the file for editing.

$ nano ~/.cassandra/cqlshrc

We will configure the shell to automatically log in with the superuser credentials. Find the following section and fill it in with your username and password.

....

[authentication]
;; If Cassandra has auth enabled, fill out these options
username = [superuser]
password = [password]

....

Edit any other settings you want to change. Some of the settings are commented using ;;. Uncomment them by removing double semi-colons and then make the change.

Once finished, save the file by pressing Ctrl + X and entering Y when prompted.

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.1 | CQL spec 3.4.5 | Native protocol v5]
Use HELP for help.
username@cqlsh>

5.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/conf/cassandra.yaml for editing.

$ sudo nano /etc/cassandra/conf/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 Cluster at 127.0.0.1:9042
[cqlsh 6.0.0 | Cassandra 4.0.1 | CQL spec 3.4.5 | Native protocol v5]
Use HELP for help.
username@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.

Share this page:

0 Comment(s)