下载地址:https://download.redis.io/redis-stable.tar.gz
[root@localhost ~]# cd /src/
[root@localhost src]# rz -b
rz waiting to receive.
Starting zmodem transfer. Press Ctrl+C to cancel.
Transferring redis-4.0.2.tar.gz...
100% 1508 KB 1508 KB/sec 00:00:01 0 Errors
[root@localhost src]# tar -xf redis-4.0.2.tar.gz
[root@localhost src]# cd redis-4.0.2
[root@localhost redis-4.0.2]# mkdir /program/redis
[root@localhost redis-4.0.2]# make PREFIX=/program/redis install # 通过PREFIX可以指定安装目录
…………………………
[root@localhost redis-4.0.2]#
使用redis:redis运行Redis服务
[root@localhost ~]# groupadd redis
[root@localhost ~]# useradd -s /sbin/nologin -g redis redis
[root@localhost ~]# chown -R redis:redis /program/redis/
[root@localhost ~]#
建立配置文件
[root@localhost ~]# find / -name "redis.conf"
/src/redis-4.0.2/redis.conf
[root@localhost ~]# cp /src/redis-4.0.2/redis.conf /program/redis/redis.conf
[root@localhost ~]# vim /program/redis/redis.conf
bind localhost # 绑定IP地址(可以设为localhost或127.0.0.1)
daemonize yes # 以守护进程方式运行Redis
pidfile /dev/shm/redis.pid # PID文件
loglevel debug # 日志级别(开发环境可设为debug)
logfile "/program/redis/redis.log" # 日志文件
requirepass ****** # 访问Redis密码,注意要和[vim /etc/rc.d/init.d/redis]里面的密码保持一致
unixsocket /dev/shm/redis.sock # socket文件路径
dbfilename dump.rdb # 本地数据库文件
dir /program/redis/ # 本地数据库文件保存目录
maxmemory 256MB # 最大可用内存(0为不限制),需结合[maxmemory-policy]一起使用
maxmemory-policy noeviction # 内存淘汰策略,设置了maxmemory必须设置
[root@localhost ~]#
备注:内存淘汰策略就是当Redis的内存使用量达到最大可用内存量之后如何处理,一共有如下几个选择:
·noeviction 当内存使用达到阈值的时候,所有引起申请内存的命令会报错
·allkeys-lru 在主键空间中,优先移除最近未使用的key
·volatile-lru 在设置了过期时间的键空间中,优先移除最近未使用的key
·allkeys-random 在主键空间中,随机移除某个key
·volatile-random 在设置了过期时间的键空间中,随机移除某个key
·volatile-ttl 在设置了过期时间的键空间中,具有更早过期时间的key优先移除
将Redis服务加入系统服务
[root@localhost ~]# find / -name "redis_init_script"
/src/redis-4.0.2/utils/redis_init_script
[root@localhost ~]# cp /src/redis-4.0.2/utils/redis_init_script /etc/rc.d/init.d/redis
[root@localhost ~]# vim /etc/rc.d/init.d/redis
#!/bin/sh
# chkconfig: 2345 80 90
#
# Simple Redis init.d script conceived to work on Linux systems
# as it does use of the /proc filesystem.
# 消除启动Redis时的[Transparent Huge Pages]警告信息
echo never > /sys/kernel/mm/transparent_hugepage/enabled
REDISPORT=6379
EXEC=/program/redis/bin/redis-server
CLIEXEC=/program/redis/bin/redis-cli
PIDFILE=/dev/shm/redis.pid # 将PID文件放到/dev/shm/目录下可加快读取速度
CONF="/program/redis/redis.conf" # 指定配置文件
case "$1" in
start)
if [ -f $PIDFILE ]
then
echo "$PIDFILE exists, process is already running or crashed"
else
echo "Starting Redis server..."
echo 511 > /proc/sys/net/core/somaxconn # 解决启动时的WARNING
$EXEC $CONF & # 注意这里要加一个空格和&符号
fi
;;
stop)
if [ ! -f $PIDFILE ]
then
echo "$PIDFILE does not exist, process is not running"
else
PID=$(cat $PIDFILE)
echo "Stopping ..."
$CLIEXEC -a "这是{$CONF}文件里的requirepass的值" -p $REDISPORT shutdown
while [ -x /proc/${PID} ]
do
echo "Waiting for Redis to shutdown ..."
sleep 1
done
echo "Redis stopped"
fi
;;
*)
echo "Please use start or stop as first argument"
;;
esac
[root@localhost ~]# chkconfig --list
…………………………
postfix 0:关闭 1:关闭 2:启用 3:启用 4:启用 5:启用 6:关闭
rdisc 0:关闭 1:关闭 2:关闭 3:关闭 4:关闭 5:关闭 6:关闭
restorecond 0:关闭 1:关闭 2:关闭 3:关闭 4:关闭 5:关闭 6:关闭
rsyslog 0:关闭 1:关闭 2:启用 3:启用 4:启用 5:启用 6:关闭
…………………………
[root@localhost ~]# chkconfig --add redis
[root@localhost ~]# chkconfig --list
…………………………
postfix 0:关闭 1:关闭 2:启用 3:启用 4:启用 5:启用 6:关闭
rdisc 0:关闭 1:关闭 2:关闭 3:关闭 4:关闭 5:关闭 6:关闭
redis 0:关闭 1:关闭 2:启用 3:启用 4:启用 5:启用 6:关闭
restorecond 0:关闭 1:关闭 2:关闭 3:关闭 4:关闭 5:关闭 6:关闭
rsyslog 0:关闭 1:关闭 2:启用 3:启用 4:启用 5:启用 6:关闭
…………………………
[root@localhost ~]# chkconfig redis on
[root@localhost ~]# service redis start # 启动Redis
[root@localhost ~]# service redis stop # 停止Redis
WARNING处理
■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.
[root@localhost ~]# cp /etc/sysctl.conf /etc/sysctl.conf.bak
[root@localhost ~]# vim /etc/sysctl.conf
…………………………
# Controls the maximum shared segment size, in bytes
kernel.shmmax = 4294967295
# Controls the maximum number of shared memory segments, in pages
kernel.shmall = 268435456
vm.overcommit_memory = 1
[root@localhost ~]#
■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.