redis安装

redis安装配置

一 安装redis

redis下载:http://download.redis.io/releases/

1.1 下载上传redis

我这里下载的redis3.2.0版本,上传到/opt/resources目录

1.2 解压

[root@localhost resources]# tar -zxvf redis-3.2.0
[root@localhost resources]# mv redis-3.2.0 /opt/apps/

1.3 编译安装

[root@localhost resources]# cd /opt/apps/redis-3.2.0
[root@localhost redis-3.2.0]# make

-------------------------------------------------------------------
make[1]: Entering directory `/opt/apps/redis-3.2.0/src'
rm -rf redis-server redis-sentinel redis-cli redis-benchmark redis-check-rdb redis-check-aof *.o *.gcda *.gcno *.gcov redis.info lcov-html
...........................................

                   : je_
install_suffix     : 
autogen            : 0
cc-silence         : 1
debug              : 0
code-coverage      : 0
stats              : 1
prof               : 0
prof-libunwind     : 0
prof-libgcc        : 0
prof-gcc           : 0
tcache             : 1
fill               : 1
utrace             : 0
valgrind           : 0
xmalloc            : 0
munmap             : 0
lazy_lock          : 0
tls                : 1
cache-oblivious    : 1
===============================================================================
ar crus lib/libjemalloc.a src/jemalloc.o src/arena.o src/atomic.o src/base.o src/bitmap.o src/chunk.o src/chunk_dss.o src/chunk_mmap.o src/ckh.o src/ctl.o src/extent.o src/hash.o src/huge.o src/mb.o src/mutex.o src/pages.o src/prof.o src/quarantine.o src/rtree.o src/stats.o src/tcache.o src/util.o src/tsd.o
make[3]: Leaving directory `/opt/apps/redis-3.2.0/deps/jemalloc'
make[2]: Leaving directory `/opt/apps/redis-3.2.0/deps'
    CC adlist.o
    CC quicklist.o
    CC ae.o
    CC anet.o
    CC dict.o
    CC server.o
    CC sds.o
    CC zmalloc.o
    CC lzf_c.o
    CC lzf_d.o
    CC pqsort.o
    CC zipmap.o
    CC sha1.o
    CC ziplist.o
    CC release.o
    CC networking.o
    CC util.o
    CC object.o
    CC db.o
    CC replication.o
    CC rdb.o
    CC t_string.o
    CC t_list.o
    CC t_set.o
    CC t_zset.o
    CC t_hash.o
    CC config.o
    CC aof.o
    CC pubsub.o
    CC multi.o
    CC debug.o
    CC sort.o
    CC intset.o
    CC syncio.o
    CC cluster.o
    CC crc16.o
    CC endianconv.o
    CC slowlog.o
    CC scripting.o
    CC bio.o
    CC rio.o
    CC rand.o
    CC memtest.o
    CC crc64.o
    CC bitops.o
    CC sentinel.o
    CC notify.o
    CC setproctitle.o
    CC blocked.o
    CC hyperloglog.o
    CC latency.o
    CC sparkline.o
    CC redis-check-rdb.o
    CC geo.o
    LINK redis-server
    INSTALL redis-sentinel
    CC redis-cli.o
    LINK redis-cli
    CC redis-benchmark.o
    LINK redis-benchmark
    INSTALL redis-check-rdb
    CC redis-check-aof.o
    LINK redis-check-aof

Hint: It's a good idea to run 'make test' ;)

make[1]: Leaving directory `/opt/apps/redis-3.2.0/src'
-------------------------------------------------------------------

如果提示gcc command不识别,请自行安装gcc;

如果提示couldn’t execute tcl : no such file or dicrectory,请自行安装tcl;

如果提示
在这里插入图片描述

请执行make distclean,然后再make

Make成功之后,会在src目录下多出一些文件,如下
redis安装_第1张图片

[root@localhost redis-3.2.0]# make install
cd src && make install
make[1]: Entering directory `/opt/apps/redis-3.2.0/src'

Hint: It's a good idea to run 'make test' ;)

    INSTALL install
    INSTALL install
    INSTALL install
    INSTALL install
    INSTALL install
make[1]: Leaving directory `/opt/apps/redis-3.2.0/src'


