ubuntu下编译安装redis

ubuntu下编译安装redis

Tags: Redis Shell

摘要

ubuntu下编译安装redis

最新redis编译安装脚本

#!/bin/bash

cp_init_redis(){

init_file="/etc/init.d/redis-server"
 cat > ${init_file} <<EOF
#! /bin/sh
### BEGIN INIT INFO
# Provides:     redis-server
# Required-Start:   $syslog
# Required-Stop:    $syslog
# Should-Start:     $local_fs
# Should-Stop:      $local_fs
# Default-Start:    2 3 4 5
# Default-Stop:     0 1 6
# Short-Description:    redis-server - Persistent key-value db
# Description:      redis-server - Persistent key-value db
### END INIT INFO

PATH=/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin
DAEMON=/usr/local/redis/bin/redis-server
DAEMON_ARGS=/usr/local/redis/etc/redis.conf
NAME=redis-server
DESC=redis-server
PIDFILE=/var/run/redis.pid

test -x \$DAEMON || exit 0
test -x \$DAEMONBOOTSTRAP || exit 0

set -e

case "\$1" in
  start)
    echo -n "Starting $DESC: "
    touch \$PIDFILE
    chown redis:redis \$PIDFILE
    if start-stop-daemon --start --quiet --pidfile \$PIDFILE --chuid redis:redis --exec \$DAEMON -- \$DAEMON_ARGS
    then
      echo "[OK]"
    else
      echo "failed"
    fi
    ;;
  stop)
    echo -n "Stopping $DESC: "
    if start-stop-daemon --stop --retry 10 --quiet --oknodo --pidfile \$PIDFILE --exec \$DAEMON
    then
      echo "[OK]"
    else
      echo "failed"
    fi
    rm -f \$PIDFILE
    ;;

  restart|force-reload)
    ${init_file} stop
    ${init_file} start
    ;;
  *)
    echo "Usage:  ${init_file}  {start|stop|restart|force-reload}" >&2
    exit 1
    ;;
esac

exit 0
EOF

chmod 777 ${init_file}
}




Install_redis-server() {

    Mem=`free -m | awk '/Mem:/{print $2}'`
    Swap=`free -m | awk '/Swap:/{print $2}'`
    redis_version="3.2.5";
    redis_install_dir="/usr/local/redis"
    oneinstack_dir="/tmp"
    pushd ${oneinstack_dir}

    if [ ! -f  "${oneinstack_dir}/redis-${redis_version}.tar.gz" ] ;then
    wget -O redis-${redis_version}.tar.gz  http://download.redis.io/releases/redis-${redis_version}.tar.gz 
    fi

    THREAD=$(grep 'processor' /proc/cpuinfo | sort -u | wc -l)
    if [ "$(getconf WORD_BIT)" == "32" ] && [ "$(getconf LONG_BIT)" == "64" ]; then
          OS_BIT=64
          SYS_BIG_FLAG=x64 #jdk
          SYS_BIT_a=x86_64;SYS_BIT_b=x86_64; #mariadb
        else
          OS_BIT=32
          SYS_BIG_FLAG=i586
          SYS_BIT_a=x86;SYS_BIT_b=i686;
    fi

  tar xzf redis-${redis_version}.tar.gz
  pushd redis-${redis_version}
  if [ "$OS_BIT" == '32' ]; then
    sed -i '1i\CFLAGS= -march=i686' src/Makefile
    sed -i 's@^OPT=.*@OPT=-O2 -march=i686@' src/.make-settings
  fi
  make -j ${THREAD}
  if [ -f "src/redis-server" ]; then
    mkdir -p ${redis_install_dir}/{bin,etc,var}
    /bin/cp src/{redis-benchmark,redis-check-aof,redis-check-rdb,redis-cli,redis-sentinel,redis-server} ${redis_install_dir}/bin/
    /bin/cp redis.conf ${redis_install_dir}/etc/
    ln -s ${redis_install_dir}/bin/* /usr/local/bin/
    sed -i 's@pidfile.*@pidfile /var/run/redis.pid@' ${redis_install_dir}/etc/redis.conf
    sed -i "s@logfile.*@logfile ${redis_install_dir}/var/redis.log@" ${redis_install_dir}/etc/redis.conf
    sed -i "s@^dir.*@dir ${redis_install_dir}/var@" ${redis_install_dir}/etc/redis.conf
    sed -i 's@daemonize no@daemonize yes@' ${redis_install_dir}/etc/redis.conf
    sed -i "s@^# bind 127.0.0.1@bind 127.0.0.1@" ${redis_install_dir}/etc/redis.conf
    redis_maxmemory=`expr $Mem / 8`000000
    [ -z "`grep ^maxmemory ${redis_install_dir}/etc/redis.conf`" ] && sed -i "s@maxmemory <bytes>@maxmemory <bytes>\nmaxmemory `expr $Mem / 8`000000@" ${redis_install_dir}/etc/redis.conf
    echo "${CSUCCESS}Redis-server installed successfully! ${CEND}"
    popd
    rm -rf redis-${redis_version}
    id -u redis >/dev/null 2>&1
    [ $? -ne 0 ] && useradd -M -s /sbin/nologin redis
    chown -R redis:redis ${redis_install_dir}/var
    #/bin/cp ../init.d/Redis-server-init /etc/init.d/redis-server
    cp_init_redis


    if [ "$OS" == 'CentOS' ]; then
      cc start-stop-daemon.c -o /sbin/start-stop-daemon
      chkconfig --add redis-server
      chkconfig redis-server on
    elif [[ $OS =~ ^Ubuntu$|^Debian$ ]]; then
      update-rc.d redis-server defaults
    fi
    sed -i "s@/usr/local/redis@${redis_install_dir}@g" /etc/init.d/redis-server
    #[ -z "`grep 'vm.overcommit_memory' /etc/sysctl.conf`" ] && echo 'vm.overcommit_memory = 1' >> /etc/sysctl.conf
    #sysctl -p
    service redis-server restart
  else
    rm -rf ${redis_install_dir}
    echo "${CFAILURE}Redis-server install failed, Please contact the author! ${CEND}"
    kill -9 $$
  fi
  popd
}

Install_redis-server

一键执行

wget -O install_redis.sh https://coding.net/u/javacore/p/dev-env-shell/git/raw/master/install_redis.sh && bash install_redis.sh

参考

其实是直接拷贝的 https://github.com/lj2007331/oneinstack/blob/master/include/redis.sh
然后稍加修改而已.

配置说明

配置文件位置,默认 bind 127.0.0.1 只允许本机访问
/usr/local/redis/etc/redis.conf

启动,关闭,重启
Usage: /etc/init.d/redis-server {start|stop|restart|force-reload}

当然如果是Ubuntu下也可以直接使用
service redis-service {start|stop|restart|force-reload}