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 %

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章