企業級Redis安裝部署(非常詳細,更適合學習使用)

前言

此篇安裝Redis適用於企業,對於安裝環境進行了優化,使Redis能夠發揮更好的性能。

更新GCC

系統版本CentOS7.7

cat /etc/redhat-release 
CentOS Linux release 7.7.1908 (Core)

GCC版本4.8.5

gcc --version
gcc (GCC) 4.8.5 20150623 (Red Hat 4.8.5-39)
Copyright © 2015 Free Software Foundation, Inc.
本程序是自由軟件;請參看源代碼的版權聲明。本軟件沒有任何擔保;
包括沒有適銷性和某一專用目的下的適用性擔保。

其中GCC版本爲4.8.5版本,而安裝Redis6.0需要GCC5.0版本以上,若GCC版本不足,則會造成編譯Redis報錯。

注:使用網絡yum源,默認網絡源即可

通過scl軟件集升級GCC
1)安裝scl源

yum install centos-release-scl scl-utils-build -y

2)安裝8版本的gcc、gcc-c++、gdb工具鏈(toolchian)

yum install -y devtoolset-8-toolchain

3)這個時候,GCC的版本還沒有更改,刷新一下環境,使其生效。

scl enable devtoolset-8 bash

4)查看gcc版本

gcc --version
gcc (GCC) 8.3.1 20190311 (Red Hat 8.3.1-3)
Copyright (C) 2018 Free Software Foundation, Inc.
This is free software; see the source for copying conditions.  There is NO
warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.

現在gcc版本已經更新到了8.3.1

下載安裝Redis

網址:http://download.redis.io/releases/

在這裏插入圖片描述
根據需求,下載指定版本的Redis,網站中包含Redis2.X到6.X。

注:本安裝以redis-6.0.5.tar.gz爲例。

在這裏插入圖片描述
複製鏈接地址,然後通過wget下載

wget http://download.redis.io/releases/redis-6.0.5.tar.gz

解壓,編譯,安裝

tar xf redis-6.0.5.tar.gz 
cd redis-6.0.5/
make -j4
cd src/
make install PREFIX=/usr/local/redis-6.0.5

創建Redis配置文件存放目錄,複製配置文件

mkdir /usr/local/redis-6.0.5/etc
cd ..
cp redis.conf /usr/local/redis-6.0.5/etc/

系統環境優化

通過腳本加載配置文件,啓動Redis

/usr/local/redis-6.0.5/bin/redis-server /usr/local/redis-6.0.5/etc/redis.conf 

接下來就會看到一些輸出信息,如下

