编译安装Redis及使用systemd管理

发布时间:2020-03-03 17:10:46 作者:何小帅
来源:网络 阅读:1050

环境

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

执行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-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参数控制的是三次握手的时候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
推荐阅读:
  1. 编译安装redis
  2. 编译安装httpd2.4.25并编写systemd脚本

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

redis4.0.14 编译安装redis sys%

上一篇:Anaconda是什么?Anconda下载安装教程

下一篇:passwd的使用方法

相关阅读

您好,登录后才能下订单哦!

密码登录
登录注册
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》