cockroach official document translation --- 1. Installation and startup

1. Installation and startup


1.1 Installation

1.1.1 Download the binary file
CockroachDB install online in linux: the URL is as follows, version v1.1.3
https://www.cockroachlabs.com/docs/stable/install-cockroachdb.html


1) Download the binary file online
[[email protected] cochroachDB] # wget -qO- https://binaries.cockroachdb.com/cockroach-v1.1.3.linux-amd64.tgz | tar xvz
cockroach-v1.1.3.linux-amd64/cockroach
[[email protected] cochroachDB]# ls
cockroach- v1.1.3.linux-amd64
2) Copy the binary file to your directory for easy execution of the CockroachDB script
[[email protected] cochroachDB]# cp -i cockroach-v1.1.3.linux-amd64/cockroach /usr/local/bin


Note: wget -q outputs in quiet mode, the default file name is / after the file cockroach-v1.1.3.linux-amd64.tgz,
           tar x restores the file from the backup file, v shows the command execution process, z processes the backup through the gzip command document;

           cp -i ask user before overwriting existing file

1. 2 Quick Start

Note: By default, each CochroachDB anonymously reports its own usage details to Cochroach Labs, how to voluntarily roll out reports, see https://www.cockroachlabs.com/docs/stable/diagnostics-reporting.html


Relevant official documentation: https://www.cockroachlabs.com/docs/stable/start-a-local-cluster.html

1.2.1 Insecure deployment
step 1: Start the first node
[[email protected] ~]# cockroach start --insecure --host=localhost
*
* WARNING: RUNNING IN INSECURE MODE!

* - Your cluster is open for any client that can access localhost.
* - Any user, even root, can log in without providing a password.
* - Any user, connecting as root, can read or write any data in your cluster.
* - There is no network encryption nor authentication, and thus no confidentiality.

* Check out how to secure your cluster: https://www.cockroachlabs .com/docs/stable/secure-a-cluster.html
*
CockroachDB node starting at 2018-01-10 01:08:26.454099704 +0000 UTC (took 0.6s)
build: CCL v1.1.3 @ 2017/11/27 13:59:10 (go1.8.3)
admin: http:// localhost:8080
sql: postgresql://[email protected]:26257?application_name=cockroach&sslmode=disable
logs: /root/cockroach-data/logs
store[0]: path=/root/cockroach-data
status: initialized new cluster
clusterID: f4fe6b48-7f5d-4d10-aebd-5a1d75c24e72
nodeID: 1

Note:
1) --insercure enables non-password authentication
2) This is a pure local cluster, --host=localhost tells the node to only listen on localhost, internal and client default The port number is 26257, and the http request is sent through port 8080.
3) The node data is stored in the cockroach-data path
4) The standard output provides the help document of the cockroach version, the URL of the admin UI, and the SQL URL of the client.

Step 2:
A single node can already be used, connect to the sql client, and create a database, but in actual deployment, you need 3 or more nodes to take advantage of cockroach's automatic replication, automatic node data balancing and default tolerance. The following is a local simulation configuration An actual deployment environment
opens a new client
[[email protected] ~]# cockroach start --insecure --store=node2 --host=localhost --port=26258 --http-port=8081 --join=localhost :26257
*
* WARNING: RUNNING IN INSECURE MODE!

* - Your cluster is open for any client that can access localhost.
* - Any user, even root, can log in without providing a password.
* - Any user, connecting as root , can read or write any data in your cluster.
* - There is no network encryption nor authentication, and thus no confidentiality.

* Check out how to secure your cluster: https://www.cockroachlabs.com/docs/stable/ secure-a-cluster.html
*
CockroachDB node starting at 2018-01-10 01:13:40.664945281 +0000 UTC (took 0.2s)
build:      CCL v1.1.3 @ 2017/11/27 13:59:10 (go1.8.3)
admin:      http://localhost:8081
sql:        postgresql://[email protected]:26258?application_name=cockroach&sslmode=disable
logs:       /root/node2/logs
store[0]:   path=/root/node2
status:     initialized new node, joined pre-existing cluster
clusterID:  f4fe6b48-7f5d-4d10-aebd-5a1d75c24e72
nodeID:     2


打开一个新的终端:
[[email protected] ~]# cockroach start --insecure --store=node3 --host=localhost --port=26259 --http-port=8082 --join=localhost:26257
*
* WARNING: RUNNING IN INSECURE MODE!

