溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊(cè)×
其他方式登錄
點(diǎn)擊 登錄注冊(cè) 即表示同意《億速云用戶服務(wù)條款》

編譯安裝Redis及使用systemd管理

發(fā)布時(shí)間:2020-03-03 17:10:46 來(lái)源:網(wǎng)絡(luò) 閱讀:1050 作者:何小帥 欄目:系統(tǒng)運(yùn)維

環(huán)境

OS redis 版本 防火墻和selinux
CentOS7 4.0.14 關(guān)閉

安裝步驟

安裝前裝備

1.因?yàn)閞edis是用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} #創(chuàng)建配置文件、日志、數(shù)據(jù)等目錄
#cp redis.conf /apps/redis/etc/

編輯redis服務(wù)啟動(dòng)腳本,使用systemd管理該服務(wù)

注:該腳本是從yum安裝的redis生成的service文件拿過(guò)來(lái)稍作修改后用的

#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

注:該腳本中關(guān)閉redis服務(wù)是通過(guò)另一個(gè)腳本/usr/libexec/redis-shutdown來(lái)實(shí)現(xiàn)的,yum安裝的redis有該腳本,如果是編譯安裝的redis沒(méi)有該腳本,內(nèi)容如下:

#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

創(chuàng)建redis用戶和設(shè)置相關(guān)權(quán)限

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

創(chuàng)建軟連接

#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性能測(cè)試工具
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服務(wù)啟動(dòng)命令

更改rdb文件存放的目錄路徑和設(shè)置相關(guān)權(quán)限

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

驗(yàn)證redis啟動(dòng)

編譯安裝Redis及使用systemd管理

解決redis的錯(cuò)誤和警告

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-denied

執(zhí)行systemctl stop redis命令的時(shí)候,在/var/log/messages日志中發(fā)現(xiàn)上面的錯(cuò)誤,出現(xiàn)該錯(cuò)誤的原因是因?yàn)殛P(guān)閉redis之前需要將數(shù)據(jù)保存到rdb文件中,但是因?yàn)闄?quán)限原因?qū)е聢?bào)錯(cuò),解決方法如下:
在redis的配置文件中指定保存rdb文件的目錄,并確保相關(guān)的權(quán)限正確
#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-file

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參數(shù)控制的是三次握手的時(shí)候server端收到client ack確認(rèn)號(hào)之后的隊(duì)列值。

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:表示內(nèi)核將檢查是否有足夠的可用內(nèi)存供應(yīng)用進(jìn)程使用;如果有足夠的可用內(nèi)存,內(nèi)存申請(qǐng)?jiān)试S;否則,內(nèi)存申請(qǐng)失敗,并把錯(cuò)誤返回給應(yīng)用進(jìn)程。 
1:表示內(nèi)核允許分配所有的物理內(nèi)存,而不管當(dāng)前的內(nèi)存狀態(tài)如何。 
2:表示內(nèi)核允許分配超過(guò)所有物理內(nèi)存和交換空間總和的內(nèi)存

由于這兩個(gè)是相關(guān)的,所以一塊解決

#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.

大頁(yè)內(nèi)存動(dòng)態(tài)分配,需要關(guān)閉讓redis負(fù)責(zé)內(nèi)存管理。

臨時(shí)生效

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

要永久解決此問(wèn)題,請(qǐng)遵循日志的建議并修改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
向AI問(wèn)一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長(zhǎng)郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI