love wife & love life —Roger的Oracle&MySQL技术博客

Phone:18180207355 提供专业Oracle&MySQL数据恢复、性能优化、迁移升级、紧急救援等服务

How to Install 3 node CockroachDB cluster

本站文章除注明转载外,均为本站原创: 转载自love wife & love life —Roger的Oracle&MySQL技术博客

本文链接地址: How to Install 3 node CockroachDB cluster

CockroachDB国内俗称小强DB,是国外开发的一款兼容PostgreSQL协议的NewSQL数据库;据了解其研发团队均为 Google 的分布式文件系统 Colossus 团队的成员组成,从设计上来讲有点类似Google的Spanner分布式数据库( 全球第一款分布式数据库)。是否好用,这里不多说,先来简单尝试一下,从安装玩起。

1、软件下载略,直接解压cp到/usr/bin即可。

2、启动节点1

[root@mysqldb1 opt]# cockroach start --insecure --host=mysqldb1 --background --http-port=8081
*
* WARNING: RUNNING IN INSECURE MODE!
*
* - Your cluster is open for any client that can access mysqldb1.
* - 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/v2.0/secure-a-cluster.html
*
[root@mysqldb1 opt]# CockroachDB node starting at 2018-05-14 03:33:31.306339131 +0000 UTC (took 0.7s)
build:               CCL v2.0.1 @ 2018/04/23 18:39:21 (go1.10)
admin:               http://mysqldb1:8081
sql:                 postgresql://root@mysqldb1:26257?sslmode=disable
logs:                /opt/cockroach-data/logs
temp dir:            /opt/cockroach-data/cockroach-temp623324603
external I/O path:   /opt/cockroach-data/extern
store[0]:            path=/opt/cockroach-data
status:              restarted pre-existing node
clusterID:           749ea85a-66ff-44eb-9161-f00873078136
nodeID:              1

从这里的启动输出信息来看,命令行的完全兼容了postgreSQL;启动之后每个节点会分配一个Nodeid和clusterID(每个集群的clusterid是唯一的)。

3、启动节点2、3

root@mysqldb2 opt]# cockroach start --insecure --store=node2 --host=mysqldb2 --http-port=8082 --join=172.16.29.154:26257 &
[1] 42677
[root@mysqldb2 opt]# *
* WARNING: RUNNING IN INSECURE MODE!
*
* - Your cluster is open for any client that can access mysqldb2.
* - 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/v2.0/secure-a-cluster.html
*

[root@mysqldb2 opt]# CockroachDB node starting at 2018-05-14 04:17:25.22193837 +0000 UTC (took 0.7s)
build:               CCL v2.0.1 @ 2018/04/23 18:39:21 (go1.10)
admin:               http://mysqldb2:8082
sql:                 postgresql://root@mysqldb2:26257?sslmode=disable
logs:                /opt/node2/logs
temp dir:            /opt/node2/cockroach-temp449846681
external I/O path:   /opt/node2/extern
store[0]:            path=/opt/node2
status:              restarted pre-existing node
clusterID:           749ea85a-66ff-44eb-9161-f00873078136
nodeID:              2


[root@mysqldb3 opt]# cockroach start --insecure --store=node3 --host=mysqldb3 --http-port=8083 --join=172.16.29.154:26257 &
[1] 53289

 

这里启动时可以直接挂后台运行,启动完毕之后,可以登录Url监控控制台进行查看。总的来讲整界面还是比较清爽的。

4、节点1创建测试表并插入测试数据

[root@mysqldb1 opt]# cockroach sql  --url=postgresql://root@mysqldb1:26257?sslmode=disable
# Welcome to the cockroach SQL interface.
# All statements must be terminated by a semicolon.
# To exit: CTRL + D.
#
# Server version: CockroachDB CCL v2.0.1 (x86_64-unknown-linux-gnu, built 2018/04/23 18:39:21, go1.10) (same version as client)
# Cluster ID: 749ea85a-66ff-44eb-9161-f00873078136
#
# Enter \? for a brief introduction.
#
warning: no current database set. Use SET database = <dbname> to change, CREATE DATABASE to make a new database.
root@mysqldb1:26257/>
root@mysqldb1:26257/> create database enmotech;
CREATE DATABASE

Time: 6.675781ms
root@mysqldb1:26257/> set database=enmotech;
SET

Time: 357.175碌s

root@mysqldb1:26257/enmotech> create table enmotech(id int,name varchar(20));
CREATE TABLE

Time: 9.786659ms

root@mysqldb1:26257/enmotech> insert into enmotech values(1,'www.enmotech.com');
INSERT 1

Time: 8.120407ms
root@mysqldb1:26257/enmotech> insert into enmotech values(2,'www.killdb.com');
INSERT 1

Time: 24.219042ms

 

可以看到单条Insert的效率似乎并不高,这是为什么呢?  从原理上来讲,CockroachDB采用了share nothing架构,节点之间需要做同步,而且集群的性能也是受限于最差的那个节点,因此也存在木桶原理的问题。

至于如何保证节点之间的数据一致性,CockroachDB采用了raft协议,并没有使用Paxos。

5、在节点2、3验证数据

root@mysqldb2 opt]# cockroach sql  --url=postgresql://root@mysqldb2:26257?sslmode=disable
# Welcome to the cockroach SQL interface.
# All statements must be terminated by a semicolon.
# To exit: CTRL + D.
#
# Server version: CockroachDB CCL v2.0.1 (x86_64-unknown-linux-gnu, built 2018/04/23 18:39:21, go1.10) (same version as client)
# Cluster ID: 749ea85a-66ff-44eb-9161-f00873078136
#
# Enter \? for a brief introduction.
#
warning: no current database set. Use SET database = <dbname> to change, CREATE DATABASE to make a new database.
root@mysqldb2:26257/>
root@mysqldb2:26257/> set database=enmotech;
SET

Time: 1.200536ms
root@mysqldb2:26257/enmotech>
root@mysqldb2:26257/enmotech> select * from enmotech;
+----+------------------+
| id |       name       |
+----+------------------+
|  1 | www.enmotech.com |
|  2 | www.killdb.com   |
+----+------------------+
(2 rows)

Time: 5.247978ms
root@mysqldb2:26257/enmotech> show create table enmotech;
+----------+------------------------------------------+
|  Table   |               CreateTable                |
+----------+------------------------------------------+
| enmotech | CREATE TABLE enmotech (                  |
|          |                                          |
|          |     id INT NULL,                         |
|          |                                          |
|          |     "name" STRING(20) NULL,              |
|          |                                          |
|          |     FAMILY "primary" (id, "name", rowid) |
|          |                                          |
|          | )                                        |
+----------+------------------------------------------+
(1 row)

Time: 16.628783ms

我们可以发现数据已经完全同步到其他节点了。这里比较奇怪的是什么呢? 细心的朋友可能已经发现,该测试表我在创建的时候并没有指定主键,然而这里数据库确自动添加了主键。

前面提到了如何启动CockroachDB,那么如何停掉一个节点呢?命令其实非常的简单:

[root@mysqldb1 opt]# cockroach quit --host=172.16.29.154 --insecure --port=26257
ok
[root@mysqldb1 opt]#
[root@mysqldb3 tmp]# cockroach quit --host=172.16.29.133 --insecure --port=26257 
ok
[root@mysqldb3 tmp]#

最后我们来欣赏一下自带的监控界面:

Leave a Reply

You must be logged in to post a comment.