一)下载源码,编译安装


  
  1. # wget http://redis.googlecode.com/files/redis-2.2.8.tar.gz 
  2. # tar xf redis-2.2.8.tar.gz 
  3. # cd redis 
  4. # make  
  5. # 网上说不能make install,可我这就是可以,奇怪,省去了手动copy redis命令的步骤
  6. # make install  

make install后显示


  
  1. cd src && make install 
  2. make[1]: Entering directory `/usr/local/src/redis-2.2.8/src' 
  3. cd ../deps/hiredis && make static ARCH="" 
  4. make[2]: Entering directory `/usr/local/src/redis-2.2.8/deps/hiredis' 
  5. make[2]: Nothing to be done for `static'. 
  6. make[2]: Leaving directory `/usr/local/src/redis-2.2.8/deps/hiredis' 
  7. cd ../deps/linenoise && make ARCH="" 
  8. make[2]: Entering directory `/usr/local/src/redis-2.2.8/deps/linenoise' 
  9. make[2]: `linenoise_example' is up to date. 
  10. make[2]: Leaving directory `/usr/local/src/redis-2.2.8/deps/linenoise' 
  11. cd ../deps/hiredis && make static 
  12. make[2]: Entering directory `/usr/local/src/redis-2.2.8/deps/hiredis' 
  13. make[2]: Nothing to be done for `static'. 
  14. make[2]: Leaving directory `/usr/local/src/redis-2.2.8/deps/hiredis' 
  15. cc -o redis-benchmark -std=c99 -pedantic -O2 -Wall -W   -lm -pthread   -g -rdynamic -ggdb  ae.o anet.o redis-benchmark.o sds.o adlist.o zmalloc.o ../deps/hiredis/libhiredis.a 
  16. cc -o redis-cli -std=c99 -pedantic -O2 -Wall -W   -lm -pthread   -g -rdynamic -ggdb  anet.o sds.o adlist.o redis-cli.o zmalloc.o release.o ../deps/hiredis/libhiredis.a ../deps/linenoise/linenoise.o 
  17.  
  18. Hint: To run 'make test' is a good idea ;) 
  19.  
  20. mkdir -p /usr/local/bin 
  21. cp -p redis-server /usr/local/bin 
  22. cp -p redis-benchmark /usr/local/bin 
  23. cp -p redis-cli /usr/local/bin 
  24. cp -p redis-check-dump /usr/local/bin 
  25. cp -p redis-check-aof /usr/local/bin 
  26. make[1]: Leaving directory `/usr/local/src/redis-2.2.8/src' 

二)修改配置

修改配置之前,请将redis.conf copy一份到/etc/目录下


  
  1. daemonize no 

改成


  
  1. daemonize yes 

这两个参数


  
  1. loglevel warning  
  2. logfile /var/log/redis.log  

取消注释


  
  1. syslog-enabled no #这个改成syslog-enabled yes
  2. syslog-facility local0

数据文件目录


  
  1. # The working directory. 
  2. # The DB will be written inside this directory, with the filename specified 
  3. # above using the 'dbfilename' configuration directive. 
  4. # Also the Append Only File will be created inside this directory. 
  5. # Note that you must specify a directory here, not a file name. 
  6. dir /var/db/redis 

内存,连接数设置


  
  1. maxmemory 256000000
  2. maxclients 500

三)启动脚本


  
  1. #!/bin/bash 
  2. # Init file for redis 
  3. # chkconfig: - 80 12 
  4. # description: redis daemon 
  5. # processname: redis 
  6. # config: /etc/redis.conf 
  7. # pidfile: /var/run/redis.pid 
  8.  
  9. . /etc/init.d/functions 
  10.  
  11. BIN="/usr/local/bin" 
  12. CONFIG="/etc/redis.conf" 
  13. PIDFILE="/var/run/redis.pid" 
  14.  
  15. ### Read configuration 
  16. [ -r "$SYSCONFIG" ] && source "$SYSCONFIG" 
  17.  
  18. RETVAL=0 
  19. prog="redis-server" 
  20. desc="Redis Server" 
  21.  
  22. start() { 
  23.  
  24.         if [ -e $PIDFILE ];then 
  25.              echo "$desc already running...." 
  26.              exit 1 
  27.         fi 
  28.  
  29.         echo -n $"Starting $desc: " 
  30.         daemon $BIN/$prog $CONFIG 
  31.  
  32.         RETVAL=$? 
  33.         echo 
  34.         [ $RETVAL -eq 0 ] && touch /var/lock/subsys/$prog 
  35.         return $RETVAL 
  36.  
  37. stop() { 
  38.         echo -n $"Stop $desc: " 
  39.         killproc $prog 
  40.         RETVAL=$? 
  41.         echo 
  42.         [ $RETVAL -eq 0 ] && rm -f /var/lock/subsys/$prog $PIDFILE 
  43.         return $RETVAL 
  44.  
  45. restart() { 
  46.         stop 
  47.         start 
  48.  
  49.  
  50. case "$1" in 
  51.   start) 
  52.         start 
  53.         ;; 
  54.   stop) 
  55.         stop 
  56.         ;; 
  57.   restart) 
  58.         restart 
  59.         ;; 
  60.   condrestart) 
  61.         [ -e /var/lock/subsys/$prog ] && restart 
  62.         RETVAL=$? 
  63.         ;; 
  64.   status) 
  65.         status $prog 
  66.         RETVAL=$? 
  67.         ;; 
  68.    *) 
  69. echo $"Usage: $0 {start|stop|restart|condrestart|status}" 
  70.         RETVAL=1 
  71. esac 
  72.  
  73. exit $RETVAL 

配置启动脚本


  
  1. #chmod 755 /etc/init.d/redis 
  2. # chkconfig --add redis 
  3. # chkconfig redis on 

四)启动

在正式启动redis之前,先创建数据目录


  
  1. # mkdir /var/db/redis 

否则会出现下面的错误


  
  1. [3030] 27 May 16:50:38 # Can't chdir to '/var/db/redis': No such file or directory 

同时配置内核参数


  
  1. sysctl vm.overcommit_memory=1 

否则提示错误


  
  1. # WARNING overcommit_memory is set to 0! Background save may fail under low memory condition. 
  2. #To fix this issue 
  3. #add 'vm.overcommit_memory = 1' to /etc/sysctl.conf and then reboot or run the command 
  4. #'sysctl vm.overcommit_memory=1' for this to take effect. 

最后,启动


  
  1. [root@web ~]# /etc/init.d/redis start 
  2. Starting Redis Server:                                     [  OK  ]

PS:不利用脚本启动,关闭redis的命令


  
  1. 启动 
  2. # redis-server /etc/redis.conf 
  3.  
  4. 关闭 
  5. # redis-cli shutdown 
  6.  
  7. 关闭某个端口上的redis 
  8. # redis-cli -p port shutdown