CentOSで複数のRedisサーバーを立ち上げる

※rootユーザーではないredisユーザー(wheel)でログインしています

Redisをインストール

$ sudo yum install redis
読み込んだプラグイン:fastestmirror, langpacks
Loading mirror speeds from cached hostfile
* base: ty1.mirror.newmediaexpress.com
* epel: nrt.edge.kernel.org
* epel-debuginfo: nrt.edge.kernel.org
* epel-source: nrt.edge.kernel.org
* extras: ty1.mirror.newmediaexpress.com
* updates: ty1.mirror.newmediaexpress.com
依存性の解決をしています
--> トランザクションの確認を実行しています。
---> パッケージ redis.x86_64 0:3.2.12-2.el7 を インストール
--> 依存性の処理をしています: libjemalloc.so.1()(64bit) のパッケージ: redis-3.2.12-2.el7.x86_64
--> トランザクションの確認を実行しています。
---> パッケージ jemalloc.x86_64 0:3.6.0-1.el7 を インストール
--> 依存性解決を終了しました。

依存性を解決しました

================================================================================
Package           アーキテクチャー
                                  バージョン               リポジトリー   容量
================================================================================
インストール中:
redis             x86_64          3.2.12-2.el7             epel          544 k
依存性関連でのインストールをします:
jemalloc          x86_64          3.6.0-1.el7              epel          105 k

トランザクションの要約
================================================================================
インストール  1 パッケージ (+1 個の依存関係のパッケージ)

総ダウンロード容量: 648 k
インストール容量: 1.7 M
Is this ok [y/d/N]: y
Downloading packages:
警告: /var/cache/yum/x86_64/7/epel/packages/jemalloc-3.6.0-1.el7.x86_64.rpm: ヘ ッダー V3 RSA/SHA256 Signature、鍵 ID 352c64e5: NOKEY
jemalloc-3.6.0-1.el7.x86_64.rpm の公開鍵がインストールされていません
(1/2): jemalloc-3.6.0-1.el7.x86_64.rpm                     | 105 kB   00:01     
(2/2): redis-3.2.12-2.el7.x86_64.rpm                       | 544 kB   00:00     
--------------------------------------------------------------------------------
合計                                               396 kB/s | 648 kB  00:01     
file:///etc/pki/rpm-gpg/RPM-GPG-KEY-EPEL-7 から鍵を取得中です。
Importing GPG key 0x352C64E5:
Userid     : "Fedora EPEL (7) <epel@fedoraproject.org>"
Fingerprint: 91e9 7d7c 4a5e 96f1 7f3e 888f 6a2f aea2 352c 64e5
Package    : epel-release-7-11.noarch (@extras)
From       : /etc/pki/rpm-gpg/RPM-GPG-KEY-EPEL-7
上記の処理を行います。よろしいでしょうか? [y/N]y
Running transaction check
Running transaction test
Transaction test succeeded
Running transaction
 インストール中          : jemalloc-3.6.0-1.el7.x86_64                     1/2 
 インストール中          : redis-3.2.12-2.el7.x86_64                       2/2 
 検証中                  : redis-3.2.12-2.el7.x86_64                       1/2 
 検証中                  : jemalloc-3.6.0-1.el7.x86_64                     2/2 

インストール:
 redis.x86_64 0:3.2.12-2.el7                                                   

依存性関連をインストールしました:
 jemalloc.x86_64 0:3.6.0-1.el7                                                 

完了しました!

Redisのワークディレクトリを作成

$ sudo mkdir /var/run/redis/ && sudo chown redis: /var/run/redis/
$ sudo mkdir /var/log/redis/ && sudo chown redis: /var/log/redis/

【1台目】設定ファイルのコピー

$ sudo cp -uvp /etc/redis.conf /etc/redis_6379.conf
`/etc/redis.conf' -> `/etc/redis_6379.conf'

【1台目】ファイルオーナーの変更

$ sudo mkdir /var/lib/redis_6379 && sudo chown redis: /var/lib/redis_6379

【1台目】設定ファイルの修正

$ sudo vi /etc/redis_6379.conf

...

# Accept connections on the specified port, default is 6379 (IANA #815344).
# If port 0 is specified Redis will not listen on a TCP socket.
port 6379

...

# If a pid file is specified, Redis writes it where specified at startup
# and removes it at exit.
#
# When the server runs non daemonized, no pid file is created if none is
# specified in the configuration. When the server is daemonized, the pid file
# is used even if not specified, defaulting to "/var/run/redis.pid".
#
# Creating a pid file is best effort: if Redis is not able to create it
# nothing bad happens, the server will start and run normally.
pidfile /var/run/redis/redis_6379.pid

...

# Specify the log file name. Also the empty string can be used to force
# Redis to log on the standard output. Note that if you use standard
# output for logging but daemonize, logs will be sent to /dev/null
logfile /var/log/redis/redis_6379.log

...

# The working directory.
#
# The DB will be written inside this directory, with the filename specified
# above using the 'dbfilename' configuration directive.
#
# The Append Only File will also be created inside this directory.
#
# Note that you must specify a directory here, not a file name.
dir /var/lib/redis_6379/​

【1台目】systemctlサービス起動ファイルのコピーと変更

$ sudo cp -upv /usr/lib/systemd/system/redis.service /usr/lib/systemd/system/redis_6379.service
`/usr/lib/systemd/system/redis.service' -> `/usr/lib/systemd/system/redis_6379.service'

