There is a new version of this tutorial available for Debian 12 (Bookworm).

How to Install a CockroachDB Cluster on Debian 11

CockroachDB is an open-source distributed and scalable SQL database for cloud applications. CockroachDB provides next-level consistency, replicated SQL database, and a transactional data store. CockroachDB stores your data in multiple locations making data delivery faster. Also, it's easy to scale and provides high availability and fault tolerance for your applications.

In this tutorial, we will show you how to install the CockroachDB cluster on Debian 11 server.

Prerequisites

  • Two or more Debian 11 Server.
  • A root password is configured on servers.

Installing CockroachDB on All 3 Servers

For the installation, cockroachdb is easy to install. It's because cockroachdb provides a binary file for the Linux system that you can download to your system.

Download the cockroachdb binary file for Linux using the following command. This command will download the cockroachdb binary file, extract the compressed file, then move the binary file of cockroachdb to the /usr/local/bin directory.

curl https://binaries.cockroachdb.com/cockroach-v21.2.8.linux-amd64.tgz | tar -xz && sudo cp -i cockroach-v21.2.8.linux-amd64/cockroach /usr/local/bin/

The cockroachdb used the custom built-in GEOS library. The GEOS library is included on the compressed file of cockroachdb and must be installed in the lib directory.

Create a new directory /usr/local/lib/cockroach using the command below.

mkdir -p /usr/local/lib/cockroach

Now copy the GEOS library to the /usr/local/lib/cockroach directory.

cp -i cockroach-v21.2.8.linux-amd64/lib/libgeos.so /usr/local/lib/cockroach/
cp -i cockroach-v21.2.8.linux-amd64/lib/libgeos_c.so /usr/local/lib/cockroach/

The basic installation of cockroachdb is installed on Debian servers.

download and install cockroachdb

Run the following command to check the cockroachdb binary file and the current version of cockroachdb that you just installed.

which cockroach
cockroach version

You will get the following output.

checking cockroachdb

Setting up Firewall

If you are running the Firewall on your Debian servers, you will need to add cockroachdb ports to the firewall configuration.

The cockroachdb used port 8080 for the web-based administration cockroachdb, and port 26257 for the user connections and cluster configuration.

Add ports 8080 and 25267 to the UFW firewall using the following command.

sudo ufw allow 8080/tcp
sudo ufw allow 26257/tcp

Now reload the UFW firewall rules and verify the current status of the firewall rules.

sudo ufw reload
sudo ufw status

Below you can see ports 8080 and 25267 are added to the UFW firewall.

setup ufw firewall

Initializing CockroachDB Cluster

To initialize the cockroachdb cluster, run the below command on the server11.

You will need to change the value of the following options:

  • --store: for storing the data of the CockroachDB cluster.
  • --listen-addr: which IP address the cockroachdb will be running on the server. The default port for cockroachdb is port 25267.
  • --http-addr: which IP address the cockroachdb web-based administration will be running on. The default web-based administration of cockroachdb is port 8080.
cockroach start \
--insecure \
--store=server11 \
--listen-addr=192.168.10.11:26257 \
--http-addr=192.168.10.11:8080 \
--join=192.168.10.11:26257,192.168.10.13:26258,192.168.10.14:26259 \
--background

You will get the following output.

start cluster on server11

Now move to server2 and run the following command to start the cockroachdb and join the cluster. Change the IP address on --listen-addr and --http-addr to the server2 IP address.

cockroach start \
--insecure \
--store=server2 \
--listen-addr=192.168.10.13:26257 \
--http-addr=192.168.10.13:8080 \
--join=192.168.10.11:26257,192.168.10.13:26258,192.168.10.14:26259 \
--background

You will get the following output from server2.

start cluster on server2

Next, move to server3 and run the following command to start the cockroachdb and join the cockroachdb cluster. Also, change the IP address of --listen-addr and --http-addr to the server3 IP address.

cockroach start \
--insecure \
--store=server3 \
--listen-addr=192.168.10.14:26257 \
--http-addr=192.168.10.14:8080 \
--join=192.168.10.11:26257,192.168.10.13:26258,192.168.10.14:26259 \
--background

You will get the following output from server3.

start cluster server3

After all is completed, back to the server11 and run the below command to initialize the cockroachdb cluster.

cockroach init --insecure --host=192.168.10.11:26257

You will get the output message 'cluster successfully initialized', which means the cockroachdb cluster is successfully initialized.

You can run the grep command below to check the log of the cockroachdb initialization. Change the server11 directory with your --store cockroachdb directory.

grep 'node starting' server11/logs/cockroach.log -A 11

Below you can see the logs from the server11 cockroachdb cluster initialization.

initialized cluster

Lastly, open your web browser and visit the server IP address followed by port 8080.

http://192.168.10.11:8080/

Below you can see there are three nodes on the cockroachdb cluster.

cockroachdb cluster

Testing Create New Database on CockroachDB

Now you have the cockroachdb cluster is running, you can use any server as the SQL gateway to access the cockroachdb.

Run the cockroach command on the server11 to connect to the cockroachdb cluster.

cockroach sql --insecure --host=192.168.10.11:26257

Login to SQL shell

After you are connected to the SQL shell, run the below query to create a new database.

CREATE DATABASE bank;

Now create a new table on the database using the following query.

CREATE TABLE bank.accounts (id INT PRIMARY KEY, balance DECIMAL);

Next, import sample data to the table.

INSERT INTO bank.accounts (1, 1000.50);

Verify the data on the database using the following query.

SELECT * FROM bank.accounts;

You will get the following output.

Create new database and insert data

Next, move to server2 or server3 and log in to the cockroachdb SQL shell using the following command.

cockroach sql --insecure --host=192.168.10.14:26257

After you are connected to the SQL shell on server3, run the following query to check and verify the database replication.

SELECT * FROM bank.accounts;

You will see the database and data from server11 are automatically replicated to server2 and server3.

checking database and replication

Conclusion

Congratulation! You have now installed and configured the cockroachdb cluster on Debian 11 servers. Also, you've learned how to connect to the cockroachdb shell and basic SQL commands for creating a database and inserting data.

Share this page:

0 Comment(s)