Redis6 集群单机安装
官网下载
https://download.redis.io/releases/redis-6.2.6.tar.gz
http://download.redis.io/redis-stable.tar.gz
文档参考
https://redis.io/documentation
解压编译
#安装编译需要的tcl
yum -y install gcc tcl
rm -rf /data/redis
mkdir -p /data/redis/bin
mkdir -p /data/redis/data
mkdir -p /data/redis/conf
mkdir -p /data/redis/log
mkdir -p /data/redis/run
cd /data/redis
wget http://download.redis.io/redis-stable.tar.gz
#wget https://download.redis.io/releases/redis-6.2.6.tar.gz -O redis-stable.tar.gz
tar xvzf redis-stable.tar.gz
cd /data/redis/redis-stable
sudo make install PREFIX=/data/redis
配置准备
#添加到全局环境
cd /data/redis/redis-stable
sudo cp src/redis-server /usr/local/bin/
sudo cp src/redis-cli /usr/local/bin/
#拷贝模板配置文件
cd /data/redis/redis-stable
\cp redis.conf /data/redis/conf/7100.conf
\cp redis.conf /data/redis/conf/7101.conf
\cp redis.conf /data/redis/conf/7102.conf
\cp redis.conf /data/redis/conf/7103.conf
\cp redis.conf /data/redis/conf/7104.conf
\cp redis.conf /data/redis/conf/7105.conf
mkdir -p /data/redis/data/7100
mkdir -p /data/redis/data/7101
mkdir -p /data/redis/data/7102
mkdir -p /data/redis/data/7103
mkdir -p /data/redis/data/7104
mkdir -p /data/redis/data/7105
修改配置
cd /data/redis/conf
sed -i "s/daemonize no/daemonize yes/g" *.conf
sed -i "s/appendonly no/appendonly yes/g" *.conf #持久化
sed -i "s/# cluster-enabled yes/cluster-enabled yes/g" *.conf #开启集群
sed -i "s/# requirepass foobared/requirepass Xskj@2021/g" *.conf #修改密码
sed -i "s/loglevel notice/loglevel warning/g" *.conf #更改日志级别
sed -i "s/port 6379/port 7100/g" 7100.conf
sed -i "s/port 6379/port 7101/g" 7101.conf
sed -i "s/port 6379/port 7102/g" 7102.conf
sed -i "s/port 6379/port 7103/g" 7103.conf
sed -i "s/port 6379/port 7104/g" 7104.conf
sed -i "s/port 6379/port 7105/g" 7105.conf
sed -i "s/dir .\//dir \/data\/redis\/data\/7100/g" 7100.conf
sed -i "s/dir .\//dir \/data\/redis\/data\/7101/g" 7101.conf
sed -i "s/dir .\//dir \/data\/redis\/data\/7102/g" 7102.conf
sed -i "s/dir .\//dir \/data\/redis\/data\/7103/g" 7103.conf
sed -i "s/dir .\//dir \/data\/redis\/data\/7104/g" 7104.conf
sed -i "s/dir .\//dir \/data\/redis\/data\/7105/g" 7105.conf
#指定日志文件,默认直接到/dev/null 如果指定了要做日志切割,redis不会自己滚动日志
#sed -i "s/logfile \"\"/logfile \"\/data\/redis\/log\/7100.log\"/g" 7100.conf
#sed -i "s/logfile \"\"/logfile \"\/data\/redis\/log\/7101.log\"/g" 7101.conf
#sed -i "s/logfile \"\"/logfile \"\/data\/redis\/log\/7102.log\"/g" 7102.conf
#sed -i "s/logfile \"\"/logfile \"\/data\/redis\/log\/7103.log\"/g" 7103.conf
#sed -i "s/logfile \"\"/logfile \"\/data\/redis\/log\/7104.log\"/g" 7104.conf
#sed -i "s/logfile \"\"/logfile \"\/data\/redis\/log\/7105.log\"/g" 7105.conf
#集群节点配置文件,这个文件是不能手动编辑的。确保每一个集群节点的配置文件不同
sed -i "s/# cluster-config-file nodes-6379.conf/cluster-config-file nodes-7100.conf/g" 7100.conf
sed -i "s/# cluster-config-file nodes-6379.conf/cluster-config-file nodes-7101.conf/g" 7101.conf
sed -i "s/# cluster-config-file nodes-6379.conf/cluster-config-file nodes-7102.conf/g" 7102.conf
sed -i "s/# cluster-config-file nodes-6379.conf/cluster-config-file nodes-7103.conf/g" 7103.conf
sed -i "s/# cluster-config-file nodes-6379.conf/cluster-config-file nodes-7104.conf/g" 7104.conf
sed -i "s/# cluster-config-file nodes-6379.conf/cluster-config-file nodes-7105.conf/g" 7105.conf
集群启动脚本
vi /usr/local/bin/redis_cluster
chmod +x /usr/local/bin/redis_cluster
#开机自启
\cp /usr/local/bin/redis_cluster /etc/init.d/redis_cluster
#!/bin/bash
# shellcheck disable=SC2181
APP_WORKDIR=/data/redis
APP_NAME=redis_cluster
#使用说明,用来提示输入参数
usage() {
echo "Usage: sh 脚本名.sh [start|stop|restart|status]"
exit 1
}
#检查程序是否在运行
is_exist() {
pid=$(ps -ef | grep "redis-server" | grep -v grep | awk '{print $2}')
#如果不存在返回1,存在返回0
if [ -z "${pid}" ]; then
return 1
else
return 0
fi
}
#启动方法
start() {
is_exist
# shellcheck disable=SC2181
if [ $? -eq "0" ]; then
echo "${APP_NAME} is already running. pid=${pid} ."
else
tmp=$(ls ${APP_WORKDIR}/conf)
for i in ${tmp}; do
${APP_WORKDIR}/bin/redis-server ${APP_WORKDIR}/conf/$i
done
echo "${APP_NAME} start success"
fi
status
}
#停止方法
stop() {
is_exist
if [ $? -eq "0" ]; then
echo "stop ${APP_NAME}!"
pgrep redis-server | xargs -exec kill -9
else
status
fi
}
#输出运行状态
status() {
is_exist
if [ $? -eq "0" ]; then
echo "${APP_NAME} is running. Pid is ${pid}"
else
echo "${APP_NAME} is NOT running."
fi
}
#重启
restart() {
stop
start
}
#根据输入参数,选择执行对应方法,不输入则执行使用说明
case "$1" in
"start")
start
;;
"stop")
stop
;;
"status")
status
;;
"restart")
restart
;;
*)
status
usage
;;
esac
启动集群
#第一次需要初始化集群,直接yes自动分配M和S
redis-cli --cluster create 127.0.0.1:7100 127.0.0.1:7101 127.0.0.1:7102 127.0.0.1:7103 127.0.0.1:7104 127.0.0.1:7105 --cluster-replicas 1 --pass Xskj@2021
模块名称 | 模块功能 | 是否需要运行 |
---|---|---|
sjn-admin-server | 管理和监控SpringBoot应用程序 | 是 |
sjn-alarm-server | 告警主题数据分析接口 | 是 |
sjn-authority-server | 应用授权程序 | 是 |
sjn-common | 通用依赖 | 否 |
sjn-device-server | 设备主题数据分析接口 | 是 |
sjn-display-client | 智慧可视化大屏接口 | 是 |
sjn-dynamic-datasource | 多数据源依赖 | 否 |
sjn-gateway-server | 微服务网关鉴权 | 是 |
sjn-judgment-server | 研判主题数据分析接口 | 是 |
sjn-kms-server | 加解密票据管理 | 是 |
sjn-operation-server | 运维统计接口 | 是 |
sjn-personnel-server | 人员主题数据接口 | 是 |
sjn-smartstatistics-server | 智能统计主题数据分析接口 | 是 |
sjn-vehicle-server | 车辆统计主题数据分析接口 | 是 |
常用命令
#查看集群信息
redis-cli -c -h 127.0.0.1 -p 7100 --pass Xskj@2021 cluster info
#查看集群节点
redis-cli -c -h 127.0.0.1 -p 7100 --pass Xskj@2021 cluster nodes
#连接集群使用
redis-cli -c -h 127.0.0.1 -p 7100 --pass Xskj@2021
/opt/redis/sbin/redis-cli --h 192.168.10.19 -p 7001 --pass gAlbNoAaWYEhdEYJ
一键脚本
#! /usr/bin/bash
##redis任何版本全程自动化源码编译安装
##用法:sh redis-install.sh 4.0.10 (后面跟的是你需要的版本号,需要什么版本就写什么版本),我这里安装的4.0.10
version=$1
usage(){
echo "usage: $0 version"
}
if [ $# -ne 1 ]
then
usage
exit -1
fi
#Redis安装包下载
cd /usr/local/src
if [ ! -f redis-${version}.tar.gz ]
then
curl -o /usr/local/src/redis-${version}.tar.gz http://download.redis.io/releases/redis-${version}.tar.gz
fi
#Redis依赖包安装
yum clean all
yum makecache fast
yum -y install gcc gcc-c++ tcl
#编译Redis所需要的gcc
yum -y install centos-release-scl
yum -y install devtoolset-9-gcc devtoolset-9-gcc-c++ devtoolset-9-binutils
source /opt/rh/devtoolset-9/enable
echo "source /opt/rh/devtoolset-9/enable" >>/etc/profile
gcc --version
##内系统参数核优化
cat >> /etc/rc.d/rc.local << "EOF"
##关闭Linux的THP(内存管理系统)通过使用更大的内存页面,来减少具有大量内存的计算机上的TLB的开销
if [ -f /sys/kernel/mm/transparent_hugepage/enabled ]
then
echo never > /sys/kernel/mm/transparent_hugepage/enabled
fi
if [ -f /sys/kernel/mm/transparent_hugepage/defrag ]
then
echo never > /sys/kernel/mm/transparent_hugepage/defrag
fi
EOF
chmod u+x /etc/rc.d/rc.local
if [ -f /sys/kernel/mm/transparent_hugepage/enabled ]
then
echo never > /sys/kernel/mm/transparent_hugepage/enabled
fi
if [ -f /sys/kernel/mm/transparent_hugepage/defrag ]
then
echo never > /sys/kernel/mm/transparent_hugepage/defrag
fi
cat >> /etc/sysctl.conf << "EOF"
#Linux系统内核参数优化
net.core.somaxconn = 2048
net.ipv4.tcp_max_syn_backlog = 2048
vm.overcommit_memory = 1
EOF
sysctl -p
cat > /etc/security/limits.conf << "EOF"
root soft nofile 65535
root hard nofile 65535
* soft nofile 65535
* hard nofile 65535
EOF
#Redis编译安装
cd /usr/local/src
tar -zxvf redis-${version}.tar.gz
cd /usr/local/src/redis-${version}
make
make PREFIX=/usr/local/redis install
#Redis基础配置
mkdir -p /usr/local/redis/{etc,logs,data}
egrep -v "^$|^#" /usr/local/src/redis-${version}/redis.conf > /usr/local/redis/etc/redis.conf
#sed -i "s/bind 127.0.0.1/bind 0.0.0.0/g" /usr/local/redis/etc/redis.conf
sed -i "s/protected-mode yes/protected-mode no/g" /usr/local/redis/etc/redis.conf
sed -i "s/daemonize no/daemonize yes/g" /usr/local/redis/etc/redis.conf
sed -i "s/pidfile \/var\/run\/redis_6379.pid/pidfile \/usr\/local\/redis\/redis.pid/g" /usr/local/redis/etc/redis.conf
sed -i "s/dir \.\//dir \/usr\/local\/redis\/data/g" /usr/local/redis/etc/redis.conf
sed -i "s/logfile \"\"/logfile \"\/usr\/local\/redis\/logs\/redis.log\"/g" /usr/local/redis/etc/redis.conf
sed -i "s/dbfilename dump.rdb/dbfilename dump.rdb/g" /usr/local/redis/etc/redis.conf
sed -i "s/appendfilename \"appendonly.aof\"/appendfilename \"appendonly.aof\"/g" /usr/local/redis/etc/redis.conf
#PATH配置
echo "export PATH=${PATH}:/usr/local/redis/bin" >>/etc/profile
source /etc/profile
#启动redis服务
/usr/local/redis/bin/redis-server /usr/local/redis/etc/redis.conf
#查看redis监听端口
netstat -tanp|grep redis
标签:单机,local,Redis6,redis,sed,集群,usr,conf,data
From: https://www.cnblogs.com/edclol/p/17282457.html