Redis集群搭建
一、准备
-
在redis根目录创建6个文件夹(8000-8005)
-
拷贝redis.conf到6个文件夹中,命名为redis-cluster.conf
-
修改配置文件内容( xxxx 代表端口号)
首先将所有原端口6379换成目标端口号,可以使用命令 %s/6379/xxxx/g
# bind 127.0.0.1 -::1 远程连接必须注释 protected-mode no daemonize yes port xxxx dbfilename dumpxxxx.rdb logfile "xxxx.log" cluster-enabled yes cluster-config-file nodes-xxxx.conf # 开启aop持久化策略 appendonly yes appendfilename "appendonlyxxxx.aof"
二、搭建集群
-
启动6个redis服务,以cluster的模式启动,为了方便,可以使用sh脚本
# 新建一个startupRedisCluster.sh redis-server /opt/redis-7.0.0/8000/redis-cluster.conf redis-server /opt/redis-7.0.0/8001/redis-cluster.conf redis-server /opt/redis-7.0.0/8002/redis-cluster.conf redis-server /opt/redis-7.0.0/8003/redis-cluster.conf redis-server /opt/redis-7.0.0/8004/redis-cluster.conf redis-server /opt/redis-7.0.0/8005/redis-cluster.conf
然后在命令行输入 sh startupRedisCluster.sh 启动
-
使用redis-cli搭建集群
注1:在redis5.0之后,redis-trib.rb is not longer available
注2:远程连接服务器的,一定要将8000-8005、18000-18005(集群总线端口)端口打开
redis-cli --cluster create host:port0 host:port1 host:port2 host:port3 host:port4 host:port5 --cluster-replicas 1 Can I set the above configuration? (type 'yes' to accept): yes
-
查看节点
redis-cli -c -p 8000 cluster nodes
成功!
三、重新搭建集群(测试环境)
集群运行坏了,需要重新搭建的时候,先关闭所有redis-server节点
然后删除所有持久化文件
-
node*
-
dump*
-
append*(可以删除整个appendonlydir)
接下来就可以重新启动节点,重新搭建集群