CentOS 7.4 安装 Apache Cassandra 3.7

近期公司提出利用Cassandra 做数据汇总分析,查阅一些文档,总结了一下安装过程
不妥之处,敬请赐教!

环境版本信息:
OS:CentOS Linux release 7.4.1708
cassandra:apache-cassandra-3.7


1.准备工作:

节点规划
计划部署一个4节点的cassandra集群
确认每个节点的hosts文件是否包含其他节点的信息:
[root@gp4 ~]# cat /etc/hosts

127.0.0.1 localhost localhost.localdomain localhost4 localhost4.localdomain4
::1 localhost localhost.localdomain localhost6 localhost6.localdomain6
192.168.2.61 gp1
192.168.2.62 gp2
192.168.2.63 gp3
192.168.2.64 gp4


在每个节点上关闭并禁用防火墙:
systemctl status firewalld.service
systemctl stop firewalld.service
systemctl disable firewalld.service

禁用SELinux
sestatus
SELinux status: disabled
vim /etc/selinux/config

安装java运行环境
java -version

wget -c --header "Cookie: oraclelicense=accept-securebackup-cookie" \
http://download.oracle.com/otn-pub/java/jdk/8u131-b11/d54c1d3a095b4ff2b6607d096fa80163/jdk-8u131-linux-x64.rpm

rpm -ivh jdk-8u131-linux-x64.rpm

检查python 是否安装
python --version
Python 2.7.5

 

对于数据库集群来说,多节点的时间同步很重要:
[root@gp1 ~]# timedatectl status
Local time: Tue 2018-08-21 16:44:42 +08
Universal time: Tue 2018-08-21 08:44:42 UTC
RTC time: Tue 2018-08-21 08:44:43
Time zone: Asia/Shanghai (+08, +0800)
NTP enabled: yes
NTP synchronized: yes
RTC in local TZ: no
DST active: n/a

如果失去有差异,设置为相同时区:
[root@gp1 ~]# timedatectl set-timezone Asia/Shanghai
rm -f /etc/localtime && cp -f /usr/share/zoneinfo/Asia/Shanghai /etc/localtime

安装设置ntp,若没有安装,需手动安装:
[root@gp1 ~]# rpm -qa ntp*
ntp-4.2.6p5-28.el7.centos.x86_64
ntpdate-4.2.6p5-28.el7.centos.x86_64


手动与时间服务器同步时间:
ntpdate 0.us.pool.ntp.org

将时间同步设置为自启动
systemctl enable ntpd

[root@gp1 ~]# ps -ef |grep ntpd
ntp 684 1 0 May31 ? 00:00:04 /usr/sbin/ntpd -u ntp:ntp -g

开启ntpd:
systemctl start ntpd
timedatectl set-ntp yes
ntpq -p


2.为了发挥出数据库的最大性能,需要修改操作系统参数:
cat /etc/sysctl.conf
net.ipv4.tcp_syn_retries = 1
net.ipv4.ip_forward = 1
net.ipv4.tcp_synack_retries = 1
net.ipv4.tcp_keepalive_time = 600
net.ipv4.tcp_keepalive_probes = 3
net.ipv4.tcp_keepalive_intvl =15
net.ipv4.tcp_retries1 = 3
net.ipv4.tcp_retries2 = 5
net.ipv4.tcp_fin_timeout = 10
net.ipv4.tcp_tw_reuse = 1
net.ipv4.tcp_syncookies = 1
net.ipv4.tcp_window_scaling = 1
net.ipv4.tcp_max_tw_buckets = 60000
net.ipv4.tcp_max_orphans = 32768
net.ipv4.tcp_max_syn_backlog = 16384
net.ipv4.tcp_mem = 94500000 915000000 927000000
net.ipv4.tcp_wmem = 4096 16384 13107200
net.ipv4.tcp_rmem = 4096 87380 17476000
net.ipv4.ip_local_port_range = 1024 65000
net.ipv4.route.gc_timeout = 100
net.core.somaxconn = 32768
net.core.netdev_max_backlog = 32768
vm.overcommit_memory = 1
vm.swappiness = 1
fs.file-max = 1024000

