集群搭建是一个Master(一次大哥,终身大哥),两个slaver。
1、首先将redis安装目录bin文件夹下的redis.conf配置文件复制三份,分别命名如下图所示
查看添加的配置文件:
2、修改每一个配置文件
① 修改redis-master.conf主节点配置文件:
修改:
# 绑定本机ip地址
# bind 127.0.0.1 -::1
bind 192.168.184.128
#设置端口号
# Accept connections on the specified port, default is 6379 (IANA #815344).
# If port 0 is specified Redis will not listen on a TCP socket.
Port 6380
# By default Redis does not run as a daemon. Use 'yes' if you need it.
daemonize yes
# The filename where to dump the DB
dbfilename dump6380.rdb 或者修改dir
# master自己的密码(没有密码可忽略)
requirepass 123456
② 修改redis-slaver1.conf主节点配置文件:
bind 192.168.184.128
Port 6381
daemonize yes
dbfilename dump6381.rdb 或者修改dir
#设置节点从属于master主节点
Replicaof 192.168.31.193 6380
# If the master is password protected (using the "requirepass" configuration
# directive below) it is possible to tell the replica to authenticate before
# starting the replication synchronization process, otherwise the master will
# refuse the replica request.
# 如果redis的master设置了密码,需要添加下列认证
# masterauth <master-password>
masterauth 123456
# slaver1自己的密码(没有密码可忽略)
requirepass 123456
③ 修改redis-slaver2.conf主节点配置文件:
修改:
bind 192.168.184.128
Port 6382
daemonize yes
dbfilename dump6383.rdb 或者修改dir
Replicaof 192.168.31.193 6380
# If the master is password protected (using the "requirepass" configuration
# directive below) it is possible to tell the replica to authenticate before
# starting the replication synchronization process, otherwise the master will
# refuse the replica request.
# 如果redis的master设置了密码,需要添加下列认证
# masterauth <master-password>
masterauth 123456
# slaver2自己的密码(没有密码可忽略)
requirepass 123456
3、测试集群
Master可以读写,slaver只能读。
Master写操作,会同步到slaver中。
缺点:Master宕机,slaver不会选举为master,slaver依然只读。
①后台启动主节点master和从节点slaver1和slaver2的服务,检查启动结果。
②主节点测试
③从节点slaver1测试,只能读取主节点master的数据,不能自己添加数据。
④从节点slaver2测试,只能读取主节点master的数据,不能自己添加数据。
⑤测试主节点master宕机,直接杀死主节点master的进程即可。
从节点slaver1和slaver2只能读取master主节点数据,不会重新选举新的master,不能添加数据。