码迷,mamicode.com
首页 > 系统相关 > 详细

Linux安装配置Redis

时间:2017-03-31 22:25:48      阅读:261      评论:0      收藏:0      [点我收藏+]

标签:redis

一、Redis介绍

Redis是当前比较热门的NOSQL系统之一,它是一个key-value存储系统。和Memcache类似,但很大程度补偿了Memcache的不足,它支持存储的value类型相对更多,包括string、list、set、zset和hash。这些数据类型都支持push/pop、add/remove及取交集并集和差集及更丰富的操作。在此基础上,Redis支持各种不同方式的排序。

和Memcache一样,Redis数据都是缓存在计算机内存中,不同的是,Memcache只能将数据缓存到内存中,无法自动定期写入硬盘,这就表示,一断电或重启,内存清空,数据丢失。所以Memcache的应用场景适用于缓存无需持久化的数据。而Redis不同的是它会周期性的把更新的数据写入磁盘或者把修改操作写入追加的记录文件,实现数据的持久化。

Redis从它的许多竞争继承来的三个主要特点:

1.Redis数据库完全在内存中,使用磁盘仅用于持久性。

2.相比许多键值数据存储,Redis拥有一套较为丰富的数据类型。

3.Redis可以将数据复制到任意数量的从服务器。


Redis 优势

         异常快速:Redis的速度非常快,每秒能执行约11万集合,每秒约81000+条记录。

         支持丰富的数据类型:Redis支持最大多数开发人员已经知道像列表,集合,有序集合,散列数据类型。这使得它非常容易解决各种各样的问题,因为我们知道哪些问题是可以处理通过它的数据类型更好。

         操作都是原子性:所有Redis操作是原子的,这保证了如果两个客户端同时访问的Redis服务器将获得更新后的值。

        多功能实用工具:Redis是一个多实用的工具,可以在多个用例如缓存,消息,队列使用(Redis原生支持发布/订阅),任何短暂的数据,应用程序,如Web应用程序会话,网页命中计数等。


二、Redis的安装

[root@localhost app]# ls
redis-3.2.8.tar.gz
[root@localhost app]# tar zxvf redis-3.2.8.tar.gz 
[root@localhost app]# cd redis-3.2.8
[root@localhost redis-3.2.8]# yum -y install gcc gcc-c++
[root@localhost redis-3.2.3]# make
Hint: It‘s a good idea to run ‘make test‘ ;)

make[1]: Leaving directory `/app/redis-3.2.3/src‘
[root@localhost redis-3.2.3]#

在执行完make之后,在最后的程序中会输出(t‘s a good idea to run ‘make test‘),它建议我们执行make test进行测试,那么接下来我们就输入make test,检查测试,是否出现问题,如出现如图所示:则说明测试没有问题:

[root@localhost redis-3.2.3]# cd src/
[root@localhost src]# make test
You need tcl 8.5 or newer in order to run the Redis test
make: *** [test] Error 1
[root@localhost src]# cd /app/
[root@localhost app]# wget 
[root@localhost app]# tar zxvf tcl8.6.1-src.tar.gz 
[root@localhost app]# cd tcl8.6.1/unix/
[root@localhost unix]# ./configure
[root@localhost unix]# make && make install
[root@localhost unix]# cd /app/redis-3.2.3/
[root@localhost redis-3.2.3]# make clean
[root@localhost redis-3.2.3]# make
[root@localhost redis-3.2.3]# cd src/
[root@localhost src]# make test
  215 seconds - integration/replication-3
  212 seconds - integration/replication-4
  98 seconds - unit/hyperloglog
  151 seconds - unit/obuf-limits

\o/ All tests passed without errors!

Cleanup: may take some time... OK
[root@localhost src]# make install

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

    INSTALL install
    INSTALL install
    INSTALL install
    INSTALL install
    INSTALL install
[root@localhost src]#


三、redis的配置

 启动:

[root@localhost src]# redis-server ../redis.conf 
                _._                                                  
           _.-``__ ‘‘-._                                             
      _.-``    `.  `_.  ‘‘-._           Redis 3.2.3 (00000000/0) 64 bit
  .-`` .-```.  ```\/    _.,_ ‘‘-._                                   
 (    ‘      ,       .-`  | `,    )     Running in standalone mode
 |`-._`-...-` __...-.``-._|‘` _.-‘|     Port: 6379
 |    `-._   `._    /     _.-‘    |     PID: 35217
  `-._    `-._  `-./  _.-‘    _.-‘                                   
 |`-._`-._    `-.__.-‘    _.-‘_.-‘|                                  
 |    `-._`-._        _.-‘_.-‘    |           http://redis.io        
  `-._    `-._`-.__.-‘_.-‘    _.-‘                                   
 |`-._`-._    `-.__.-‘    _.-‘_.-‘|                                  
 |    `-._`-._        _.-‘_.-‘    |                                  
  `-._    `-._`-.__.-‘_.-‘    _.-‘                                   
      `-._    `-.__.-‘    _.-‘                                       
          `-._        _.-‘                                           
              `-.__.-‘                                               

