您的当前位置:首页正文

docker-compose安装并简单配置Cassandra

2024-11-23 来源:个人技术集锦


本文的目标是在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

2.启动容器

使用如下指令启动容器:

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

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

➜  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

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

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

3.简单设置Cassandra

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

3.1 cqlsh 是什么?

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

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

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

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

-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 为什么没有输入用户名和密码就直接登录了?

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

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

authenticator: PasswordAuthenticator

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

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

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

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

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

docker restart cdf4f5b56a88

重启完之后再进入容器,再使用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:/#

看上面的提示,似乎也看不出来什么错误,只是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>

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

3.3 创建新用户

cassnadra划分了三种角色类型:

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

这里先创建一个superuser:

create user root_cassandra with password '123456' superuser;

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

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>

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

drop user cassandra

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()));

可以看到已经创建了一个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>

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

显示全文