Before you begin
Make sure you have already .Step 1. Generate certificates
-
Create two directories:
-
Create the CA (Certificate Authority) certificate and key pair:
-
Create the certificate and key pair for your nodes:
Because you’re running a local cluster and all nodes use the same hostname (
localhost), you only need a single node certificate. Note that this is different than running a production cluster, where you would need to generate a certificate and key for each node, issued to all common names and IP addresses you might use to refer to the node as well as to any load balancer instances. -
Create a client certificate and key pair for the
rootuser:
Step 2. Start a 3-node cluster
-
Use the command to start node 1:
-
In a new terminal, start node 2:
-
In a new terminal, start node 3:
-
In a new terminal, use the command to perform a one-time initialization of the cluster, sending the request to any node on the
--joinlist:You’ll see the following message:At this point, each node also prints helpful to its log.
Step 3. Create a SQL user
You’ll use a non-root user for running a client workload and accessing the DB Console.
-
In the same terminal, as the
rootuser, open the against any node: -
Create the
maxroachuser with a password: -
Assign the
maxroachuser to theadminrole:This role assignment is for convenience for this tutorial; it gives the user access to all data without the need for additional privileges. For more details, see . -
Exit the SQL shell:
Step 4. Write data
-
In the same terminal, run the command to generate an example
introdatabase. In the connection string, replace<passwordwith the password you created earlier formaxroach: -
Re-open the SQL shell, this time as the
maxroachuser:Enter the user’s password when prompted. -
Verify that the new
introdatabase was added with one table,mytable: -
Exit the SQL shell:
Step 5. Verify replication
-
To understand replication in CockroachDB, it’s important to review a few concepts from the :
Concept Description Range CockroachDB stores all user data (tables, indexes, etc.) and almost all system data in a giant sorted map of key-value pairs. This keyspace is divided into “ranges”, contiguous chunks of the keyspace, so that every key can always be found in a single range.
From a SQL perspective, a table and its secondary indexes initially map to a single range, where each key-value pair in the range represents a single row in the table (also called the primary index because the table is sorted by the primary key) or a single row in a secondary index. As soon as that range reaches , it . This process continues for these new ranges as the table and its indexes continue growing.Replica CockroachDB replicates each range (3 times by default) and stores each replica on a different node. -
With those concepts in mind, open the DB Console at http://localhost:8080 and log in with the
maxroachuser. -
On the Overview page, note that the Replicas count is the same on all three nodes. This indicates:
- There are this many “ranges” of data in the cluster. These are mostly internal “system” ranges since you haven’t added much table data.
- Each range has been replicated 3 times (according to the CockroachDB default).
- For each range, each replica is stored on different nodes.
Step 6. Add two more nodes
-
Back in the terminal, add a fourth node:
-
In a new terminal, add a fifth node:
Step 7. Watch data rebalance
Back in the DB Console, you’ll see that there are now 5 nodes listed. At first, the replica count will be lower for nodes 4 and 5. Very soon, however, you’ll see those numbers even out across all nodes, indicating that data is being automatically rebalanced to utilize the additional capacity of the new nodes.Step 8. Stop the cluster
- When you’re done with your test cluster, press
ctrl-cin each terminal where a node is running.
For the last 2 nodes, the shutdown process will take longer (about a minute each) and will eventually force the nodes to stop. This is because, with only 2 of 5 nodes left, a majority of replicas are not available, and so the cluster is no longer operational. To speed up this process, you can press
ctrl-c a second time.-
To restart the cluster at a later time, run the same
cockroach startcommands as earlier from the directory containing the nodes’ data stores. If you do not plan to restart the cluster, you may want to remove the cluster’s certificates and data stores:

