centos7安装redis5

centos7安装redis5

单节点编译安装脚本

  • 安装位置/opt/redis5
  • 把脚本保存到redis.sh,执行sh redis.sh
  • 也可以分步执行每一行
  • 关闭防火墙:systemctl stop firewalld
#!/bin/bash
redisHome=/opt/redis5
binDir=${redisHome}/bin
confDir=${redisHome}/etc
dataDir=${redisHome}/data
customeFile=${confDir}/local.conf
backupFile=${confDir}/redis.conf.backup
# 安装依赖
yum install -y wget
yum install -y net-tools
yum -y install gcc gcc-c++ libstdc++-devel
# 本机IP
ip=$(ifconfig | grep "inet " | grep -Fv 127.0.0.1 |  awk '{print $2}' | tail -1)
# 下载
cd /opt
wget https://github.com/redis/redis/archive/5.0.10.tar.gz
# 解压
tar -zxf 5.0.10.tar.gz
# 安装到/opt/redis5目录下
cd redis-5.0.10
make
make PREFIX=${redisHome} install
# 复制配置文件
mkdir ${confDir}
cp redis.conf ${confDir}
cp ${confDir}/redis.conf ${backupFile}
mkdir ${dataDir}
# 清理
cd ..
rm -rf redis-5.0.10 5.0.10.tar.gz
# 自定义配置
touch ${customeFile}
echo "bind ${ip} 127.0.0.1" >> ${customeFile}
echo "dir ${dataDir}" >> ${customeFile}
echo "daemonize yes" >> ${customeFile}
echo "appendonly yes" >> ${customeFile}
echo "pidfile ${dataDir}/redis.pid" >> ${customeFile}
echo "logfile ${dataDir}/redis.log" >> ${customeFile}
# 引入自定义配置到主配置文件
echo "include ${customeFile}" >> ${confDir}/redis.conf
# 配置启动单节点redis
${binDir}/redis-server ${confDir}/redis.conf

查看配置项生效

  • 使用/opt/redis5/bin/redis-cli连接后执行
config get bind
config get daemonize
config get appendonly
config get dir

集群安装

  • 前提,已经安装好单节点

6节点集群配置

#!/bin/bash
portList=(6380 6381 6382 6383 6384 6385)
baseDir=/opt/redis5
ip=$(ifconfig | grep "inet " | grep -Fv 127.0.0.1 |  awk '{print $2}' | tail -1)
for port in ${portList[*]}
do
        echo start to create node ${port} conf
        curDir=${baseDir}/etc${port}
        mkdir ${curDir}
        dataDir=${baseDir}/data${port}
        mkdir ${dataDir}
        cp ${baseDir}/etc/redis.conf.backup ${curDir}/redis.conf
        curConf=${curDir}/local.conf
        touch ${curConf}
        echo "bind ${ip} 127.0.0.1" >> ${curConf}
        echo "daemonize yes" >> ${curConf}
        echo "appendonly yes" >> ${curConf}
        echo "port ${port}" >> ${curConf}
        echo "dir ${dataDir}" >> ${curConf}
        echo "pidfile ${curDir}/redis.pid" >> ${curConf}
        echo "logfile ${curDir}/redis.log" >> ${curConf}
        echo "cluster-enabled yes" >> ${curConf}
        echo "cluster-config-file ${curDir}/nodes-${port}.conf" >> ${curConf}
        cat ${curConf}
        echo "*********************************"
       	echo "include ${curConf}" >> ${curDir}/redis.conf
done

6节点集群启动

#!/bin/bash
portList=(6380 6381 6382 6383 6384 6385)
for port in ${portList[*]}
do
        echo starting node ${port} .
        /opt/redis5/bin/redis-server /opt/redis5/etc${port}/redis.conf
        echo starting node ${port} success.
done

组成6节点集群

/opt/redis5/bin/redis-cli --cluster create 127.0.0.1:6380 127.0.0.1:6381 127.0.0.1:6382 127.0.0.1:6383 127.0.0.1:6384 127.0.0.1:6385 --cluster-replicas 1 

查看集群信息

/opt/redis5/bin/redis-cli -c -h 127.0.0.1 -p 6380 cluster nodes
/opt/redis5/bin/redis-cli -c -h 127.0.0.1 -p 6380 cluster info
posted @ 2020-12-21 18:09  java拌饭  阅读(441)  评论(0编辑  收藏  举报