• docker-compose安装并简单配置Cassandra



    本文的目标是在docker环境下安装4.1版本的Cassandra,并且简单设置,可以远程访问,设置用户名密码等。

    1. docker-compose.yaml

    version: "3.8"
    services:
      cassandra:
        image: cassandra:4.1
        container_name: cassandra
        ports:
          - 9042:9042
        volumes:
          - $PWD/commitlog:/var/lib/cassandra/commitlog
          - $PWD/hints:/var/lib/cassandra/hints
          - $PWD/data:/var/lib/cassandra/data
          - $PWD/saved_caches:/var/lib/cassandra/saved_caches
          - $PWD/logs:/var/log/cassandra
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13

    2.启动容器

    使用如下指令启动容器:

    docker-compose up -d
    
    • 1

    注意:如果如果没有在docker-compose.yaml文件所在目录或者文件名不是docker-compose.yaml,需要通过-f指定文件所在位置。即如:

    docker-compose -f cassandra-start-up.yaml up -d
    
    • 1

    启动好之后,可以进入到容器之中:

    ➜  cassandra docker-compose up -d
    Creating network "cassandra_default" with the default driver
    Pulling cassandra (cassandra:4.1)...
    4.1: Pulling from library/cassandra
    eaead16dc43b: Pull complete
    46e1869246ce: Pull complete
    bbd45db92608: Pull complete
    6fcfd0f47989: Pull complete
    996685dfbe33: Pull complete
    4927828dcc1b: Pull complete
    7f67cde8352d: Pull complete
    09bb07e15655: Pull complete
    b8d7c6610af3: Pull complete
    Status: Downloaded newer image for cassandra:4.1
    Creating cassandra ... done
    ➜  cassandra docker ps
    CONTAINER ID   IMAGE           COMMAND                  CREATED          STATUS          PORTS                                                       NAMES
    cdf4f5b56a88   cassandra:4.1   "docker-entrypoint.s…"   10 minutes ago   Up 10 minutes   7000-7001/tcp, 7199/tcp, 9160/tcp, 0.0.0.0:9042->9042/tcp   cassandra
    ➜  cassandra docker exec -it cdf4f5b56a88 bash
    root@cdf4f5b56a88:/# cqlsh
    Connected to Test Cluster at 127.0.0.1:9042
    [cqlsh 6.1.0 | Cassandra 4.1-beta1 | CQL spec 3.4.6 | Native protocol v5]
    Use HELP for help.
    cqlsh> desc keyspaces;
    
    system       system_distributed  system_traces  system_virtual_schema
    system_auth  system_schema       system_views
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27

    可以看到,我们已经通过cqlsh命令,登录到了当前的Cassandra数据库。
    但是这里会有疑惑产生:

    • 登录数据库的指令太过简单了吧?!如果需要登录指定主机地址的数据库,应该怎么设置主机地址?
    • 登录数据库的指令不需要用户名和密码嘛?
    • 需要的话,我的用户名和密码是什么?就目前而言,我并没有做任何设置。
    • 如何设置用户名和密码?

    3.简单设置Cassandra

    既然有了上面的疑问,那就来一一弄明白:

    3.1 cqlsh 是什么?

    首先,cqlsh是什么?官网有解释:

    cqlsh is a command-line interface for interacting with Cassandra using CQL (the Cassandra Query Language). It is shipped with every Cassandra package, and can be found in the bin/ directory alongside the cassandra executable. cqlsh is implemented with the Python native protocol driver, and connects to the single specified node.

    可以知道,cqlsh是个Python的脚本来连接操作Cassandra的。基本用法就是:

    cqlsh.py [options] [host [port]]
    
    • 1

    这里的options包括哪些呢? 官网写的很详细,我就不抄了,看这里 cqlsh: the CQL shell
    我就挑几个最关心的看一下:

    -u USERNAME --username=USERNAME
    Authenticate as user.

    -p PASSWORD --password=PASSWORD
    Authenticate using password.

    -k KEYSPACE --keyspace=KEYSPACE
    Authenticate to the given keyspace.

    这三个是用来指定用户名和密码以及keySpace的。

    –credentials=CREDENTIALS
    Specify an alternative credentials file location.

    这个是用来指定credentials的。

    3.2 为什么没有输入用户名和密码就直接登录了?

    这里是由于没有做任何个性化配置,我们使用的相关配置是默认配置文件cassandra.yaml文件里的配置,这个配置文件在容器里的/opt/cassandra/conf目录下。这里的配置内容是

    #AllowAllAuthenticator performs no checks - set it to disable authentication.
    authenticator: AllowAllAuthenticator
    
    • 1
    • 2

    当这里配置为AllowAllAuthenticator的时候,将不做任何的检查。是设置为关闭认证。
    要想启用用户名密码登录,就需要将其设置为PasswordAuthenticator:

    authenticator: PasswordAuthenticator
    
    • 1

    不过这个时候会发现,容器中没有vim,也没有vi,那就只能把容器里的文件copy出来,修改完再copy回去了。

    docker cp cdf4f5b56a88:/opt/cassandra/conf/cassandra.yaml .
    
    • 1

    我这是把文件从容器中copy当当前位置,修改完之后再copy回去,source和destination位置互换,即:

    docker cp cassandra.yaml cdf4f5b56a88:/opt/cassandra/conf/
    
    • 1

    替换完成之后,再重新启动容器:

    docker restart cdf4f5b56a88
    
    • 1

    重启完之后再进入容器,再使用cqlsh指令登录,发得到如下错误:

    ➜  cassandra docker exec -it cdf4f5b56a88 bash
    root@cdf4f5b56a88:/# cqlsh
    Connection error: ('Unable to connect to any servers', {'127.0.0.1:9042': ConnectionRefusedError(111, "Tried connecting to [('127.0.0.1', 9042)]. Last error: Connection refused")})
    root@cdf4f5b56a88:/#
    
    • 1
    • 2
    • 3
    • 4

    看上面的提示,似乎也看不出来什么错误,只是connection refused。那就想着用用户名和密码尝试一下吧?由于我们没有设置用户名和密码,这个时候就只能使用系统默认设置的用户名和密码都是cassandra的账户进行登录。

    root@cdf4f5b56a88:/# cqlsh -u cassandra -p cassandra
    
    Warning: Using a password on the command line interface can be insecure.
    Recommendation: use the credentials file to securely provide the password.
    
    Connected to Test Cluster at 127.0.0.1:9042
    [cqlsh 6.1.0 | Cassandra 4.1-beta1 | CQL spec 3.4.6 | Native protocol v5]
    Use HELP for help.
    cassandra@cqlsh>
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9

    可以看到这样我们就已经进来了。

    3.3 创建新用户

    cassnadra划分了三种角色类型:

    xxopr: 应用账号,只能进行对表的查询、数据插入、数据删除等DML操作
    xxdata: 相当于数据OWNER用户,对表空间内的对象拥有增删改查等DDL操作
    cassandra: 超级用户,用于创建表空间的,DBA权限管理

    这里先创建一个superuser:

    create user root_cassandra with password '123456' superuser;
    
    • 1

    然后使用这个用户进行登录:

    root@cdf4f5b56a88:/# cqlsh -u root_cassandra -p 123456
    
    Warning: Using a password on the command line interface can be insecure.
    Recommendation: use the credentials file to securely provide the password.
    
    Connected to Test Cluster at 127.0.0.1:9042
    [cqlsh 6.1.0 | Cassandra 4.1-beta1 | CQL spec 3.4.6 | Native protocol v5]
    Use HELP for help.
    root_cassandra@cqlsh> list users;
    
     name           | super | datacenters
    ----------------+-------+-------------
          cassandra |  True |         ALL
     root_cassandra |  True |         ALL
    
    (2 rows)
    root_cassandra@cqlsh>
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17

    可以看到,这里已经有两个超级用户了,不想保留cassandra这个用户的可以直接drop掉。

    drop user cassandra
    
    • 1

    3.4 简单操作

    既然已经到这里了,那就创建一个用户,试试简单操作先:

    root_cassandra@cqlsh> create user test_data with password '123456' nosuperuser;
    root_cassandra@cqlsh> exit;
    root@cdf4f5b56a88:/# cqlsh -u test_data -p 123456
    
    Warning: Using a password on the command line interface can be insecure.
    Recommendation: use the credentials file to securely provide the password.
    
    Connected to Test Cluster at 127.0.0.1:9042
    [cqlsh 6.1.0 | Cassandra 4.1-beta1 | CQL spec 3.4.6 | Native protocol v5]
    Use HELP for help.
    test_data@cqlsh> CREATE KEYSPACE IF NOT EXISTS store WITH REPLICATION = { 'class' : 'SimpleStrategy', 'replication_factor' : '1' };
    test_data@cqlsh> desc keyspaces;
    
    store   system_auth         system_schema  system_views
    system  system_distributed  system_traces  system_virtual_schema
    
    test_data@cqlsh> desc keyspace store
    
    CREATE KEYSPACE store WITH replication = {'class': 'SimpleStrategy', 'replication_factor': '1'}  AND durable_writes = true;
    test_data@cqlsh> CREATE TABLE IF NOT EXISTS store.shopping_cart (
       ... userid text PRIMARY KEY,
       ... item_count int,
       ... last_update_timestamp timestamp
       ... );
    test_data@cqlsh> INSERT INTO store.shopping_cart
       ... (userid, item_count, last_update_timestamp)
       ... VALUES ('9876', 2, toTimeStamp(now()));
    test_data@cqlsh> INSERT INTO store.shopping_cart
       ... (userid, item_count, last_update_timestamp)
       ... VALUES ('1234', 5, toTimeStamp(now()));
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30

    可以看到已经创建了一个keyspace为store的库,创建了一个表shopping_cart,并插入了一些数据。查看一下表结构以及数据:

    test_data@cqlsh:store> select * from store.shopping_cart;
    
     userid | item_count | last_update_timestamp
    --------+------------+---------------------------------
       1234 |          5 | 2022-11-05 08:39:47.077000+0000
       9876 |          2 | 2022-11-05 08:39:46.226000+0000
    
    (2 rows)
    test_data@cqlsh:store> desc table shopping_cart;
    
    CREATE TABLE store.shopping_cart (
        userid text PRIMARY KEY,
        item_count int,
        last_update_timestamp timestamp
    ) WITH additional_write_policy = '99p'
        AND bloom_filter_fp_chance = 0.01
        AND caching = {'keys': 'ALL', 'rows_per_partition': 'NONE'}
        AND cdc = false
        AND comment = ''
        AND compaction = {'class': 'org.apache.cassandra.db.compaction.SizeTieredCompactionStrategy', 'max_threshold': '32', 'min_threshold': '4'}
        AND compression = {'chunk_length_in_kb': '16', 'class': 'org.apache.cassandra.io.compress.LZ4Compressor'}
        AND memtable = 'default'
        AND crc_check_chance = 1.0
        AND default_time_to_live = 0
        AND extensions = {}
        AND gc_grace_seconds = 864000
        AND max_index_interval = 2048
        AND memtable_flush_period_in_ms = 0
        AND min_index_interval = 128
        AND read_repair = 'BLOCKING'
        AND speculative_retry = '99p';
    test_data@cqlsh:store>
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32

    可以看到,数据可以查到,表结构还是有很多默认选项在里面的。这里的默认设置具体是什么意思,我们要怎么设置,先挖坑,填不填再说(我是大坑货)。

  • 相关阅读:
    2024rk(案例三)
    C#S7.NET实现西门子PLCDB块数据采集的完整步骤
    C# 正确实现IDisposable 释放非托管资源
    官方零基础入门 Jetpack Compose 的中文课程来啦!
    单商户商城系统功能拆解35—分销应用—分销概览
    贴花、射线、动画通知——足迹01
    sql注入的数据提交方式和查询方式
    Java笔记(工厂模式、动态代理、XML)
    使用imx 8m 测试matter协议功能
    时代和人民的精神脉动
  • 原文地址:https://blog.csdn.net/Apple_wolf/article/details/127704102