35217:M 29 Mar 11:30:21.454 # WARNING: The TCP backlog setting of 511 cannot be enforced because /proc/sys/net/core/somaxconn is set to the lower value of 128.
35217:M 29 Mar 11:30:21.454 # Server started, Redis version 3.2.3
35217:M 29 Mar 11:30:21.454 # 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.
35217:M 29 Mar 11:30:21.454 * The server is now ready to accept connections on port 6379
^C35217:signal-handler (1490758240) Received SIGINT scheduling shutdown...
35217:M 29 Mar 11:30:40.180 # User requested shutdown...
35217:M 29 Mar 11:30:40.180 * Saving the final RDB snapshot before exiting.
35217:M 29 Mar 11:30:40.191 * DB saved on disk
35217:M 29 Mar 11:30:40.191 * Removing the pid file.
35217:M 29 Mar 11:30:40.191 # Redis is now ready to exit, bye bye...
[root@localhost src]#

这里直接执行Redis-server 启动的Redis服务,是在前台直接运行的(效果如上图),也就是说,执行完该命令后,如果Lunix关闭当前会话,则Redis服务也随即关闭。正常情况下,启动Redis服务需要从后台启动,并且指定启动配置文件。 

编辑conf文件,将daemonize属性改为yes(表明需要在后台运行)

[root@localhost src]# vim ../redis.conf 
daemonize yes
[root@localhost src]# redis-server ../redis.conf 
[root@localhost src]# netstat -anotp|grep :6379
tcp        0      0 127.0.0.1:6379              0.0.0.0:*                   LISTEN      35415/redis-server  off (0.00/0/0)
[root@localhost src]# redis-cli shutdown   #停止redis服务
[root@localhost src]# netstat -anotp|grep :6379

配置

为了方便管理,将Redis文件中的conf配置文件和常用命令移动到统一文件中

[root@localhost app]# mkdir -p redis6379/{log,conf,data,bin}
[root@localhost src]# cp redis-server redis-benchmark redis-cli kreleasehdr.sh redis-check-aof /app/redis6379/bin/
[root@localhost src]# cp ../redis.conf /app/redis6379/conf/
[root@localhost src]# pwd
/app/redis-3.2.3/src
[root@localhost src]# cd /app/redis6379/bin/
[root@localhost bin]# redis-server ../conf/redis.conf 
[root@localhost bin]# netstat -antp|grep :6379
tcp        0      0 127.0.0.1:6379              0.0.0.0:*                   LISTEN      36334/redis-server  
[root@localhost bin]# cp /etc/sysctl.conf{,.bak}
[root@localhost bin]# vim /etc/sysctl.conf
vm.overcommit_memory = 1
[root@localhost bin]# sysctl -p

设置内存分配策略vm.overcommit_memory = 1 ;否则Redis脚本在重启或停止redis时,将会报错,并且不能自动在停止服务前同步数据到磁盘上

