環境情報
まずはOS情報です。以下の環境にインストールします。
$ uname -a Linux localhost.localdomain 3.10.0-229.el7.x86_64 #1 SMP Fri Mar 6 11:36:42 UTC 2015 x86_64 x86_64 x86_64 GNU/Linux $ cat /etc/redhat-release CentOS Linux release 7.1.1503 (Core)
Redisのインストール
公式サイトに沿ってファイルをダウンロードして展開します。
http://redis.io/download
$ wget http://download.redis.io/releases/redis-3.0.7.tar.gz $ tar zxf redis-3.0.7.tar.gz
必要なファイルのインストールをします。
$ cd redis-3.0.7 $ make $ sudo mkdir /etc/redis $ sudo cp redis.conf /etc/redis/6379.conf $ cd src $ sudo cp redis-server redis-cli redis-sentinel redis-benchmark redis-check-dump redis-check-aof /usr/local/bin => これでも可 find ./src -executable -type f | while read f;do sudo cp -ai $f /usr/local/bin;done
動作確認
まずはデフォルトの設定ファイルを指定して起動してみます。フォアグラウンドで起動して以下のようなメッセージが出てくるはずです。
$ redis-server /etc/redis/6379.conf 7465:M 17 Feb 08:15:10.419 # You requested maxclients of 10000 requiring at least 10032 max file descriptors. 7465:M 17 Feb 08:15:10.419 # Redis can't set maximum open files to 10032 because of OS error: Operation not permitted. 7465:M 17 Feb 08:15:10.419 # Current maximum open files is 4096. maxclients has been reduced to 4064 to compensate for low ulimit. If you need higher maxclients increase 'ulimit -n'. _._ _.-``__ ''-._ _.-`` `. `_. ''-._ Redis 3.0.7 (00000000/0) 64 bit .-`` .-```. ```\/ _.,_ ''-._ ( ' , .-` | `, ) Running in standalone mode |`-._`-...-` __...-.``-._|'` _.-'| Port: 6379 | `-._ `._ / _.-' | PID: 7465 `-._ `-._ `-./ _.-' _.-' |`-._`-._ `-.__.-' _.-'_.-'| | `-._`-._ _.-'_.-' | http://redis.io `-._ `-._`-.__.-'_.-' _.-' |`-._`-._ `-.__.-' _.-'_.-'| | `-._`-._ _.-'_.-' | `-._ `-._`-.__.-'_.-' _.-' `-._ `-.__.-' _.-' `-._ _.-' `-.__.-' 7465:M 17 Feb 08:15:10.420 # WARNING: The TCP backlog setting of 511 cannot be enforced because /proc/sys/net/core/somaxconn is set to the lower value of 128. 7465:M 17 Feb 08:15:10.420 # Server started, Redis version 3.0.7 7465:M 17 Feb 08:15:10.420 # WARNING overcommit_memory is set to 0! Background save may fail under low memory condition. To fix this issue add 'vm.overcommit_memory = 1' to /etc/sysctl.conf and then reboot or run the command 'sysctl vm.overcommit_memory=1' for this to take effect. 7465:M 17 Feb 08:15:10.420 # WARNING you have Transparent Huge Pages (THP) support enabled in your kernel. This will create latency and memory usage issues with Redis. To fix this issue run the command 'echo never > /sys/kernel/mm/transparent_hugepage/enabled' as root, and add it to your /etc/rc.local in order to retain the setting after a reboot. Redis must be restarted after THP is disabled. 7465:M 17 Feb 08:15:10.420 * The server is now ready to accept connections on port 6379
Port 6379で待っているので別コンソールからtelnetなどでアクセスすることができます。
$ telnet localhost 6379 Trying ::1... Connected to localhost. Escape character is '^]'.
単純なSETとGETで動きを確認してみます。
set server:name "myserver" +OK get server:name $8 myserver
終わったらquitで接続を閉じます。
quit +OK Connection closed by foreign host.
Redisを停止するため以下のコマンドを実行します。
$ redis-cli -p 6379 shutdown
そうすると、以下のようなメッセージが表示され、Redisがシャットダウンされます。
7543:M 17 Feb 08:23:50.170 # User requested shutdown... 7543:M 17 Feb 08:23:50.170 * Saving the final RDB snapshot before exiting. 7543:M 17 Feb 08:23:50.173 * DB saved on disk 7543:M 17 Feb 08:23:50.173 # Redis is now ready to exit, bye bye...
メッセージを読むとデータがSaveされたとのことがわかるので、カレントのディレクトリを見るとdump.rdbというファイルが生成されているのがわかります。これが保存されたデータベースファイル(スナップショット)です。
$ ls dump.rdb dump.rdb
もう一度同じコマンドで起動してみるとDBがディスクからロードされたという内容のメッセージが出ているのがわかります。
$ redis-server /etc/redis/6379.conf ... 19475:M 17 Feb 08:28:59.072 * DB loaded from disk: 0.000 seconds 19475:M 17 Feb 08:28:59.072 * The server is now ready to accept connections on port 6379 ...
今度はtelnetではなくredis-cliで接続して先ほどのデータが削除されていないことを確認してみます。
# 通信確認 $ redis-cli ping PONG # 対話モード $ redis-cli 127.0.0.1:6379> get server:name "myserver"
memcachedと違い、サービスを停止してもディスクに保存されたデータは消えないことがわかりました。保存のタイミングや、スナップショットファイルについては設定ファイルで設定可能です。
Redisではmemcachedと比較して多くのデータ操作コマンドが利用できます。コマンドの動作だけ手っ取り早く試したい場合は以下のサイトのチュートリアルをやってみてください。
http://try.redis.io/
systemdへ登録
Redisをsystemdへ登録していきます。
OS関連設定
Redisの実行ユーザと、データファイルとログのディレクトリを作成します。
# useradd redis # mkdir -p /var/lib/redis/6379 # mkdir -p /var/log/redis # mkdir -p /etc/redis # chown -R redis:redis /var/log/redis /var/lib/redis
設定ファイル調整
先ほどまではデフォルト設定で利用していた設定ファイルを確認してみます。
# vi /etc/redis/6379.conf
以下のように設定してみました。
daemonize no port 6379 save 900 1 save 300 10 save 60 10000 dir /var/lib/redis/6379/ dbfilename dump.rdb logfile /var/log/redis/redis_6379.log supervised systemd
設定内容説明です。
項目 | 説明 |
---|---|
daemonize | デーモン化する場合はyes(Defaultはno) |
port | 待ち受けポート(Defaultは6379) |
save <秒> <変更数> | <秒>の間に<変更数>の変更があればスナップショット |
dir | スナップショットの書き出しディレクトリ |
dbfilename | スナップショットのファイル名 |
logfile | ログファイルのパス |
supervise | 起動処理でsystemdとredisを連携させるType=notifyにする場合は必要(Type=simpleの場合は不要) |
設定パラメータはこのページに日本語で纏まってます。
設定ファイル — redis 2.0.3 documentation
systemdへの登録
systemdのユニットを設定します。
# vim /etc/systemd/system/redis.service
設定内容は以下の通りです。
[Unit] Description=Redis [Service] Type=notify ExecStart=/usr/local/bin/redis-server /etc/redis/6379.conf ExecStop=/usr/local/bin/redis-cli -p 6379 shutdown User=redis Group=redis [Install] WantedBy=multi-user.target
Systemd使って起動します。
# systemctl daemon-reload # systemctl start redis # systemctl status redis redis.service - Redis Loaded: loaded (/etc/systemd/system/redis.service; disabled) Active: active (running) since Sat 2016-02-20 01:46:08 UTC; 933ms ago Main PID: 3423 (redis-server) CGroup: /system.slice/redis.service mq3423 /usr/local/bin/redis-server *:6379 Feb 20 01:46:08 localhost.localdomain systemd[1]: Starting Redis... Feb 20 01:46:08 localhost.localdomain systemd[1]: Started Redis.