Installation and Configuration
Share:
Welcome to this comprehensive tutorial chapter on how to install and configure CockroachDB. In this chapter, you'll learn everything you need to know about setting up and getting started with CockroachDB. Stay tuned as we proceed on this exciting journey of using databases to store and manipulate data.
Let's begin with understanding what exactly is CockroachDB.
CockroachDB is a cloud-native SQL database that is designed for building, scaling, and managing modern, data-intensive applications. Its main highlights include handling massive volumes of data, supporting various client drivers, and guaranteeing transactional consistency, to name a few.
Before you install CockroachDB, ensure that your system meets the following requirements:
- Operating System: Linux or Mac OS.
- Hardware: Minimum 2 GB of RAM.
- Docker (Optional): If you prefer a containerized environment.
Let's dive into the installation process now.
Linux Installation
If you have a Linux system, you can easily download and install CockroachDB using the curl command to fetch the binary file.
$ curl https://binaries.cockroachdb.com/cockroach-v20.2.7.linux-amd64.tgz | tar -xvz
Change directory into 'cockroach-v20.2.7.linux-amd64'. Here, you find the executable file named 'cockroach' which needs to be copied to '/usr/local/bin' for accessing it system-wide.
$ cp -i cockroach-v20.2.7.linux-amd64/cockroach /usr/local/bin/
Mac OS Installation
For Mac OS, you can use brew to install CockroachDB.
$ brew install cockroach
Once the installation is done, you can verify it by executing the following command:
$ cockroach version
You should see the version of CockroachDB that got installed.
Docker Installation (Optional)
One of the best ways to run CockroachDB is within a Docker container. Here is the command to pull CockroachDB image and run the container:
$ docker pull cockroachdb/cockroach:latest
$ docker run -d --name=roach1 --hostname=roach1 --net=cockroachnet -p 26257:26257 -p 8080:8080 cockroachdb/cockroach:latest start --insecure
Let's now learn to configure CockroachDB.
CockroachDB Configuration
Start a Single-Node Cluster
The first step after installation is to start a new cluster. In this 'movie database' example, we are starting a single-node cluster named 'hollywood'.
$ cockroach start --insecure --store=hollywood --listen-addr=localhost
Start a Multi-Node Cluster
Let's say, the 'hollywood' movie database has grown massively and now you need to handle more data. In such a case, starting a multi-node cluster is beneficial. To do this, first stop the 'hollywood' node, if it's running.
Now, start a three-node cluster as follows:
$ cockroach start --insecure --store=node1 --listen-addr=localhost:26257 --http-addr=localhost:8080 --join=localhost:26257,localhost:26258,localhost:26259 --background
$ cockroach start --insecure --store=node2 --listen-addr=localhost:26258 --http-addr=localhost:8081 --join=localhost:26257,localhost:26258,localhost:26259 --background
$ cockroach start --insecure --store=node3 --listen-addr=localhost:26259 --http-addr=localhost:8082 --join=localhost:26257,localhost:26258,localhost:26259 --background
Connecting to the Cluster and Creating a Database
After initializing the nodes, the next thing to do is to connect to the cluster and create a database, let's name it 'moviedb'.
$ cockroach sql --insecure --host=localhost:26257
You are then connected to node1, where you can execute SQL queries. Let's create our movie database.
> CREATE DATABASE moviedb;
Adding Tables and Data
Now, create a table 'films' within 'moviedb'. This table will consist of three columns: 'id', 'title', and 'release_year'.
> USE moviedb;
> CREATE TABLE films (id INT PRIMARY KEY, title STRING, release_year INT);
You can now insert a movie record into this table.
> INSERT INTO films VALUES (1, 'Interstellar', 2014);
That's it! With this, you've got a basic understanding of how to install and configure CockroachDB for your projects.
So, stay tuned until our next rendezvous, where you'd acquaint yourself with more advanced features of CockroachDB aligned with your project needs.
0 Comment
Sign up or Log in to leave a comment