分布式NoSQL数据库Cassandra集群搭建

1 Cassandra介绍

1.1 Cassandra介绍

  Cassandra是一套开源分布式NoSQL数据库系统。它最初由Facebook开发,用于储存收件箱等简单格式数据,集GoogleBigTable的数据模型与Amazon Dynamo的完全分布式的架构于一身Facebook于2008将 Cassandra 开源,此后,由于Cassandra良好的可扩展性,被Digg、Twitter等知名Web 2.0网站所采纳,成为了一种流行的分布式结构化数据存储方案。
Cassandra是一个混合型的非关系的数据库,类似于Google的BigTable。其主要功能比Dynamo (分布式的Key-Value存储系统)更丰富,但支持度却不如文档存储MongoDB(介于关系数据库和非关系数据库之间的开源产品,是非关系数据库当中功能最丰富,最像关系数据库的。支持的数据结构非常松散,是类似json的bjson格式,因此可以存储比较复杂的数据类型)。Cassandra最初由Facebook开发,后转变成了开源项目。它是一个网络社交云计算方面理想的数据库。以Amazon专有的完全分布式的Dynamo为基础,结合了Google BigTable基于列族(Column Family)的数据模型。P2P去中心化的存储。很多方面都可以称之为Dynamo 2.0。

1.2 Cassandra的特点

弹性可扩展性 - Cassandra是高度可扩展的; 它允许添加更多的硬件以适应更多的客户和更多的数据根据要求。
数据存储灵活 - Cassandra适应所有可能的数据格式,包括:结构化,半结构化和非结构化。它可以根据您的需要动态地适应变化的数据结构。
便捷的数据分发 - Cassandra通过在多个数据中心之间复制数据,可以灵活地在需要时分发数据。
快速写入 - Cassandra被设计为在廉价的商品硬件上运行。 它执行快速写入,并可以存储数百TB的数据,而不牺牲读取效率。

2 准备环境

  Cassandra选择了无中心的P2P架构,网络中的所有节点都是对等的,它们构成了一个环,节点之间通过Gossip协议每秒钟和至多三个节点交换一次数据,这样每个节点都拥有其它所有节点的信息,包括位置、状态等。为了保证数据交换的准确性,所有的节点必须使用同一份集群列表,这样的节点又被称作seed节点。

 

 

 

 主机

环境 部署内容
 192.168.220.129(master) centos7.6

apache-cassandra-3.11.6
jdk1.8.0_202

Python-3.9.6

192.168.220.130(seed) centos7.6

apache-cassandra-3.11.6
jdk1.8.0_202

Python-3.9.6

192.168.220.131(seed) centos7.6

apache-cassandra-3.11.6
jdk1.8.0_202

Python-3.9.6

3 集群搭建

安装包下载地址:http://archive.apache.org/dist/cassandra
wget http://archive.apache.org/dist/cassandra/3.11.6/apache-cassandra-3.11.6-bin.tar.gz

3.1 搭建java环境

[root@test1 ~]# mkdir /usr/java
[root@test1 ~]# tar -zxvf jdk-8u202-linux-x64.tar.gz -C /usr/java >/dev/null 2>&1
[root@test1 ~]# vim /etc/profile.d/java.sh 
export JAVA_HOME=/usr/java/jdk1.8.0_202
export PATH=$PATH:$JAVA_HOME/bin
[root@test1 ~]# source /etc/profile.d/java.sh
验证:
[root@test1 ~]# java -version
java version "1.8.0_202"
Java(TM) SE Runtime Environment (build 1.8.0_202-b08)
Java HotSpot(TM) 64-Bit Server VM (build 25.202-b08, mixed mode)

3.2 部署Cassandra

添加用户
[root@test1 ~]# useradd cassandra

创建目录
[root@test1 ~]# mkdir -pv /data01/cassandra/{commitlog,data,saved_caches}
[root@test1 ~]# mkdir -pv /var/log/cassandra/
目录授权
chown -R cassandra.cassandra /data01/cassandra/{commitlog,data,saved_caches} 
[root@test1 ~]# chown -R cassandra.cassandra /var/log/cassandra/

