Redis安裝指令碼

2020-08-09 11:53:42

安裝環境:Centos7.3

安裝包:redis-5.0.3.tar.gz

用法:

1)將安裝包和伺服器上傳至伺服器的/data/src/目錄

2)執行指令碼:sh install_redis.sh

廢話不多說,直接上指令碼:vim install_redis.sh

#!/bin/bash
Redis_Tar_Path="/data/src/redis-5.0.3.tar.gz"
#redis安裝目錄
Redis_Dir="/data/software/redis"
#master的ip
master_ip="192.168.171.23"
#redis密碼
mima="Redis789@789"



function install(){
    if [ ! -d "/data/src/redis" ]; then
        echo -------建立解壓路徑-------
        mkdir -p "/data/src/redis"
    fi
    if [ ! -d ${Redis_Dir} ];then
        echo -------建立redis安裝目錄-------
        mkdir -p ${Redis_Dir}
    fi
    tar -xf ${Redis_Tar_Path} -C /data/src/redis
    echo ${Redis_Tar_Path}
    mv /data/src/redis/*/* ${Redis_Dir}
    rm -rf /data/src/redis/*
    cd ${Redis_Dir}
    # 判斷是否支援gcc命令,不支援則安裝
    gcc -v || yum -y install gcc+ gcc-c++
    make MALLOC=libc
}


function change_conf(){
	echo 
	sed -i 's/^bind 127.*/bind 0\.0\.0\.0/g' $Redis_Dir/redis.conf
	sed -i 's/^protected-mode yes/protected-mode no/g' $Redis_Dir/redis.conf
	sed -i 's/^daemonize no/daemonize yes/g' $Redis_Dir/redis.conf
	sed -i 's/^logfile .*/logfile \"\/data\/logs\/redis\.log\"/g' $Redis_Dir/redis.conf
	echo 'masterauth "'`echo $mima`'"' >> $Redis_Dir/redis.conf
	echo 'requirepass "'`echo $mima`'"' >> $Redis_Dir/redis.conf
	
	sed -i "s/^sentinel monitor mymaster 127.*\.1/sentinel monitor mymaster ${master_ip}/g" $Redis_Dir/sentinel.conf
	sed -ri 's/(down-after-milliseconds mymaster) 30000/\1 3000/g' $Redis_Dir/sentinel.conf
	sed -ri 's/(failover-timeout mymaster) 180000/\1 10000/g' $Redis_Dir/sentinel.conf
	sed -ri 's/# protected-mode no/protected-mode no/g' $Redis_Dir/sentinel.conf
	sed -ri 's/^logfile .*/logfile \"\/data\/logs\/sentinel6379\.log\"/g' $Redis_Dir/sentinel.conf
	echo "sentinel auth-pass mymaster ${mima}" >> $Redis_Dir/sentinel.conf
	hostname -I | grep $master_ip
	if [ $? -ne 0 ];then
		echo "slaveof ${master_ip} 6379" >> $Redis_Dir/redis.conf
	fi
	
}

function qidong(){
	$Redis_Dir/src/redis-server $Redis_Dir/redis.conf
	sleep 2
	$Redis_Dir/src/redis-sentinel $Redis_Dir/sentinel.conf &
	sleep 2
	ps -aux | grep 'redis-server' | grep -v 'grep'
	if [ $? -ne 0 ];then
		echo "redis-server未成功啓動"
	else
		echo "redis-server已啓動" 
	fi
	
	ps -aux | grep 'redis-sentinel' | grep -v 'grep'
	if [ $? -ne 0 ];then
		echo "redis-sentinel未成功啓動"
	else
		echo "redis-sentinel已啓動" 
	fi
	
	
}

install
change_conf
qidong