16364:C 10 Jun 2020 17:18:46.460 # oO0OoO0OoO0Oo Redis is starting oO0OoO0OoO0Oo
16364:C 10 Jun 2020 17:18:46.460 # Redis version=6.0.5, bits=64, commit=00000000, modified=0, pid=16364, just started
16364:C 10 Jun 2020 17:18:46.460 # Configuration loaded
16364:M 10 Jun 2020 17:18:46.461 * Increased maximum number of open files to 10032 (it was originally set to 1024).
                _._                                                  
           _.-``__ ''-._                                             
      _.-``    `.  `_.  ''-._           Redis 6.0.5 (00000000/0) 64 bit
  .-`` .-```.  ```\/    _.,_ ''-._                                   
 (    '      ,       .-`  | `,    )     Running in standalone mode
 |`-._`-...-` __...-.``-._|'` _.-'|     Port: 6379
 |    `-._   `._    /     _.-'    |     PID: 16364
  `-._    `-._  `-./  _.-'    _.-'                                   
 |`-._`-._    `-.__.-'    _.-'_.-'|                                  
 |    `-._`-._        _.-'_.-'    |           http://redis.io        
  `-._    `-._`-.__.-'_.-'    _.-'                                   
 |`-._`-._    `-.__.-'    _.-'_.-'|                                  
 |    `-._`-._        _.-'_.-'    |                                  
  `-._    `-._`-.__.-'_.-'    _.-'                                   
      `-._    `-.__.-'    _.-'                                       
          `-._        _.-'                                           
              `-.__.-'                                               

16364:M 10 Jun 2020 17:18:46.461 # WARNING: The TCP backlog setting of 511 cannot be enforced because /proc/sys/net/core/somaxconn is set to the lower value of 128.
16364:M 10 Jun 2020 17:18:46.461 # Server initialized
16364:M 10 Jun 2020 17:18:46.461 # 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.
16364:M 10 Jun 2020 17:18:46.461 # 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.
16364:M 10 Jun 2020 17:18:46.461 * Ready to accept connections

通過翻譯可得知,系統環境需要進行優化:

1)將最大打開文件數增加到10032(原設置爲1024)。

2)/proc/sys/net/core/somaxconn被設置爲較低的值128,建議設置爲511。

3)警告overcommit_memory被設置爲0!在內存不足的情況下,後臺保存可能會失敗。要解決這個問題,添加’vm。overcommit_memory = 1’到/etc/sysctl.conf,然後重新啓動或運行命令’sysctl vm。overcommit_memory=1’使其生效。

4)警告您的內核啓用了透明巨頁(THP)支持。這將造成延時和內存使用問題與Redis。要解決這個問題,運行命令’echo never > /sys/kernel/mm/transparent_hugepage/enabled’作爲根用戶,並把它添加到你的/etc/rc.local,以便在重新啓動後保留該設置。Redis必須重新啓動後,THP是禁用的。

我們來依次進行優化,按照提示執行相應的操作。

1.修改文件最大打開數

通過ulimit -n查看系統最大打開數

ulimit -n
1024

修改limits.conf文件,在最後添加兩行配置項項

vim /etc/security/limits.conf
*       soft    nofile  10032
*       hard    nofile  10032

修改/etc/pam.d/login,在最後添加一行配置項

session    required     /usr/lib64/security/pam_limits.so

用戶登出,重新登錄生效

logout	#登出

重新登錄後,查看文件最大打開數

ulimit -n
10032

2&3修改/etc/sysctl.conf文件

修改/etc/sysctl.conf 文件,並在最後添加兩行配置項

vim /etc/sysctl.conf 
net.core.somaxconn = 511
vm.overcommit_memory = 1

執行sysctl -p,使其配置項立即生效,否則需要重啓

sysctl -p
net.core.somaxconn = 511
vm.overcommit_memory = 1

4.關閉透明巨頁

直接按照提示執行命令即可,根據提示:運行命令’echo never > /sys/kernel/mm/transparent_hugepage/enabled’。

echo never > /sys/kernel/mm/transparent_hugepage/enabled

啓動Redis

再次通過腳本加載配置文件啓動Redis。

/usr/local/redis-6.0.5/bin/redis-server /usr/local/redis-6.0.5/etc/redis.conf 

通過翻譯可得知,已沒有任何注意事項

16806:C 10 Jun 2020 17:37:46.045 # oO0OoO0OoO0Oo Redis is starting oO0OoO0Oo0Oo
16806:C 10 Jun 2020 17:37:46.045 # Redis version=6.0.5, bits=64, commit=0000000, modified=0, pid=16806, just started
16806:C 10 Jun 2020 17:37:46.045 # Configuration loaded
                _._                                                  
           _.-``__ ''-._                                             
      _.-``    `.  `_.  ''-._           Redis 6.0.5 (00000000/0) 64 bit
  .-`` .-```.  ```\/    _.,_ ''-._                                   
 (    '      ,       .-`  | `,    )     Running in standalone mode
 |`-._`-...-` __...-.``-._|'` _.-'|     Port: 6379
 |    `-._   `._    /     _.-'    |     PID: 16806
  `-._    `-._  `-./  _.-'    _.-'                                   
 |`-._`-._    `-.__.-'    _.-'_.-'|                                  
 |    `-._`-._        _.-'_.-'    |           http://redis.io        
  `-._    `-._`-.__.-'_.-'    _.-'                                   
 |`-._`-._    `-.__.-'    _.-'_.-'|                                  
 |    `-._`-._        _.-'_.-'    |                                  
  `-._    `-._`-.__.-'_.-'    _.-'                                   
      `-._    `-.__.-'    _.-'                                       
          `-._        _.-'                                           
              `-.__.-'                                               

16806:M 10 Jun 2020 17:37:46.046 # Server initialized
16806:M 10 Jun 2020 17:37:46.047 * Loading RDB produced by version 6.0.5
16806:M 10 Jun 2020 17:37:46.047 * RDB age 893 seconds
16806:M 10 Jun 2020 17:37:46.047 * RDB memory usage when created 0.77 Mb
16806:M 10 Jun 2020 17:37:46.047 * DB loaded from disk: 0.000 seconds
16806:M 10 Jun 2020 17:37:46.047 * Ready to accept connections

Redis默認是前臺運行,也就是會佔用前臺bash,導致我們無法在當前命令行執行其他操作。通過修改配置文件或在執行命令後加&即可轉爲後臺運行,建議修改配置文件。

對於初安裝的配置文件,添加以下配置項即可,後續可以根據需求自行設置。

vim /usr/local/redis-6.0.5/etc/redis.conf 
bind 127.0.0.1 192.168.254.130	#主機ip
daemonize yes	#是否作爲守護進程進行
port 6379		#服務端口
logfile "/usr/local/redis-6.0.5/log/redis.log"	#Redis日誌文件路徑
dir /data/redis		#redis數據存放路徑

創建文件與目錄,用於存放日誌與數據

mkdir -p /data/redis
mkdir /usr/local/redis-6.0.5/log
touch /usr/local/redis-6.0.5/log/redis.log

啓動redis

/usr/local/redis-6.0.5/bin/redis-server /usr/local/redis-6.0.5/etc/redis.conf 

查看Redis的端口號是否開啓

netstat -anput | grep redis
tcp        0      0 192.168.254.132:6379    0.0.0.0:*               LISTEN      17080/redis-server  
tcp        0      0 127.0.0.1:6379          0.0.0.0:*               LISTEN      17080/redis-server  

查看Redis進程是否存在

ps -ef |grep redis | grep -v grep
root      17080      1  0 17:48 ?        00:00:00 /usr/local/redis-6.0.5/bin/redis-server 127.0.0.1:6379

登錄測試

通過Redis安裝的bin/目錄下的redis-cli 客戶端,可直接登錄Redis。

 /usr/local/redis-6.0.5/bin/redis-cli 
127.0.0.1:6379> 
127.0.0.1:6379> exit

不指定IP地址,默認是以本機地址登錄,可以通過-h選項,加上主機IP登錄Redis。

/usr/local/redis-6.0.5/bin/redis-cli -h 192.168.254.132
192.168.254.132:6379> 
192.168.254.132:6379> exit

Redis環境優化

關於Redis的bin目錄下的幾個工具

ls /usr/local/redis-6.0.5/bin/
redis-benchmark  redis-check-rdb  redis-sentinel
redis-check-aof  redis-cli        redis-server
文件 說明
redis-benchmark Redis基準/性能測試工具
redis-check-aof Redis Append Only Files(AOF)檢查工具
redis-check-rdb Redis RDB檢查工具
redis-cli Redis命令行工具
redis-sentinel redis-server的軟鏈接
redis-server redis服務端

將Redis的bin目錄軟件一個軟連接

ln -s /usr/local/redis-6.0.5/bin/* /usr/local/bin/

最後添加開機自啓

chmod +x /etc/rc.d/rc.local
echo " /usr/local/redis/bin/redis-server /usr/local/redis/etc/redis.conf" >> /etc/rc.d/rc.local

關閉Redis

關閉Redis的兩種方法:通過Redis客戶端關閉,殺死進程關閉(不推薦)。

方式1:通過Redis客戶端關閉

redis-cli shutdown
netstat -anput | grep redis

方式2:強制殺死Redis進程

pkill redis-server
netstat -anput | grep redis
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章