/proc/sys/vm/overcommit_memory可选值:0、1、2。

0, 表示内核将检查是否有足够的可用内存供应用进程使用;如果有足够的可用内存,内存申请允许;否则,内存申请失败,并把错误返回给应用进程。

1, 表示内核允许分配所有的物理内存,而不管当前的内存状态如何。

2, 表示内核允许分配超过所有物理内存和交换空间总和的内存

值得注意的一点是,redis在dump数据的时候,会fork出一个子进程,理论上child进程所占用的内存和parent是一样的,比如parent占用的内存为8G,这个时候也要同样分配8G的内存给child,如果内存无法负担,往往会造成redis服务器的down机或者IO负载过高,效率下降。所以这里比较优化的内存分配策略应该设置为 1(表示内核允许分配所有的物理内存,而不管当前的内存状态如何)

redis服务关闭后,缓存数据会自动dump到硬盘上,硬盘地址为redis.conf中的配置项dbfilename dump.rdb所设定


为了方便管理,将Redis配置成服务

[root@localhost conf]# cat /etc/rc.d/init.d/redis 
#!/bin/bash
#chkconfig: 2345 10 90
#description: Redis server is an open source, advanced key-value store.
# source function library
source /etc/rc.d/init.d/functions
port="6379"
pidfile="/var/run/redis_$port.pid"
lockfile="/var/lock/subsys/redis-server"
rootpath="/app/redis$port"
config="$rootpath/conf/redis.conf"
binpath="$rootpath/bin"
[ -r "$SYSCONFIG" ] && source "$SYSCONFIG"
reids_status(){
               status -p $pidfile redis-server
}
start() {
        if [ -e $pidfile ];then
            echo "Redis Server aleady running......"
            exit 1
        else
            echo -n "Starting Redis Server......"
            $binpath/redis-server $config
            value=$?
            [ $value -eq 0 ] && touch $lockfile && echo "OK"
            return $value
        fi
}
stop() {
       echo -n "Stop Redis Server......"
       killproc redis-server
#      $binpath/redis-cli save && $binpath/redis-cli shutdown
       value=$?
       [ $value -eq 0 ] && rm -rf $lockfile $pidfile 
       return $value
}
restart() {
          stop
          start
}
case "$1" in
start)
      start
;;
stop)
     stop
;;
restart)
        restart
;;
status)
       reids_status
;;
*)
  echo $"Usage: $0 {start|stop|restart|status}"
esac
[root@localhost conf]# chkconfig --add redis
[root@localhost conf]# chkconfig --level 2345 redis on
[root@localhost conf]# chmod +x /etc/rc.d/init.d/redis


三、redis的测试

[root@localhost src]# redis-cli  #redis-cli -a password 进入redis客户端(有密码状态)
127.0.0.1:6379> set justin "WeChat ID:ityunwei2017"  #向redis中插入键值对数据,键为justin,值为WeChat ID:ityunwei2017
OK
127.0.0.1:6379> get justin   #根据键取值
"WeChat ID:ityunwei2017"
127.0.0.1:6379> exists justin    #查看键是否存在
(integer) 1
127.0.0.1:6379> exists justin1
(integer) 0
127.0.0.1:6379> del justin    #删除当前key
(integer) 1
127.0.0.1:6379> exists justin
(integer) 0
127.0.0.1:6379> quit
[root@localhost src]# redis-benchmark -h 127.0.0.1 -p 6379 -n 10 -c 50

redis-benchmark -h 127.0.0.1 -p 6379 -n 10 -c 50    向redis服务器发送10个请求,每个请求附带50个并发客户端,-n 接请求数,-c 接并发数  


本文出自 “我本不是菜鸟” 博客,请务必保留此出处http://ityunwei2017.blog.51cto.com/7662323/1912128

Linux安装配置Redis

标签:redis

原文地址:http://ityunwei2017.blog.51cto.com/7662323/1912128

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!