The Best Way to Learn CockroachDB

Introduction

CockroachDB is a very new technology for creating distributed SQL databases that a lot of new companies are curious about and so wanted to talk about the best way to start learning CockroachDB. In this tutorial we’ll show you the best way to learn how to interact with CockroachDB which is to create a temporary cluster that you can interact with. Having a little hands on experience with the technology is the best way to learn and CockroachDB provides you some commands that quickly get you up and running with a temporary cluster. You don’t have to worry about messing anything up because this is a temporary cluster that you can feel free to experiment with. We feel like experimenting is the best way to learn so if you’re looking to learn CockroachDB in a free, local environment with no consequences, we hope you’ll follow along with this tutorial and start experimenting.

Prerequisites

  • You should have CockroachDB installed. ( If you are running OS X you can learn how to install it by going to one of our previous articles “How to Install CockroachDB on Mac OS X” )
  • It’s recommended that you have some experience working in the Terminal.

Setup a Temporary Cluster

To setup a temporary cluster that you can start experimenting, CockroachDB provides you a command that gets you up and running with a database instantly. Just enter the following command and cockroachDB will put you into a SQL shell interacting with database called defaultdb.

cockroach demo

Here is the response letting you know that your in demo mode.

#
# Welcome to the CockroachDB demo database!
#
# You are connected to a temporary, in-memory CockroachDB
# instance. Your changes will not be saved!
#
# Web UI: http://127.0.0.1:50926
#
# Server version: CockroachDB CCL v19.1.0 (x86_64-apple-darwin14, built 2019/04/29 18:31:15, go1.11.6) (same version as client)
# Cluster ID: 72246b38-e829-4b7c-a2a7-2758190085b3
#
# Enter \? for a brief introduction.
#
root@127.0.0.1:50925/defaultdb>

The last line is your prompt to start interacting with the databse. To get you started interacting with the database we’ll show you how to create a table.

root@127.0.0.1:50925/defaultdb> CREATE TABLE defaultdb.newtable (id INT PRIMARY KEY, comment STRING);

You should get a response like this letting you know that the command succeded.

CREATE TABLE

Time: 3.806ms

Now you can see that the table was created by showing all the tables in the database with this command:

root@127.0.0.1:50925/defaultdb> SHOW TABLES;

Response:

  table_name  
+------------+
  newtable    
(1 row)

As you can see you’re quickly up and running with a database and executing SQL commands on you cockroachDB database. We’ll show you a few more useful tricks to help you learn CockroachDB but already you have an environment to interact with a CockroachDB database through SQL. You should experiment in here and execute any SQL commands you’re familiar with and you’ll notice that for most commands the syntax is exactly the same.

Start a Temporary Cluster Already Populated with Data

One struggle to experimenting with a new database technology is you always have to worry about getting a database populated with some actual data. Luckily, CockroachDB lets you enter demo mode with a database that’s already populated with data.

Let’s get started with our favorite demo set that comes in CockroachDB:

$ cockroach demo startrek

This response again lets you know that you’ve entered demo mode. Notice the prompt has the database name at the end.

#
# Welcome to the CockroachDB demo database!
#
# You are connected to a temporary, in-memory CockroachDB
# instance. Your changes will not be saved!
#
# Web UI: http://127.0.0.1:51307
#
# Server version: CockroachDB CCL v19.1.0 (x86_64-apple-darwin14, built 2019/04/29 18:31:15, go1.11.6) (same version as client)
# Cluster ID: 0fbfd764-0348-4697-912d-495413e13ece
#
# Enter \? for a brief introduction.
#
root@127.0.0.1:51306/startrek>

Now you can execute the following command to see what tables are avaiable:

root@127.0.0.1:51306/startrek> SHOW TABLES;

As you can see there are already two existing tables. `js table_name
+————+ episodes
quotes
(2 rows)

Time: 1.305ms `

Now let’s look in one of the tables to see what kind of data is stored.

root@127.0.0.1:51400/startrek> SELECT * FROM episodes;
  id | season | num |                       title                        | stardate  
+----+--------+-----+----------------------------------------------------+----------+
   1 |      1 |   1 | The Man Trap                                       |   1531.1  
   2 |      1 |   2 | Charlie X                                          |   1533.6  
   3 |      1 |   3 | Where No Man Has Gone Before                       |   1312.4  
   4 |      1 |   4 | The Naked Time                                     |   1704.2  
   5 |      1 |   5 | The Enemy Within                                   |   1672.1  
   6 |      1 |   6 | Mudd's Women                                       |   1329.8  
...
  77 |      3 |  22 | The Savage Curtain                                 |   5906.4  
  78 |      3 |  23 | All Our Yesterdays                                 |   5943.7  
  79 |      3 |  24 | Turnabout Intruder                                 |   5928.5  
(79 rows)

Time: 4.156ms

You can see that there is plenty of data in here for you to experiment with updates, deletes, joins etc.

Explore the Admin UI

The last thing we recommend for learning about CockroachDB is to visit the Admin UI which gives you an overview with what is happening with your cluster. Even if all the stats don’t apply to your local demo environment you can see what your possibilities are. Getting to the Admin UI is made super easy once you’ve entered demo mode because in the Terminal it gives you a url that you can go to access it.

Here was the resonse when we demo’d with the startrek database:

#
# Welcome to the CockroachDB demo database!
#
# You are connected to a temporary, in-memory CockroachDB
# instance. Your changes will not be saved!
#
# Web UI: http://127.0.0.1:51537
#
# Server version: CockroachDB CCL v19.1.0 (x86_64-apple-darwin14, built 2019/04/29 18:31:15, go1.11.6) (same version as client)
# Cluster ID: 4629fddf-bae1-4798-98c5-5134c4bcd9d1
#
# Enter \? for a brief introduction.
#

The part we care about is Web UI: http://127.0.0.1:51537. Enter this url enter your favorite browser and you’ll enter the Admin UI that should look something like this:

Image from Gyazo

We think the database tab is a good place to get oriented, as it shows the basics of which tables are in each database with the number of columns and indices in each table. From here we recommend you explore each tab in detail to check out all the data that is being tracked and what is possible in CockroachDB. If you’re looking at CockroachDB you probably have specific concerns in mind and you should find the statistics that you’re particularly concerned with. If you’re concerned with backups then you might look at the replication data.

We also recommend you execute SQL commands on the startrek database and see how the Admin UI tracks those events and look at the SQL and performance statistics.

Conclusion

CockroachDB is a very new technology that’s hoping to gain some traction with companies looking to take their SQL database to a distributed system. Installing and running a cluster locally is the best and easiest way to get your hands on the technology. We hope you can use the demo mode and Admin UI to get a fundamental understanding of CockroachDB. If you have any questions about CockroachDB please don’t hesitate to reach out to us.

Pilot the ObjectRocket Platform Free!

Try Fully-Managed CockroachDB, Elasticsearch, MongoDB, PostgreSQL (Beta) or Redis.

Get Started

Keep in the know!

Subscribe to our emails and we’ll let you know what’s going on at ObjectRocket. We hate spam and make it easy to unsubscribe.