編譯安裝Redis及使用systemd管理

環境

OS redis 版本 防火牆和selinux
CentOS7 4.0.14 關閉

安裝步驟

安裝前裝備

1.由於redis是用C編寫的,因此須要安裝gcc
#yum -y install gcc
2.下載redis源碼包
#wget -P /usr/local/src/ http://download.redis.io/releases/redis-4.0.14.tar.gz

編譯安裝

#cd /usr/local/src/
#tar xf redis-4.0.14.tar.gz
#cd redis-4.0.14
#make PREFIX=/apps/redis install #PREFIX表示指定redis的安裝目錄
#mkdir /apps/redis/{etc,logs,data,run} #建立配置文件、日誌、數據等目錄
#cp redis.conf /apps/redis/etc/

編輯redis服務啓動腳本,使用systemd管理該服務

注:該腳本是從yum安裝的redis生成的service文件拿過來稍做修改後用的

#cat > /usr/lib/systemd/system/redis.service <<EOF
[Unit]
Description=Redis persistent key-value database
After=network.target
After=network-online.target
Wants=network-online.target

[Service]
ExecStart=/apps/redis/bin/redis-server /apps/redis/etc/redis.conf --supervised systemd
ExecStop=/usr/libexec/redis-shutdown
Type=notify
User=redis
Group=redis
RuntimeDirectory=redis
RuntimeDirectoryMode=0755
LimitNOFILE=65536

[Install]
WantedBy=multi-user.target
EOF

注:該腳本中關閉redis服務是經過另外一個腳本/usr/libexec/redis-shutdown來實現的,yum安裝的redis有該腳本,若是是編譯安裝的redis沒有該腳本,內容以下:

#cat /usr/libexec/redis-shutdown 
#!/bin/bash
#
# Wrapper to close properly redis and sentinel
test x"$REDIS_DEBUG" != x && set -x

REDIS_CLI=/usr/bin/redis-cli

# Retrieve service name
SERVICE_NAME="$1"
if [ -z "$SERVICE_NAME" ]; then
   SERVICE_NAME=redis
fi

# Get the proper config file based on service name
CONFIG_FILE="/apps/redis/etc/$SERVICE_NAME.conf"

# Use awk to retrieve host, port from config file
HOST=`awk '/^[[:blank:]]*bind/ { print $2 }' $CONFIG_FILE | tail -n1`
PORT=`awk '/^[[:blank:]]*port/ { print $2 }' $CONFIG_FILE | tail -n1`
PASS=`awk '/^[[:blank:]]*requirepass/ { print $2 }' $CONFIG_FILE | tail -n1`
SOCK=`awk '/^[[:blank:]]*unixsocket\s/ { print $2 }' $CONFIG_FILE | tail -n1`

# Just in case, use default host, port
HOST=${HOST:-127.0.0.1}
if [ "$SERVICE_NAME" = redis ]; then
    PORT=${PORT:-6379}
else
    PORT=${PORT:-26739}
fi

# Setup additional parameters
# e.g password-protected redis instances
[ -z "$PASS"  ] || ADDITIONAL_PARAMS="-a $PASS"

# shutdown the service properly
if [ -e "$SOCK" ] ; then
    $REDIS_CLI -s $SOCK $ADDITIONAL_PARAMS shutdown
else
    $REDIS_CLI -h $HOST -p $PORT $ADDITIONAL_PARAMS shutdown
fi
#chmod +x /usr/libexec/redis-shutdown

建立redis用戶和設置相關權限

#groupadd -g 888 redis && useradd -r -u 888 -g 888 redis -s /sbin/nologin
#chown redis.redis -R /apps/redis/

建立軟鏈接

#ln -s /apps/redis/bin/* /usr/bin/
#ll /usr/bin/redis-*
lrwxrwxrwx 1 root root 31 Feb  9 21:34 /usr/bin/redis-benchmark -> /apps/redis/bin/redis-benchmark #redis性能測試工具
lrwxrwxrwx 1 root root 31 Feb  9 21:34 /usr/bin/redis-check-aof -> /apps/redis/bin/redis-check-aof #AOF文件檢查工具
lrwxrwxrwx 1 root root 31 Feb  9 21:34 /usr/bin/redis-check-rdb -> /apps/redis/bin/redis-check-rdb #RDB文件檢查工具
lrwxrwxrwx 1 root root 25 Feb  9 21:34 /usr/bin/redis-cli -> /apps/redis/bin/redis-cli #redis客戶端工具
lrwxrwxrwx 1 root root 30 Feb  9 21:34 /usr/bin/redis-sentinel -> /apps/redis/bin/redis-sentinel #哨兵,軟鏈接到server
lrwxrwxrwx 1 root root 28 Feb  9 21:34 /usr/bin/redis-server -> /apps/redis/bin/redis-server #redis服務啓動命令

更改rdb文件存放的目錄路徑和設置相關權限

#sed -i '/^dir/s#./#/apps/redis/data#' /apps/redis/etc/redis.conf

驗證redis啓動

編譯安裝Redis及使用systemd管理

解決redis的錯誤和警告

Failed opening the RDB file dump.rdb (in server root dir /) for saving: Permission denied

參考資料:https://stackoverflow.com/questions/22160753/redis-failed-opening-rdb-for-saving-permission-deniedlinux

執行systemctl stop redis命令的時候,在/var/log/messages日誌中發現上面的錯誤,出現該錯誤的緣由是由於關閉redis以前須要將數據保存到rdb文件中,可是由於權限緣由致使報錯,解決方法以下:
在redis的配置文件中指定保存rdb文件的目錄,並確保相關的權限正確
#ll /apps/redis/data/ -d
drwxr-xr-x 2 redis redis 22 Feb 11 20:33 /apps/redis/data/
#vim /apps/redis/etc/redis.conf
dir /apps/redis/data

Socket Maximum Connection

參考資料:https://stackoverflow.com/questions/36880321/why-redis-can-not-set-maximum-open-fileredis

WARNING: The TCP backlog setting of 511 cannot be enforced because /proc/sys/net/core/somaxconn is set to the lower value of 128.

backlog參數控制的是三次握手的時候server端收到client ack確認號以後的隊列值。

Memory Overcommit

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.

0:表示內核將檢查是否有足夠的可用內存供應用進程使用;若是有足夠的可用內存,內存申請容許;不然,內存申請失敗,並把錯誤返回給應用進程。 
1:表示內核容許分配全部的物理內存,而無論當前的內存狀態如何。 
2:表示內核容許分配超過全部物理內存和交換空間總和的內存

因爲這兩個是相關的,因此一塊解決

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

要使這些配置生效,須要從新加載配置

#sysctl -p

Transparent Huge Pages

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.

大頁內存動態分配,須要關閉讓redis負責內存管理。

臨時生效

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

要永久解決此問題,請遵循日誌的建議並修改rc.local

#vim /etc/rc.local
if test -f /sys/kernel/mm/transparent_hugepage/enabled; then
    echo never > /sys/kernel/mm/transparent_hugepage/enabled
fi
#chmod +x /etc/rc.d/rc.local
相關文章
相關標籤/搜索