redis主从环境搭建

1主从两台:

上传redis-4.0.14.tar.gz包到/usr/local目录
解压tar包
cd /usr/local/redis-4.0.14
make && make install
两台安装完成

2配置主服务器:
vi /usr/local/redis-4.0.14/redis.conf
修改内容如下:
将port 6379 默认端口改为16379
将bind 127.0.0.1这一行注释掉(这一行代表只允许本地回环接口才可以访问redis,不注释的话,其他服务器就没权限访问redis,添加一行:bind 0.0.0.0代表任意机器可以访问,或者绑定固定应用IP,只允许特定IP可以访问)
将protected-mode yes改为protected-mode no
将daemonize no改为daemonize yes(允许redis后台服务允许)
将logfile ""改为logfile"/var/log/redis.log"

vi /usr/local/redis-4.0.14/sentinel.conf
将sentinel monitor mymaster ip 16379 2修改 (IP写主redis的 ip地址)
添加以下几行:
daemonize yes
protected-mode no
logfile "/var/log/sentinel.log"

3配置从服务器:
一样的安装redis
vi /usr/local/redis-4.0.14/redis.conf
修改内容如下:
将port 6379 默认端口改为16379
将bind 127.0.0.1这一行注释掉
将protected-mode yes改为protected-mode no
将daemonize no改为daemonize yes
将logfile ""改为logfile"/var/log/redis.log"
找到这一行:

# slaveof <masterip> <masterport>

在下面添加:

slaveof  主ip  16379

vi /usr/local/redis-4.0.14/sentinel.conf

找到这一行:

# sentinel monitor <master-name> <ip> <redis-port> <quorum>

在下面添加:

sentinel monitor mymaster 主ip 16379 2

添加以下几行:
daemonize yes
protected-mode no
logfile "/var/log/sentinel.log"

4启动主从服务器
进入 /usr/local/redis-4.0.14/src目录
启动redis服务: ./redis-server redis.conf
启动sentinel服务: ./redis-sentinel sentinel.conf

备注:

启动redis客户端: ./redis-cli –p (端口)
关闭redis服务:Pkill redis-server
关闭redis客户端:redis-cli shutdown

5测试
主服务器上:
Redis-cli连接进去后
输入:set a 1

从服务器:
Redis-cli连接后:
输入:get a

从机是只读的,验证
输入:set b 2
提示

6测试主从切换
把主服务器进程kill掉
info看主从关系
再把主服务器启动起来
info 看主从关系

猜你喜欢

转载自www.cnblogs.com/wangnengwu/p/12714740.html