Cassandra 使用笔记
2019-03-22 本文已影响0人
走在成长的道路上
环境安装
- 添加仓库配置
vim /etc/yum.repos.d/cassandra.repo
文件, 追加如下内容:
[cassandra]
name=Apache Cassandra
baseurl=https://www.apache.org/dist/cassandra/redhat/311x/
gpgcheck=1
repo_gpgcheck=1
gpgkey=https://www.apache.org/dist/cassandra/KEYS
- 安装基础服务
yum install cassandra -y
- 配置集群
默认配置被安装到 /etc/cassandra/conf
目录下,在此配置 conf/cassandra.yaml
文件即可:
cluster_name: 'DBACluster'
num_tokens: 256
seed_provider:
- class_name: org.apache.cassandra.locator.SimpleSeedProvider
parameters:
- seeds: "172.xxx.xxx.110,172.xxx.xxx.111,172.xxx.xxx.112" # 集群 ip 列表
start_rpc: true
listen_address: 172.xxx.xxx.110 # 本机 ip
rpc_address: 172.xxx.xxx.110 # 本机 ip
endpoint_snitch: SimpleSnitch
注
关键性的修改如上所示,其余内容可以不必修改,维持现状即可。
- 启动服务
这里使用 service
命令进行启动,也可以使用 systemctl
需要 先执行下 systemctl daemon-reload
否则找不到服务。
service cassandra start
简单使用
查看节点状态
[root@localhost conf]$ nodetool status
Datacenter: datacenter1
=======================
Status=Up/Down
|/ State=Normal/Leaving/Joining/Moving
-- Address Load Tokens Owns (effective) Host ID Rack
UN 172.xxx.xxx.112 69.91 KiB 256 69.7% b094898b-231a-4354-9a93-026b83b26200 rack1
UN 172.xxx.xxx.111 69.92 KiB 256 66.8% 0be19954-b3dc-496b-a153-1b6b611ff889 rack1
UN 172.xxx.xxx.110 103.67 KiB 256 63.4% 553d2de4-3f30-49c2-82e7-b48e092fbffd rack1
基础操作
使用 cqlsh
命令作为 cassandra
默认客户端程序,如下操作:
[root@localhost conf]# cqlsh 172.xxx.xxx.110
Connected to DBACluster at 172.xxx.xxx.110:9042.
[cqlsh 5.0.1 | Cassandra 3.11.4 | CQL spec 3.4.4 | Native protocol v4]
Use HELP for help.
cqlsh>
- 查询当前属性
cqlsh> SELECT cluster_name, listen_address FROM system.local;
cluster_name | listen_address
--------------+----------------
DBACluster | 172.xxx.xxx.110
(1 rows)
- 查看版本信息
cqlsh> SHOW VERSION
[cqlsh 5.0.1 | Cassandra 3.11.4 | CQL spec 3.4.4 | Native protocol v4]
- 查看主机信息
cqlsh> SHOW HOST
Connected to DBACluster at 172.xxx.xxx.110:9042.
- 创建用户
- 开启密码保护
修改 cassandra.yaml
文件,将
authenticator: AllowAllAuthenticator
authorizer: AllowAllAuthorizer
修改为
authenticator: PasswordAuthenticator
authorizer: CassandraAuthorizer
注
此时默认用户为 cassandra/cassandra
- 创建用户
[root@localhost conf]# cqlsh 172.xxx.xxx.110 -ucassandra -pcassandra
Connected to DBACluster at 172.xxx.xxx.110:9042.
[cqlsh 5.0.1 | Cassandra 3.11.4 | CQL spec 3.4.4 | Native protocol v4]
Use HELP for help.
cqlsh> CREATE USER 用户名 WITH PASSWORD '密码' SUPERUSER ;
- 删除用户
cqlsh> DROP USER cassandra ;
代码中设置密码如下:
Cluster cluster = Cluster.builder()
.addContactPoint("172.xxx.xxx.110")
.withCredentials("用户名", "密码")
.build();
权限信息
权限管理这块数据保存在 system_auth
里面,如下所示
cqlsh> desc keyspace system_auth
CREATE KEYSPACE system_auth WITH replication = {'class': 'SimpleStrategy', 'replication_factor': '1'} AND durable_writes = true;
CREATE TABLE system_auth.resource_role_permissons_index (
resource text,
role text,
PRIMARY KEY (resource, role)
)
CREATE TABLE system_auth.role_permissions (
role text,
resource text,
permissions set<text>,
PRIMARY KEY (role, resource)
)
CREATE TABLE system_auth.role_members (
role text,
member text,
PRIMARY KEY (role, member)
)
CREATE TABLE system_auth.roles (
role text PRIMARY KEY,
can_login boolean,
is_superuser boolean,
member_of set<text>,
salted_hash text
)
- 查看权限信息
cqlsh> use system_auth;
cqlsh:system_auth> select * from resource_role_permissons_index;
resource | role
----------+------
(0 rows)
cqlsh:system_auth> select * from role_permissions;
role | resource | permissions
------+----------+-------------
(0 rows)
cqlsh:system_auth> select * from role_permissions;
role | resource | permissions
------+----------+-------------
(0 rows)
cqlsh:system_auth> select * from roles;
role | can_login | is_superuser | member_of | salted_hash
-----------+-----------+--------------+-----------+-------------------------------
cassandra | True | True | null | $2a$10...RhFCCKQwT6wNyucgANW
(1 rows)
注
参考 用户密码设置
keyspace
相关
- 查看
keyspace
列表
cqlsh> describe keyspaces;
system_schema system xxxx
system_auth system_distributed system_traces
- 查看
keyspace
具体详情
cqlsh> desc keyspace xxxx
CREATE KEYSPACE xxxx WITH replication = {'class': 'SimpleStrategy', 'replication_factor': '3'} AND durable_writes = true;
CREATE TABLE xxxx.t (
pk int,
a int,
b int,
c int,
PRIMARY KEY (pk, a)
)
- 创建/使用
keyspace
# 创建 `keyspace`
cqlsh> CREATE KEYSPACE knet WITH REPLICATION = { 'class' : 'SimpleStrategy', 'replication_factor' : 1 };
# 使用 `keyspace`
cqlsh> use knet ;
# 查看 `keyspace` 中所有表列表
cqlsh:knet> desc tables;
todayscore t
# 查看表结构
cqlsh:knet> desc table todayscore;
注
Replication Factor : 复制因数。 表示一份数据在一个DC 之中包含几份。常用奇数~ 比如我们项目组设置的replication_factor=3
注
Replica placement strategy : 复制策略。 默认的是SimpleStrategy. 如果是单机架、单数据中心的模式,保持使用SimpleStrtegy即可。