1. 程式人生 > >CentOS 7.4 安裝 Apache Cassandra 3.7

CentOS 7.4 安裝 Apache Cassandra 3.7

name 文件 x64 interval ram nproc ora ace eba

近期公司提出利用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可以刪除用戶,任何用戶不能刪除自己。

CentOS 7.4 安裝 Apache Cassandra 3.7