cp -i 覆盖既有文件之前先询问用户
相关官方文档:https://www.cockroachlabs.com/docs/stable/start-a-local-cluster.html
1.2.1 不安全的部署
步骤1:启动第一个节点
[root@localhost ~]# 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://root@localhost: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
注意:
1)--insercure 启用非密码认证
2)这是一个纯粹的本地集群,--host=localhost 告诉节点只监听localhost,内部和客户端默认的端口号26257,http请求通过8080端口发出
3)节点数据存储在cockroach-data路径
4)标准输出提供了cockroach版本的帮助文档,admin UI的URL,客户端的SQL URL
步骤2:
单节点已经可以使用,连接sql客户端,建立数据库,但在实际的部署中,你需要3个或者以上节点去利用cockroach的自动复制,自动节点数据平衡和默认容忍优点,如下是在本地模拟配置一个实际的部署环境
打开一个新的客户端
[root@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 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://root@localhost: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
打开一个新的终端:
[root@localhost ~]# 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://root@localhost: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
在部署中最重要的不同点是
--join 将新节点加入到集群中,定义第一个节点的地址和端口号,例如localhost:26257
因为在同一台机器上运行所有节点,需要设置--store --port 和--http-port 标识位置和端口没有被其他节点使用,但是在实际的部署环境中,每个节点在不同的机器上,默认配置可以满足要求
步骤3:
现在你部署了3个节点,你可以将任意节点作为sql的入口,为了证实这一点,打开一个新的终端,使用SQL客户端连接节点1
注意:SQL客户端已经部署在cockroach的二进制包里,不需要额外操作
[forget@localhost 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.
#
root@:26257/> create database bank;
CREATE DATABASE
Time: 46.596945ms
root@:26257/> create table bank.accounts(id int primary key,balance decimal);
CREATE TABLE
Time: 49.272314ms
root@:26257/> insert into bank.accounts values (1,1000.50) ;
INSERT 1
Time: 48.679601msroot@:26257/> select * from bank.accounts;
+----+---------+
| id | balance |
+----+---------+
| 1 | 1000.50 |
+----+---------+
(1 row)
Time: 4.778574ms
root@:26257/> \q
连接节点2,需要定义端口号不使用默认端口[forget@localhost 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.
#
注意在实际环境中,所有的节点可以使用默认端口26257,可以不需要—port选项root@:26258/> select * from bank.accounts;
+----+---------+
| id | balance |
+----+---------+
| 1 | 1000.50 |
+----+---------+
(1 row)
Time: 29.444091ms
你可以看到节点2和节点1作为sql入口表现是相同的
步骤4 监控集群
通过admin UI 在你的集群中,在浏览器中打开http://localhost:8080,或者在admin 中制定任意启动节点的标准输出
cockroachDB在后台自动复制你的数据,为了政府复制成功,向下滚动查看Replicas per Node .
副本在每个节点是相同的,在集群中复制3次
步骤5 停止cluster
一旦你完成测试cluster,切换到第一个节点,按ctrl+c 停止该节点
此时其他2个节点仍然是在线的,集群仍然是可以使用的,因为多数副本是可达的。为了证明集群可以容忍失败(其中一个节点down),通过SQL shell 连接节点2和节点3
[forget@localhost 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.
#
root@:26258/> select * from bank.accounts;
+----+---------+
| id | balance |
+----+---------+
| 1 | 1000.50 |
| 2 | 800.50 |
+----+---------+
(2 rows)
Time: 28.962136ms
停止节点2节点3,使用ctrl+c,输出如下:
^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"
注意:对于节点3,关闭时间较长(大约1分钟),因为只剩于3个节点中的一个节点,多数副本不可达,cluster不可用。为了加速这个过程,需要按2次ctrl+c,输出如下:
^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"
步骤6:重启cluster
如果你决定继续使用集群进行后续的测试,你需要重启至少3个节点中的两个,使用节点的数据存储。
重新启动节点1,数据路径cockroach-data/
[root@localhost ~]# 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
*
只有一个节点在线,集群是不可用的,你不会看到任何回应,执行了以上命令之后,知道你重启第二个节点
在一个新的终端,重启第二个节点,使用父路径节点node2/
[root@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://root@localhost: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/[root@localhost ~]# 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://root@localhost: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