Apache Cassandra 的安装及使用 (一)

1、安装Cassandra:
- 检查java版本,确保安装jdk8以上版本:

$ java -version.
  • 在文件 /etc/yum.repos.d/datastax.repo 中添加 Apache Cassandra 3.0 yum配置:
[datastax]
name = DataStax Repo for Apache Cassandra
baseurl = http://rpm.datastax.com/community
enabled = 1
gpgcheck = 0
  • 使yum命令直接安装 assandra 3.0.x 的最新版本:
$ sudo yum install dsc30
$ sudo yum install cassandra30-tools ## Installs optional utilities.
  • 点击链接,安装cassandra早期版本

  • 现在只安装了单个cluster实例 ,使用下面命令操作cassandra

    • 启动assandra:
$ sudo service cassandra start
- 如果不存在服务,直接执行:
$ sudo /etc/init.d/cassandra start
Note: Cassandra 3.8 and later: Startup is aborted if corrupted transaction log files are found and the affected log files are logged. See the log files for information on resolving the situation.

使用 DataStax 查看 Cassandra 是否启动:

$ nodetool status

这里写图片描述

安装参考:http://docs.datastax.com/en/cassandra/3.0/cassandra/install/installRHEL.html

2、或者直接下载Cassandra

wget
http://mirrors.tuna.tsinghua.edu.cn/apache/cassandra/3.10/apache-cassandra-3.10-bin.tar.gz
tar -zxvf apache-cassandra-3.10-bin.tar.gz
sudo mv apache-cassandra-3.10 /usr/local/cassandra
sudo vi /etc/profile

添加 CASSANDRA_HOME=/usr/local/cassandra
PATH=$CASSANDRA_HOME/bin:$PATH

sudo source /etc/profile sudo cassandra
……一系列启动日志……

新建窗口执行 nodetool status
Datacenter: datacenter1
======================= Status=Up/Down |/ State=Normal/Leaving/Joining/Moving
– Address Load Tokens Owns (effective) Host ID Rack UN 127.0.0.1 241.25 KB 256 100.0% 101d5eaf-3430-4609-a812-2aeef22f3065 rack1

3、常用命令:

  • 启动cassabdra

    sudo service cassandra start|stop|atatus

  • 查看链接状态

    nodetool status

  • 进入cqlsh

    >>cqlsh
    Connected to Test Cluster at 127.0.0.1:9042.
    [cqlsh 5.0.1 | Cassandra 3.0.9 | CQL spec 3.4.0 | Native protocol v4] —-(cassandra版本号)
    Use HELP for help.
    cqlsh >>show version (查看版本号命令)
    >>cqlsh -cassandra (使用cassandra用户登录cql)

  • 查看命令

    describe | desc

     查看当前 cluster :  describe cluster |  desc  cluster
     查看所有 keyspace   :  describe cluster |  desc  cluster
     查看指定 keyspace 内容  :  describe keyspace mykeyspace; --( mykeyspace 为键空间)

     查看所有  table :  describe tables
     查看指定  table :  describe columnfamaliy mytable;|  desc table  mytable --( mytable 为表名)
  • 创建命令
创建 keyspace :  CREATE KEYSPACE mykeyspace  WITH REPLICATION = { 'class' : 'SimpleStrategy', 'replication_factor' : 1 };
    Replication Factor : 复制因数。 表示一份数据在一个DC 之中包含几份。常用奇数~ 比如我们项目组设置的replication_factor=3
    Replica placement strategy : 复制策略。 默认的是SimpleStrategy. 如果是单机架、单数据中心的模式,保持使用SimpleStrtegy即可。
使用 keyspace:    use  mykeyspace

创建 table : create table mytable ( id int primary key, name varchar, age int );
删除 table :  drop table mytable

4、配置 用户角色

  • 创建可以登陆的角色:
CREATE ROLE cass WITH PASSWORD = '111111' AND LOGIN = true;
  • 创建可以访问指定空间的角色
CREATE ROLE ksadmin WITH PASSWORD = '1234abcd';
GRANT ALL PERMISSIONS ON KEYSPACE mykeyspace TO ksadmin;
  • 给 cass 授予 ksadmin 权限
GRANT ksadmin TO cass ;
  • 给 ksaccount 授予 创建用户 权限
GRANT ALL PERMISSIONS ON KEYSPACE mykeyspace TO ksaccount;
GRANT CREATE ON ALL ROLES TO ksaccount
  • 给 ksaccount 授予 grant 权限
GRANT AUTHORIZE ON KEYSPACE mykeyspace TO ksaccount ;

安装cassandra后,进入cqlsh直接创建用户时 会报下面错误:

**InvalidRequest: Error from server: code=2200 [Invalid query] message="org.apache.cassandra.auth.CassandraRoleManager doesn't support PASSWORD"**

原因是没有开启password验证,可以使用下面步骤重新设置:

  • 编辑cassandra 配置项, 将 authenticator :AllowAllAuthenticator 改为 PasswordAuthenticator
  • 重启cassandra服务
  • 使用cassandra用户进入cql
  • 创建新的用户

    >>sudo  vi /etc/cassandra/conf/cassandra.yaml
    >>sudo service cassandra restart
    
    >>cqlsh -u cassandra (输入密码:默认cassandra)
    cassandra@cqlsh>> CREATE ROLE cass WITH PASSWORD = '111111' AND LOGIN = true;
    cassandra@cqlsh>> exit;
    
    >>cqlsh -u  cass (输入密码:111111)
    
    cassandra@cqlsh>>
    

5、配置sudo cassandra 命令
修改 /etc/sudoers 文件,在secure_path 处添加cassandra 路径。
在保存此文件时,及时使用root用户仍然提示文件不可修改,可以使用下面命令强制保存修改:

:w !sudo tee %

  • 2
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值