Ubuntu18.04 LTS Cockroach集群搭建

集群规划

节点 IP 数据目录
node01 192.168.180.130 node01
node02 192.168.180.131 node02
node03 192.168.180.132 node03
node04 192.168.180.133 node04
node05 192.168.180.134 node05

CockroachDB安装

  1. 从官网下载core二进制包

  2. 上传并解压到指定目录

    tar -zxvf cockroach-v19.2.4.linux-amd64.tgz -C /opt/ronnie/
    
  3. 修改目录名称

    cd /opt/ronnie/
    mv cockroach-v19.2.4.linux-amd64/ cockroach
    
  4. 将目录传送至其他节点

    cd /opt/ronnie
    scp -r cockroach/ root@node02:`pwd`
    scp -r cockroach/ root@node03:`pwd`
    scp -r cockroach/ root@node04:`pwd`
    scp -r cockroach/ root@node05:`pwd`
    
  5. 启动节点

    # node01
    /opt/ronnie/cockroach/cockroach start --insecure --store=node01 --host=192.168.180.130 --port=26257 --http-port=8080 &
    
    # node02
    /opt/ronnie/cockroach/cockroach start --insecure --store=node02 --host=192.168.180.131 --port=26257 --http-port=8080 --join=192.168.180.130:26257 &
    
    # node03
    /opt/ronnie/cockroach/cockroach start --insecure --store=node03 --host=192.168.180.132 --port=26257 --http-port=8080 --join=192.168.180.130:26257 &
    
    # node04
    /opt/ronnie/cockroach/cockroach start --insecure --store=node04 --host=192.168.180.133 --port=26257 --http-port=8080 --join=192.168.180.130:26257 &
    
    # node05
    /opt/ronnie/cockroach/cockroach start --insecure --store=node04 --host=192.168.180.134 --port=26257 --http-port=8080 --join=192.168.180.130:26257 &
    
    • 启动界面

      * WARNING: RUNNING IN INSECURE MODE!
      * 
      * - Your cluster is open for any client that can access 192.168.180.130.
      * - 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/v19.2/secure-a-cluster.html
      *
      *
      * WARNING: running 'cockroach start' without --join is deprecated.
      * Consider using 'cockroach start-single-node' or 'cockroach init' instead.
      *
      CockroachDB node starting at 2020-03-24 03:07:58.791675436 +0000 UTC (took 0.3s)
      build:               CCL v19.2.4 @ 2020/02/06 21:55:19 (go1.12.12)
      webui:               http://192.168.180.130:8080
      sql:                 postgresql://root@192.168.180.130:26257?sslmode=disable
      RPC client flags:    /opt/ronnie/cockroach/cockroach <client cmd> --host=192.168.180.130:26257 --insecure
      logs:                /root/node01/logs
      temp dir:            /root/node01/cockroach-temp051514561
      external I/O path:   /root/node01/extern
      store[0]:            path=/root/node01
      status:              initialized new cluster
      clusterID:           ba298af3-801c-433c-a3ea-a8158dddb878
      nodeID:              1
      
  6. 查询集群状态信息

cd /opt/ronnie/cockroach
./cockroach node status --insecure --host=192.168.180.130

# 查询结果
  id |        address        |      sql_address      |  build  |            started_at            |            updated_at            | locality | is_available | is_live  
+----+-----------------------+-----------------------+---------+----------------------------------+----------------------------------+----------+--------------+---------+
   1 | 192.168.180.130:26257 | 192.168.180.130:26257 | v19.2.4 | 2020-03-24 03:07:58.660822+00:00 | 2020-03-24 03:16:13.640827+00:00 |          | true         | true     
   2 | 192.168.180.131:26257 | 192.168.180.131:26257 | v19.2.4 | 2020-03-24 03:08:06.856781+00:00 | 2020-03-24 03:16:12.903671+00:00 |          | true         | true     
   3 | 192.168.180.132:26257 | 192.168.180.132:26257 | v19.2.4 | 2020-03-24 03:08:08.054824+00:00 | 2020-03-24 03:16:14.065921+00:00 |          | true         | true     
   4 | 192.168.180.133:26257 | 192.168.180.133:26257 | v19.2.4 | 2020-03-24 03:08:08.886852+00:00 | 2020-03-24 03:16:14.886065+00:00 |          | true         | true     
   5 | 192.168.180.134:26257 | 192.168.180.134:26257 | v19.2.4 | 2020-03-24 03:08:09.710603+00:00 | 2020-03-24 03:16:15.735922+00:00 |          | true         | true     
