今回は、Redisをインストールしてみました。
インストール後は、Javaからアクセスしたり、DBMSと連携させてみたいと思います。
Redisとは?
永続化可能なインメモリデータベースです。
データ構造は、stringやハッシュ、リスト型などがあります。
インストール
Redisは、yumでインストールすることが出来ます。
yumでインストールする場合は、EPELリポジトリをインストールしておく必要があります。
yum install -y epel-release yum install -y redis
あとは、サービスを起動するだけです。
systemctl start redis.service
redis-serverとの簡易的なやりとりは、redis-cliコマンドで行います。
nameキーにtanakaというデータを設定し、nameキーから値を取得してみます。
[root@localhost vagrant]# redis-cli # nameキーに"tanaka"という値を設定 127.0.0.1:6379> set name tanaka OK # nameキーの値を取得 127.0.0.1:6379> get name "tanaka" 127.0.0.1:6379>
yumが使えない時はどうする??
外部ネットワークに繋げない環境もあったりすると思いますが、その場合は、
redisの媒体をダウンロードし、コンパイルすれば使えます。
媒体は、下記のURLからダウンロードが出来ます。
download.redis.io
コンパイル後の動作チェックで、tclパッケージが必要となりますので、
事前にインストールしておきましょう。
redisには、jemallocのライブラリが必要となるので、jemallocもコンパイルします。初めに、jemallocをコンパイルします。
cd ~ wget https://github.com/jemalloc/jemalloc/releases/download/5.2.1/jemalloc-5.2.1.tar.bz2 tar jxf jemalloc-5.2.1.tar.bz2 cd jemalloc-5.2.1/ ./configure make make install
redisをコンパイルする
cd ~ wget http://download.redis.io/releases/redis-5.0.7.tar.gz tar zxvf redis-5.0.7.tar.gz cd redis-5.0.7 make make test
make testでエラーが発生しました。
・・・・ ・・・・ !!! WARNING The following tests failed: *** [err]: Active defrag in tests/unit/memefficiency.tcl defrag didn't stop. Cleanup: may take some time... OK make[1]: *** [test] Error 1 make[1]: Leaving directory `/root/redis-5.0.7/src' make: *** [test] Error 2
再実行すると、全てのテストが成功しました。
make test ・・・・ ・・・・ \o/ All tests passed without errors! Cleanup: may take some time... OK make[1]: Leaving directory `/root/redis-5.0.7/src'
make installを実行して完了です。
redis-serverコマンドは、/usr/local/bin配下に配置されます。
make install
サービス登録するために
サービス登録するためのコマンドが用意されています。
install_server.shコマンドを使うようにしましょう。
cd ~/redis-5.0.7/utils ./install_server.sh Welcome to the redis service installer This script will help you easily set up a running redis server Please select the redis port for this instance: [6379] Selecting default: 6379 Please select the redis config file name [/etc/redis/6379.conf] Selected default - /etc/redis/6379.conf Please select the redis log file name [/var/log/redis_6379.log] Selected default - /var/log/redis_6379.log Please select the data directory for this instance [/var/lib/redis/6379] Selected default - /var/lib/redis/6379 Please select the redis executable path [] /usr/local/bin/redis-server Selected config: Port : 6379 Config file : /etc/redis/6379.conf Log file : /var/log/redis_6379.log Data dir : /var/lib/redis/6379 Executable : /usr/local/bin/redis-server Cli Executable : /usr/local/bin/redis-cli Is this ok? Then press ENTER to go on or Ctrl-C to abort. Copied /tmp/6379.conf => /etc/init.d/redis_6379 Installing service... Successfully added to chkconfig! Successfully added to runlevels 345! Starting Redis server... Installation successful!
サービスファイルは、redis_ポート番号.serviceで作成されます。
systemctl start redis_6379.service
最後に
とりあえず、インストールからサービス登録まで出来ました。
次は、プログラムからRedisに接続してデータのやり取りをやってみたいと思います。