linux 7 redis 4.0.11主从复制及哨兵模式

在这里本人的三台机器为

192.168.163.129 (主)
192.168.163.130   (从)
192.168.163.131 (从)

一、将下载好的redis版本包上传至服务器

yum -y install gcc automake autoconf libtool make (这里主要的是gcc)

解压上传的包,并指定路径存放,本文存放为/usr/local/下,可以将解压出来的包改个名,redis-4.0.11 改为redis

进入redis 目录下 make && make install (编译安装)

在redis 目录下 src 目录里有三个文件 redis-cli redis-server redis-sentinel 可以移动到redis目录下(启动服务时方便)

主从和哨兵模式主要的配置文件就两个 redis.conf (主从的配置文件) sentinel.conf (哨兵模式的配置文件) 三台机器的这两个文件都要配置


二、首先: 配置主从模式 (这里先配置两台从的)

vim redis-conf

bind 0.0.0.0 (不管之前配置的是啥,都改为这个参数,有注释的就将注释符注销 0.0.0.0 表示允许任何IP连接此服务器)
slaveof 192.168.163.129 6379 (标明主的ip和端口)
protected-mode no (把保护模式设置为no,如果这里是yes,那么修改为no)
port 6379 (端口号6379保持不变)
daemonize yes (这里设置yes 有的博客设置no,你会发现服务起不来)
logfile "/usr/local/redis/redis.log" (路径最好也放在这,本主放在其他位置也报错)
requirepass abcdefg (密码自定义,但是三台机器要一致)

wq (保存退出)

在/usr/local/redis/redis/下,执行 nohup redis-server redis.conf >myout.file 2>&1 &

配置主的方法同上,只不过 redis.conf 中不需要配置slaveof 这项。然后也启动服务,方法同上。

进行查看是否同步成功,在主上操作

redis-cli

> auth abcdefg
> set name kk
然后在从上查询

redis-cli
> auth abcdefg
> get name
'kk'
说明配置主从成功,如有不通请关闭防火墙或者selinux

三、接下来配置哨兵模式 三台机器还是一样 ,配置sentinel.conf ,配置过后重启。

vim sentinel.conf

bind 0.0.0.0
port 26379
protected-mode no
# Generated by CONFIG REWRITE
sentinel deny-scripts-reconfig yes
sentinel monitor mymaster 192.168.163.129 6379 2 (主的ip和端口)
sentinel down-after-milliseconds mymaster 60000
sentinel auth-pass mymaster abcdefg
sentinel config-epoch mymaster 2528
sentinel leader-epoch mymaster 2529
sentinel known-slave mymaster 192.168.163.130 6379 (从的ip和端口)
sentinel known-slave mymaster 192.168.163.131 6379(从的ip和端口)
sentinel known-sentinel mymaster 192.168.163.129 26379
sentinel current-epoch 2529

wq

启动哨兵服务 在/usr/local/redis/redis/下,nohup redis-sentinel sentinel.conf >myout.file 2>&1 & (这条语句是将命令放到后台执行)

查看 ps -ef | grep redis

root 5818 1 0 09:30 ? 00:01:29 redis-server 0.0.0.0:6379
root 6138 2319 0 10:50 pts/0 00:01:36 redis-sentinel *:26379 [sentinel]
root 6825 6667 0 13:53 pts/1 00:00:00 grep --color=auto redis

启动成功

四、这时我们测试下,在主上进行

kill -9 5818 (关掉redis进程)

然后看在两个从上是否选举出新的主,在从上执行

redis-cli
> auth abcdefg
> info

# Replication
role:master
connected_slaves:1
slave0:ip=192.168.163.130,port=6379,state=online,offset=370877,lag=0
master_replid:98aa0e1b122df167386398d83155fff94c404368
master_replid2:a129e74f75f68e683915b4f7ee3ea5236874da78
master_repl_offset:370877
second_repl_offset:243123
repl_backlog_active:1
repl_backlog_size:1048576
repl_backlog_first_byte_offset:1
repl_backlog_histlen:370877

说明选举成功。
如果这两个文件配置的没有问题,基本上都能搭建成功,如果配置文件里没有以上配置,可以在配置文件里写进去。

猜你喜欢

转载自www.cnblogs.com/MUQINGFENG123/p/11897637.html