* - Your cluster is open for any client that can access localhost.
* - Any user, even root, can log in without providing a password.
* - Any user, connecting as root, can read or write any data in your cluster.
* - There is no network encryption nor authentication, and thus no confidentiality.

* Check out how to secure your cluster: https://www.cockroachlabs.com/docs/stable/secure-a-cluster.html
*
CockroachDB node starting at 2018-01-10 01:32:52.559260389 +0000 UTC (took 0.2s)
build:      CCL v1.1.3 @ 2017/11/27 13:59:10 (go1.8.3)
admin:      http://localhost:8082
sql:        postgresql://[email protected]:26259?application_name=cockroach&sslmode=disable
logs:       /root/node3/logs
store[0]: path=/root/node3
status: initialized new node, joined pre-existing cluster
clusterID: f4fe6b48-7f5d-4d10-aebd-5a1d75c24e72
nodeID: 3


The most important difference in deployment is that
--join will The node is added to the cluster, and the address and port number of the first node are defined, such as localhost:26257.
Because all nodes are running on the same machine, you need to set --store --port and --http-port to identify the location and port. Used by other nodes, but in the actual deployment environment, each node is on a different machine, the default configuration can meet the requirements


 Step 3:
Now that you have deployed 3 nodes, you can use any node as the entry of sql, in order to confirm At this point, open a new terminal and use the SQL client to connect to node 1.
Note: The SQL client has been deployed in the cockroach binary package, no additional operations are required
[[email protected] cockroachDB]$ cockroach sql --insecure
# Welcome to the cockroach SQL interface.
# All statements must be terminated by a semicolon.
# To exit: CTRL + D.
#
# Server version: CockroachDB CCL v1.1.3 (linux amd64, built 2017/11/27 13:59:10, go1.8.3) (same version as client)
# Cluster ID: f4fe6b48-7f5d-4d10-aebd-5a1d75c24e72
#
# Enter \? for a brief introduction.
#
[email protected]:26257/> create database bank;
CREATE DATABASE

Time: 46.596945ms

[email protected]:26257/> create table bank.accounts(id int primary key,balance decimal);
CREATE TABLE

Time: 49.272314ms

[email protected]:26257/> insert into bank.accounts values (1,1000.50) ;
INSERT 1

Time: [email protected]:26257/> select * from bank.accounts;
+----+---------+
| id | balance |
+----+---------+
|  1 | 1000.50 |
+----+---------+
(1 row)

Time: 4.778574ms


[email protected]:26257/> \q To

connect to node 2, you need to define the port number instead of using the default port [[email protected] cockroachDB]$ cockroach sql --insecure --port=26258
# Welcome to the cockroach SQL interface.
# All statements must be terminated by a semicolon.
# To exit: CTRL + D.
#
# Server version: CockroachDB CCL v1.1.3 (linux amd64, built 2017/11/27 13:59:10, go1.8.3) (same version as client)
# Cluster ID: f4fe6b48-7f5d-4d10-aebd-5a1d75c24e72
#
# Enter \? for a brief introduction.
#Note
that in the actual environment, all nodes can use the default port 26257, and do not need the --port option [email protected]:26258/> select * from bank.accounts;
+----+---------+
| id | balance |
+----+---------+
| 1 | 1000.50 |
+ ----+---------+
(1 row)

Time: 29.444091ms

You can see that node 2 and node 1 behave the same as the sql entry


Step 4 Monitor the cluster


through the admin UI In your cluster, open http://localhost:8080 in the browser, or specify any startup node in the admin The stdout of



cockroachDB automatically replicates your data in the background, for government replication to succeed, scroll down to see Replicas per Node. Replicas




are the same on each node, replicate 3 times across the cluster


Step 5 Stop the cluster 
Once you're done testing the cluster, Switch to the first node and press ctrl+c to stop the node.
At this time, the other 2 nodes are still online, and the cluster is still available because most replicas are reachable. To prove that the cluster can tolerate failure (one of the nodes is down), connect node 2 and node 3 via SQL shell


