Loading

redis安装、配置、启停

Redis is an open source (BSD licensed), in-memory data structure store, used as database, cache and message broker. It supports data structures such as strings, hashes, lists, sets, sorted sets with range queries, bitmaps, hyperloglogs and geospatial indexes with radius queries. Redis has built-in replication, Lua scripting, LRU eviction, transactions and different levels of on-disk persistence, and provides high availability via Redis Sentinel and automatic partitioning with Redis Cluster.

安装

redis是以源码方式发行的,先下载源码,然后在linux下编译。
http://www.redis.io/download 先到这里下载Stable稳定版,目前最新版本是3.0.7。
上传到linux,解压

tar xzf redis-3.0.7.tar.gz

编译

#安装tcl支持
yum install tcl
cd redis-3.0.7
make
make test
make install

测试通过后安装,安装后会自动把redis-server,redis-cli,redis-benchmark,redis-check-aof,redis-check-dump复制到/usr/local/bin目录下

配置启动脚本,开机启动

编辑redis.conf文件,修改daemonize no改成yes,也可以修改日志文件,数据文件路径等。然后复制并重命名redis.conf 到/etc/redis/6379.conf

redis源码中带有启动脚本redis_init_script,在解压目录下的utils目录中。复制并重命名redis_init_script到/etc/init.d/redis

修改启动脚本

vim /etc/init.d/redis

避免设置开机启动时的 service redis does not support chkconfig异常(详情),在第二行增加

#chkconfig: 2345 80 90

最后的启动脚本如下:

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

REDISPORT=6379
EXEC=/usr/local/bin/redis-server
CLIEXEC=/usr/local/bin/redis-cli

PIDFILE=/var/run/redis_${REDISPORT}.pid
CONF="/etc/redis/${REDISPORT}.conf"

case "$1" in
    start)
        if [ -f $PIDFILE ]
        then
                echo "$PIDFILE exists, process is already running or crashed"
        else
                echo "Starting Redis server..."
                $EXEC $CONF
        fi
        ;;
    stop)
        if [ ! -f $PIDFILE ]
        then
                echo "$PIDFILE does not exist, process is not running"
        else
                PID=$(cat $PIDFILE)
                echo "Stopping ..."
                $CLIEXEC -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

注意pid文件要和conf中的pid一致,要不关闭redis时会失败,如果conf没有改动pid,那么变动下上面的启动脚本中的PIDFILE

PIDFILE=/var/run/redis_${REDISPORT}.pid 
改为
PIDFILE=/var/run/redis.pid 

设置开机启动

sudo chkconfig redis on
#启停服务
service redis start
service redis stop

配置防火墙打开对应的端口:

vi /etc/sysconfig/iptables
#增加开放6379端口的规则
-A INPUT -m state --state NEW -m tcp -p tcp --dport 6379 -j ACCEPT
#重启防火墙
service iptables restart 

redis-cli 验证

切换到redis bin目录

cd /usr/local/bin

指定端口6379,连接到本机redis,同时设置一个key为test,value为'Hello Redis'的缓存项

redis-cli -p 6379 set test 'hello redis'
#成功返回OK

读取缓存

./redis-cli -p 6379 get test

删除缓存

./redis-cli -p 6379 del test 

建议查看官方入门文档:http://www.redis.io/topics/data-types-intro

配置redis仅做为缓存使用

如果不打算使用事务、管线等一堆复杂功能,仅仅把redis当成cache server使用,可以在配置文件中,找到maxmemory、maxmemory-policy这二项,参考下面修改

# 限定最多使用2GB内存
maxmemory 2048mb
#所有key全都按LRU(近期最少使用)算法淘汰,这种情况下,不用设置过期时间,只要内存使用达到上限,不怎么使用的key自然被干掉
maxmemory-policy allkeys-lru
posted @ 2016-04-08 10:58  sloong  阅读(3238)  评论(0编辑  收藏  举报