这里只是简单的搭建了redis cluster,缺少哨兵检测集群。
环境准备
hostnamectl set-hostname Redis-3-3
logout root
yum install wget
#修改ip 10.0.0.60
cd /etc/sysconfig/network-scripts
sed -i '/.*BOOTPROTO=*/c\BOOTPROTO="static"' ifcfg-ens33
sed -i '/.*IPADDR=*/c\IPADDR=10.0.0.60' ifcfg-ens33
systemctl restart network
脚本
#redis安装
#编译工具
yum -y install tcl gcc gcc-c++
yum -y install gcc automake autoconf libtool make
#下载
cd /etc/local
wget http://download.redis.io/releases/redis-5.0.5.tar.gz
#编译代码
tar -zxvf redis-5.0.5.tar.gz
cd /usr/local/redis-5.0.5
make
cd /usr/local/redis-5.0.5/src
make install
#配置文件复制
mkdir -p /data/redis-5.0.5/cluster/7000
mkdir -p /data/redis-5.0.5/cluster/7001
mkdir -p /data/redis-5.0.5/cluster/7002
mkdir -p /data/redis-5.0.5/cluster/7003
mkdir -p /data/redis-5.0.5/cluster/7004
mkdir -p /data/redis-5.0.5/cluster/7005
cp /usr/local/redis-5.0.5/redis.conf /data/redis-5.0.5/cluster/7000
cp /usr/local/redis-5.0.5/redis.conf /data/redis-5.0.5/cluster/7001
cp /usr/local/redis-5.0.5/redis.conf /data/redis-5.0.5/cluster/7002
cp /usr/local/redis-5.0.5/redis.conf /data/redis-5.0.5/cluster/7003
cp /usr/local/redis-5.0.5/redis.conf /data/redis-5.0.5/cluster/7004
cp /usr/local/redis-5.0.5/redis.conf /data/redis-5.0.5/cluster/7005
#7000
sed -i 's/port 6379/port 7000/' /data/redis-5.0.5/cluster/7000/redis.conf
sed -i 's/pidfile \/var\/run\/redis_6379.pid/pidfile \/var\/run\/redis_7000.pid/' /data/redis-5.0.5/cluster/7000/redis.conf
sed -i 's/# cluster-enabled yes/cluster-enabled yes/' /data/redis-5.0.5/cluster/7000/redis.conf
sed -i 's/daemonize no/daemonize yes/' /data/redis-5.0.5/cluster/7000/redis.conf
sed -i 's/# cluster-config-file nodes-6379.conf/cluster-config-file nodes-7000.conf/' /data/redis-5.0.5/cluster/7000/redis.conf
sed -i 's/# cluster-node-timeout 15000/cluster-node-timeout 15000/' /data/redis-5.0.5/cluster/7000/redis.conf
sed -i 's/appendonly no/appendonly yes/' /data/redis-5.0.5/cluster/7000/redis.conf
sed -i 's/protected-mode yes/protected-mode no/' /data/redis-5.0.5/cluster/7001/redis.conf
#cluster use config
sed -i 's/bind 127.0.0.1/#bind 127.0.0.1/' /data/redis-5.0.5/cluster/7000/redis.conf
sed -i 's/# requirepass foobared/requirepass Redis123/' /data/redis-5.0.5/cluster/7000/redis.conf
sed -i 's/# masterauth <master-password>/masterauth Redis123/' /data/redis-5.0.5/cluster/7000/redis.conf
#7001
sed -i 's/port 6379/port 7001/' /data/redis-5.0.5/cluster/7001/redis.conf
sed -i 's/pidfile \/var\/run\/redis_6379.pid/pidfile \/var\/run\/redis_7001.pid/' /data/redis-5.0.5/cluster/7001/redis.conf
sed -i 's/# cluster-enabled yes/cluster-enabled yes/' /data/redis-5.0.5/cluster/7001/redis.conf
sed -i 's/daemonize no/daemonize yes/' /data/redis-5.0.5/cluster/7001/redis.conf
sed -i 's/# cluster-config-file nodes-6379.conf/cluster-config-file nodes-7001.conf/' /data/redis-5.0.5/cluster/7001/redis.conf
sed -i 's/# cluster-node-timeout 15000/cluster-node-timeout 15000/' /data/redis-5.0.5/cluster/7001/redis.conf
sed -i 's/appendonly no/appendonly yes/' /data/redis-5.0.5/cluster/7001/redis.conf
sed -i 's/protected-mode yes/protected-mode no/' /data/redis-5.0.5/cluster/7001/redis.conf
#cluster use config
sed -i 's/bind 127.0.0.1/#bind 127.0.0.1/' /data/redis-5.0.5/cluster/7001/redis.conf
sed -i 's/# requirepass foobared/requirepass Redis123/' /data/redis-5.0.5/cluster/7001/redis.conf
sed -i 's/# masterauth <master-password>/masterauth Redis123/' /data/redis-5.0.5/cluster/7001/redis.conf
#7002
sed -i 's/port 6379/port 7002/' /data/redis-5.0.5/cluster/7002/redis.conf
sed -i 's/pidfile \/var\/run\/redis_6379.pid/pidfile \/var\/run\/redis_7002.pid/' /data/redis-5.0.5/cluster/7002/redis.conf
sed -i 's/# cluster-enabled yes/cluster-enabled yes/' /data/redis-5.0.5/cluster/7002/redis.conf
sed -i 's/daemonize no/daemonize yes/' /data/redis-5.0.5/cluster/7002/redis.conf
sed -i 's/# cluster-config-file nodes-6379.conf/cluster-config-file nodes-7002.conf/' /data/redis-5.0.5/cluster/7002/redis.conf
sed -i 's/# cluster-node-timeout 15000/cluster-node-timeout 15000/' /data/redis-5.0.5/cluster/7002/redis.conf
sed -i 's/appendonly no/appendonly yes/' /data/redis-5.0.5/cluster/7002/redis.conf
sed -i 's/protected-mode yes/protected-mode no/' /data/redis-5.0.5/cluster/7002/redis.conf
#cluster use config
sed -i 's/bind 127.0.0.1/#bind 127.0.0.1/' /data/redis-5.0.5/cluster/7002/redis.conf
sed -i 's/# requirepass foobared/requirepass Redis123/' /data/redis-5.0.5/cluster/7002/redis.conf
sed -i 's/# masterauth <master-password>/masterauth Redis123/' /data/redis-5.0.5/cluster/7002/redis.conf
#7003
sed -i 's/port 6379/port 7003/' /data/redis-5.0.5/cluster/7003/redis.conf
sed -i 's/pidfile \/var\/run\/redis_6379.pid/pidfile \/var\/run\/redis_7003.pid/' /data/redis-5.0.5/cluster/7003/redis.conf
sed -i 's/# cluster-enabled yes/cluster-enabled yes/' /data/redis-5.0.5/cluster/7003/redis.conf
sed -i 's/daemonize no/daemonize yes/' /data/redis-5.0.5/cluster/7003/redis.conf
sed -i 's/# cluster-config-file nodes-6379.conf/cluster-config-file nodes-7003.conf/' /data/redis-5.0.5/cluster/7003/redis.conf
sed -i 's/# cluster-node-timeout 15000/cluster-node-timeout 15000/' /data/redis-5.0.5/cluster/7003/redis.conf
sed -i 's/appendonly no/appendonly yes/' /data/redis-5.0.5/cluster/7003/redis.conf
sed -i 's/protected-mode yes/protected-mode no/' /data/redis-5.0.5/cluster/7003/redis.conf
sed -i 's/bind 127.0.0.1/#bind 127.0.0.1/' /data/redis-5.0.5/cluster/7003/redis.conf
sed -i 's/# requirepass foobared/requirepass Redis123/' /data/redis-5.0.5/cluster/7003/redis.conf
sed -i 's/# masterauth <master-password>/masterauth Redis123/' /data/redis-5.0.5/cluster/7003/redis.conf
#7004
sed -i 's/port 6379/port 7004/' /data/redis-5.0.5/cluster/7004/redis.conf
sed -i 's/pidfile \/var\/run\/redis_6379.pid/pidfile \/var\/run\/redis_7004.pid/' /data/redis-5.0.5/cluster/7004/redis.conf
sed -i 's/# cluster-enabled yes/cluster-enabled yes/' /data/redis-5.0.5/cluster/7004/redis.conf
sed -i 's/daemonize no/daemonize yes/' /data/redis-5.0.5/cluster/7004/redis.conf
sed -i 's/# cluster-config-file nodes-6379.conf/cluster-config-file nodes-7004.conf/' /data/redis-5.0.5/cluster/7004/redis.conf
sed -i 's/# cluster-node-timeout 15000/cluster-node-timeout 15000/' /data/redis-5.0.5/cluster/7004/redis.conf
sed -i 's/appendonly no/appendonly yes/' /data/redis-5.0.5/cluster/7004/redis.conf
sed -i 's/protected-mode yes/protected-mode no/' /data/redis-5.0.5/cluster/7004/redis.conf
#cluster use config
sed -i 's/bind 127.0.0.1/#bind 127.0.0.1/' /data/redis-5.0.5/cluster/7004/redis.conf
sed -i 's/# requirepass foobared/requirepass Redis123/' /data/redis-5.0.5/cluster/7004/redis.conf
sed -i 's/# masterauth <master-password>/masterauth Redis123/' /data/redis-5.0.5/cluster/7004/redis.conf
#
7005
sed -i 's/port 6379/port 7005/' /data/redis-5.0.5/cluster/7005/redis.conf
sed -i 's/pidfile \/var\/run\/redis_6379.pid/pidfile \/var\/run\/redis_7005.pid/' /data/redis-5.0.5/cluster/7005/redis.conf
sed -i 's/# cluster-enabled yes/cluster-enabled yes/' /data/redis-5.0.5/cluster/7005/redis.conf
sed -i 's/daemonize no/daemonize yes/' /data/redis-5.0.5/cluster/7005/redis.conf
sed -i 's/# cluster-config-file nodes-6379.conf/cluster-config-file nodes-7005.conf/' /data/redis-5.0.5/cluster/7005/redis.conf
sed -i 's/# cluster-node-timeout 15000/cluster-node-timeout 15000/' /data/redis-5.0.5/cluster/7005/redis.conf
sed -i 's/appendonly no/appendonly yes/' /data/redis-5.0.5/cluster/7005/redis.conf
sed -i 's/protected-mode yes/protected-mode no/' /data/redis-5.0.5/cluster/7005/redis.conf
#cluster use config
sed -i 's/bind 127.0.0.1/#bind 127.0.0.1/' /data/redis-5.0.5/cluster/7005/redis.conf
sed -i 's/# requirepass foobared/requirepass Redis123/' /data/redis-5.0.5/cluster/7005/redis.conf
sed -i 's/# masterauth <master-password>/masterauth Redis123/' /data/redis-5.0.5/cluster/7005/redis.conf
#redis集群启动
redis-server /data/redis-5.0.5/cluster/7000/redis.conf
redis-server /data/redis-5.0.5/cluster/7001/redis.conf
redis-server /data/redis-5.0.5/cluster/7002/redis.conf
redis-server /data/redis-5.0.5/cluster/7003/redis.conf
redis-server /data/redis-5.0.5/cluster/7004/redis.conf
redis-server /data/redis-5.0.5/cluster/7005/redis.conf
netstat -ntl
#requirepass masterauth密码模式使用,最后的参数-a后面跟着密码,
redis-cli --cluster create 10.0.0.60:7000 10.0.0.60:7001 10.0.0.60:7002 10.0.0.60:7003 10.0.0.60:7004 10.0.0.60:7005 --cluster-replicas 2 -a Redis123
停止服务
#有密码
redis-cli -h 10.0.0.60 -p 7000 -a Redis123 shutdown
redis-cli -h 10.0.0.60 -p 7001 -a Redis123 shutdown
redis-cli -h 10.0.0.60 -p 7002 -a Redis123 shutdown
redis-cli -h 10.0.0.60 -p 7003 -a Redis123 shutdown
redis-cli -h 10.0.0.60 -p 7004 -a Redis123 shutdown
redis-cli -h 10.0.0.60 -p 7005 -a Redis123 shutdown
netstat -ntl
集群查看指令
redis-cli -p 7000 info replication
redis-cli -p 7000 cluster info
redis-cli -p 7000 cluster nodes
删除节点
redis-cli -p 7000 -a Redis123 cluster nodes
redis-cli -p 7001 -a Redis123 cluster nodes
redis-cli -p 7002 -a Redis123 cluster nodes
redis-cli -p 7003 -a Redis123 cluster nodes
redis-cli -p 7004 -a Redis123 cluster nodes
redis-cli -p 7005 -a Redis123 cluster nodes
#最后一个参数是上面查询值来的id
redis-cli --cluster del-node -a Redis123 10.0.0.60:7000 fbbc99129366036fc6d73adf240d75d6ab4c7970
redis-cli --cluster del-node -a Redis123 10.0.0.60:7001 38727fb4f818deb56ba603e715f5947c23236cf7
redis-cli --cluster del-node -a Redis123 10.0.0.60:7002 11321f20d970733de116b5e5559ce014137f25c9
redis-cli --cluster del-node -a Redis123 10.0.0.60:7003 11321f20d970733de116b5e5559ce014137f25c9
redis-cli --cluster del-node -a Redis123 10.0.0.60:7004 7dc8090a95a9f09c4a650ef742c6d3b7a065c987
redis-cli --cluster del-node -a Redis123 10.0.0.60:7005 fbbc99129366036fc6d73adf240d75d6ab4c7970
参考资料
如果是用apt-get或者yum install安装的redis,可以直接通过下面的命令停止/启动/重启redis
/etc/init.d/redis-server stop
/etc/init.d/redis-server start
/etc/init.d/redis-server restart
如果是通过源码安装的redis,则可以通过redis的客户端程序redis-cli的shutdown命令来重启redis
1.redis关闭
redis-cli -h 127.0.0.1 -p 6379 shutdown
redis-cli -h 127.0.0.1 -p 6379 -a password shutdown
如果上述方式都没有成功停止redis,则可以使用 kill -9 杀死redis进程
2.redis启动
第一种方式 默认无密码启动方式 ./redis-server 或者 ./redis-server --port 6380
如果设置密码,这种方式启动redis,调用时就会出现异常 Caused by: redis.clients.jedis.exceptions.JedisDataException: ERR Client sent AUTH, but no password is set
第二种 已设置访问密码启动方式 ./redis-server …/redis.conf
————————————————
版权声明:本文为CSDN博主「m0_67390969」的原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接及本声明。
原文链接:https://blog.csdn.net/m0_67390969/article/details/123771584
详解如何清理redis集群的所有数据
h5w3.com/254461.html
https://github.com/eyjian/redis-tools/blob/master/clear_redis_cluster.sh