调整用户资源限制:
cat /etc/security/limits.conf
* soft nofile 1024000
* hard nofile 1024000
* soft nproc 1024000
* hard nproc 1024000
hive - nofile 1024000
hive - nproc 1024000

 

3.安装步骤
获取安装包:
wget https://archive.apache.org/dist/cassandra/3.7/apache-cassandra-3.7-bin.tar.gz


添加cassandra 组和用户
先检查用户是否存在
id cassandra
id: cassandra: no such user

添加组和用户
groupadd cassandra && useradd -d /home/cassandra -g cassandra cassandra
passwd cassandra


解压安装Cassandra:
tar -zxvf apache-cassandra-3.7-bin.tar.gz -C /usr/local
chown -R cassandra:cassandra /usr/local/apache-cassandra-3.7


切换到cassandra 用户
su - cassandra

修改环境变量:
vi /home/cassandra/.bash_profile
PATH=$PATH:$HOME/.local/bin:$HOME/bin:/usr/local/apache-cassandra-3.7/bin
export CQLSH_NO_BUNDLED=TRUE
export PATH

让环境变量生效
source /home/cassandra/.bash_profile


创建数据和日志目录:
cd /home/cassandra
mkdir ./{commitlog,data,saved_caches}


准备配置文件,主要修改有三个配置文件
cassandra.yaml 里面的参数,都要顶格写,参数前面不能有空格,并且参数名称后面的冒号和参数取值之间必须有空格

cd /usr/local/apache-cassandra-3.7/conf/

vi cassandra.yaml


##需要关注的是以下参数,其他参数可以保持默认值
cluster_name: 'cluster01'

authenticator: PasswordAuthenticator

authorizer: CassandraAuthorizer

roles_validity_in_ms: 600000

roles_update_interval_in_ms: 50000

permissions_validity_in_ms: 600000

permissions_update_interval_in_ms: 50000

credentials_validity_in_ms: 600000

credentials_update_interval_in_ms: 50000

data_file_directories:
- /home/cassandra/data

commitlog_directory: /home/cassandra/commitlog

row_cache_size_in_mb: 1024

saved_caches_directory: /home/cassandra/saved_cache

seed_provider:
# Addresses of hosts that are deemed contact points.
# Cassandra nodes use this list of hosts to find each other and learn
# the topology of the ring. You must change this if you are running
# multiple nodes!
- class_name: org.apache.cassandra.locator.SimpleSeedProvider
parameters:
# seeds is actually a comma-delimited list of addresses.
# Ex: "<ip1>,<ip2>,<ip3>"
- seeds: "192.168.2.61,192.168.2.62,192.168.2.63,192.168.2.64"

 


disk_optimization_strategy: ssd

memtable_allocation_type: offheap_buffers

listen_address: <host_ip>

broadcast_address: <host_ip>

rpc_address: <host_ip>

broadcast_rpc_address: <host_ip>

endpoint_snitch: GossipingPropertyFileSnitch

batch_size_warn_threshold_in_kb: 20

batch_size_fail_threshold_in_kb: 200


##设置DC 数据中心和 Rack 机架信息
vi cassandra-rackdc.properties
dc1
rack1 <or rack2>


##cassandra基于JIAVA开发,需要为JVM 参数,重点是内存要分配充分。
vi jvm.options


启动Cassandra,
-f 选项将所有引导日志都输出到前台。在首次启动时,可以使用该选项来检查 Cassandra 启动期间的错误。
su - cassandra
cassandra -f

查看集群状态
IP 地址前面的 UN 字母表示节点在正常 (N) 运行 (U)
nodetool status
Datacenter: dc1
===============
Status=Up/Down
|/ State=Normal/Leaving/Joining/Moving
-- Address Load Tokens Owns (effective) Host ID Rack
UN 192.168.2.61 69.33 KiB 256 48.6% d3e764be-43a8-4c03-a562-55a7cf9f8a41 rack1
UN 192.168.2.62 69.34 KiB 256 50.8% fccc7ad7-e6e6-4ee0-a438-3c728b6aaac1 rack2
UN 192.168.2.63 86.77 KiB 256 51.8% a8dd6a51-dcd4-4341-af75-16aee2358c40 rack1
UN 192.168.2.64 105.7 KiB 256 48.9% 769180fd-7d4c-4382-827d-22fdd4349572 rack2

 

