編譯安裝redis-5.0.4


編譯安裝為redis官方推薦安裝方式。

本例中使用linux版本為:CentOS Linux release 7.0.1406 (Core),Basic Web Server

一、安裝依賴包

yum -y install gcc
yum -y install libc

libc安裝過程,出現Error:Nothing to do,但並沒有影響到接下來的redis安裝,通過whereis libc,發現libc已經存在。

二、下載redis安裝包

1、通過https://redis.io/download/下載redis最新穩定版本

2、使用wget獲取redis-stable版本

wget http://download.redis.io/redis-stable.tar.gz

通過http://download.redis.io/redis-stable.tar.gz獲取到的版本始終為當前的最新文檔版本。

以上兩種方式獲取到的版本是相同的。本例以下載redis-5.0.4.tar.gz為例

3、將redis-5.0.4上傳到linux上某位置,解壓到/usr/local下

tar -zxvf redis-5.0.4.tar.gz
mv redis-5.0.4 /usr/local/redis

4、進入到redis-5.0.4目錄

cd /usr/local/redis/

5、安裝

make && make install

當執行make install之后,在/usr/local/bin下將出現一些redis腳本

/usr/local/bin為環境變量,其下的腳本可以直接使用,故make進行安裝,make install為設置環境變量

6、執行測試(可選)

執行make test之前需要安裝tcl

yum -y install tcl
make test

make test出現如下異常

vim tests/unit/memefficiency.tcl

將150和100的值改大點,如1500,1000,重新執行make test

7、拷貝配置文件

cp redis.conf /etc/

8、修改配置

新建日志地址

mkdir -p /usr/local/redis/logs
touch  /usr/local/redis/logs/redis.log

vim /etc/redis.config

logfile "/usr/local/redis/logs/redis.log"
daemonize yes     ## 若使用開機啟動,生成pid,該項必須設置為誒yes,否則redis將不能夠正常執行開機啟動(systemctl start redis,執行后一直卡着,直到超時)
#bind 127.0.0.1  ##注釋掉,允許所有其他ip訪問,真實使用最好坐下限制,只允許某些主機訪問
protected-mode no ## 允許其他機器上的客戶端連接當前redis,配置文件設置該項,則開機啟動處就可以去掉--protected no
dir /usr/local/redis/ ## db保存路徑,由於db持久化到磁盤,占用空間可能比較大,所以選擇比較打的文件夾設置
requirepass 12345 ##設置密碼

9、設置開機啟動

vim /usr/lib/systemd/system/redis.service

添加如下內容

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

[Service]
ExecStart=/usr/local/bin/redis-server /etc/redis.conf --protected-mode no
ExecStop=/usr/local/bin/redis-cli shutdown
#Restart=always
Type=forking
#User=redis
#Group=redis
RuntimeDirectory=redis
RuntimeDirectoryMode=0755

[Install]
WantedBy=multi-user.target

Restart取值及含義

no(默認值):退出后不會重啟
on-success:只有正常退出時(退出狀態碼為0),才會重啟
on-failure:非正常退出時(退出狀態碼非0),包括被信號終止和超時,才會重啟
on-abnormal:只有被信號終止和超時,才會重啟
on-abort:只有在收到沒有捕捉到的信號終止時,才會重啟
on-watchdog:超時退出,才會重啟
always:不管是什么退出原因,總是重啟

Type字段取值及含義

simple(默認值):ExecStart字段啟動的進程為主進程
forking:ExecStart字段將以fork()方式啟動,此時父進程將會退出,子進程將成為主進程
oneshot:類似於simple,但只執行一次,Systemd 會等它執行完,才啟動其他服務
dbus:類似於simple,但會等待 D-Bus 信號后啟動
notify:類似於simple,啟動結束后會發出通知信號,然后 Systemd 再啟動其他服務
idle:類似於simple,但是要等到其他任務都執行完,才會啟動該服務。一種使用場合是為讓該服務的輸出,不與其他服務的輸出相混合

使配置生效

systemctl enable redis
systemctl daemon-reload

10、啟動redis

systemctl start redis

 11、啟動之后檢查redis啟動日志

vim /usr/local/redis/logs/redis.log

如有必要,根據WARNING中提示信息修改系統配置

83199:M 25 Mar 2019 09:23:29.873 # 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.
83199:M 25 Mar 2019 09:23:29.873 # 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.

 vim /etc/sysctl.conf

添加

vm.overcommit_memory=1
net.core.somaxconn = 1024

使配置生效

sysctl -p

進制內核中啟用Transparent Huge Pages(THP)支持,防止Redis延遲和內存使用問題

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

將該命令寫入/etc/rc.local中,使其重啟后也生效

重新啟動redis,檢查日志中是否還有異常

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM