shell脚本企业实战系列-redis主从集群搭建脚本

#!/bin/bash
#editor by yhzong
#date 2020-08-04
#redis主从模式搭建,会提示你输入redis是主节点还是从节点
echo "
      ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
      + 1、此脚本为redis主从脚本,请首先确定安装机器是主还是从.  +
      + 2、此脚本默认的版本是redis-5.0.5,如果你需要的版本不是这  +
      +	   这个,请修改脚本中的redis版本号		         +
      +  3、默认的安装路径在/home下,如果你要改变安装路径,请修  +
      +     改脚本中安装路径          			         +
      +  4、运行脚本会让您选择是主节点还是从节点,如果是从节点会 +
      +     让你输入主节点ip地址自动安装                         +
      ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
"
read -p "请确认该安装节点是主节点还是从节点,主节点输入1,从节点输入0: " redis_info
#确定主从节点的ip地址
if [ ${redis_info} -eq 1 ];then
	echo "继续下面的安装"
elif [ ${redis_info} -eq 0 ];then
	echo "请输入您的主节点ip地址"
	read -p "redis主节点materip地址为: " masterip
else 
	exit 1
	echo "请检查您的输入是否有问题,将推出脚本的运行"
fi

#安装常用的软件包
yum install -y nmap unzip wget lsof xz net-tools gcc make gcc-c++

#下载redis-5.0.5的版本
redis_setup=5.0.5
redis_path=/home/redis
port=6379
#wget http://download.redis.io/releases/redis-${redis_setup}.tar.gz

#解压redis安装包
tar zxvf redis-${redis_setup}.tar.gz

#创建redis安装日志、启动、配置文件等目录
cd redis-${redis_setup}
mkdir  -p ${redis_path}
mkdir  -p ${redis_path}/bin
mkdir  -p ${redis_path}/log
mkdir  -p ${redis_path}/conf
cp redis.conf ${redis_path}/conf

#编译并安装
make MALLOC=jemalloc && make PREFIX=${redis_path} install

#修改配置文件,可以后台启动
if [ ${redis_info} -eq 1 ];then
	sed -i 's/daemonize no/daemonize yes/g' ${redis_path}/conf/redis.conf
	sed -i 's/bind 127.0.0.1/bind 0.0.0.0/g'  ${redis_path}/conf/redis.conf
	sed -i 's/port 6379/port '${port}'/'  ${redis_path}/conf/redis.conf
	sed -i "s#pidfile \/var\/run\/redis_6379.pid#pidfile ${redis_path}\/redis_${port}.pid#g" ${redis_path}/conf/redis.conf
	sed -i "s#logfile \"\"#logfile ${redis_path}\/log\/redis.log#g"  ${redis_path}/conf/redis.conf
elif [ ${redis_info} -eq 0 ];then
	sed -i 's/daemonize no/daemonize yes/g' ${redis_path}/conf/redis.conf
        sed -i 's/bind 127.0.0.1/bind 0.0.0.0/g'  ${redis_path}/conf/redis.conf
        sed -i 's/port 6379/port '${port}'/'  ${redis_path}/conf/redis.conf
        sed -i "s#pidfile \/var\/run\/redis_6379.pid#pidfile ${redis_path}\/redis_${port}.pid#g" ${redis_path}/conf/redis.conf
        sed -i "s#logfile \"\"#logfile ${redis_path}\/log\/redis.log#g"  ${redis_path}/conf/redis.conf
   	echo "slaveof ${masterip} ${port}" >> ${redis_path}/conf/redis.conf
fi

#优化参数
echo vm.overcommit_memory=1 >> /etc/sysctl.conf
echo never > /sys/kernel/mm/transparent_hugepage/enabled
echo "net.core.somaxconn=1024">> /etc/sysctl.conf
sysctl -p

#加入到开机的环境变量里面去
echo "export PATH=${redis_path}/bin:$PATH">>/etc/profile
source /etc/profile

#启动redis
redis-server ${redis_path}/conf/redis.conf

#检测redis是否启动
status=`netstat -nltp|grep ${port}|grep -v "grep"`
if [ -z "${status}" ];then
  echo "please check the config"
else
  echo "redis successful install"
fi
echo "您已经成功完成安装。安装路径为${redis_path}/"


你可能感兴趣的:(脚本shell,shell,负载均衡,运维)