编辑文件
[root@test1 ~]# vim /opt/cassandra/conf/cassandra.yaml
#集群名称
cluster_name: 'test'
#数据存储的目录,默认为/var/lib/cassandra/data,这里按照自己的路径配置,这项可以有多个目录分行写
data_file_directories:
- /data01/cassandra/data
#提交日志存放目录,默认为/var/lib/cassandra/data。
data_file_directories:
- /data01/cassandra/data
#缓存目录,默认为/var/lib/cassandra/saved_caches。
saved_caches_directory: /data01/cassandra/saved_caches
#seed节点 这里我使用192.168.220.130和192.168.220.131服务器作为种子节点,ip中间使用逗号隔开
seeds: “192.168.220.130,192.168.220.131”
#监听地址 这项配置当前服务器ip,rpc_address 和 listen_address一样填写当前服务器ip
listen_address:192.168.220.129
rpc_address:192.168.220.129
其他两台机器如上配置,注意的是 listen_address, rpc_address 这两项填写该台服务器地址($ip)

JVM内存堆大小和gc日志文件路径修改
默认为4G
[root@test1 ~]# vim /opt/cassandra/conf/jvm.options
#JVM内存堆
#-Xms4G
#-Xmx4G
#gc日志文件路径
-Xloggc:/var/log/cassandra/gc.log

cassandra日志路径文件修改
vim /opt/cassandra/conf/logback.xml
<level>INFO</level>
    </filter>
    <file>/var/log/cassandra/system.log</file>
    <rollingPolicy class="ch.qos.logback.core.rolling.FixedWindowRollingPolicy">
      <fileNamePattern>/var/log/cassandra/system.log.%i.zip</fileNamePattern>

<file>/var/log/cassandra/debug.log</file>
    <rollingPolicy class="ch.qos.logback.core.rolling.FixedWindowRollingPolicy">
      <fileNamePattern>/var/log/cassandra/debug.log.%i.zip</fileNamePattern>

3.3 集群启动

创建启动文件
[root@test1 ~]# vim /etc/systemd/system/cassandra.service
[Unit]
Description=Cassandra Server Service
After=network.service

[Service]
Type=simple
Environment=JAVA_HOME=/usr/java/jdk1.8.0_202

PIDFile=/home/cassandra/cassandra.pid
User=cassandra
ExecStart=/opt/cassandra/bin/cassandra -f -p /home/cassandra/cassandra.pid
StandardOutput=journal
StandardError=journal
LimitNOFILE=100000
LimitMEMLOCK=infinity
LimitNPROC=32768
LimitAS=infinity

[Install]
WantedBy=multi-user.target

启动
[root@test1 ~]# systemctl start cassandra
[root@test1 ~]# systemctl enable cassandra
[root@test1 ~]# ps -ef |grep cassandra

3.4搭建python环境

Python 下载地址https://www.python.org/ftp/python/3.9.6/Python-3.9.6.tgz
[root@test1 ~]# tar -xzvf /opt/softs/Python-3.9.6.tgz && cd Python-3.9.6
[root@test1 ~]# ./configure --prefix=/usr/local/python-3.9.6
[root@test1 ~]# make && make install -j8
[root@test1 ~]# python3 -V
Python 3.9.6

 

3.4 添加到系统环境

[root@test1 ~]# vim /etc/profile
export CASSANDRA_HOME=/opt/cassandra
export JAVA_HOME=/usr/java/jdk1.8.0_202
export PYTHON_HOME=/usr/local/python-3.9.6
export PATH=$PYTHON_HOME/bin:$JAVA_HOME/bin:$CASSANDRA_HOME/bin:$CASSANDRA_HOME/tools/bin:$PATH:$HOME/bin:/usr/local/bin
[root@test1 ~]# source /etc/profile
测试
[root@test1 ~]# cqlsh 192.168.220.129
Connected to test at 192.168.220.129:9042.
[cqlsh 5.0.1 | Cassandra 3.11.6 | CQL spec 3.4.4 | Native protocol v4]
Use HELP for help.
root@cqlsh> exit

 

4 Cassandra集群验证及简单使用

4.1 Cassandra集群验证

