命令:
[root@localhost ~]# yum list|grep redis
[root@localhost ~]#
发现没有可以安装的,如果有则忽略下面的步骤
命令:
[root@localhost ~]# yum install epel-release
安装好以后再次查看可以安装的redis版本
命令:
[root@localhost ~]# yum list|grep redis
php-nrk-Predis.noarch 0.8.6-1.el6 epel
php-pecl-redis.x86_64 2.2.8-1.el6 epel
php-redis.x86_64 2.2.2-5.git6f7087f.el6 epel
python-redis.noarch 2.0.0-1.el6 epel
redis.x86_64 2.4.10-1.el6 epel
uwsgi-logger-redis.x86_64 2.0.15-1.el6 epel
uwsgi-router-redis.x86_64 2.0.15-1.el6 epel
[root@localhost ~]#
现在有了可以安装的版本了。但是版本还是太低是2.4的版本。
结论:CentOS6不能直接通过yum安装redis3.X版本。需要源码安装,centos7则可以。具体安装方式可根据使用的系统版本来抉择。
官网地址: https://redis.io/download
Redis 3.2.9下载地址: http://download.redis.io/releases/redis-3.2.9.tar.gz
直接下载命令:
[root@localhost ~]# wget http://download.redis.io/releases/redis-3.2.9.tar.gz
下载完成后在当前目录会有redis-3.2.9.tar.gz包
检查系统中是否有gcc编译器,因为源码安装需要使用gcc编译
命令:
[root@localhost ~]# rpm -qa|grep gcc
gcc-4.4.7-4.el6.x86_64
libgcc-4.4.7-4.el6.x86_64
[root@localhost ~]#
上面的命令执行结果标识系统已经安装有gcc编译器,如果没有则执行下面的命令,有则忽略下面步骤
安装GCC命令
[root@localhost ~]# yum install –y gcc
命令:
[root@localhost ~]# mkdir -p /usr/local/redis-3.2.9
[root@localhost ~]#
命令:
[root@localhost ~]# tar -xzvf redis-3.2.9.tar.gz
首先进入解压后的redis目录
命令:
[root@localhost ~]# cd redis-3.2.9
[root@localhost redis-3.2.9]#
指定目录安装redis
命令:
[root@localhost redis-3.2.9]#make PREFIX=/usr/local/redis-3.2.9 install
命令:
ln -s /usr/local/redis-3.2.9 /usr/local/redis
默认的redis配置文件还在刚才解压的文件目录中,在Linux系统使用中,通常把配置的文件放在/etc目录下。算一个规范。
将源码目录下的redis配置文件添加一份到/etc目录下
命令:
[root@localhost redis-3.2.9]# pwd
/root/redis-3.2.9
[root@localhost redis-3.2.9]# ll redis.conf
-rw-rw-r--. 1 root root 46695 May 17 08:39 redis.conf
[root@localhost redis-3.2.9]# cp redis.conf /etc/redis.conf
编辑/etc/redis.conf配置redis以后台方式启动
[root@localhost redis-3.2.9]# vi /etc/redis.conf
在文件中找到daemonize配置,默认是no,修改成yes
提示:配置文件配置项后面不要有空格
编辑/etc/redis.conf配置redis允许远程访问
[root@localhost redis-3.2.9]# vi /etc/redis.conf
将bind 127.0.0.1注释掉即可随意访问,如果不注释则只能通过bind后面的ip进行访问。如:
bind 127.0.0.1 192.168.1.3
上面的配置标识只能本地或者访问自己的ip地址192.168.1.3进行访问
为了测试方便,注释掉即可。生产请bind自己主机的局域网IP地址
但是远程访问还不能读写,所以继续配置
找到protected-mode yes(如果使用bind的方式则不需要修改)
修改为protected-mode no
编辑/etc/redis.conf配置redis设置访问密码
找到配置文件中的requirepass配置
默认情况是注释的,配置的requirepass 后面跟的foobared就是密码
例如现在启用密码,并且将密码设置为123456,则配置为:
去掉#并将后面的密码修改为123456
提示:配置前后不要留空格
配置redis最大使用内存以及超出最大内存后的处理策略
#maxmemory <bytes>
redis-cache所能使用的最大内存(bytes),默认为0,表示"无限制",最终由OS物理内存大小决定(如果物理内存不足,有可能会使用swap)。此值尽量不要超过机器的物理内存尺寸,从性能和实施的角度考虑,可以为物理内存3/4。此配置需要和"maxmemory-policy"配合使用,当redis中内存数据达到maxmemory时,触发"清除策略"。在"内存不足"时,任何write操作(比如set,lpush等)都会触发"清除策略"的执行。在实际环境中,建议redis的所有物理机器的硬件配置保持一致(内存一致),同时确保master/slave中"maxmemory""policy"配置一致。
当内存满了的时候,如果还接收到set 命令,redis 将先尝试剔除设置过expire 信息的key,而不管该key 的过期时间还没有到达。在删除时,
将按照过期时间进行删除,最早将要被过期的key 将最先被删除。如果带有expire 信息的key 都删光了,内存还不够用,那么将返回错误。这样,redis 将不再接收写请求,只接收get 请求。maxmemory 的设置比较适合于把redis 当作于类似memcached的缓存来使用。
# maxmemory-policy noeviction
内存不足"时,数据清除策略,默认为"noeviction。
volatile-lru ->对"过期集合"中的数据采取LRU(近期最少使用)算法.如果对key使用"expire"指令指定了过期时间,那么此key将会被添加到"过期集合"中。将已经过期/LRU的数据优先移除.如果"过期集合"中全部移除仍不能满足内存需求,将OOM.
allkeys-lru ->对所有的数据,采用LRU算法
volatile-random ->对"过期集合"中的数据采取"随即选取"算法,并移除选中的K-V,直到"内存足够"为止. 如果如果"过期集合"中全部移除全部移除仍不能满足,将OOM
allkeys-random ->对所有的数据,采取"随机选取"算法,并移除选中的K-V,直到"内存足够"为止
volatile-ttl ->对"过期集合"中的数据采取TTL算法(最小存活时间),移除即将过期的数据.
noeviction ->不做任何干扰操作,直接返回OOM异常
另外,如果数据的过期不会对"应用系统"带来异常,且系统中write操作比较密集,建议采取"allkeys-lru"
举个栗子:
配置最大使用内存2GB,超出后对不常使用的缓存清理,快过期缓存清理的配置为:
#设置默认内存大小3GB
maxmemory 3GB
#设置清楚策略allkeys-lru
maxmemory-policy allkeys-lru
命令:
# useradd -r -s /sbin/nologin -M redis
4.6创建redis启动弄脚本便于使用service管理redis
命令:
#vi /etc/init.d/redis
脚本内容:
#!/bin/bash
#chkconfig: 2345 55 25
#description: Starts,stops and restart the redis-server
#Ver:1.1
#Write by ND chengh(200808)
#usage: ./script_name -p [port] {start|stop|status|restart}
# Source function library.
. /etc/rc.d/init.d/functions
# Source networking configuration.
. /etc/sysconfig/network
# Check networking is up.
[ "$NETWORKING" = "no" ] && exit 0
RETVAL=0
REDIS_PORT=6379
PID=
if [ "$1" = "-p" ]; then
REDIS_PORT=$2
shift 2
fi
REDIS_DIR="/usr/local/redis"
REDIS_CONF_DIR="/etc"
REDIS="${REDIS_DIR}/bin/redis-server"
PROG=$(basename $REDIS)
CONF="${REDIS_CONF_DIR}/redis-${REDIS_PORT}.conf"
if [ ! -f $CONF ]; then
if [ -f "${REDIS_CONF_DIR}/redis.conf" ];then
CONF="${REDIS_CONF_DIR}/redis.conf"
else
echo -n $"$CONF not exist.";warning;echo
exit 1
fi
fi
PID_FILE=`grep "pidfile" ${CONF}|cut -d ' ' -f2`
PID_FILE=${PID_FILE:=/var/run/redis.pid}
LOCKFILE="/var/lock/subsys/redis-${REDIS_PORT}"
if [ ! -x $REDIS ]; then
echo -n $"$REDIS not exist.";warning;echo
exit 0
fi
start() {
echo -n $"Starting $PROG: "
$REDIS $CONF
RETVAL=$?
if [ $RETVAL -eq 0 ]; then
success;echo;touch $LOCKFILE
else
failure;echo
fi
return $RETVAL
}
stop() {
echo -n $"Stopping $PROG: "
if [ -f $PID_FILE ] ;then
read PID < "$PID_FILE"
else
failure;echo;
echo -n $"$PID_FILE not found.";failure;echo
return 1;
fi
if checkpid $PID; then
kill -TERM $PID >/dev/null 2>&1
RETVAL=$?
if [ $RETVAL -eq 0 ] ;then
success;echo
echo -n "Waiting for Redis to shutdown .."
while checkpid $PID;do
echo -n "."
sleep 1;
done
success;echo;rm -f $LOCKFILE
else
failure;echo
fi
else
echo -n $"Redis is dead and $PID_FILE exists.";failure;echo
RETVAL=7
fi
return $RETVAL
}
restart() {
stop
start
}
rhstatus() {
status -p ${PID_FILE} $PROG
}
hid_status() {
rhstatus >/dev/null 2>&1
}
case "$1" in
start)
hid_status && exit 0
start
;;
stop)
rhstatus || exit 0
stop
;;
restart)
restart
;;
status)
rhstatus
RETVAL=$?
;;
*)
echo $"Usage: $0 -p [port] {start|stop|status|restart}"
RETVAL=1
esac
exit $RETVAL
上面脚本中可根据具体redis安装位置配置redis-server的路径和conf的路径
redis默认安装在
/usr/local/redis目录下
Redis配置文件存放在
/etc/redis.conf
修改脚本权限
#chmod 755 /etc/init.d/redis
添加进service管理
#chkconfig --add redis
设置开机启动
# chkconfig redis on
4.7启动redis
[root@localhost redis-3.2.9]# service redis start
Starting redis-server: [ OK ]
连接命令:
#/usr/local/redis/bin/redis-cli -h 127.0.0.1 -p 6379
设置一个值:
127.0.0.1:6379> set testKey testValue
获取上面的设置值
命令:
127.0.0.1:6379> get testKey
删除上面的值
命令
del testKey
http://blog.xqlee.com/article/227.html