歡迎來到Linux教程網
Linux教程網
Linux教程網
Linux教程網
Linux教程網 >> Linux基礎 >> Linux教程 >> Redis安裝配置

Redis安裝配置

日期:2017/2/28 16:13:19   编辑:Linux教程

一)下載源碼,編譯安裝

  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. Hint: To run 'make test' is a good idea ;)
  18. mkdir -p /usr/local/bin
  19. cp -p redis-server /usr/local/bin
  20. cp -p redis-benchmark /usr/local/bin
  21. cp -p redis-cli /usr/local/bin
  22. cp -p redis-check-dump /usr/local/bin
  23. cp -p redis-check-aof /usr/local/bin
  24. 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. #
  3. # The DB will be written inside this directory, with the filename specified
  4. # above using the 'dbfilename' configuration directive.
  5. #
  6. # Also the Append Only File will be created inside this directory.
  7. #
  8. # Note that you must specify a directory here, not a file name.
  9. dir /var/db/redis

內存,連接數設置

  1. maxmemory 256000000
  2. maxclients 500

三)啟動腳本

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