[[email protected] cockroachDB]$ cockroach sql --insecure --port=26258
# Welcome to the cockroach SQL interface.
# All statements must be terminated by a semicolon.
# To exit: CTRL + D.
#
# Server version: CockroachDB CCL v1.1.3 (linux amd64, built 2017/11/27 13:59:10, go1.8.3) (same version as client )
# Cluster ID: f4fe6b48-7f5d-4d10-aebd-5a1d75c24e72
#
# Enter \? for a brief introduction.
#
[email protected]:26258/> select * from bank.accounts;
+----+------- --+
| id | balance |
+----+---------+
| 1 | 1000.50 |
| 2 | 800.50 |
+----+---------+
(2 rows)


Time: 28.962136ms


Stop node 2 node 3, use ctrl+c, the output is as follows:
^CNote: a second interrupt will skip graceful shutdown and terminate forcefully
initiating graceful shutdown of server
server drained and shutdown completed
*
* ERROR: interrupted
*
Failed running "start"


Note: For node 3, the shutdown time is longer (about 1 minute), because only one of the 3 nodes is left, most replicas are unreachable, and the cluster is unavailable. To speed up this process, you need to press ctrl+c twice, the output is as follows:
^CNote: a second interrupt will skip graceful shutdown and terminate forcefully
initiating graceful shutdown of server
^C*
* ERROR: received signal 'interrupt' during shutdown, initiating hard shutdown
*
Failed running "start"




Step 6 : Restart the cluster








if you decide to continue For subsequent testing with the cluster, you will need to reboot at least two of the three nodes, using the node's datastore.
Restart node 1, data path cockroach-data/
[[email protected] ~]# cockroach start --insecure --host=localhost
*
* WARNING: RUNNING IN INSECURE MODE!

* - Your cluster is open for any client that can access localhost.
* - Any user, even root, can log in without providing a password.
* - Any user, connecting as root, can read or write any data in your cluster.
* - There is no network encryption nor authentication, and thus no confidentiality.

* Check out how to secure your cluster: https://www.cockroachlabs.com/docs/stable/secure-a-cluster.html
*






Only one node Online, the cluster is unavailable, you will not see any response, after executing the above command, until you restart the second node


in a new terminal, restart the second node, use the parent path node node2/


[[email protected] localhost ~]# cockroach start --insecure --store=node2 --host=localhost --port=26258 --http-port=8081 --join=localhost:26257
*
* WARNING: RUNNING IN INSECURE MODE!

* - Your cluster is open for any client that can access localhost.
* - Any user, even root, can log in without providing a password.
* - Any user, connecting as root, can read or write any data in your cluster.
* - There is no network encryption nor authentication, and thus no confidentiality.

* Check out how to secure your cluster: https://www.cockroachlabs.com/docs/stable/secure-a-cluster.html
*
CockroachDB node starting at 2018-01-10 06:44:52.586972516 +0000 UTC (took 17.8s)
build:      CCL v1.1.3 @ 2017/11/27 13:59:10 (go1.8.3)
admin:      http://localhost:8081
sql:        postgresql://[email protected]:26258?application_name=cockroach&sslmode=disable
logs:       /root/node2/logs
store[0]:   path=/root/node2
status:     restarted pre-existing node
clusterID:  f4fe6b48-7f5d-4d10-aebd-5a1d75c24e72
nodeID:     2


打开一个新的终端,启动节点3,使用父路径node3/[[email protected] ~]# cockroach start --insecure --store=node3 --host=localhost --port=26259 --http-port=8082 --join=localhost:26257
*
* WARNING: RUNNING IN INSECURE MODE!

* - Your cluster is open for any client that can access localhost.
* - Any user, even root, can log in without providing a password.
* - Any user, connecting as root, can read or write any data in your cluster.
* - There is no network encryption nor authentication, and thus no confidentiality.

* Check out how to secure your cluster: https://www.cockroachlabs.com/docs/stable/secure-a-cluster.html
*
CockroachDB node starting at 2018-01-10 06:45:21.365022797 +0000 UTC (took 12.6s)
build:      CCL v1.1.3 @ 2017/11/27 13:59:10 (go1.8.3)
admin:      http://localhost:8082
sql:        postgresql://[email protected]:26259?application_name=cockroach&sslmode=disable
logs:       /root/node3/logs
store[0]:   path=/root/node3
status:     restarted pre-existing node
clusterID:  f4fe6b48-7f5d-4d10-aebd-5a1d75c24e72
nodeID:     3


Related: cockroach official document translation --- 1. Installation and startup