一、docker安装cassandra数据库
1.拉取cassandra数据库镜像
[root@localhost ~]# docker pull cassandra
Using default tag: latest
latest: Pulling from library/cassandra
Digest: sha256:c186d79dca1cebe1ce382aaba201cce08b0f415e850d4b9eb6c1317f02bea293
Status: Image is up to date for cassandra:latest
docker.io/library/cassandra:latest
2.查看挂载网络
[root@localhost ~]# docker network ls
NETWORK ID NAME DRIVER SCOPE
74f885e8ff56 bridge bridge local
003e4697a848 cassandra-net bridge local
653aafe6b946 host host local
ede8314218a4 none
3.创建持久化文件夹
[root@localhost ~]# mkdir -p /root/cassandra
4.关闭防火墙或放行端口(此处只演示关闭防火墙)
[root@localhost ~]# systemctl stop firewalld
[root@localhost ~]# systemctl status firewalld
● firewalld.service - firewalld - dynamic firewall daemon
Loaded: loaded (/usr/lib/systemd/system/firewalld.service; disabled; vendor preset: enabled)
Active: inactive (dead)
Docs: man:firewalld(1)
5.启动cassandra数据库并进入cassandra容器
[root@localhost ~]# docker run -d --name cassandra --restart always -p 9042:9042 --network cassandra-net -v /root/cassandra:/var/lib/cassandra -e CASSANDRA_USER=cassandra -e CASSANDRA_PASSWORD=cassandra cassandra
836cf7a0983f487a707cf02b77a45eb96880e03e86949bfea02e0e3c69e9b422
[root@localhost ~]# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
836cf7a0983f cassandra "docker-entrypoint.s…" 12 seconds ago Up 10 seconds 7000-7001/tcp, 7199/tcp, 9160/tcp, 0.0.0.0:9042->9042/tcp, :::9042->9042/tcp cassandra
[root@localhost ~]# docker exec -it 836cf7a0983f bash
二、启动cassandra数据库并创建所需数据
1.启动cassandra数据库
root@836cf7a0983f:/# cqlsh
Connected to Test Cluster at 127.0.0.1:9042
[cqlsh 6.0.0 | Cassandra 4.0.1 | CQL spec 3.4.5 | Native protocol v5]
Use HELP for help.
2.将cassandra数据库配置文件复制至容器外进行修改(因为容器内没有基本文档编辑器)
cqlsh> exit
root@836cf7a0983f:/# exit
exit
[root@localhost ~]# docker cp 836cf7a0983f:/opt/cassandra/conf/cassandra.yaml .
Successfully copied 71.2kB to /root/.
3.编辑cassandra.yaml
# 配置 Cassandra 监听的地址,允许远程连接。默认情况下,该项通常是设置为本地地址(127.0.0.1)。您可以将其设置为具体的 IP 地址或者 0.0.0.0(表示接受任何 IP 地址的连接)。
listen_address: <your_ip_address_or_0.0.0.0>
# 监听端口(Listen Port):配置 Cassandra 监听的端口,用于接受客户端和其他节点的连接。默认情况下,Cassandra 使用 9042 端口。
native_transport_port: 9042
# 安全配置(Security Configuration):如果需要启用认证和授权,您还需要配置用户名、密码以及相应的角色权限。
authenticator: PasswordAuthenticator
4.将配置文件复制回容器,并重启容器
[root@localhost ~]# docker cp cassandra.yaml 836cf7a0983f:/opt/cassandra/conf/
Successfully copied 71.2kB to 836cf7a0983f:/opt/cassandra/conf/
[root@localhost ~]# docker restart 836cf7a0983f
836cf7a0983f
[root@localhost ~]# docker exec -it 836cf7a0983f bash
5.使用默认账号启动cassandra,并创建新用户以供外部连接
root@836cf7a0983f:/# cqlsh
Connection error: ('Unable to connect to any servers', {'127.0.0.1:9042': AuthenticationFailed('Remote end requires authentication')})
root@836cf7a0983f:/# cqlsh -u cassandra -p cassandra
Connected to Test Cluster at 127.0.0.1:9042
[cqlsh 6.0.0 | Cassandra 4.0.1 | CQL spec 3.4.5 | Native protocol v5]
Use HELP for help.
cassandra@cqlsh> create user test_data with password '123456' nosuperuser;
cassandra@cqlsh> exit
root@836cf7a0983f:/# cqlsh -u test_data -p 123456
Connected to Test Cluster at 127.0.0.1:9042
[cqlsh 6.0.0 | Cassandra 4.0.1 | CQL spec 3.4.5 | Native protocol v5]
Use HELP for help.
6.创建键空间
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;
7.插入测试数据
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()));
test_data@cqlsh> select * from store.shopping_cart;
userid | item_count | last_update_timestamp
--------+------------+---------------------------------
1234 | 5 | 2024-03-26 02:02:02.098000+0000
9876 | 2 | 2024-03-26 02:00:55.666000+0000
(2 rows)
三、整合spring webflux(maven工程)
1.导入依赖
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-cassandra-reactive</artifactId>
</dependency>
2.补充配置文件
server:
port: 8081
spring:
data:
cassandra:
contact-points: 你的虚拟机地址
port: 9042
local-datacenter: datacenter1 # 本地数据中心必须指定
session-name: JtTistCluster # 创建Cassandra会话对象,建立有效的连接。必须指定
keyspace-name: store # 键空间名
username: 你的账号
password: 你的密码