下载cassandra
cd /opt
wget https://dlcdn.apache.org/cassandra/3.11.13/apache-cassandra-3.11.13-bin.tar.gz
tar xvf apache-cassandra-3.11.13-bin.tar.gz
mv apache-cassandra-3.11.13-bin cassandra
mkdir /opt/cassandra/{data,commitlog,saved_caches,hints}
创建cassandra用户及将文件属主改为cassandra
useradd cassandra
chown -R cassandra.cassandra cassandra
切换到cassandra用户
su - casssandra
配置JAVA_HOME, PATH,确保可正常访问到jdk(jdk版本不宜太高,否则可能会创建jvm失败,建议jdk8)
JAVA_HOME=/usr/java_jre
PATH=/usr/java_jre/bin:$PATH
export PATH JAVA_HOME
修改cassandra配置文件
vi /opt/cassandra/cassandra.yaml
cluster_name: 'Test Cluster’
hints_directory: /opt/cassandra/hints
data_file_directories:
- /opt/cassandra/data
commitlog_directory: /opt/cassandra/commitlog
saved_caches_directory: /opt/cassandra/saved_caches
seeds: "127.0.0.1” //集群种子节点ip,新加入集群的节点从种子节点中同步数据。为内网地址,不要将整个集群的节点都设置成种子节点。选出两个最稳定的节点就行
listen_address: localhost // 需要监听的IP或主机名。改成本机IP
# broadcast_address: 1.2.3.4
start_rpc: true
rpc_address: 0.0.0.0
broadcast_rpc_address: 1.2.3.4
可直接使用./bin/cassandra 来启动服务
也可使用systemd管理Cassandra
cat /usr/lib/systemd/system/cassandra.service
[Unit]
Description=Cassandra Server Service
After=network.service
[Service]
Type=simple
Environment=JAVA_HOME=/usr/java_jre
PIDFile=/var/run/cassandra.pid
#新建一个用户和用户组,Cassandra无法使用root账号启动
User=cassandra
Group=cassandra
#此处为Cassandra包解压后的路径
ExecStart=/opt/cassandra/bin/cassandra -f -p /var/run/cassandra.pid
StandardOutput=journal
StandardError=journal
LimitNOFILE=100000
LimitMEMLOCK=infinity
LimitNPROC=32768
LimitAS=infinity
[Install]
WantedBy=multi-user.target
systemctl daemon-reload
systemctl enable cassandra
systemctl start cassandra
cassandra使用:
连接远程数据库:
./cqlsh -u cassandra -p cassandra 192.168.1.99 9042
连接本地数据库:
./cqlsh -u cassandra -p cassandra
# 新建superuser,禁用默认cassandra用户
cassandra@cqlsh:system_schema> use system_auth;
cassandra@cqlsh:system_auth> create user cosmo with password '123456' superuser;
cassandra@cqlsh:system_auth> select * from roles;
# 使用新用户登录
./cqlsh -u cosmo -p 123456 192.168.1.99 9042
cosmo@cqlsh> use system_auth;
cosmo@cqlsh:system_auth> describe tables;
resource_role_permissons_index role_permissions role_members roles
cosmo@cqlsh:system_auth> select * from roles;
role | can_login | is_superuser | member_of | salted_hash
-----------+-----------+--------------+-----------+--------------------------------------------------------------
cassandra | True | True | null | $2a$10$wU8LD5NcJw5birYdmes0pu09sIVuZrud.j6zbW5E/Mr42tWXbPiOq
cosmo | True | True | null | $2a$10$so7GQX3NrodGfaNMUc9BueeG6sgGrdHMn74YbGMf5O3xdDWNeJ2JS
(2 rows)
cosmo@cqlsh:system_auth> alter role cassandra with superuser=false and login=false;
cosmo@cqlsh:system_auth> select * from roles;
role | can_login | is_superuser | member_of | salted_hash
-----------+-----------+--------------+-----------+--------------------------------------------------------------
cassandra | False | False | null | $2a$10$wU8LD5NcJw5birYdmes0pu09sIVuZrud.j6zbW5E/Mr42tWXbPiOq
cosmo | True | True | null | $2a$10$so7GQX3NrodGfaNMUc9BueeG6sgGrdHMn74YbGMf5O3xdDWNeJ2JS
(2 rows)
cosmo@cqlsh:system_auth>
cosmo@cqlsh:system_auth> create keyspace if not exists mycas with replication = {'class': 'SimpleStrategy','replication_factor':1};
cosmo@cqlsh:system_auth> describe keyspaces;
mycas system_schema system_auth system system_distributed system_traces
cosmo@cqlsh:system_auth> use mycas;
cosmo@cqlsh:mycas> create table xx(id int, name varchar,primary key(id));
cosmo@cqlsh:mycas> describe tables;
xx
cosmo@cqlsh:mycas>
cosmo@cqlsh:mycas> drop table xx;
cosmo@cqlsh:mycas> create table students(id int, name varchar,primary key(id));
cosmo@cqlsh:mycas> insert into students(id,name) values(1,'linuxdba');
cosmo@cqlsh:mycas> select * from students;
id | name
----+----------
1 | linuxdba
(1 rows)
cosmo@cqlsh:mycas> describe students;
参考:
https://www.freesion.com/article/9285663467/
https://www.it610.com/article/1306191718198054912.htm
https://www.bianchengquan.com/article/365488.html