1.搭建redis主从,一个master两个slave,加一个哨兵监听(sentinel),可以新建三个虚拟机,模拟环境,我的电脑没那么多虚拟机,就在一台虚拟机上弄的.
2.安装redis,如果是三台机器,安装redis的步骤一样.
3.安装完redis后,复制同样的文件两份用于当slave
#复制两份redis文件,当从redis(端口6379为主redis,端口6380,6381两个为从redis)[root@localhost src]# cp -r /usr/redis/redis-3.2.4 /usr/redis/redis-6380 [root@localhost src]# cp -r /usr/redis/redis-3.2.4 /usr/redis/redis-6381
4.修改两份 redis.conf 配置文件
[root@localhost src]# vi /usr/redis/redis-6380/redis.conf [root@localhost src]# vi /usr/redis/redis-6381/redis.conf
redis.conf 配置文件中主要改的地方(以/usr/redis/redis-6380/redis.conf文件为例)
1.bind 192.168.200.108(改为虚拟机的ip) 2.port 6380(端口号) 3.daemonize yes(守护进程) 4.pidfile "/var/run/redis_6380.pid"(进程的目录) 5.slaveof 192.168.200.108 6380(设置为此redis为slave,端口号是6380) 6.slave-read-only yes(只读)
5.修改两份 sentinel.conf 配置文件
[root@localhost src]# vi /usr/redis/redis-6380/sentinel.conf [root@localhost src]# vi /usr/redis/redis-6381/sentinel.conf
sentinel.conf 配置文件修改的地方
1.port 26379(端口号,可以使用默认端口) 2.sentinel monitor master 192.168.200.108 6379 2(主redis的名称,ip和端口)
6.在主redis配置哨兵监听
[root@localhost src]# vi /usr/redis/redis-3.2.4/sentinel.conf
#修改要监听的主机ip和端口 sentinel monitor mymaster 192.168.200.108 6379 1
7.在/etc/init.d文件中复制两份redis启动脚本(脚本内容见之前写的 启动redis脚本 文章)
修改端口号和文件路径(以redis-6380为例)
8.启动三台redis
启动哨兵
[root@localhost ~]# ps -ef|grep redis root 3403 1 0 20:28 ? 00:00:00 /usr/redis/redis-3.2.4/src/redis-server 192.168.200.108:6379 root 3415 1 0 20:28 ? 00:00:00 /usr/redis/redis-6380/src/redis-server 192.168.200.108:6380 root 3427 1 0 20:28 ? 00:00:00 /usr/redis/redis-6381/src/redis-server 192.168.200.108:6381 root 3432 3373 0 20:28 pts/1 00:00:00 grep redis [root@localhost ~]# cd /usr/redis/redis-3.2.4 [root@localhost redis-3.2.4]# src/redis-sentinel sentinel.conf & [1] 3443 [root@localhost redis-3.2.4]# 3443:X 02 Mar 20:30:29.076 * Increased maximum number of open files to 10032 (it was originally set to 1024). _._ _.-``__ ''-._ _.-`` `. `_. ''-._ Redis 3.2.4 (00000000/0) 64 bit .-`` .-```. ```\/ _.,_ ''-._ ( ' , .-` | `, ) Running in sentinel mode |`-._`-...-` __...-.``-._|'` _.-'| Port: 26379 | `-._ `._ / _.-' | PID: 3443 `-._ `-._ `-./ _.-' _.-' |`-._`-._ `-.__.-' _.-'_.-'| | `-._`-._ _.-'_.-' | http://redis.io `-._ `-._`-.__.-'_.-' _.-' |`-._`-._ `-.__.-' _.-'_.-'| | `-._`-._ _.-'_.-' | `-._ `-._`-.__.-'_.-' _.-' `-._ `-.__.-' _.-' `-._ _.-' `-.__.-' 3443:X 02 Mar 20:30:29.104 # WARNING: The TCP backlog setting of 511 cannot be enforced because /proc/sys/net/core/somaxconn is set to the lower value of 128. 3443:X 02 Mar 20:30:29.104 # Sentinel ID is ec55f3b5cca3a78aae0ad0fe94c78d7612221e0b 3443:X 02 Mar 20:30:29.104 # +monitor master master 192.168.200.108 6379 quorum 1 3443:X 02 Mar 20:30:29.104 # +monitor master mymaster 127.0.0.1 6379 quorum 2 3443:X 02 Mar 20:30:59.106 # +sdown master mymaster 127.0.0.1 6379
9.查看三台redis的从属关系
[root@localhost redis-3.2.4]# ps -ef|grep redis root 3677 1 0 21:18 ? 00:00:02 /usr/redis/redis-3.2.4/src/redis-server 192.168.200.108:6379 root 3777 1 0 21:35 ? 00:00:00 /usr/redis/redis-6380/src/redis-server 192.168.200.108:6380 root 3791 1 0 21:35 ? 00:00:00 /usr/redis/redis-6381/src/redis-server 192.168.200.108:6381 root 3804 3730 0 21:37 pts/3 00:00:00 src/redis-sentinel *:26379 [sentinel] root 3810 3730 0 21:38 pts/3 00:00:00 grep redis [root@localhost redis-3.2.4]# cd /usr/redis/redis-3.2.4/src [root@localhost src]# ./redis-cli -h 192.168.200.108 -p 6379 192.168.200.108:6379> info replication --->查看当前redis信息 # Replication role:master --->说明是主redis connected_slaves:2 slave0:ip=192.168.200.108,port=6380,state=online,offset=6165957,lag=0 --->从redis端口和ip 192.168.200.108 6380 slave1:ip=192.168.200.108,port=6381,state=online,offset=6165667,lag=0 --->从redis端口和ip 192.168.200.108 6381 master_repl_offset:6165957 repl_backlog_active:1 repl_backlog_size:1048576 repl_backlog_first_byte_offset:5117382 repl_backlog_histlen:1048576 [root@localhost src]# ./redis-cli -h 192.168.200.108 -p 6380 --->连接192.168.200.108 6380查看信息 192.168.200.108:6380> info replication # Replication role:slave --->从redis master_host:192.168.200.108 --->主redis ip master_port:6379 --->主redis端口 master_link_status:up master_last_io_seconds_ago:0 master_sync_in_progress:0 slave_repl_offset:4430 slave_priority:100 slave_read_only:1 connected_slaves:0 master_repl_offset:0 repl_backlog_active:0 repl_backlog_size:1048576 repl_backlog_first_byte_offset:0 repl_backlog_histlen:0 192.168.200.108:6380> [root@localhost src]# ./redis-cli -h 192.168.200.108 -p 6381 --->连接192.168.200.108 6381查看信息 192.168.200.108:6381> info replication # Replication role:slave --->从redis master_host:192.168.200.108 master_port:6379 master_link_status:up master_last_io_seconds_ago:1 master_sync_in_progress:0 slave_repl_offset:73275 slave_priority:100 slave_read_only:1 connected_slaves:0 master_repl_offset:0 repl_backlog_active:0 repl_backlog_size:1048576 repl_backlog_first_byte_offset:0 repl_backlog_histlen:0
10.当把主redis kill 后,哨兵会监听会master,当发现master挂了后,哨兵开始故障恢复,选取一个从redis升级为主库
常用命令
slaveof no one : 手动设置从redis,从属服务器关闭复制功能,并从从属服务器转变回主服务器,原来同步所得的数据集不会被丢弃
192.168.200.108:6380> slaveof no one OK
slaveof host port : 将当前服务器转变为指定服务器的从属服务器(slave server)。
192.168.200.108:6381> slaveof 192.168.200.108 6379 OK