本文共 1853 字,大约阅读时间需要 6 分钟。
实验环境:关闭防火墙以及selinux,各节点间时间进行同步,能够互相通信
Master 172.10.251.224Slave1 172.10.251.225Slave2 172.10.251.226Slave3 172.10.251.227四台机器都安装redis yun install -y redis步骤:
Master:1.vim /etc/redis.confbind 172.10.251.224 #自己主机的IPprotected-mode yes #开启保护模式daemonize yes #启动守护进程appendonly yes #启动aof模式(根据实际情况判断是否开启)2.systemctl start redisSlave1:
1.vim /etc/redis.confbind 172.10.251.225slaveof 172.10.251.224 6379 #master的IP地址和监听的端口slave-server-stale-data yes #当slave与master连接断开或者slave正处于同步状态时,如果slave收到请求允许响应,no表示返回错误。slave-read-only yes #slave节点是否为只读。slave-priority 100 #设定此节点的优先级,是否优先被同步。2.systemctl start redisSlave2:
1.vim /etc/redis.confbind 172.10.251.226slaveof 172.10.251.224 6379slave-server-stale-data yes slave-read-only yes slave-priority 100 2.systemctl start redisSlave3:
1.vim /etc/redis.confbind 172.10.251.227slaveof 172.10.251.224 6379 slave-server-stale-data yes slave-read-only yes slave-priority 100 2.systemctl start redisredis-cli -h 172.10.251.224 info #master上执行命令,查看Replication这一项:
#Replicationrole:master #角色masterconnected_slaves:3 #3台从节点slave0:ip=172.10.251.225,port=6379,state=online,offset=897,lag=0slave1:ip=172.10.251.226,port=6379,state=online,offset=897,lag=0slave2:ip=172.10.251.227,port=6379,state=online,offset=897,lag=0redis-cli -h 172.10.251.225 info #也可以在slave1上执行命令,查看Replication这一项
#Replicationrole:slave #角色slavemaster_host:172.10.251.224 #主节点地址为172.10.251.224测试:
master上执行:[root@centos7 ~]# redis-cli -h 172.10.251.224172.10.251.224:6379>set master mage #设置缓存值172.10.251.224:6379> get master #查看缓存值"mage"172.10.251.224:6379> keys * #查看当前db库中所有的缓存key1) "master"slave上执行:(以slave1为例)
[root@centos7 ~]# redis-cli -h 172.10.251.225172.10.251.225:6379> keys *1) "master"172.10.251.225:6379> get master"mage"可以看出,主从已经实现了同步,就是这么easy,实验完成了。转载于:https://blog.51cto.com/600600880/2051240