root@jlive:~#java -version
java version "1.8.0_77"
Java(TM) SE Runtime Environment (build 1.8.0_77-b03)
Java HotSpot(TM) 64-Bit Server VM (build 25.77-b03, mixed mode)
root@jlive:~#python -V
Python 2.7.5
ln -s /opt/apache-cassandra-3.4/ /opt/cassandra
useradd -r
chown -R cassandra: /opt/cassandra
三.优化系统环境变量内核参数
http://docs.datastax.com/en/cassandra/3.x/cassandra/install/installRecommendSettings
1.时间同步(所有节点)
2.SSD参数调优
cat >>/etc/rc.d/rc.local <<HERE
echo never > /sys/kernel/mm/transparent_hugepage/enabled
echo
noop
>
/sys/block/sda/queue/scheduler
echo 0 > /sys/class/block/sda/queue/rotational
echo 8 > /sys/class/block/sda/queue/read_ahead_kb
echo 0 > /proc/sys/vm/zone_reclaim_mode
HERE
3.资源限制
cat >/etc/security/limits.d/99-cassandra.conf <<HERE
cassandra - memlock unlimited
cassandra - nofile 100000
cassandra - nproc 32768
cassandra - as unlimited
HERE
echo "vm.max_map_count = 131072" >>/usr/lib/sysctl.d/00-system.conf
sysctl -p /usr/lib/sysctl.d/00-system.conf
4.禁用swap
swapoff -a
5.RAID readhead优化
blockdev --setra 128 /dev/
blockdev --report /dev/
注意:避免readhead为65536
启动cassandra:
http://wiki.apache.org/cassandra/RunningCassandra
1.启动
su - cassandra -c /opt/cassandra/bin/cassandra -f
-f #foreground,不带该参数时是后台运行
2.停止
pkill
cqlsh测试:
1.创建KEYSPACE
CREATE KEYSPACE mykeyspace WITH REPLICATION = { 'class' : 'SimpleStrategy', 'replication_factor' : 1 };
2.创建表
USE mykeyspace;
CREATE TABLE mykeyspace.users ( user_id int PRIMARY KEY, fname text, lname text );
3.插入数据
INSERT INTO mykeyspace.users (user_id, fname, lname) VALUES (1745, 'john', 'smith'); INSERT INTO mykeyspace.users (user_id, fname, lname) VALUES (1744, 'john', 'doe'); INSERT INTO mykeyspace.users (user_id, fname, lname) VALUES (1746, 'john', 'smith');
4.查询
SELECT * FROM users;
5.创建index
CREATE INDEX ON users (lname); SELECT * FROM users WHERE lname = 'smith';
root@jlive:~#/opt/cassandra/bin/cqlsh
Connected to Test Cluster at 127.0.0.1:9042.
[cqlsh 5.0.1 | Cassandra 3.4 | CQL spec 3.4.0 | Native protocol v4]
Use HELP for help.
cqlsh> CREATE KEYSPACE mykeyspace
cqlsh> USE mykeyspace;
cqlsh:mykeyspace> CREATE TABLE mykeyspace.users (
cqlsh:mykeyspace>
INSERT INTO mykeyspace.users (user_id,
cqlsh:mykeyspace>
INSERT INTO mykeyspace.users (user_id,
cqlsh:mykeyspace>
INSERT INTO mykeyspace.users (user_id,
注意:这里只能分3条语句插入,一条语句插入多条数据不支持
cqlsh:mykeyspace> SELECT * FROM users;
---------+-------+-------
(3 rows)
cqlsh:mykeyspace> CREATE INDEX ON users (lname);
cqlsh:mykeyspace> SELECT * FROM users WHERE lname = 'smith';
---------+-------+-------
(2 rows)
语法和SQL非常相似,更多关键字可以在cqlsh下输入HELP获取
客户端:
客户端工具需要根据不同语言来封装,datastax公司也给出了主流的语言调用接口,
http://www.datastax.com/documentation/cql/3.0/webhelp/index.html
创建集群:
步骤同上,这里以OS X 10.11.4为例
ln -s /opt/apache-cassandra-3.4 /opt/cassandra
3.添加用户
dscl . -create /Users/cassandra UserShell /bin/bash
dscl . -create /Users/cassandra RealName "Cassandra"
dscl . -create /Users/cassandra UniqueID 1005
dscl . -create /Users/cassandra PrimaryGroupID 1005
dscl . -create /Users/cassandra HomeDirectory /Users/cassandra
dscl . -passwd /Users/cassandra foo.123
MacBook-Pro:opt root# id cassandra
uid=1005(cassandra) gid=1005 groups=1005,12(everyone),61(localaccounts),100(_lpoperator),701(com.apple.sharepoint.group.1)
4.修改cassandra目录的属主
chown -R cassandra: /opt/apache-cassandra-3.4
5.修改cassandra配置文件/opt/cassandra/conf/cassandra.yaml
listen_address: 192.168.130.1
rpc_address: 192.168.130.1
seeds:
说明:默认是监听在localhost, 集群环境必须指定IP或interface,
因为是去中心集群,种子节点可以指定多个,GossIP协议会自动协商,配置文件很直白,就不多啰嗦,其它节点也要相应修改
6.启动cassandra
su cassandra -c
/opt/cassandra/bin/cassandra
8.添加集群节点
http://wiki.apache.org/cassandra/Operations
添加集群节点就是在配置文件中指明seeds所在,上面已经修改过,不出问题的话,应该可以看到类似的如下结果
root@jlive:~#/opt/cassandra/bin/nodetool status
Datacenter: datacenter1
=======================
Status=Up/Down
|/ State=Normal/Leaving/Joining/Moving
--
UN
UN
/opt/cassandra/log/system.log
INFO
INFO
INFO
INFO
INFO
INFO
INFO
INFO
INFO
集群监控MX4J(可选)
https://issues.apache.org/jira/browse/CASSANDRA-1068
http://mx4j.sourceforge.net/docs/ch05.html
root@jlive:~#unzip /mnt/hgfs/linux_soft/mx4j-3.0.2.zip mx4j-3.0.2/lib/mx4j-tools.jar
Archive:
root@jlive:~#mv mx4j-3.0.2/lib/mx4j-tools.jar /opt/cassandra/lib/
重启cassandra,日志里会显示
-
HttpAdaptor version 3.0.2 started on port 8081
访问http://192.168.130.254:8081/即可调用,xml调用需要再添加一个参数&template=identity
本地集群管理---ccm(可选)
https://github.com/pcmanus/ccm
The goal of ccm and ccmlib is to make it easy to create, manage and destroy a small Cassandra cluster on a local box. It is meant for testing a Cassandra cluster.
brew install ccm
或
pip install ccm
或
git
clone
cd ccm
python setup install