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.

<img alt="download and install cockroachdb" data-ezsrc="https://kirelos.com/wp-content/uploads/2022/04/echo/1download-install-cockroachdb.png625591e8cfcf5.jpg" ezimgfmt="rs rscb5 src ng ngcb5" height="111" loading="lazy" src="data:image/svg xml,” width=”750″>

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.

<img alt="checking cockroachdb" data-ezsrc="https://kirelos.com/wp-content/uploads/2022/04/echo/2checking-cockroach-version.png625591e8ebd10.jpg" ezimgfmt="rs rscb5 src ng ngcb5" height="302" loading="lazy" src="data:image/svg xml,” width=”667″>

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.

<img alt="setup ufw firewall" data-ezsrc="https://kirelos.com/wp-content/uploads/2022/04/echo/3setup-ufw-firewall.png625591e929f6a.jpg" ezimgfmt="rs rscb5 src ng ngcb5" height="454" loading="lazy" src="data:image/svg xml,” width=”552″>

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.

<img alt="start cluster on server11" data-ezsrc="https://kirelos.com/wp-content/uploads/2022/04/echo/4-start-cluster-server1.png625591e974b7c.jpg" ezimgfmt="rs rscb5 src ng ngcb5" height="412" loading="lazy" src="data:image/svg xml,” width=”750″>

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.

<img alt="start cluster on server2" data-ezsrc="https://kirelos.com/wp-content/uploads/2022/04/echo/5start-cluster-server2.png625591e9af644.jpg" ezimgfmt="rs rscb5 src ng ngcb5" height="394" loading="lazy" src="data:image/svg xml,” width=”750″>Advertisement

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.

<img alt="start cluster server3" data-ezsrc="https://kirelos.com/wp-content/uploads/2022/04/echo/6start-cluster-server3.png625591e9ed101.jpg" ezimgfmt="rs rscb5 src ng ngcb5" height="394" loading="lazy" src="data:image/svg xml,” width=”750″>

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.

<img alt="initialized cluster" data-ezsrc="https://kirelos.com/wp-content/uploads/2022/04/echo/7initialized-cluster.png625591ea414ce.jpg" ezimgfmt="rs rscb5 src ng ngcb5" height="240" loading="lazy" src="data:image/svg xml,” width=”750″>

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.

<img alt="cockroachdb cluster" data-ezsrc="https://kirelos.com/wp-content/uploads/2022/04/echo/8checking-web-based-cockroachdb.png625591ea82541.jpg" ezimgfmt="rs rscb5 src ng ngcb5" height="463" loading="lazy" src="data:image/svg xml,” width=”750″>

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

<img alt="Login to SQL shell" data-ezsrc="https://kirelos.com/wp-content/uploads/2022/04/echo/9login-to-cockroachdb-sql.png625591eab3db7.jpg" ezimgfmt="rs rscb5 src ng ngcb5" height="184" loading="lazy" src="data:image/svg xml,” width=”750″>

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.

<img alt="Create new database and insert data" data-ezsrc="https://kirelos.com/wp-content/uploads/2022/04/echo/10create-new-database.png625591eb03a7f.jpg" ezimgfmt="rs rscb5 src ng ngcb5" height="343" loading="lazy" src="data:image/svg xml,” width=”750″>

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.

<img alt="checking database and replication" data-ezsrc="https://kirelos.com/wp-content/uploads/2022/04/echo/11checking-database.png625591eb3a23e.jpg" ezimgfmt="rs rscb5 src ng ngcb5" height="338" loading="lazy" src="data:image/svg xml,” width=”750″>

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.