一、文档地址

https://www.wueasy.com/docs/wueasy/ty/cassandra.html

二、连接工具

https://tableplus.com/windows

三、下载镜像

docker pull cassandra:latest

四、启动容器

version: '3'
services:
  cassandra:
    image: cassandra:latest
    container_name: cassandra
    restart: always
    privileged: true
    ports:
      - '9042:9042'
    volumes:
      - "/home/qiang/data/cassandra/data:/var/lib/cassandra"

复制配置文件

docker cp cassandra:/etc/cassandra/cassandra.yaml /home/qiang/data/cassandra/conf/

修改配置文件

# 查看用户命令 list users 报错
message="You have to be logged in and not anonymous to perform this request"
# 报错原因如下
当authenticator:AllowAllAuthenticator,它实际上允许所有用户,因此不会检查是否已登录.
# 解决报错问题
应该将设置更改为authenticator:PasswordAuthenticator,然后重新启动cassandra.
version: '3'
services:
  cassandra:
    image: cassandra:latest
    container_name: cassandra
    restart: always
    privileged: true
    ports:
      - '9042:9042'
    volumes:
      - "/home/qiang/data/cassandra/data:/var/lib/cassandra"
      - "/home/qiang/data/cassandra/conf/cassandra.yaml:/etc/cassandra/cassandra.yaml"

五、连接服务

激活工具

image-20211211001948231

点击按钮后在启动客户端

image-20211211002100820

连接数据库

image-20211001085854509

创建数据库

image-20211001090406819

创建表

CREATE TABLE user(
    id int PRIMARY KEY,
    name text
);

添加数据

insert into
    loki.user (id, name)
VALUES
    (1, 'qiang');

修改数据

UPDATE
    user
SET
    name = 'QIANG'
WHERE
    id = 1;

查询数据

SELECT
    *
FROM
    user

image-20211001092154794

删除数据

DELETE from
    user
where
    id = 1;

六、常用命令

基本操作命令

-- 查看用户下信息
describe cluster; 
desc cluster;
-- 查看所有keyspace
describe keyspaces;
desc keyspaces;
-- 查看keyspace内容,knet为键空间
describe keyspace knet;
desc keyspace knet;
--创建keyspace
-- replication_factor:复制因数,表示一份数据在一个DC之中包含几份,常用奇数
-- SimpleStrategy:复制策略,默认的是SimpleStrategy,单数据中心的模式,保持使用SimpleStrtegy即可
CREATE KEYSPACE knet WITH REPLICATION = { 'class': 'SimpleStrategy',
'replication_factor': 1 };
-- 使用keyspace
use knet;
-- 查示所有表
describe tables;
desc tables;
-- 查看表结构
desc table stocks;
-- 删除表
drop table user;
-- 创建用户
CREATE USER myusername WITH PASSWORD 'mypassword' SUPERUSER;
-- 删除用户
DROP USER cassandra;
-- 创建角色
create role cdhu1;
-- 查看用户
list users;
-- 修改密码
ALTER USER cassandra WITH PASSWORD '123456';
-- 修改角色
ALTER USER cassandra SUPERUSER;
-- 导出数据库表结构
cqlsh -k loki -u cassandra -p cassandra 127.0.0.1 9042 -e "describe keyspace loki" > loki.cql
-- 导入数据库表结构
cqlsh -u cassandra -p cassandra 127.0.0.1 9042 -e "SOURCE 'loki.cql'"
-- 创建普通用户loki_dev
CREATE USER loki_dev WITH PASSWORD 'WJ0Qem2jxzHtqmCa' NOSUPERUSER;
-- 权限可分为:{ALL,ALTER,AUTHORIZE,CREATE,DROP,MODIFY,SELECT}
-- 目标可分为:{ALL KEYSPACES,KEYSPACE keyspace_name, TABLE keyspace_name.table_name}
-- 给loki_dev用户授予对于KeySpace:APPDB的建表权限:
GRANT CREATE ON KEYSPACE APPDB TO loki_dev; 
-- 撤回loki_dev用户对于KeySpace:APPDB的建表权限:
REVOKE CREATE ON KEYSPACE APPDB FROM loki_dev;
-- 给用户user1授予KEYSPACE test的所有权限
LIST ALL PERMISSIONS ON KEYSPACE test OF user1
-- 查看已授权情况
LIST ALL PERMISSIONS ON KEYSPACE test OF user1;

创建用户并授权

-- 用户:loki_dev
-- 密码:123456
-- 键空间:loki_dev
-- 创建一个loki_dev用户并对loki_dev键空间有所有权限
CREATE USER loki_dev WITH PASSWORD '123456' NOSUPERUSER;
GRANT ALL ON KEYSPACE loki_dev TO loki_dev;