Apache Cassandra is a powerful and scalable NoSQL database server that is widely used in distributed systems. It offers high availability and low latency, making it an ideal choice for applications that require continuous operation, even in the face of data location failures. This includes banking systems, healthcare management systems, social media applications, and e-commerce stores.
In this guide, we will walk you through the process of installing and securing the Apache Cassandra Database Server on a Ubuntu 22.04 server. By the end of this article, you will have a fully operational and secure Cassandra database server ready to use.
Prerequisites
Before we begin, make sure you have the following prerequisites in place:
- A Ubuntu 22.04 server with at least 8GB RAM (for development) or 32GB RAM (for production).
- SSH access to the server.
- A non-root user with sudo privileges.
- Switch to the sudo user account.
# su shapehost
- Update the server.
$ sudo apt update && sudo apt upgrade -y
Installing Apache Cassandra
To install Apache Cassandra on your Ubuntu 22.04 server, follow these steps:
Step 1: Install the Java Development Kit
The first step is to install the Java Development Kit (JDK), as Cassandra requires Java to run. Run the following command to install the default JDK:
$ sudo apt installdefault-jdk -y
Step 2: Install Dependencies
Next, we need to install the required dependencies for Apache Cassandra. Run the following command to install apt-transport-https
and gnupg2
:
$ sudo apt install apt-transport-https gnupg2 -y
Step 3: Add the Apache Cassandra Repository
To add the Apache Cassandra repository to the APT sources list, run the following command:
$ echo "deb https://debian.cassandra.apache.org 41x main" | sudo tee -a /etc/apt/sources.list.d/cassandra.sources.list
This command adds version 4.1 of Apache Cassandra. If you want to use the latest version, visit the Apache Cassandra download page for the appropriate repository source.
Step 4: Update and Install Apache Cassandra
Now that we have added the Apache Cassandra repository, let’s update the server packages to enable the Cassandra repository:
$ sudo apt update
Finally, install Apache Cassandra by running the following command:
$ sudo apt install cassandra -y
The installation process may take a few moments. Once it is complete, Apache Cassandra will automatically start up. You can verify that it starts correctly by checking the application logs:
$ cat /var/log/cassandra/system.log | tail
The output should indicate that Apache Cassandra has started successfully:
...
INFO [main] ... - Starting listening for CQL clients on localhost/127.0.0.1:9042 (unencrypted)...
INFO [OptionalTasks:1] ... Created default superuser role 'cassandra'
You can also check the status of the Apache Cassandra service by running the following command:
$ sudo systemctl status cassandra
The output should indicate that the service is active and running:
● cassandra.service - LSB: distributed storage system for structured data Loaded: loaded (/etc/init.d/cassandra; generated) Active: active (running)
Additionally, you can verify the status of the Cassandra node by running the following command:
$ nodetool status
The output will display the status of the Cassandra node:
Datacenter: datacenter1 ======================= Status=Up/Down |/ State=Normal/Leaving/Joining/Moving -- Address Load Tokens Owns (effective) Host ID Rack UN 127.0.0.1 104.33 KiB 16 100.0% 15addadb-68b2-48f4-b076-ed020c2e926b rack1
Congratulations! You have successfully installed and verified the Apache Cassandra database server on your Ubuntu 22.04 server.
Using the Apache Cassandra Command-line Interface
Apache Cassandra provides a command-line interface (CLI) called cqlsh, which allows you to interact with the database server using Cassandra Query Language (CQL) commands. In this section, we will show you how to access the database server and perform basic operations.
Step 1: Log in to the Cassandra Database Server
To log in to the Cassandra database server, run the following command:
$ cqlsh
This will open the cqlsh console, where you can execute CQL commands.
Step 2: Create a Keyspace
In Cassandra, a keyspace is an object that stores tables and defines how data replication works on different nodes. Let’s create a new keyspace called “my_company” by running the following command in the cqlsh console:
cqlsh> CREATE KEYSPACE my_company WITH REPLICATION = { 'class': 'SimpleStrategy', 'replication_factor' : 1 };
Step 3: Switch to the Keyspace
To switch to the newly created keyspace, use the following command:
cqlsh> USE my_company;
Step 4: Create a Table
Now, let’s create a sample table called “products” in the “my_company” keyspace. This table will store product information, including the product ID, product name, and retail price. Run the following command in the cqlsh console:
cqlsh:my_company> CREATE TABLE products ( product_id UUID PRIMARY KEY, product_name TEXT, retail_price DOUBLE );
The above command creates a table with three columns: “productid,” “productname,” and “retailprice.” The “productid” column is set as the primary key, ensuring the uniqueness of each record. The “productname” column stores the name of the product, and the “retailprice” column stores the retail price of the product.
Step 5: Insert Data into the Table
Let’s insert some sample data into the “products” table. Run the following commands in the cqlsh console:
cqlsh:my_company> INSERT INTO products (product_id, product_name, retail_price) VALUES (UUID(), 'PLIERS', 3.52); cqlsh:my_company> INSERT INTO products (product_id, product_name, retail_price) VALUES (UUID(), 'FILE', 4.38); cqlsh:my_company> INSERT INTO products (product_id, product_name, retail_price) VALUES (UUID(), 'PADLOCK', 14.30);
The above commands use the UUID() function to generate a unique product ID for each record. You can insert as many records as needed.
Step 6: Retrieve Data from the Table
To retrieve data from the “products” table, run the following command:
cqlsh:my_company> SELECT product_id, product_name, retail_price FROM products;
The output will display the product ID, product name, and retail price for each record:
product_id | product_name | retail_price --------------------------------------+--------------+-------------- 56c0f7f7-be59-4f0d-a474-5a2174c19060 | FILE | 4.38 12e720cf-f191-4e59-8c66-360339deae65 | PADLOCK | 14.30 9c62c3a0-0e31-41ab-90d1-09f4d35b6e26 | PLIERS | 3.52 (3 rows)
Step 7: Exit the Cassandra Database Console
To exit the Cassandra database console, simply type “QUIT” and press Enter:
cqlsh:my_company> QUIT;
Congratulations! You have successfully used the Apache Cassandra command-line interface to create a keyspace, a table, insert data, and retrieve data from the database.
Securing Apache Cassandra with Password Authentication
It is crucial to secure your Apache Cassandra database server to prevent unauthorized access. In this section, we will show you how to secure Cassandra with password authentication.
Step 1: Edit the Cassandra Configuration File
To enable password authentication for Cassandra, we need to modify the main configuration file. Use a text editor like Nano to open the file:
$ sudo nano /etc/cassandra/cassandra.yaml
Step 2: Change the Authenticator Setting
In the configuration file, locate the “authenticator” directive and change its value from “AllowAllAuthenticator” to “PasswordAuthenticator”:
... authenticator: PasswordAuthenticator ...
Step 3: Save and Close the File
Save the changes and close the file.
Step 4: Restart Apache Cassandra
To apply the changes, restart the Apache Cassandra server:
$ sudo systemctl restart cassandra
Wait for at least one minute before re-accessing the database console. You can check the application logs to verify that the startup is complete:
$ cat /var/log/cassandra/system.log | tail
The output should indicate that the startup is complete.
Step 5: Create a Superuser Account
To create a new superuser account, log in to the Cassandra database console using the default username and password:
$ cqlsh -u cassandra -p cassandra
In the cqlsh console, run the following command to create a new superuser account:
cassandra@cqlsh> CREATE ROLE db_administrator WITH SUPERUSER = true AND LOGIN = true AND PASSWORD = 'EXAMPLE_PASSWORD';
Replace “dbadministrator” with your desired username and “EXAMPLEPASSWORD” with a strong password.
Step 6: Exit the Cassandra Database Console
Exit the Cassandra database console by typing “QUIT” and pressing Enter:
cassandra@cqlsh> QUIT;
Step 7: Verify the New User Account
To verify that the new user account is available, log in to the Cassandra database server again using the new username and password:
$ cqlsh -u db_administrator
Enter the password you set earlier when prompted.
Step 8: Disable the Default Superuser Account
For enhanced security, it is recommended to disable the default superuser account. In the cqlsh console, run the following command:
db_administrator@cqlsh> ALTER ROLE cassandra WITH SUPERUSER = false AND LOGIN = false;
Step 9: Exit the Cassandra Database Console
Exit the Cassandra database console:
db_administrator@cqlsh> QUIT;
Step 10: Verify the Default User Account
To verify that the default user account can no longer access the database server, run the following command:
$ cqlsh -u cassandra -p cassandra
The output should indicate that the connection was refused due to authentication failure.
Congratulations! You have successfully secured your Apache Cassandra database server with password authentication.
Conclusion
In this guide, we have covered the installation and security of the Apache Cassandra database server on a Ubuntu 22.04 server. We walked through the installation process, including the installation of dependencies and the configuration of Cassandra. Additionally, we demonstrated how to use the cqlsh command-line interface to create a keyspace, a table, insert data, and retrieve data from the database.
By following the steps outlined in this guide, you now have a fully functional and secure Apache Cassandra database server that can be used to power your distributed systems. Apache Cassandra’s scalability and high availability make it an excellent choice for applications that require continuous operation.
For more information and advanced configuration options, refer to the official Apache Cassandra documentation.
Shape.host provides reliable and scalable Cloud VPS solutions to help businesses thrive in the digital world. Consider Shape.host for your cloud hosting needs.