shell脚本一键部署redis

Posted 码海小虾米_

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了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

以上是关于shell脚本一键部署redis的主要内容,如果未能解决你的问题,请参考以下文章

shell脚本一键部署redis

shell脚本一键部署redis

shell脚本一键部署redis

Shell脚本------一键部署Redis

Shell脚本------一键部署Redis

shell脚本集合