Install CockroachDB cluster on Ubuntu 22.04

In this tutorial, you will learn how to install CockroachDB cluster on Ubuntu 22.04. CockroachDB is globally-distributed and cloud based SQL database. The deployment options are dedicated, serverless and self-hosted.

The best features of CockroachDB are data availability, scalability and fault tolerance. Data is replicated in different nodes geo-graphically around the world making it easier and fast to access the data.

This article demonstrates how to install CockroachDB cluster on Ubuntu 22.04 using self-hosted deployment option with two servers.

Install CockroachDB cluster on Ubuntu 22.04

Prerequisites

  • Two or more Ubuntu 22.04 servers
  • Root privileges on both servers

The two servers will have the IP addresses below.

  • Server 1 IP: 192.168.0.14
  • Server 2 IP: 192.168.0.15

Connection through firewall

Open ports 8080 and 26257 on both your servers if your have firewall installed and running.

  • If you use UFW firewall, use the commands:
ufw allow 8080/tcp
ufw allow 26257/tcp

Then reload the firewall.

ufw reload

Install CockroachDB cluster on Ubuntu 22.04

Before you continue, please setup NTP server and use non-root user with superuser privileges if you will be using the servers in a production environment.

Download the binary file for installation on your server 1. Extract and copy the files to your system’s path

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

Create a folder to store library files that provide spatial features.

mkdir -p /usr/local/lib/cockroach

Copy the library files to the folder above:

sudo cp -i cockroach-v22.1.0.linux-amd64/lib/libgeos.so /usr/local/lib/cockroach/
cp -i cockroach-v22.1.0.linux-amd64/lib/libgeos_c.so /usr/local/lib/cockroach/

You have just installed CockroachDB on the first server. Check the version:

$ cockroach version
Build Tag:        v22.1.2
Build Time:       2022/06/22 15:54:12
Distribution:     CCL
Platform:         linux amd64 (x86_64-pc-linux-gnu)
Go Version:       go1.17.11
C Compiler:       gcc 6.5.0
Build Commit ID:  fc456a26830067b6dfbb8cd87e093a28d9b833d1
Build Type:       release

Now head to the second server and repeat the process to install CockroachDB.

Create clusters

Begin to create the first cluster on the first server by entering the --store, --listen-addr and --http-addr details.

cockroach start --insecure --store=server1 \
--listen-addr=192.168.0.14:26257 \
--http-addr=192.168.0.14:8080 \
--join=192.168.0.14:26257,192.168.0.15:26258 --background

This is what the flags mean:

  • –store is point to the cluster for storing the data
  • –listen-addr is the IP address for running on the server using port 25267.
  • –http-addr is the IP address the Cockroachdb web-based administration will be running on accessed via port 8080.
  • –insecure for using unsecure(http) connection

Set up cluster two on the second server updated IP address(192.168.0.15)

cockroach start --insecure --store=server2 \
--listen-addr=192.168.0.15:26258 \
--http-addr=192.168.0.15:8081 \
--join=192.168.0.14:26257,192.168.0.15:26258 --background

Now join the clusters into one cluster to the first server.

cockroach init --insecure --host=192.168.0.14:26257
Cluster successfully initialized

Access the web interface

In your web browser, enter the URL http://SERVER:8080 (SERVER is the IP address of server1) in my case http://192.168.0.14:8080

The web-base management console launches. Be informed that you can’t make changes in the web console.

Install CockroachDB cluster on Ubuntu 22.04
CockroachDB web console

Testing the clusters

I will perform simple SQL operations to confirm if data can be replicated to all the servers.

Connect to server 1 cluster using the command:

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

Create a database called itnixpro

CREATE DATABASE itnixpro;

Next create a table users under the same database

CREATE TABLE itnixpro.users (id INT PRIMARY KEY, name TEXT); 

Now insert some data to the table:

INSERT INTO itnixpro.users (id, name) values (1,'Joash');

Display the data:

select * from itnixpro.users;                   
  id | name
-----+--------
   1 | Joash
(1 row)


Time: 3ms total (execution 2ms / network 1ms)

Check if the data is replicated to the second server. Thus, log in to server 2 and connect to the second cluster.

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

Populate the data on the second cluster. There you go! The data has been replicated to the second cluster successfully.

select * from itnixpro.users;                   
  id | name
-----+--------
   1 | Joash
(1 row)


Time: 101ms total (execution 101ms / network 0ms)

Conclusion

This is the end of this article on how to install CockroachDB cluster on Ubuntu 22.04.

Find out more features and setting up up other configuration during installation in CockroachDB documentation

Similar interesting tutorials

Install CockroachDB on Ubuntu 22.04

Install MongoDB compass GUI on Ubuntu 22.04

Install MariaDB 10.7 on Ubuntu 18.04

Android Developer | Linux | Technical Writer | Backend Developer

Leave a Comment