centos9 Redisをインストールする手順

centos9 Redisをインストールする手順

centos9に、nosqlであるRedisをインストールする手順です。dnfからインストールする方法とリポジトリを追加して最新版をインストールする方法の2つパターンを掲載してます。起動してから外部接続可能にするまでの手順も掲載してます。

環境

  • OS  CentOS Stream release 9

Redisインストール

redisをインストールします。
※最新版のインストールは、後述してます。

$ sudo dnf install -y redis

==================================================================================================================================================== パッケージ                      アーキテクチャー                 バージョン                              リポジトリー                        サイズ
====================================================================================================================================================インストール:
 redis                           x86_64                           6.2.7-1.el9                             appstream                           1.3 M

トランザクションの概要
====================================================================================================================================================インストール  1 パッケージ

ダウンロードサイズの合計: 1.3 M
インストール後のサイズ: 4.7 M
パッケージのダウンロード:
redis-6.2.7-1.el9.x86_64.rpm                                                                                        144 kB/s | 1.3 MB     00:09    
----------------------------------------------------------------------------------------------------------------------------------------------------合計                                                                                                                 94 kB/s | 1.3 MB     00:14     
トランザクションの確認を実行中
トランザクションの確認に成功しました。
トランザクションのテストを実行中
トランザクションのテストに成功しました。
トランザクションを実行中
  準備             :                                                                                                                            1/1 
  scriptletの実行中: redis-6.2.7-1.el9.x86_64                                                                                                   1/1 
  インストール中   : redis-6.2.7-1.el9.x86_64                                                                                                   1/1 
  scriptletの実行中: redis-6.2.7-1.el9.x86_64                                                                                                   1/1 
  検証             : redis-6.2.7-1.el9.x86_64                                                                                                   1/1 

インストール済み:
  redis-6.2.7-1.el9.x86_64                                                                                                                          

完了しました!

自動起動と起動を設定しておきます。

$ sudo systemctl enable --now redis

Created symlink /etc/systemd/system/multi-user.target.wants/redis.service → /usr/lib/systemd/system/redis.service.

ステータスを確認します。

$ sudo systemctl status redis

● redis.service - Redis persistent key-value database
     Loaded: loaded (/usr/lib/systemd/system/redis.service; enabled; vendor preset: disabled)
    Drop-In: /etc/systemd/system/redis.service.d
             └─limit.conf
     Active: active (running) since Tue 2022-08-23 12:06:46 JST; 13s ago
   Main PID: 80889 (redis-server)
     Status: "Ready to accept connections"
      Tasks: 5 (limit: 21898)
     Memory: 7.3M
        CPU: 29ms
     CGroup: /system.slice/redis.service
             └─80889 "/usr/bin/redis-server 127.0.0.1:6379"

 8月 23 12:06:46 localhost.localdomain systemd[1]: Starting Redis persistent key-value database...
 8月 23 12:06:46 localhost.localdomain systemd[1]: Started Redis persistent key-value database.

バージョンは、以下のコマンドで確認可能です。

$ redis-server -v

Redis server v=6.2.7 sha=00000000:0 malloc=jemalloc-5.1.0 bits=64 build=ec192bdd77ecd321

最新版をインストール

最新版をインストールする場合は、リポジトリを追加します。

$ sudo dnf config-manager --set-enabled crb

$ sudo dnf install \
    https://dl.fedoraproject.org/pub/epel/epel-release-latest-9.noarch.rpm \
    https://dl.fedoraproject.org/pub/epel/epel-next-release-latest-9.noarch.rpm

$ sudo dnf install dnf-utils http://rpms.remirepo.net/enterprise/remi-release-9.rpm -y

$ sudo dnf module list redis -y

Remi's Modular repository for Enterprise Linux 9 - x86_64
Name                        Stream                         Profiles                         Summary                                                 
redis                       remi-5.0                       common [d]                       Redis persistent key-value database                     
redis                       remi-6.0                       common [d]                       Redis persistent key-value database                     
redis                       remi-6.2                       common [d]                       Redis persistent key-value database                     
redis                       remi-7.0                       common [d]                       Redis persistent key-value database                     

ヒント: [d]efault, [e]nabled, [x]disabled, [i]nstalled

$ sudo dnf module enable redis:remi-7.0 -y

$ redis-server -v
Redis server v=7.0.4 sha=00000000:0 malloc=jemalloc-5.2.1 bits=64 build=222b5ae120437328

Redis起動

インストールが終われば、Redisを使用することが可能になります。

$ redis-cli

127.0.0.1:6379> set foo bar
OK

127.0.0.1:6379> get foo
"bar"

127.0.0.1:6379> exit

設定ファイル

設定ファイルは、以下に存在します。

/etc/redis/redis.conf

外部接続許可

外部からの接続を許可してみます。
「bind 127.0.0.1 -::1」をコメントアウトして「bind 0.0.0.0」を追加します。

$ sudo nano /etc/redis/redis.conf

bind 0.0.0.0
# bind * -::*                     # like the default, all available interfaces
#
# ~~~ WARNING ~~~ If the computer running Redis is directly exposed to the
# internet, binding to all the interfaces is dangerous and will expose the
# instance to everybody on the internet. So by default we uncomment the
# following bind directive, that will force Redis to listen only on the
# IPv4 and IPv6 (if available) loopback interface addresses (this means Redis
# will only be able to accept client connections from the same host that it is
# running on).
#
# IF YOU ARE SURE YOU WANT YOUR INSTANCE TO LISTEN TO ALL THE INTERFACES
# JUST COMMENT OUT THE FOLLOWING LINE.
# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
#bind 127.0.0.1 -::1

「password」を設定する場合は、以下を追加します。

requirepass <任意のパスワード>

再起動します。

$ sudo systemctl restart redis

firewall設定していれば、ポート「6379」を許可しておきます。

$ sudo firewall-cmd --permanent --add-port=6379/tcp
success

$ sudo firewall-cmd --reload
success

これで外部から接続可能になります。