shell脚本一键部署redis

shell脚本一键部署redis

脚本说明:
1、版本为:redis-6.2.6.tar.gz,下载地址为:https://download.redis.io/releases/redis-6.2.6.tar.gz
2、需要有yum安装gcc和gcc-c++,自行安装下
3、若需要部署三台服务器6节点的集群,每台服务器分别执行脚本,然后选择集群模式,部署完执行添加集群命令即可,命令如下:

建立集群:在任意服务器上输入命令:(redis有密码,需要在改命令后加 -a <密码>
./redis-cli --cluster create 192.168.81.225:6379 192.168.81.225:6380 192.168.81.227:6379 192.168.81.227:6380 192.168.81.228:6379 192.168.81.228:6380  --cluster-replicas 1 -a <密码>

脚本内容如下:

#!/bin/sh
#tar包部署安装redis单机
#tar包部署安装redis单机2实例脚本,占用端口63796380

#判断有无要安装的redis包
if [ -e /data/redis-6.2.6.tar.gz ];then
    continue
else
    echo -e '\033[35m /data目录中没有安装包[redis-6.2.6.tar.gz]\033[0m'
    exit
fi

bushu(){
#获取密码和本机ip
read -p "输入你要创建redis用户的密码: " passwd
ip=`/sbin/ifconfig -a|grep inet|grep -v 127.0.0.1|grep -v inet6|awk 'NR==1 {print $2}'`

#解压并安装
cd /data
tar xf redis-6.2.6.tar.gz
cd redis-6.2.6
make && make install 
}

danji(){
#创建redis启动63796380所需的目录和配置文件
mkdir -p /data/redis-6.2.6/conf/6379
/usr/bin/cp /data/redis-6.2.6/redis.conf /data/redis-6.2.6/conf/6379/redis_6379.conf

#修改相关配置文件
sed -i "s/^bind.*$/bind ${ip}/" conf/6379/redis_6379.conf

sed -i "s/protected-mode yes/protected-mode no/" conf/6379/redis_6379.conf

sed -i "s/^timeout 0/timeout 300/" conf/6379/redis_6379.conf

sed -i "s/^daemonize no/daemonize yes/" conf/6379/redis_6379.conf

sed -i "s/^pidfile.*$/pidfile \/data\/redis-6.2.6\/conf\/6379\/redis_6379.pid/" conf/6379/redis_6379.conf

sed -i "s/^logfile.*$/logfile \/data\/redis-6.2.6\/conf\/6379\/redis_6379.log/" conf/6379/redis_6379.conf

sed -i "s/^dir.*/dir \/data\/redis-6.2.6\/conf\/6379/" conf/6379/redis_6379.conf

sed -i "s/^appendfsync .*/appendfsync always/" conf/6379/redis_6379.conf

## 设置本机和集群密码
sed -i "s/^# requirepass.*/requirepass ${passwd}/" conf/6379/redis_6379.conf

#进入src目录下启动redis
/data/redis-6.2.6/src/redis-server /data/redis-6.2.6/conf/6379/redis_6379.conf
}

jiqun(){
mkdir -p /data/redis-6.2.6/conf/6379
mkdir -p /data/redis-6.2.6/conf/6380
/usr/bin/cp /data/redis-6.2.6/redis.conf /data/redis-6.2.6/conf/6379/redis_6379.conf
/usr/bin/cp /data/redis-6.2.6/redis.conf /data/redis-6.2.6/conf/6380/redis_6380.conf

#修改相关配置文件
sed -i "s/^bind.*$/bind ${ip}/" conf/6379/redis_6379.conf
sed -i "s/^bind.*$/bind ${ip}/" conf/6380/redis_6380.conf

sed -i "s/protected-mode yes/protected-mode no/" conf/6379/redis_6379.conf
sed -i "s/protected-mode yes/protected-mode no/" conf/6380/redis_6380.conf

sed -i "s/^port 6379/port 6380/" conf/6380/redis_6380.conf

sed -i "s/^timeout 0/timeout 300/" conf/6379/redis_6379.conf
sed -i "s/^timeout 0/timeout 300/" conf/6380/redis_6380.conf

sed -i "s/^daemonize no/daemonize yes/" conf/6379/redis_6379.conf
sed -i "s/^daemonize no/daemonize yes/" conf/6380/redis_6380.conf

sed -i "s/^pidfile.*$/pidfile \/data\/redis-6.2.6\/conf\/6379\/redis_6379.pid/" conf/6379/redis_6379.conf
sed -i "s/^pidfile.*$/pidfile \/data\/redis-6.2.6\/conf\/6380\/redis_6380.pid/" conf/6380/redis_6380.conf

sed -i "s/^logfile.*$/logfile \/data\/redis-6.2.6\/conf\/6379\/redis_6379.log/" conf/6379/redis_6379.conf
sed -i "s/^logfile.*$/logfile \/data\/redis-6.2.6\/conf\/6380\/redis_6380.log/" conf/6380/redis_6380.conf

sed -i "s/^dir.*/dir \/data\/redis-6.2.6\/conf\/6379/" conf/6379/redis_6379.conf
sed -i "s/^dir.*/dir \/data\/redis-6.2.6\/conf\/6380/" conf/6380/redis_6380.conf

sed -i "s/^appendfsync .*/appendfsync always/" conf/6379/redis_6379.conf
sed -i "s/^appendfsync .*/appendfsync always/" conf/6380/redis_6380.conf

sed -i "s/^# cluster-enabled.*/cluster-enabled yes/" conf/6379/redis_6379.conf
sed -i "s/^# cluster-enabled.*/cluster-enabled yes/" conf/6380/redis_6380.conf

sed -i "s/^# cluster-config-file.*/cluster-config-file \/data\/redis-6.2.6\/conf\/6379\/nodes-6379.conf/" conf/6379/redis_6379.conf
sed -i "s/^# cluster-config-file.*/cluster-config-file \/data\/redis-6.2.6\/conf\/6380\/nodes-6380.conf/" conf/6380/redis_6380.conf

sed -i "s/^# cluster-node-timeout.*/cluster-node-timeout 15000/" conf/6379/redis_6379.conf
sed -i "s/^# cluster-node-timeout.*/cluster-node-timeout 15000/" conf/6380/redis_6380.conf

## 设置本机和集群密码
sed -i "s/^# requirepass.*/requirepass ${passwd}/" conf/6379/redis_6379.conf
sed -i "s/^# requirepass.*/requirepass ${passwd}/" conf/6380/redis_6380.conf

sed -i "s/^# masterauth.*/masterauth ${passwd}/" conf/6379/redis_6379.conf
sed -i "s/^# masterauth.*/masterauth ${passwd}/" conf/6380/redis_6380.conf

#进入src目录下启动redis
/data/redis-6.2.6/src/redis-server /data/redis-6.2.6/conf/6379/redis_6379.conf
/data/redis-6.2.6/src/redis-server /data/redis-6.2.6/conf/6380/redis_6380.conf
}

## 选择单机
read -p "部署单机输入[1] 集群部署(单机2实例)输入[2]: " choose
if [ $choose == 1 ];then
  bushu
  danji
  #查看进程
  ps -ef |grep redis
elif [ $choose == 2 ];then
  bushu
  jiqun
  #查看进程
  if netstat -natp |grep 6379 &> /dev/null && netstat -natp |grep 6380 &> /dev/null
  then
    ps -ef |grep redis
    echo "单机2实例部署完成,全部完成后执行加集群命令即可!"
  else
    echo "单机2实例部署失败,服务未启动,请检查......"
  fi
fi

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

码海小虾米_

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值