[root@test1]# nodetool status
Datacenter: datacenter1
=======================
Status=Up/Down
|/ State=Normal/Leaving/Joining/Moving
--  Address          Load       Tokens       Owns (effective)  Host ID                               Rack
UN  192.168.220.129  263.76 KiB  256          35.3%             c425b8ec-68e7-4be6-8a09-1e5f57e10da0  rack1
UN  192.168.220.130  223.55 KiB  256          31.2%             ca9b052b-fd7d-4e1f-8627-c5946dbfa9ea  rack1
UN  192.168.220.131  358.28 KiB  256          33.5%             51b921a2-f378-46ff-aa21-fe1021aeb1a6  rack1

4.2 Cassandra简单使用

官方文档:http://cassandra.apache.org/doc/latest/cql/index.html

[root@test1]# cqlsh 192.168.220.129
Connected to test at 192.168.220.129:9042.
[cqlsh 5.0.1 | Cassandra 3.11.6 | CQL spec 3.4.4 | Native protocol v4]
Use HELP for help.
cqlsh>
1 查询全部的keyspace
cqlsh> describe keyspaces;
ks1  system_schema  system_auth  system  system_distributed  system_traces

2 创建一个keyspace:CREATE KEYSPACE IF NOT EXISTS test WITH REPLICATION = {'class': 'SimpleStrategy','replication_factor':1};
   class : 副本配置策略(总共有三种):
    Simple Strategy(RackUnaware Strategy):副本不考虑机架的因素,按照Token放置在连续下几个节点。假如副本数为3,属于A节点的数据在B.C两个节点中也放置副本。
        OldNetwork Topology Strategy(RackAware Strategy):考虑机架的因素,除了基本的数据外,先找一个处于不同数据中心的点放置一个副本,其余N-2个副本放置在同一数据中心的不同机架中。
        Network Topology Strategy(DatacneterShard Strategy):将M个副本放置到其他的数据中心,将N-M-1的副本放置在同一数据中心的不同机架中。

3 使用某个keyspace:use myCas;

4 查询全部的table:desc tables;                                    

5 创建一张表:CREATE TABLE user (id int, user_name varchar, PRIMARY KEY (id) );
创建表的时候至少指定一个主键

6 向表中插入一条记录:INSERT INTO user (id,user_name) VALUES (1,'test');
列名必须要显示指定,如果表中已存在相同主键的记录,那么该操作会覆盖表中已存在的记录

使用参考
https://www.cnblogs.com/youzhibing/p/6549960.html
https://www.cnblogs.com/leiwenbin627/p/11780223.html

4.3 Cassandra设置访问密码

修改配置文件 cassandra.yaml
把 authenticator: AllowAllAuthenticator改为authenticator: PasswordAuthenticator

重启cassandra
[root@test1 ~]# systemctl restart cassandra

此时之前的登录方式无法再登录进去
[root@test1 /opt/cassandra/bin]# ./cqlsh 192.168.220.129 
Connection error: ('Unable to connect to any servers', {'192.168.220.129': AuthenticationFailed('Remote end requires authentication.',)})

使用默认用户名cassandra和默认密码cassandra登录
[root@test1]# cqlsh 192.168.220.129 -ucassandra -pcassandra
Connected to test at 192.168.220.129:9042.
[cqlsh 5.0.1 | Cassandra 3.11.6 | CQL spec 3.4.4 | Native protocol v4]
Use HELP for help.
cassandra@cqlsh> 

创建用户
CREATE USER myusername WITH PASSWORD 'mypassword' SUPERUSER ;
cassandra@cqlsh> CREATE USER root WITH PASSWORD 'flz_3qc' SUPERUSER ;

删除默认帐号
注意:需要用新创建的账号重新登录,才可以删除默认账号
[root@test1]# cqlsh 192.168.220.129 -uroot -pflz_3qc
Connected to test at 192.168.220.129:9042.
[cqlsh 5.0.1 | Cassandra 3.11.6 | CQL spec 3.4.4 | Native protocol v4]
Use HELP for help.
root@cqlsh> DROP USER cassandra;
root@cqlsh> 

无密码登录
vim /root/.cassandra/cqlshrc
[authentication]
username = root
password = flz_3qc
测试
[root@test1]# cqlsh 192.168.220.129
Connected to test at 192.168.220.129:9042.
[cqlsh 5.0.1 | Cassandra 3.11.6 | CQL spec 3.4.4 | Native protocol v4]
Use HELP for help.
root@cqlsh> 

 

 

posted @ 2021-07-28 21:20  alisapine  阅读(668)  评论(0编辑  收藏  举报