连接登录Cassandra:
[cassandra@gp1 ~]$ cqlsh 192.168.2.61 -u cassandra -p cassandra

Connected to cluster01 at 192.168.2.61:9042.
[cqlsh 5.0.1 | Cassandra 3.7 | CQL spec 3.4.2 | Native protocol v4]
Use HELP for help.
cassandra@cqlsh>
cassandra@cqlsh>
cassandra@cqlsh>
cassandra@cqlsh>


如果 cqlsh连不上库,提示如下:

Python Cassandra driver not installed, or not on PYTHONPATH.
You might try "pip install cassandra-driver".

需要安装驱动
sudo su -
curl https://bootstrap.pypa.io/get-pip.py -o get-pip.py && python get-pip.py && pip install cassandra-driver

su - cassandra
export CQLSH_NO_BUNDLED=TRUE
cqlsh -u cassandra -p cassandra


##安装完成后的准备工作

如果在配置文件中 authorizer 启用用户的授权
authorizer: CassandraAuthorizer

需要增加 system_auth 键空间复制因子,让该键空间在集群中有足够多的副本,用于用户连接授权,只需在一个节点执行:
alter KEYSPACE system_auth WITH replication = { 'class': 'NetworkTopologyStrategy', 'dc1': '3' } AND durable_writes = true;


常用命令:
#查看cluster信息:
cassandra@cqlsh> describe cluster;

Cluster: cluster01
Partitioner: Murmur3Partitioner

#创建 KEYSPACE,复制因子为4

cqlsh> CREATE KEYSPACE test_keyspace WITH replication = {'class': 'SimpleStrategy', 'replication_factor' : 4};

#查看所有keyspaces
cassandra@cqlsh> describe keyspaces;

test_keyspace system_schema system_auth system system_distributed system_traces

#查看keyspaces定义:
desc test_keyspace;

CREATE KEYSPACE test_keyspace WITH replication = {'class': 'SimpleStrategy', 'replication_factor': '4'} AND durable_writes = true;


#创建表
CREATE TABLE test_keyspace.tb01 (
user_id int,
id int,
date timeuuid,
details text,
PRIMARY KEY (user_id, id));

#查示所有表:
use knet ;
describe tables;

desc tables;


##插入数据
use test_keyspace;
INSERT INTO tb01 (user_id,id,date,details) values (1,1,now(),'first tb01 test_keyspace 1');
INSERT INTO tb01 (user_id,id,date,details) values (1,2,now(),'second tb01 test_keyspace 1');
INSERT INTO tb01 (user_id,id,date,details) values (2,1,now(),'first tb01 test_keyspace 2');
INSERT INTO tb01 (user_id,id,date,details) values (3,1,now(),'first tb01 test_keyspace 3');


#查询数据:
cqlsh:test_keyspace> select * from tb01 where user_id=1;


用户管理
#初始安装的cassandra默认只有一个用户,实际环境中需要创建应用账户并授予权限:
list users;

name | super
-----------+-------
cassandra | True

(1 rows)

#创建账户,在单个节点执行即可:
CREATE USER testuser_888 WITH PASSWORD 'test12345678' NOSUPERUSER ;

#新建的用户可以连接登入数据库,但是没有操作权限:
testuser_888@cqlsh:test_keyspace> select * from tb01;
Unauthorized: code=2100 [Unauthorized] message="User testuser_888 has no SELECT permission on <table test_keyspace.tb01> or any of its parents

#为用户授权:
GRANT select PERMISSION ON KEYSPACE test_keyspace TO testuser_888;
GRANT modify PERMISSION ON KEYSPACE test_keyspace TO testuser_888;

#查看用户权限:
LIST ALL PERMISSIONS OF testuser_888 NORECURSIVE;

#修改用户密码
alter user testuser_888 with password 'asd!@#456';
drop user cassandra;
list users;

注意:
只有SUPERUSER可以创建用户,创建的用户默认为NOSUPERUSER;
只有SUPERUSER可以删除用户,任何用户不能删除自己。

posted @ 2018-09-29 11:51  典羲居士  阅读(442)  评论(0编辑  收藏  举报