脚本功能

redis单机单实例一键安装脚本

注意事项

1.仅适用于Linux/Centos 64位

2.安装时需联网

步骤
1.下载并安装libunwind软件包(是TCMalloc依赖包)
2.下载并安装TCMalloc
3.下载并安装redis
4.配置redis
5.准备redis启动停止脚本
6.启动redis
 
脚本内容
 
 
   
  1. #!/bin/bash 
  2.  
  3. # 2013-1-10 LEO chanyipiaomiao@163.com 
  4. # Blog: http://linux5588.blog.51cto.com 
  5.  
  6. # 脚本功能 
  7. # redis单机单实例一键安装脚本 
  8.  
  9. # 注意事项 
  10. # 仅适用于Linux/Centos 64位 
  11. # 安装时需联网 
  12.  
  13. # 步骤 
  14. # 1.下载并安装libunwind软件包(是TCMalloc依赖包) 
  15. # 2.下载并安装TCMalloc 
  16. # 3.下载并安装redis 
  17. # 4.配置redis 
  18. # 5.准备redis启动停止脚本 
  19. # 6.启动redis 
  20.  
  21. #输出PATH变量 
  22. export PATH=/usr/kerberos/sbin:/usr/kerberos/bin:/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin:/root/bin 
  23.  
  24. #定义存放软件目录 
  25. software="/root/software" 
  26.  
  27. #如果软件目录不存在则新建该目录 
  28. if [[ ! -e $software ]]; then 
  29.     mkdir -p $software 
  30. fi 
  31.  
  32. #定义判断是否安装成功函数 
  33. function installIsOK(){ 
  34.     if [[ $2 == 0 ]]; then 
  35.         echo "$1 install ......  OK !" 
  36.     else 
  37.         echo "$1 install ......  Failure!" 
  38.         exit 1 
  39.     fi 
  40.  
  41. #进入软件目录 
  42. cd $software 
  43.  
  44. # 1.下载并安装libunwind软件包(是TCMalloc依赖包) 
  45. libunwind='libunwind-1.1' 
  46. wget http://download.savannah.gnu.org/releases/libunwind/${libunwind}.tar.gz 
  47. tar zxf ${libunwind}.tar.gz 
  48. cd $libunwind 
  49. ./configure && make && make install 
  50. if [[ $? == 0 ]]; then 
  51.     installIsOK ${libunwind} 0 
  52. else 
  53.     installIsOK ${libunwind} 1 
  54. fi 
  55. cd $software 
  56.  
  57. # 2.下载并安装TCMalloc 
  58. # TCMalloc (google-perftools) 是用于优化C++写的多线程应用,比glibc 2.3的malloc快。这个模块可以用来优化redis性能 
  59. gperftools='gperftools-2.0' 
  60. wget http://gperftools.googlecode.com/files/${gperftools}.tar.gz 
  61. tar zxf ${gperftools}.tar.gz 
  62. cd $gperftools 
  63. ./configure 
  64. make && make install 
  65. if [[ $? == 0 ]]; then 
  66.     installIsOK ${gperftools} 0 
  67. else 
  68.     installIsOK ${gperftools} 2 
  69. fi 
  70. echo "/usr/local/lib" > /etc/ld.so.conf.d/usr_local_lib.conf 
  71. ldconfig 
  72. cd $software 
  73.  
  74. # 3.下载并安装redis 
  75. redis='redis-2.6.7' 
  76. redis_dir='/usr/local/redis' 
  77. wget http://redis.googlecode.com/files/${redis}.tar.gz 
  78. tar zxf ${redis}.tar.gz 
  79. cd $redis 
  80. make PREFIX=${redis_dir} USE_TCMALLOC=yes install 
  81. if [[ $? == 0 ]]; then 
  82.     installIsOK ${redis} 0 
  83. else 
  84.     installIsOK ${redis} 3 
  85. fi 
  86.  
  87. # 4.配置redis 
  88. mkdir -p ${redis_dir}/etc 
  89. mkdir -p ${redis_dir}/run 
  90. mkdir -p ${redis_dir}/data/6379 
  91. mkdir -p ${redis_dir}/log 
  92. cp redis.conf ${redis_dir}/redis.conf 
  93. #cp ${redis_dir}/redis.conf ${redis_dir}/etc/redis_6379.conf 
  94.  
  95. #生成配置文件 
  96. redis_6379="${redis_dir}/etc/redis_6379.conf" 
  97. cat >> ${redis_6379} << "EOF" 
  98. daemonize yes 
  99. pidfile /usr/local/redis/run/redis_6379.pid 
  100. port 6379 
  101. #bind 127.0.0.1 
  102. timeout 300 
  103. loglevel notice 
  104. logfile /usr/local/redis/log/redis.log 
  105. databases 16 
  106. save 900 1 
  107. save 300 10 
  108. save 60 10000 
  109. stop-writes-on-bgsave-error no 
  110. rdbcompression yes 
  111. rdbchecksum no 
  112. dbfilename dump.rdb 
  113. dir /usr/local/redis/data/6379 
  114. #slave-serve-stale-data yes 
  115. maxmemory 256mb 
  116. maxmemory-policy volatile-lru 
  117. maxmemory-samples 3 
  118. appendonly yes 
  119. appendfsync everysec 
  120. no-appendfsync-on-rewrite no 
  121. auto-aof-rewrite-percentage 100 
  122. auto-aof-rewrite-min-size 64mb 
  123. lua-time-limit 5000 
  124. slowlog-log-slower-than 10000 
  125. slowlog-max-len 1024 
  126. hash-max-ziplist-entries 512 
  127. hash-max-ziplist-value 64 
  128. list-max-ziplist-entries 512 
  129. list-max-ziplist-value 64 
  130. set-max-intset-entries 512 
  131. zset-max-ziplist-entries 128 
  132. zset-max-ziplist-value 64 
  133. activerehashing yes 
  134. client-output-buffer-limit normal 0 0 0 
  135. client-output-buffer-limit slave 256mb 64mb 60 
  136. client-output-buffer-limit pubsub 32mb 8mb 60 
  137. EOF 
  138.  
  139. # 5.redis启动停止脚本 
  140. redis_start="/etc/rc.d/init.d/redis" 
  141. cat >> ${redis_start} << "END" 
  142. #!/bin/bash 
  143. export PATH="/usr/local/redis/bin:$PATH" 
  144. EXEC="/usr/local/redis/bin/redis-server" 
  145. CLIEXEC="/usr/local/redis/bin/redis-cli" 
  146. PIDFILE="/usr/local/redis/run/redis_6379.pid" 
  147. CONF="/usr/local/redis/etc/redis_6379.conf" 
  148. PORT="6379" 
  149.  
  150. case "$1" in 
  151.     start) 
  152.         if [ -f $$PIDFILE ] 
  153.         then 
  154.                 echo "$PIDFILE exists, process is already running or crashed." 
  155.         else 
  156.                 echo "Starting Redis server..." 
  157.                 $EXEC $CONF 
  158.         fi 
  159.         ;; 
  160.     stop) 
  161.         if [ ! -f $PIDFILE ] 
  162.         then 
  163.                 echo "$PIDFILE does not exist, process is not running." 
  164.         else 
  165.                 PID=$(cat $PIDFILE
  166.                 echo "Stopping ..." 
  167.                 $CLIEXEC -p $PORT shutdown 
  168.                 while [ -x /proc/${PID} ] 
  169.                 do 
  170.                     echo "Waiting for Redis to shutdown ..." 
  171.                     sleep 1 
  172.                 done 
  173.                 echo "Redis stopped." 
  174.         fi 
  175.         ;; 
  176.     restart) 
  177.         $0 stop && $0 start 
  178.         ;; 
  179.     *) 
  180.         echo "Usage: $0 {start|stop|restart}" >&2 
  181.         exit 1 
  182.         ;; 
  183. esac 
  184. END 
  185.  
  186. #增加可执行权限 
  187. chmod u+x ${redis_start} 
  188.  
  189. # 6.启动redis 
  190. ${redis_start} start 
  191. if [[ $? == 0 ]]; then 
  192.     echo "redis start ......  OK" 
  193. else 
  194.     echo "redis start ...... Failure" 
  195. fi