Centos7安裝Redis5.0.5並加入Systemd服務

1. 安裝gcc-c++, tclnode

yum install gcc-c++ tcl

2. 解壓縮, 編譯, 測試c++

tar zxvf redis-5.0.5.tar.gz
make
make test

3. 安裝至/optredis

make PREFIX=/opt/redis/redis-5.0.5 install
# 建立軟鏈
ln -s redis-5.0.5 latest

4. 配置文件, 在源文件目錄下有例子 redis.conf, 最後的配置內容爲(後半部分使用默認, 無改動)bash

[root@p01 ~]# cat /opt/redis/latest/conf/redis_16379.conf | grep -v '^$'|grep -v '^#'|grep -v '^;'
bind 192.168.123.32
protected-mode yes
port 16379
tcp-backlog 511
timeout 0
tcp-keepalive 300
daemonize yes
supervised auto
pidfile /var/run/redis_16379.pid
loglevel notice
logfile "/data/redis/logs/redis_16379.log"
databases 16
always-show-logo yes
save 900 1
save 300 10
save 60 10000
stop-writes-on-bgsave-error yes
rdbcompression yes
rdbchecksum yes
dbfilename dump.rdb
dir /data/redis/db/
replica-serve-stale-data yes
replica-read-only yes
repl-diskless-sync no
repl-diskless-sync-delay 5
repl-disable-tcp-nodelay no
replica-priority 100
requirepass rzczurzlx4xzs|yjpkdjzhljlevY0bLh
....

5. 在配置daemonize爲no的時候, 直接用命令行啓動測試, 觀察日誌輸出less

./bin/redis-server ./conf/redis_16379.conf

6. 解決warningtcp

對於 /proc/sys/net/core/somaxconn 和 overcommit_memory is set to 0, 修改/etc/sysctl.conf , 增長測試

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

而後執行 sysctl -pui

對於transparent_hugepage, 首先實時修改spa

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

檢查命令行

[root@middle ~]# cat /sys/kernel/mm/transparent_hugepage/enabled
[always] madvise never
[root@middle ~]# cat /sys/kernel/mm/transparent_hugepage/defrag
[always] madvise never
# 以上都須要變成never

加入啓動自動修改

# 在/etc/rc.local中增長以下內容

if test -f /sys/kernel/mm/transparent_hugepage/enabled; then
echo never > /sys/kernel/mm/transparent_hugepage/enabled
fi

if test -f /sys/kernel/mm/transparent_hugepage/defrag; then
echo never > /sys/kernel/mm/transparent_hugepage/defrag
fi

.

7. 加入Systemd服務. 增長 /lib/systemd/system.redis.service, 內容以下

[Unit]
Description=Redis
After=network.target

[Service]
Type=forking
PIDFile=/var/run/redis_16379.pid
ExecStart=/opt/redis/latest/bin/redis-server /opt/redis/latest/conf/redis_16379.conf
ExecReload=/bin/kill -s HUP $MAINPID
ExecStop=/opt/redis/latest/bin/redis-cli -p 16379 shutdown
PrivateTmp=true

[Install]
WantedBy=multi-user.target

注意: 這邊並無使用 #ExecStop=/bin/kill -s QUIT $MAINPID 這樣的命令來中止redis, 由於使用這個語句在運行 systemctl stop redis後, redis並未執行關閉動做, 而是直接退出. 這時候用 systemctl status redis 查看狀態是failed. 只有用ExecStop=/opt/redis/latest/bin/redis-cli -p 16379 shutdown 才能正確中止redis, 即便conf中配置了口令, 這裏也不須要指定口令. 

加入服務並啓動

systemctl enable redis.service
systemctl start redis
systemctl status redis

.

相關文章
相關標籤/搜索