$ sudo vi /usr/lib/systemd/system/redis_6379.service

[Unit]
Description=Redis persistent key-value database
After=network.target
After=network-online.target
Wants=network-online.target

[Service]
ExecStart=/usr/bin/redis-server /etc/redis_6379.conf --supervised systemd
ExecStop=/usr/bin/redis-cli -p 6379 shutdown
Type=notify
User=redis
Group=redis
RuntimeDirectory=redis
RuntimeDirectoryMode=0755

[Install]
WantedBy=multi-user.target

【1台目】起動設定

$ sudo systemctl stop redis.service

$ sudo systemctl disable redis.service

$ systemctl start redis_6379.service
==== AUTHENTICATING FOR org.freedesktop.systemd1.manage-units ===
Authentication is required to manage system services or units.
Authenticating as: redis
Password: [パスワード入れてね]
==== AUTHENTICATION COMPLETE ===

$ systemctl enable redis_6379.service
==== AUTHENTICATING FOR org.freedesktop.systemd1.manage-unit-files ===
Authentication is required to manage system service or unit files.
Authenticating as: redis
Password: [パスワード入れてね]
==== AUTHENTICATION COMPLETE ===
Created symlink from /etc/systemd/system/multi-user.target.wants/redis_6379.service to /usr/lib/systemd/system/redis_6379.service.
==== AUTHENTICATING FOR org.freedesktop.systemd1.reload-daemon ===
Authentication is required to reload the systemd state.
Authenticating as: redis
Password: [パスワード入れてね]
==== AUTHENTICATION COMPLETE ===

【2台目】設定ファイルのコピー

$ sudo cp -uvp /etc/redis.conf /etc/redis_6380.conf
`/etc/redis.conf' -> `/etc/redis_6380.conf'

【2台目】ファイルオーナーの変更

$ sudo mkdir /var/lib/redis_6380 && sudo chown redis: /var/lib/redis_6380

【2台目】設定ファイルの修正

$ sudo vi /etc/redis_6380.conf

...

# Accept connections on the specified port, default is 6379 (IANA #815344).
# If port 0 is specified Redis will not listen on a TCP socket.
port 6380

...

# If a pid file is specified, Redis writes it where specified at startup
# and removes it at exit.
#
# When the server runs non daemonized, no pid file is created if none is
# specified in the configuration. When the server is daemonized, the pid file
# is used even if not specified, defaulting to "/var/run/redis.pid".
#
# Creating a pid file is best effort: if Redis is not able to create it
# nothing bad happens, the server will start and run normally.
pidfile /var/run/redis/redis_6380.pid

...

# Specify the log file name. Also the empty string can be used to force
# Redis to log on the standard output. Note that if you use standard
# output for logging but daemonize, logs will be sent to /dev/null
logfile /var/log/redis/redis_6380.log

...

# The working directory.
#
# The DB will be written inside this directory, with the filename specified
# above using the 'dbfilename' configuration directive.
#
# The Append Only File will also be created inside this directory.
#
# Note that you must specify a directory here, not a file name.
dir /var/lib/redis_6379/

【2台目】systemctlサービス起動ファイルのコピーと変更

$ sudo cp -upv /usr/lib/systemd/system/redis.service /usr/lib/systemd/system/redis_6380.service
`/usr/lib/systemd/system/redis.service' -> `/usr/lib/systemd/system/redis_6380.service'

$ sudo vi /usr/lib/systemd/system/redis_6380.service

[Unit]
Description=Redis persistent key-value database
After=network.target
After=network-online.target
Wants=network-online.target

[Service]
ExecStart=/usr/bin/redis-server /etc/redis_6380.conf --supervised systemd
ExecStop=/usr/bin/redis-cli -p 6380 shutdown
Type=notify
User=redis
Group=redis
RuntimeDirectory=redis
RuntimeDirectoryMode=0755

[Install]
WantedBy=multi-user.target

【2台目】起動設定

$ sudo systemctl stop redis.service

$ sudo systemctl disable redis.service

$ systemctl start redis_6380.service
==== AUTHENTICATING FOR org.freedesktop.systemd1.manage-units ===
Authentication is required to manage system services or units.
Authenticating as: redis
Password: [パスワード入れてね]
==== AUTHENTICATION COMPLETE ===

$ systemctl enable redis_6380.service
==== AUTHENTICATING FOR org.freedesktop.systemd1.manage-unit-files ===
Authentication is required to manage system service or unit files.
Authenticating as: redis
Password: [パスワード入れてね]
==== AUTHENTICATION COMPLETE ===
Created symlink from /etc/systemd/system/multi-user.target.wants/redis_6379.service to /usr/lib/systemd/system/redis_6379.service.
==== AUTHENTICATING FOR org.freedesktop.systemd1.reload-daemon ===
Authentication is required to reload the systemd state.
Authenticating as: redis
Password: [パスワード入れてね]
==== AUTHENTICATION COMPLETE ===

接続テスト

$ redis-cli -p 6379
127.0.0.1:6379> set key "hoge"
OK
127.0.0.1:6379> get key
"hoge"
127.0.0.1:6379> del key
(integer) 1
127.0.0.1:6379> get key
(nil)

$ redis-cli -p 6380
127.0.0.1:6380> set key "hoge"
OK
127.0.0.1:6380> get key
"hoge"
127.0.0.1:6380> del key
(integer) 1
127.0.0.1:6380> get key
(nil)


いいなと思ったら応援しよう!