CockroachDB is a powerful, open-source distributed SQL database designed for cloud applications. Installing a CockroachDB cluster on Ubuntu 24.04 can enhance your application's scalability and resilience. This guide will walk you through the installation process on your VPS server. For reliable hosting solutions, consider using windowsvps for your next VPS server deployment.
Prerequisites
Before you begin, ensure that you have the following:
- A VPS server running Ubuntu 24.04.
- Root or sudo access to the server.
- At least 2 GB of RAM and 2 CPU cores for each node.
Step 1: Update Your System
Start by updating your package list and installed packages:
sudo apt update && sudo apt upgrade -y
Step 2: Install CockroachDB
Download the latest stable version of CockroachDB:
wget https://binaries.cockroachdb.com/cockroach-v22.1.6.linux-amd64.tgz
Extract the downloaded archive:
tar -xzvf cockroach-v22.1.6.linux-amd64.tgz
Move the Cockroach binary to the appropriate location:
sudo cp cockroach-v22.1.6.linux-amd64/cockroach /usr/local/bin/
Make the Cockroach binary executable:
sudo chmod +x /usr/local/bin/cockroach
Step 3: Start the CockroachDB Cluster
To start a single-node CockroachDB cluster, run:
cockroach start --insecure --listen-addr=localhost:26257 --http-port=8080 --store=store=path/to/store
Make sure to replace path/to/store
with a directory where you want to store the data.
Step 4: Initialize the Database
In a new terminal window, initialize the database by running:
cockroach init --insecure --host=localhost:26257
Step 5: Access the CockroachDB Admin UI
You can access the CockroachDB Admin UI by navigating to http://localhost:8080
in your web browser.
Step 6: Joining Additional Nodes (Optional)
If you want to add more nodes to your cluster, run the following command on each additional node:
cockroach start --insecure --join=localhost:26257 --listen-addr=:26257 --http-port=8080 --store=path/to/store
Replace <node_ip>
with the actual IP address of the node you are adding.
Step 7: Configure CockroachDB as a Service (Optional)
To run CockroachDB as a service, create a systemd service file:
sudo nano /etc/systemd/system/cockroach.service
Add the following configuration:
[Unit]
Description=CockroachDB
After=network.target
[Service]
User=root
ExecStart=/usr/local/bin/cockroach s