(5 rows)

  1. 查询集群ID信息

    ./cockroach node ls --insecure --host=192.168.180.130
    
    # 查询结果
      id  
    +----+
       1  
       2  
       3  
       4  
       5  
    (5 rows)
    
  2. 查询单个节点信息

    ./cockroach node status 1 --insecure --host=192.168.180.131
    
    # 查询结果
      id |        address        |      sql_address      |  build  |            started_at            |            updated_at            | locality | is_available | is_live  
    +----+-----------------------+-----------------------+---------+----------------------------------+----------------------------------+----------+--------------+---------+
       1 | 192.168.180.130:26257 | 192.168.180.130:26257 | v19.2.4 | 2020-03-24 03:07:58.660822+00:00 | 2020-03-24 03:17:34.650296+00:00 |          | true         | true     
    
    
  3. 集群验证

    • 登录第一个节点

      ./cockroach sql --insecure --host=192.168.180.130
      
    • 创建数据库

      root@192.168.180.130:26257/defaultdb> create database pandemic;
      CREATE DATABASE
      
      Time: 15.636534ms
      
      • 查看数据库

        root@192.168.180.130:26257/defaultdb> show databases;
          database_name  
        +---------------+
          defaultdb      
          pandemic       
          postgres       
          system         
        (4 rows)
        
        Time: 1.049492ms
        
    • 创建表

      create table pandemic.countries(id int not null  primary key, country_name varchar(20), comfirmed_cases int, total_deaths int, total_recovered int);
      
    • 插入数据

      insert into pandemic.countries(id,country_name,comfirmed_cases,total_deaths,total_recovered)values(1,'China', 81498, 3274, 72822);
      
      insert into pandemic.countries(id,country_name,comfirmed_cases,total_deaths,total_recovered)values(2,'Italy', 63927, 6077, 7432);
      
      insert into pandemic.countries(id,country_name,comfirmed_cases,total_deaths,total_recovered)values(3,'United State', 43901, 610, 0);
      
      insert into pandemic.countries(id,country_name,comfirmed_cases,total_deaths,total_recovered)values(4,'Spain', 35136, 2311, 3355);
      
      insert into pandemic.countries(id,country_name,comfirmed_cases,total_deaths,total_recovered)values(5,'Germany', 29056, 123, 453);
      
      insert into pandemic.countries(id,country_name,comfirmed_cases,total_deaths,total_recovered)values(6,'Iran', 23049, 1812, 8376);
      
      insert into pandemic.countries(id,country_name,comfirmed_cases,total_deaths,total_recovered)values(7,'France', 20123, 862, 2207);
      
      insert into pandemic.countries(id,country_name,comfirmed_cases,total_deaths,total_recovered)values(8,'South Korea', 8961, 111, 3507);
      
      insert into pandemic.countries(id,country_name,comfirmed_cases,total_deaths,total_recovered)values(9,'Switzerland', 8795, 120, 131);
      
      insert into pandemic.countries(id,country_name,comfirmed_cases,total_deaths,total_recovered)values(10,'United Kingdom', 67267, 336, 140);
      
      insert into pandemic.countries(id,country_name,comfirmed_cases,total_deaths,total_recovered)values(11,'Netherlands', 4764, 214, 3);
      
      insert into pandemic.countries(id,country_name,comfirmed_cases,total_deaths,total_recovered)values(12,'Austria', 4474, 21, 9);
      
      insert into pandemic.countries(id,country_name,comfirmed_cases,total_deaths,total_recovered)values(13,'Belgium', 3743, 88, 401);
      
      insert into pandemic.countries(id,country_name,comfirmed_cases,total_deaths,total_recovered)values(14,'Norway', 2621, 10, 6);
      
      insert into pandemic.countries(id,country_name,comfirmed_cases,total_deaths,total_recovered)values(15,'Canada', 2621, 25, 0);
      
      insert into pandemic.countries(id,country_name,comfirmed_cases,total_deaths,total_recovered)values(16,'Portugal', 2060, 23, 14);
      
      insert into pandemic.countries(id,country_name,comfirmed_cases,total_deaths,total_recovered)values(17,'Sweden', 2046, 27, 16);
      
      insert into pandemic.countries(id,country_name,comfirmed_cases,total_deaths,total_recovered)values(18,'Brazil', 1924, 34, 2);
      
      insert into pandemic.countries(id,country_name,comfirmed_cases,total_deaths,total_recovered)values(19,'Australia', 1924, 7, 119);
      
      insert into pandemic.countries(id,country_name,comfirmed_cases,total_deaths,total_recovered)values(20,'Denmark', 1572, 24, 24);
      
    • 查看数据

      root@192.168.180.130:26257/defaultdb> select * from pandemic.countries;
      
        id |  country_name  | comfirmed_cases | total_deaths | total_recovered  
      +----+----------------+-----------------+--------------+-----------------+
         1 | China          |           81498 |         3274 |           72822  
         2 | Italy          |           63927 |         6077 |            7432  
         3 | United State   |           43901 |          610 |               0  
         4 | Spain          |           35136 |         2311 |            3355  
         5 | Germany        |           29056 |          123 |             453  
         6 | Iran           |           23049 |         1812 |            8376  
         7 | France         |           20123 |          862 |            2207  
         8 | South Korea    |            8961 |          111 |            3507  
         9 | Switzerland    |            8795 |          120 |             131  
        10 | United Kingdom |           67267 |          336 |             140  
        11 | Netherlands    |            4764 |          214 |               3  
        12 | Austria        |            4474 |           21 |               9  
        13 | Belgium        |            3743 |           88 |             401  
        14 | Norway         |            2621 |           10 |               6  
        15 | Canada         |            2621 |           25 |               0  
        16 | Portugal       |            2060 |           23 |              14  
        17 | Sweden         |            2046 |           27 |              16  
        18 | Brazil         |            1924 |           34 |               2  
        19 | Australia      |            1924 |            7 |             119  
        20 | Denmark        |            1572 |           24 |              24  
      
      
    • 查找死亡率最低和最高的国家

      • 死亡率最低的国家

        select country_name, total_deaths/comfirmed_cases as death_rate from pandemic.countries order by death_rate asc limit 1;
        
        • 查询结果
          country_name |        death_rate         
        +--------------+--------------------------+
          Australia    | 0.0036382536382536382536  
        (1 row)
        
      • 死亡率最高的国家

        select country_name, total_deaths/comfirmed_cases as death_rate from pandemic.countries order by death_rate desc limit 1;
        
        • 查询结果
          country_name |       death_rate         
        +--------------+-------------------------+
          Italy        | 0.095061554585699313279  
        (1 row)
        
    • 查找治愈率最高和最低的国家

      • 治愈率最低的国家

        select country_name, total_recovered/comfirmed_cases as recover_rate from pandemic.countries order by recover_rate asc limit 1;
        
        • 查询结果

            country_name | recover_rate  
          +--------------+--------------+
            United State |            0  
          (1 row)
          
      • 治愈率最高的国家

        select country_name, total_recovered/comfirmed_cases as recover_rate from pandemic.countries order by recover_rate desc limit 1;
        
        • 查询结果

            country_name |      recover_rate       
          +--------------+------------------------+
            China        | 0.89354339983803283516  
          (1 row)
          
      • 登录其他节点查看数据

        cd /opt/ronnie/cockroach
        ./cockroach sql --insecure --host=192.168.180.130
        
        root@192.168.180.130:26257/defaultdb> select * from pandemic.countries;
          id |  country_name  | comfirmed_cases | total_deaths | total_recovered  
        +----+----------------+-----------------+--------------+-----------------+
           1 | China          |           81498 |         3274 |           72822  
           2 | Italy          |           63927 |         6077 |            7432  
           3 | United State   |           43901 |          610 |               0  
           4 | Spain          |           35136 |         2311 |            3355  
           5 | Germany        |           29056 |          123 |             453  
           6 | Iran           |           23049 |         1812 |            8376  
           7 | France         |           20123 |          862 |            2207  
           8 | South Korea    |            8961 |          111 |            3507  
           9 | Switzerland    |            8795 |          120 |             131  
          10 | United Kingdom |           67267 |          336 |             140  
          11 | Netherlands    |            4764 |          214 |               3  
          12 | Austria        |            4474 |           21 |               9  
          13 | Belgium        |            3743 |           88 |             401  
          14 | Norway         |            2621 |           10 |               6  
          15 | Canada         |            2621 |           25 |               0  
          16 | Portugal       |            2060 |           23 |              14  
          17 | Sweden         |            2046 |           27 |              16  
          18 | Brazil         |            1924 |           34 |               2  
          19 | Australia      |            1924 |            7 |             119  
          20 | Denmark        |            1572 |           24 |              24  
        (20 rows)
        
        Time: 2.119906ms
        
        root@192.168.180.130:26257/defaultdb> select * from pandemic.countries;
          id |  country_name  | comfirmed_cases | total_deaths | total_recovered  
        +----+----------------+-----------------+--------------+-----------------+
           1 | China          |           81498 |         3274 |           72822  
           2 | Italy          |           63927 |         6077 |            7432  
           3 | United State   |           43901 |          610 |               0  
           4 | Spain          |           35136 |         2311 |            3355  
           5 | Germany        |           29056 |          123 |             453  
           6 | Iran           |           23049 |         1812 |            8376  
           7 | France         |           20123 |          862 |            2207  
           8 | South Korea    |            8961 |          111 |            3507  
           9 | Switzerland    |            8795 |          120 |             131  
          10 | United Kingdom |           67267 |          336 |             140  
          11 | Netherlands    |            4764 |          214 |               3  
          12 | Austria        |            4474 |           21 |               9  
          13 | Belgium        |            3743 |           88 |             401  
          14 | Norway         |            2621 |           10 |               6  
          15 | Canada         |            2621 |           25 |               0  
          16 | Portugal       |            2060 |           23 |              14  
          17 | Sweden         |            2046 |           27 |              16  
          18 | Brazil         |            1924 |           34 |               2  
          19 | Australia      |            1924 |            7 |             119  
          20 | Denmark        |            1572 |           24 |              24  
        (20 rows)
        
        Time: 2.5979ms
        
        root@192.168.180.130:26257/defaultdb> select * from pandemic.countries;
          id |  country_name  | comfirmed_cases | total_deaths | total_recovered  
        +----+----------------+-----------------+--------------+-----------------+
           1 | China          |           81498 |         3274 |           72822  
           2 | Italy          |           63927 |         6077 |            7432  
           3 | United State   |           43901 |          610 |               0  
           4 | Spain          |           35136 |         2311 |            3355  
           5 | Germany        |           29056 |          123 |             453  
           6 | Iran           |           23049 |         1812 |            8376  
           7 | France         |           20123 |          862 |            2207  
           8 | South Korea    |            8961 |          111 |            3507  
           9 | Switzerland    |            8795 |          120 |             131  
          10 | United Kingdom |           67267 |          336 |             140  
          11 | Netherlands    |            4764 |          214 |               3  
          12 | Austria        |            4474 |           21 |               9  
          13 | Belgium        |            3743 |           88 |             401  
          14 | Norway         |            2621 |           10 |               6  
          15 | Canada         |            2621 |           25 |               0  
          16 | Portugal       |            2060 |           23 |              14  
          17 | Sweden         |            2046 |           27 |              16  
          18 | Brazil         |            1924 |           34 |               2  
          19 | Australia      |            1924 |            7 |             119  
          20 | Denmark        |            1572 |           24 |              24  
        (20 rows)
        
        Time: 2.077044ms
        
        root@192.168.180.130:26257/defaultdb> select * from pandemic.countries;
          id |  country_name  | comfirmed_cases | total_deaths | total_recovered  
        +----+----------------+-----------------+--------------+-----------------+
           1 | China          |           81498 |         3274 |           72822  
           2 | Italy          |           63927 |         6077 |            7432  
           3 | United State   |           43901 |          610 |               0  
           4 | Spain          |           35136 |         2311 |            3355  
           5 | Germany        |           29056 |          123 |             453  
           6 | Iran           |           23049 |         1812 |            8376  
           7 | France         |           20123 |          862 |            2207  
           8 | South Korea    |            8961 |          111 |            3507  
           9 | Switzerland    |            8795 |          120 |             131  
          10 | United Kingdom |           67267 |          336 |             140  
          11 | Netherlands    |            4764 |          214 |               3  
          12 | Austria        |            4474 |           21 |               9  
          13 | Belgium        |            3743 |           88 |             401  
          14 | Norway         |            2621 |           10 |               6  
          15 | Canada         |            2621 |           25 |               0  
          16 | Portugal       |            2060 |           23 |              14  
          17 | Sweden         |            2046 |           27 |              16  
          18 | Brazil         |            1924 |           34 |               2  
          19 | Australia      |            1924 |            7 |             119  
          20 | Denmark        |            1572 |           24 |              24  
        (20 rows)
        
        Time: 2.177364ms
        
        
        
        • 看到数据都是一致的就OK了。

    可视化UI界面

  • 默认端口为8080, 可以在启动时自行修改

  • 集群总览

    1585023665372

  • 度量指标

    1585023696793

    • 包括 SQL查询, 执行SQL的延迟, 每个节点的副本数, 事件的记录等。
  • 数据库

    1585023814972

  • 对操作的一些陈述, 包括底层的一些执行

    1585023835583

    • 点进去可以看到底层的一些操作

      1585023905494

      • 比如使用了Hash Join, 渲染, 虚拟表, 过滤 等等。

      1585023996790

      • 任务跑了多久, 预计多少延迟, 实际多少延迟, 执行次数, 重试次数, 最大重试次数......
  • 任务

    1585024093586

posted @ 2020-03-24 12:42  wellDoneGaben  阅读(292)  评论(0编辑  收藏  举报