centos7脚本一键安装redis5.0.9集群
说明使用方法脚本和资源包启动脚本主运行脚本
说明
1.本文分享,实现的是单机redis5.0.9三主三从的一键安装。 2.安装环境:在centos7和ubuntu7下,都可以一键安装成功。主要以centos7为准。 3.本安装是离线安装,不依赖网络。 4.端口是从8001~8006 5.无论防火墙是否启动。启动脚本中都进行了处理。都可以安装成功。 6.只有用root账号才能安装成功,非root用户,启动脚本会有提示。 7.如果用户环境安装的有redis5.0.9,并已经启动,需注意,该脚本会关闭redis5.0.9.因为会重新安装redis5.0.9 8.全程只需要上传本文提供的压缩包、脚本,启动脚本即可,不需其他任何操作 9.多机redis集群,亦可以参考本文。 10.最后贴出的是启动脚本链接和需要的资源链接。
使用方法
1.将redis_127.zip和start_redis_127.sh放到centos任意目录。 2.执行
sh start_redis_127.sh
30秒左右,集群就能安装完成
脚本和资源包
链接:https://pan.baidu.com/s/1ldxd70iDKKzhnO-ePtQpxQ 提取码:5y8h
启动脚本
#!/bin/sh
function f_unzipAndStart
(){
resource_name
="redis_127"
unzip -o
${resource_name}.zip
cd ${resource_name}
sh ./kill.sh
"default"
FLAG
=$?
if [ $FLAG != 0
]
then
echo "------杀死redis进程失败,停止安装"
exit;
else
sh ./install_redis_127.sh
fi
}
f_unzipAndStart
主运行脚本
#!/bin/bash
START_PORT
=8001
REDIS_CLUSTER_IP_1
="127.0.0.1"
REDIS_CLUSTER_IP_2
="127.0.0.1"
REDIS_CLUSTER_IP_3
="127.0.0.1"
PASSWORD
="wang"
SINGLE_MACHINE_CLUSTER_COUNT
=6
REDIS_BASE_PATH
="/usr/local/redis-5.0.9"
REDIS_CLUSTER_PATH
="/usr/local/redis-cluster"
FIREWALLD_STATUS
=`firewall-cmd --state`
function f_editConfig
(){
file_name
=$1
old_str
=$2
new_str
=$3
echo "$old_str 替换成----> $new_str"
sed -i
"s|$old_str|$new_str|g" $file_name
}
function f_checkIsRoot
(){
if [ $UID -ne 0
]
then
echo "-----------------------------------权限不足...请切换至root用户"
exit;
fi
}
function f_setup_gcc
(){
gcc -v
&>/tmp/null
FLAG
=$?
if [ $FLAG != 0
]
then
rpm -Uvh ./gcc/*.rpm --nodeps --force
;rpm -Uvh ./gcc++/*.rpm --nodeps --force
else
echo "------gcc was setup------"
echo "------redis is installing------"
fi
}
function f_addPortToFireWalld
(){
if [ $FIREWALLD_STATUS ]
then
newPort
=$1
is_open
=`firewall-cmd --query-port=${newPort}/tcp`
if [ $is_open = "no" ];then
firewall-cmd --zone
=public --add-port
=${newPort}/tcp --permanent
echo "------${newPort}端口添加到防火墙白名单"
fi
fi
}
function f_restartFirewalld
(){
if [ $FIREWALLD_STATUS ];then
systemctl restart firewalld
echo "------防火墙重启"
fi
}
f_checkIsRoot
START_PATH
=`pwd`
cd $START_PATH
tar -xvzf redis-5.0.9.tar.gz
tar -xvzf gcc.tar.gz
tar -xvzf gcc++.tar.gz
rm -rf
${REDIS_BASE_PATH}
rm -rf
${REDIS_CLUSTER_PATH}
echo `pwd`
echo "${START_PATH}/redis-5.0.9"
cp -R
${START_PATH}/redis-5.0.9 /usr/local/
f_setup_gcc
mkdir -p
${REDIS_CLUSTER_PATH}
i
=0
while (($i<$SINGLE_MACHINE_CLUSTER_COUNT))
do
port
=`expr $START_PORT + $i`
echo "--------------------------------端口:$port"
mkdir -p
${REDIS_CLUSTER_PATH}/
${port}/
cp ${REDIS_BASE_PATH}/redis.conf
"${REDIS_CLUSTER_PATH}/${port}/"
f_editConfig
${REDIS_CLUSTER_PATH}/
${port}/redis.conf
"daemonize no" "daemonize yes"
f_editConfig
${REDIS_CLUSTER_PATH}/
${port}/redis.conf
"port 6379" "port ${port}"
f_editConfig
${REDIS_CLUSTER_PATH}/
${port}/redis.conf
"dir ./" "dir ${REDIS_CLUSTER_PATH}/${port}/"
f_editConfig
${REDIS_CLUSTER_PATH}/
${port}/redis.conf
"# cluster-enabled yes" "cluster-enabled yes"
f_editConfig
${REDIS_CLUSTER_PATH}/
${port}/redis.conf
"# cluster-config-file nodes-6379.conf" "cluster-config-file nodes-${port}.conf"
f_editConfig
${REDIS_CLUSTER_PATH}/
${port}/redis.conf
"# cluster-enabled yes" "cluster-enabled yes"
f_editConfig
${REDIS_CLUSTER_PATH}/
${port}/redis.conf
"# cluster-node-timeout 15000" "cluster-node-timeout 5000"
f_editConfig
${REDIS_CLUSTER_PATH}/
${port}/redis.conf
"bind 127.0.0.1" "#bind 127.0.0.1"
f_editConfig
${REDIS_CLUSTER_PATH}/
${port}/redis.conf
"protected-mode yes" "protected-mode no"
f_editConfig
${REDIS_CLUSTER_PATH}/
${port}/redis.conf
"appendonly no" "appendonly yes"
f_editConfig
${REDIS_CLUSTER_PATH}/
${port}/redis.conf
"pidfile /var/run/redis_6379.pid" "pidfile /var/run/redis_${port}.pid"
f_editConfig
${REDIS_CLUSTER_PATH}/
${port}/redis.conf
"# requirepass foobared" "requirepass ${PASSWORD}"
f_editConfig
${REDIS_CLUSTER_PATH}/
${port}/redis.conf
"# masterauth <master-password>" "masterauth ${PASSWORD}"
i
=`expr $i + 1`
done
cd ${REDIS_BASE_PATH}
make MALLOC
=libc
make && make install
i
=0
while (($i<$SINGLE_MACHINE_CLUSTER_COUNT))
do
port
=`expr $START_PORT + $i`
/usr/local/redis-5.0.9/src/redis-server /usr/local/redis-cluster/
$port/redis.conf
;
i
=`expr $i + 1`
f_addPortToFireWalld
$port
done
f_restartFirewalld
echo yes|/usr/local/redis-5.0.9/src/redis-cli -a wang --cluster create --cluster-replicas 1
${REDIS_CLUSTER_IP_1}:8001
${REDIS_CLUSTER_IP_1}:8002
${REDIS_CLUSTER_IP_2}:8003
${REDIS_CLUSTER_IP_2}:8004
${REDIS_CLUSTER_IP_3}:8005
${REDIS_CLUSTER_IP_3}:8006