0
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 3 years have passed since last update.

Cassandra構築

Last updated at Posted at 2021-07-31

構築環境

Cassandra:Apache Cassandra 3.11.6
OS:Amazon Linux 2
java:Amazon Corretto 8

Cassandraを管理するユーザーを作成

1.ユーザーを作成

cassandra管理用のユーザーを作成するため、ユーザーをrootに切り替えます。
useraddコマンドでユーザーを追加し、passwdコマンドでユーザーのパスワードを指定します。

$ sudo su -
# useradd cassandra
# passwd cassandra
Changing password for user postgres.
New password: 
BAD PASSWORD: The password contains the user name in some form
Retype new password: 
passwd: all authentication tokens updated successfully.

2.root権限実行(sudo)を付与

作成したcassandraユーザーでroot権限実行(sudo)が出来るように設定します。

# visudo
以下を追記する
cassandra ALL=(ALL)      NOPASSWD: ALL

Javaをインストール

1.javaのtarファイルをダウンロード

Amazonからjavaのtarファイルをダウンロードします。
JavaはCassandraを動かすためだけなので、厳密に言えばJREのみでもOKです。

$ wget https://corretto.aws/downloads/latest/amazon-corretto-8-x64-linux-jdk.tar.gz -O ~/jdk.tar.gz

2.tarファイルを展開・配置

ダウンロードしたtarファイルを展開し、「/usr/local/java」に配置します。

$ mkdir ~/java && tar -xzvf ~/jdk.tar.gz -C ~/java --strip-components 1
$ sudo mv ~/java /usr/local/java

3.PATHを通す

javaコマンドを使用出来るように環境変数を設定します。

$ sudo vi /etc/profile

# 以下を追記
export JAVA_HOME=/usr/local/java
export PATH=$PATH:$JAVA_HOME/bin

環境変数を設定したらsourceコマンドで設定を再読込みします。

$ source /etc/profile

Cassandraをインストール

1.Cassandraのtarファイルをダウンロードする

apacheのアーカイブからCassandraのtarファイルをダウンロードする

$ wget http://archive.apache.org/dist/cassandra/3.11.6/apache-cassandra-3.11.6-bin.tar.gz -O ~/cassandra.tar.gz

2.tarファイルを配置・展開

ダウンロードしたtarファイルを展開し、「/usr/sbin/cassandra」に配置します。

$ mkdir ~/cassandra && tar -xzvf ~/cassandra.tar.gz -C ~/cassandra --strip-components 1
$ sudo mv ~/cassandra /usr/sbin/cassandra

3.cassandraディレクトリの所有者を設定

cassandraユーザーを配置したcassandraディレクトリの所有者にする

sudo chown -R cassandra:cassandra /usr/sbin/cassandra

4.CassandraのPATHを通す

「.bashrc」を修正し、CassandraのPATHを通す

$ sudo vi /etc/profile

# 以下を追記
export CASSANDRA_HOME=/usr/sbin/cassandra
export PATH=$PATH:$CASSANDRA_HOME/bin

5.PATHの設定変更を再度読み込む

sourceコマンドでPATHの設定変更を再度読み込む

$ source /etc/profile

6.バージョンを確認

バージョンが確認できればPATHの設定成功です。

$ cassandra -v
3.11.6

cassandra.yamlを修正

1.外部接続を許可する

$ vim /usr/sbin/cassandra/conf/cassandra.yaml 

rpc_address: サーバのプライベートIPアドレス

Systemdの設定

サーバ起動と同時に自動起動するようにSystemdで管理するように設定します。

1.ユニット定義ファイルを作成

Tomcat管理用にユニットファイルを作成します。
CATALINA_HOMEなど、環境変数の設定が必要な場合、Environmentを使用して設定します。

$ sudo vim /etc/systemd/system/cassandra.service

[Unit]
Description=Cassandra
After=network.target

[Service]
RuntimeDirectory=cassandra
PIDFile=/var/run/cassandra/cassandra.pid
Type=forking

User=cassandra
Group=cassandra
Environment=JAVA_HOME=/usr/local/java
Environment=JRE_HOME=/usr/local/java/jre

ExecStart=/usr/sbin/cassandra/bin/cassandra -p /var/run/cassandra/cassandra.pid
Restart=always

[Install]
WantedBy=multi-user.target

2.自動起動するように設定

サーバを起動時にcassandraが自動起動されるように設定する

$ sudo systemctl enable cassandra

3.Systemdで起動・停止

systemctlコマンドで起動します。

$ sudo systemctl start cassandra

ステータスを確認し、active (running)となっていればOKです。

$ sudo systemctl status cassandra 
● cassandra.service - Cassandra
   Loaded: loaded (/etc/systemd/system/cassandra.service; enabled; vendor preset: disabled)
   Active: active (running) since 月 2021-03-29 13:29:55 UTC; 12s ago
  Process: 7027 ExecStart=/usr/sbin/cassandra/bin/cassandra -p /var/run/cassandra/cassandra.pid (code=exited, status=0/SUCCESS)
 Main PID: 7101 (java)
   CGroup: /system.slice/cassandra.service
           └─7101 /usr/local/java/bin/java -Xloggc:/usr/sbin/cassandra/bin/../logs/gc.log -ea -XX:+UseThreadPriorities -XX:ThreadPriorityPolicy=42 -XX:+HeapDumpOnOutOfMemoryError -Xss256k -XX:StringTableSize=1000003 -XX:+AlwaysPreTouch -XX:-UseBiasedLocking ...

 3月 29 13:29:55 ip-172-31-47-93.ap-northeast-1.compute.internal systemd[1]: Starting Cassandra...
 3月 29 13:29:55 ip-172-31-47-93.ap-northeast-1.compute.internal systemd[1]: Started Cassandra.

停止したいときはstopで停止します。

$ sudo systemctl stop cassandra

もし、ユニットファイルの内容を変更した場合、設定ファイルの再読込を実施してから操作します。

$ sudo systemctl daemon-reload

参考文献

0
1
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?