CockroachDB is an open-source distributed and scalable SQL database that was created specifically for cloud-native applications and development. Cockroach features database replication with strong consistency, SQL query language support, automated scaling and repairing, and high availability.
SEE: 40+ open source and Linux terms you need to know (TechRepublic Premium)
CockroachDB is an ideal database system for distributed-only transaction processing, geo-partitioning for low-latency or regulatory compliance, metadata, system of record, as a legacy bridge to the cloud, and for any application that requires reliable, available, scalable and correct data.
I want to walk you through the process of not only installing CockroachDB on Ubuntu Server 20.04 but how to deploy it as a cluster. I’ll be demonstrating with only 2 servers, but you can expand it to however many nodes you need.
What you’ll need
To deploy a two-node CockroachDB cluster, you’ll need two instances of Ubuntu Server and a user with sudo privileges. That’s it, let’s toss this against the wall and see if it sticks.
How to install CockroachDB
You need to install CockroachDB on both servers. I’m going to demonstrate with one node at IP address 192.168.1.50 and another at IP address 192.168.1.51.
Log into your first server. Download and copy the binary file to /usr/local/bin with the command:
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/
Once the file has been downloaded, create a new directory with the command:
sudo mkdir -p /usr/local/lib/cockroach
With the directory created, we need to copy two libgeos files into it with the following two commands:
sudo cp -i cockroach-v21.2.8.linux-amd64/lib/libgeos.so /usr/local/lib/cockroach/
sudo cp -i cockroach-v21.2.8.linux-amd64/lib/libgeos_c.so /usr/local/lib/cockroach/
If you issue the command cockroach version, you should see a line similar to:
Build Tag: v21.2.8
CockroachDB is installed on your first server. Do the same thing on the second server before moving on.
How to configure the firewall in CockroachDB
The next step is to open your firewall to the necessary CockroachDB ports. For this, issue the following commands to add the ports:
sudo ufw allow 8080/tcp
sudo ufw allow 26257/tcp
Now, reload the firewall with:
sudo ufw reload
How to create the cluster
Now comes the fun part. We’re going to first configure our individual nodes and then we’ll initialize the cluster. On the first server (which we’ll call server1), issue the command (changing the IP addresses to match those you’ve assigned to each node):
cockroach start --insecure --store=server1 --listen-addr=192.168.1.50:26257 --http-addr=192.168.1.50:8080 --join=192.168.1.50:26257,192.168.1.51:26258 --background
Do the same thing on server2 only the command will look something like this (again, changing the IP addresses to match your scheme):
cockroach start --insecure --store=server2 --listen-addr=192.168.1.51:26257 --http-addr=192.168.1.51:8080 --join=192.168.1.50:26257,192.168.1.51:26258 --background
Once both servers are configured, go back to server 1 and initialize the cluster with (changing the IP address to that of your server1):
cockroach init --insecure --host=192.168.1.50:26257
Your CockroachDB cluster should now be up and running.
How to access the web interface
Open a web browser and point it to http://SERVER:8080 (where SERVER is the IP address of server1). You will be immediately given access to the CockroachDB web-based management console (Figure A).
One thing to note is that you cannot create or edit databases from the web console. This tool is all about keeping tabs on the cluster’s health and to help you optimize performance. To create and work with databases, you’ll use the CockroachDB command-line console, which can be accessed with a command like (editing the IP address to that of server1):
cockroach sql --insecure --host=192.168.1.51:26257
Congratulations, you are all set. You can start building and managing your Cockroach databases and keeping track of the cluster from a user-friendly, web-based UI.
Subscribe to TechRepublic’s How To Make Tech Work on YouTube for all the latest tech advice for business pros from Jack Wallen.