一.安装redis
1.解压
tar -zxvf redis-3.2.9.tar.gz
2.安装
* 切换目录: cd redis-3.2.9
* 执行:make
* 安装可能报错,一般是没有安装gcc导致,yum -y install gcc
* make失败后再make,先执行 make clean 、make distclean 清理一下
* 出现错误 error: jemalloc/jemalloc.h: No such file or directory执行 make MALLOC=libc
3.启动与关闭
切换到 redis-3.2.9/src/ 目录执行:
./redis-server &(后台启动)
./redis-cli shutdown(关闭)
或者直接kill (-9) pid 这种方式不推荐,太粗暴了
二.redis主从配置
1.修改配置文件
[root@localhost redis-3.2.9]# cp redis.conf redis6380.conf
[root@localhost redis-3.2.9]# cp redis.conf redis6381.conf
[root@localhost redis-3.2.9]# cp redis.conf redis6382.conf
注:
redis6380.conf是主配置文件
include /usr/local/redis-3.2.9/redis.conf
daemonize yes
port 6380
pidfile /var/run/redis_6380.pid
logfile 6380.log
dbfilename dump6380.rdb
redis6381.conf和redis6382.conf是从配置文件
include /usr/local/redis-3.2.9/redis.conf
daemonize yes
port 6382
slaveof 127.0.0.1 6380
pidfile /var/run/redis_6382.pid
logfile 6382.log
dbfilename dump6382.rdb
slaveof 127.0.0.1 6380(主的端口号) 只配置在从配置文件中
2.以不同的配置文件启动redis
[root@localhost src]# ./redis-server ../redis6380.conf
[root@localhost src]# ./redis-server ../redis6381.conf
[root@localhost src]# ./redis-server ../redis6382.conf
3.查看进程是否启动
[root@localhost ~]# ps -ef | grep redis
root 4088 1 0 21:23 ? 00:00:00 ./redis-server *:6380
root 4095 1 0 21:24 ? 00:00:00 ./redis-server *:6381
root 4099 1 0 21:25 ? 00:00:00 ./redis-server *:6382
4.指定端口号进入命令模式并查看角色
[root@localhost src]# ./redis-cli -p 6381
主的角色信息
127.0.0.1:6380> info replication
# Replication
role:master
connected_slaves:2
slave0:ip=127.0.0.1,port=6382,state=online,offset=1219,lag=1
slave1:ip=127.0.0.1,port=6381,state=online,offset=1219,lag=0
master_repl_offset:1219
repl_backlog_active:1
repl_backlog_size:1048576
repl_backlog_first_byte_offset:2
repl_backlog_histlen:1218
从的角色信息
127.0.0.1:6381> info replication
# Replication
role:slave
master_host:127.0.0.1
master_port:6380
master_link_status:up
master_last_io_seconds_ago:6
master_sync_in_progress:0
slave_repl_offset:183
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
5.测试
在主里面写数据,自动复制到从中
可以分担主服务器的压力,实现读写分离
写的时候向主服务器写,读的时候从从服务器读
(类同于mysql的主从)
三.高可用哨兵
1.背景
一个Master可以有多个Slave
Slave下线,只是读请求的处理性能下降
Master下线,写请求无法执行
其中一台Slave使用slaveof no one命令成为Master,其它Slave执行slaveof命令指向这个新的Master,从它这里同步数据
以上过程是手动的,要实现自动化处理,这就需要Sentinel哨兵,实现故障转移Failover操作
7*24不间断使用
2.修改配置文件
默认端口号是26379
[root@localhost redis-3.2.9]# cp sentinel.conf sentinel26380.conf
[root@localhost redis-3.2.9]# cp sentinel.conf sentinel26381.conf
[root@localhost redis-3.2.9]# cp sentinel.conf sentinel26382.conf
修改配置文件
[root@localhost redis-3.2.9]# vim sentinel26380.conf
三份sentinel配置文件修改:
1、修改 port 26380、 port 26381、 port 26382
2、修改 sentinel monitor mymaster 127.0.0.1 6380 2
注意:如果输入vim,提示-bash: vim: command not found
输入 rpm -qa|grep vim 命令, 如果vim已经正确安装,会返回下面的三行代码:
[root@centos]# rpm -qa |grep vim
vim-minimal-7.0.109-6.el5
vim-common-7.0.109-7.2.el5
vim-enhanced-7.0.109-7.2.el5
如果少了其中的某一条,比如 vim-enhanced 的,就用命令 yum -y install vim-enhanced 来安装:
yum -y install vim-enhanced
如果上面的三条都沒有返回, 可以直接用 yum -y install vim* 命令
yum -y install vim*
3.启动哨兵
./redis-sentinel ../sentinel26380.conf
./redis-sentinel ../sentinel26381.conf
./redis-sentinel ../sentinel26382.conf
4.测试
shutdown 6380端口的redis的进程,哨兵监测并转移故障
4266:X 08 Jul 22:12:06.192 # +sdown master mymaster 127.0.0.1 6380(主服务下线了)
4266:X 08 Jul 22:12:06.211 # +new-epoch 1
4266:X 08 Jul 22:12:06.214 # +vote-for-leader 3508680a52db8da31b5a224ed0a73b3bdb5cefc6 1(投票)
4266:X 08 Jul 22:12:06.276 # +odown master mymaster 127.0.0.1 6380 #quorum 3/2
4266:X 08 Jul 22:12:06.277 # Next failover delay: I will not start a failover before Sat Jul 8 22:18:06 2017
4266:X 08 Jul 22:12:06.948 # +config-update-from sentinel 3508680a52db8da31b5a224ed0a73b3bdb5cefc6 127.0.0.1 26382 @ mymaster 127.0.0.1 6380
4266:X 08 Jul 22:12:06.948 # +switch-master mymaster 127.0.0.1 6380 127.0.0.1 6381(主服务变成6381端口了)
4266:X 08 Jul 22:12:06.948 * +slave slave 127.0.0.1:6382 127.0.0.1 6382 @ mymaster 127.0.0.1 6381
4266:X 08 Jul 22:12:06.948 * +slave slave 127.0.0.1:6380 127.0.0.1 6380 @ mymaster 127.0.0.1 6381
4266:X 08 Jul 22:12:36.963 # +sdown slave 127.0.0.1:6380 127.0.0.1 6380 @ mymaster 127.0.0.1 6381