腳本功能linux
redis單機單實例一鍵安裝腳本redis
注意事項bash
1.僅適用於Linux/Centos 64位多線程
2.安裝時需聯網app
- #!/bin/bash
- # 2013-1-10 LEO chanyipiaomiao@163.com
- # Blog: http://linux5588.blog.51cto.com
- # 腳本功能
- # redis單機單實例一鍵安裝腳本
- # 注意事項
- # 僅適用於Linux/Centos 64位
- # 安裝時需聯網
- # 步驟
- # 1.下載並安裝libunwind軟件包(是TCMalloc依賴包)
- # 2.下載並安裝TCMalloc
- # 3.下載並安裝redis
- # 4.配置redis
- # 5.準備redis啓動中止腳本
- # 6.啓動redis
- #輸出PATH變量
- export PATH=/usr/kerberos/sbin:/usr/kerberos/bin:/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin:/root/bin
- #定義存放軟件目錄
- software="/root/software"
- #若是軟件目錄不存在則新建該目錄
- if [[ ! -e $software ]]; then
- mkdir -p $software
- fi
- #定義判斷是否安裝成功函數
- function installIsOK(){
- if [[ $2 == 0 ]]; then
- echo "$1 install ...... OK !"
- else
- echo "$1 install ...... Failure!"
- exit 1
- fi
- }
- #進入軟件目錄
- cd $software
- # 1.下載並安裝libunwind軟件包(是TCMalloc依賴包)
- libunwind='libunwind-1.1'
- wget http://download.savannah.gnu.org/releases/libunwind/${libunwind}.tar.gz
- tar zxf ${libunwind}.tar.gz
- cd $libunwind
- ./configure && make && make install
- if [[ $? == 0 ]]; then
- installIsOK ${libunwind} 0
- else
- installIsOK ${libunwind} 1
- fi
- cd $software
- # 2.下載並安裝TCMalloc
- # TCMalloc (google-perftools) 是用於優化C++寫的多線程應用,比glibc 2.3的malloc快。這個模塊能夠用來優化redis性能
- gperftools='gperftools-2.0'
- wget http://gperftools.googlecode.com/files/${gperftools}.tar.gz
- tar zxf ${gperftools}.tar.gz
- cd $gperftools
- ./configure
- make && make install
- if [[ $? == 0 ]]; then
- installIsOK ${gperftools} 0
- else
- installIsOK ${gperftools} 2
- fi
- echo "/usr/local/lib" > /etc/ld.so.conf.d/usr_local_lib.conf
- ldconfig
- cd $software
- # 3.下載並安裝redis
- redis='redis-2.6.7'
- redis_dir='/usr/local/redis'
- wget http://redis.googlecode.com/files/${redis}.tar.gz
- tar zxf ${redis}.tar.gz
- cd $redis
- make PREFIX=${redis_dir} USE_TCMALLOC=yes install
- if [[ $? == 0 ]]; then
- installIsOK ${redis} 0
- else
- installIsOK ${redis} 3
- fi
- # 4.配置redis
- mkdir -p ${redis_dir}/etc
- mkdir -p ${redis_dir}/run
- mkdir -p ${redis_dir}/data/6379
- mkdir -p ${redis_dir}/log
- cp redis.conf ${redis_dir}/redis.conf
- #cp ${redis_dir}/redis.conf ${redis_dir}/etc/redis_6379.conf
- #生成配置文件
- redis_6379="${redis_dir}/etc/redis_6379.conf"
- cat >> ${redis_6379} << "EOF"
- daemonize yes
- pidfile /usr/local/redis/run/redis_6379.pid
- port 6379
- #bind 127.0.0.1
- timeout 300
- loglevel notice
- logfile /usr/local/redis/log/redis.log
- databases 16
- save 900 1
- save 300 10
- save 60 10000
- stop-writes-on-bgsave-error no
- rdbcompression yes
- rdbchecksum no
- dbfilename dump.rdb
- dir /usr/local/redis/data/6379
- #slave-serve-stale-data yes
- maxmemory 256mb
- maxmemory-policy volatile-lru
- maxmemory-samples 3
- appendonly yes
- appendfsync everysec
- no-appendfsync-on-rewrite no
- auto-aof-rewrite-percentage 100
- auto-aof-rewrite-min-size 64mb
- lua-time-limit 5000
- slowlog-log-slower-than 10000
- slowlog-max-len 1024
- hash-max-ziplist-entries 512
- hash-max-ziplist-value 64
- list-max-ziplist-entries 512
- list-max-ziplist-value 64
- set-max-intset-entries 512
- zset-max-ziplist-entries 128
- zset-max-ziplist-value 64
- activerehashing yes
- client-output-buffer-limit normal 0 0 0
- client-output-buffer-limit slave 256mb 64mb 60
- client-output-buffer-limit pubsub 32mb 8mb 60
- EOF
- # 5.redis啓動中止腳本
- redis_start="/etc/rc.d/init.d/redis"
- cat >> ${redis_start} << "END"
- #!/bin/bash
- export PATH="/usr/local/redis/bin:$PATH"
- EXEC="/usr/local/redis/bin/redis-server"
- CLIEXEC="/usr/local/redis/bin/redis-cli"
- PIDFILE="/usr/local/redis/run/redis_6379.pid"
- CONF="/usr/local/redis/etc/redis_6379.conf"
- PORT="6379"
- 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 $PORT shutdown
- while [ -x /proc/${PID} ]
- do
- echo "Waiting for Redis to shutdown ..."
- sleep 1
- done
- echo "Redis stopped."
- fi
- ;;
- restart)
- $0 stop && $0 start
- ;;
- *)
- echo "Usage: $0 {start|stop|restart}" >&2
- exit 1
- ;;
- esac
- END
- #增長可執行權限
- chmod u+x ${redis_start}
- # 6.啓動redis
- ${redis_start} start
- if [[ $? == 0 ]]; then
- echo "redis start ...... OK"
- else
- echo "redis start ...... Failure"
- fi