docker-compose安装并简单配置Cassandra
admin
2024-04-04 01:19:57
0

文章目录

    • 1. docker-compose.yaml
    • 2.启动容器
    • 3.简单设置Cassandra
      • 3.1 cqlsh 是什么?
      • 3.2 为什么没有输入用户名和密码就直接登录了?
      • 3.3 创建新用户
      • 3.4 简单操作

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

1. docker-compose.yaml

version: "3.8"
services:cassandra:image: cassandra:4.1container_name: cassandraports:- 9042:9042volumes:- $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.yaml文件所在目录或者文件名不是docker-compose.yaml,需要通过-f指定文件所在位置。即如:

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 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]]

这里的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

当这里配置为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 cassandraWarning: 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 123456Warning: 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 |         ALLroot_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 123456Warning: 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_schematest_data@cqlsh> desc keyspace storeCREATE 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+00009876 |          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.01AND caching = {'keys': 'ALL', 'rows_per_partition': 'NONE'}AND cdc = falseAND 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.0AND default_time_to_live = 0AND extensions = {}AND gc_grace_seconds = 864000AND max_index_interval = 2048AND memtable_flush_period_in_ms = 0AND min_index_interval = 128AND read_repair = 'BLOCKING'AND speculative_retry = '99p';
test_data@cqlsh:store>

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

相关内容

热门资讯

十二星座对应剑灵哪些职业 十二星座对应剑灵哪些职业这样你也能扯到一快去
泡荔枝用什么酒最好呢?选择合适... 近年来,泡荔枝等水果酒的风潮越来越热,不仅能在家轻松制作美味的果酒,还能根据个人口味调整酒的风味。然...
分享3款适合小暑时节的手工甜品... 小暑至,盛夏始。随着气温节节攀升,人体易生内热,出现心烦口渴、食欲不振等症状。此时,一碗清甜解暑的甜...
“贵州省汤”火了!解暑又减脂,... 最近不少地方遭遇持续高温,不少网友表示,在厨房里待十几分钟,浑身都是汗。这种天气下,吃什么,成了很多...
轻盈细腻的澳洲甜点 巴甫洛娃(... 主料:鲜鸡蛋白12个、糖450克 配料:白醋、香草精、玉米淀粉各1汤匙 做法: 1.用打蛋器打发鸡蛋...
西游记中唐僧和猪八戒误喝子母河... 西游记中唐僧和猪八戒误喝子母河水腹痛难忍。泉主人如意真仙为何不给悟空落胎?手机中唐僧和猪八戒,巫师蝎...
学生一般毕业的话,可以第二次进... 学生一般毕业的话,可以第二次进入大学重读吗?首先是一般情况下是不可以的,除非你是准备提升学历考研考博...
小说是一种作家写的假的事什么意... 小说是一种作家写的假的事什么意思?作家写小说,是一个根据真人真事进行艺术加工的过程,并不是完全虚构的...
自称为行者是什么意思? 自称为行者是什么意思?“行者”指的是出家而未经过剃度的佛教徒。自称行者就是说他自己是修行佛道之人。那...
有哪些带不字的四字成语? 有哪些带不字的四字成语?不毛之地 不眠之夜 不明不白 不明真相 不谋而合 不谋私利 不...
赞美女老师的诗句有哪些? 赞美女老师的诗句有哪些? 春蚕到死丝方尽,蜡炬成灰泪始干.----- 李商隐为人性僻耽佳句,语不惊人...
做手工巧克力会花多少钱? 做手工巧克力会花多少钱?那个是跟据你做多大的巧克力来定价的就跟做巧克力蛋糕一样跟据重量算。当地物价不...
中国人为什么很喜欢羽生结弦?他... 中国人为什么很喜欢羽生结弦?他有何特别之处?因为羽生结弦是一位相当阳光帅气的运动员,他特别的善良,热...
爱情伤感文章要有文采! 爱情伤感文章要有文采!不是故事才可以要有文采!11不过,最真实的故事才是最感人的。
真的有‘运气’存在吗 真的有‘运气’存在吗那么人转运是以什么为界限的?是根据自己的生日吗?运气好办事容易成吗从局部看来有,...
古时候月亮升起和落下的地方各叫... 古时候月亮升起和落下的地方各叫什么名字?我知道太阳起在扶桑,落在禺谷,那么月亮呢??我想写篇散文,用...
有没有那种赚钱 布置房子 买家... 有没有那种赚钱 布置房子 买家具的手机游戏?模拟人生合集版女生的话可以玩养成游戏,如爱丽丝梦幻茶话会...
求几本能激励人的书 求几本能激励人的书我虽是城里人,但也是小地方的穷人,正要去大城市工作发展,请问有什么关于这样的励志的...
网络小说(都市类)有那些好书,... 网络小说(都市类)有那些好书,介绍点,要写完的,谢谢.要写完了的书哦,最好是07到08年的新书,老书...
中国现当代文学考研 中国现当代文学考研现当代前景还是不错的。 考的具体因学校要求而不同,有的只考现当代文学像浙大,有的则...