[root@localhost redis-3.2.0]# redis-server -v
Redis server v=3.2.0 sha=00000000:0 malloc=jemalloc-4.0.3 bits=64 build=34ce3b7cb41a96ac

二 配置

2.1 创建目录

创建/etc/redis
/var/redis/data
/var/redis/run
/var/redis/log

2.2 修改配置文件,配置参数

[root@localhost redis]# cd /opt/apps/redis-3.2.0/

[root@localhost redis-3.2.0]# cp redis.conf /etc/redis/
[root@localhost redis-3.2.0]# vi /etc/redis/redis.conf
-------------------------------------------------------------------
修改一下内容
port 6379   #端口

pidfile /var/redis/run/redis.pid    #pid目录

dir /var/redis/data     #dump目录

logfile /var/redis/log/redis.log    #日志文件

#默认rdb,可选择是否开启aof,若开启,修改配置文件appendonly

# appendonly  yes

# 添加登录密码验证
 requestpass MhxzKhl
 
 #允许远程访问
 bound 0.0.0.0


redis配置持久化的两种方式:

  1. rdb
# 当用户设置了多个save的选项配置,只要其中任一条满足,Redis都会触发一次BGSAVE操作,比如:900秒之内至少一次写操作、300秒之内至少发生10次写操作、60秒之内发生至少10000次写操作都会触发发生快照操作
save 900 1
save 300 10
save 60 10000
dbfilename "dump.rdb"
dir "/data/dbs/redos/6397"
  1. aof
# redis默认关闭AOF机制,可以将no改成yes实现AOF持久化
appendonly no
# AOF文件
appendfilename "appendonly.aof"
# AOF持久化同步频率,always表示每个Redis写命令都要同步fsync写入到磁盘中,但是这种方式会严重降低redis的速度;everysec表示每秒执行一次同步fsync,显示的将多个写命令同步到磁盘中;no表示让操作系统来决定应该何时进行同步fsync,Linux系统往往可能30秒才会执行一次
# appendfsync always
appendfsync everysec
-------------------------------------------------------------------

#到这里就可以启动redis了
[root@localhost redis]# redis-server /etc/redis/redis.conf 

#如果想让redis后台运行,修改配置文件使得redis在background运行
[root@localhost redis-3.2.0]# vi /etc/redis/redis.conf
-------------------------------------------------------------------
修改参数为
daemonize yes
-------------------------------------------------------------------

三 服务及开机自启动

3.1 创建redis启动脚本

拷贝解压包下utils下redis启动脚本至/etc/init.d/

[root@localhost redis-3.2.0]# cp /opt/apps/redis3.2.0/utils/redis_init_script /etc/inti.d/

[root@localhost redis-3.2.0]# cd /etc/inti.d

#修改脚本名称(也可不修改)为redis
[root@localhost redis-3.2.0]# mv redis_init_script redis

修改脚本pid及conf路径为实际路径

[root@localhost init.d]# vi redis 
-------------------------------------------------------------------
#!/bin/sh
#
# 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/redis/run/redis.pid
CONF="/etc/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..."
                $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
-------------------------------------------------------------------

[root@localhost bin]# chmod -R 777 /etc/init.d/redis 
[root@localhost bin]# service redis stop
Stopping ...
Redis stopped
[root@localhost bin]# service redis start
Starting Redis server...

3.3 设置开机自启

chkconfig redis on
如果提示:service does not support

是因为没有在启动脚本里加入redis启动优先级信息,可添加如下: 添加的这一行代码要加上#
[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-nY92xuqV-1591076926347)(http://note.youdao.com/yws/res/3671/WEBRESOURCE0edb52b3b0ac3ddb0ac787e4de8c845f)]

再次执行chkconfig redis on
redis安装_第2张图片

批量删除固定前缀/后缀的数据

删除所有以test开头的所有key
redis-cli -a 密码 keys test* | xargs redis-cli -a 密码 del
删除所有以test结尾的所有key
redis-cli -a 密码 keys *test | xargs redis-cli -a 密码 del

如果没有密码可以不用写-a 密码

你可能感兴趣的:(技术文档)