Redis单机搭建

1、下载源包

2、解压缩

上传文件到 /usr/tools/redisInstall/ 目录

1
tar -zxvf redis-4.0.11.tar.gz

3、编译源码

1
2
3
4
5
6
7
8
9
cd /usr/tools/redisInstall/redis-4.0.11/
make
cd src/
make install PREFIX=/usr/tools/redis
cd /usr/tools/redis
mkdir etc
cd /usr/tools/redisInstall/redis-4.0.11/
cp redis.conf /usr/tools/redis/etc/
cp sentinel.conf /usr/tools/redis/etc/

4、修改配置文件

1
2
3
bind 127.0.0.1    #注释掉
protected-mode yes #改为 protected-mode no
daemonize no #改为daemonize yes
5、启动
1
2
cd /usr/tools/redis/bin
./redis-server ../etc/redis.conf

搭建其他机器时,可以直接把编译安装好的文件夹打包拷贝过去,不用重新编译源码。

主从配置

需要在从机的 redis.conf 文件中增加 slaveof 主机IP 6379 配置(新版本使用replicaof关键字),然后启动从机即可。可以在主机上进行操作,验证数据是否同步到从机。

如果主机设置了链接密码,需要配置 masterauth

使用客户端连接,执行 info replation 查看主从配置信息

1
2
3
4
5
6
7
8
9
10
11
12
# Replication
role:master
connected_slaves:1
slave0:ip=ip,port=6379,state=online,offset=535,lag=0
master_replid:7f74c4a4d037e5558bbc50443598762f745b0463
master_replid2:0000000000000000000000000000000000000000
master_repl_offset:535
second_repl_offset:-1
repl_backlog_active:1
repl_backlog_size:1048576
repl_backlog_first_byte_offset:1
repl_backlog_histlen:535

redis sentinel配置

1
2
3
4
5
6
7
8
9
10
11
bind 127.0.0.1 192.168.1.1 #限定链接的客户端,可以注释掉
protected-mode no #保护模式关闭
daemonize yes #设置后台启动
sentinel announce-ip <ip> #如果sentinel机器都是内网部署,但是机器之间网络不通或者要跨公网访问
sentinel announce-port <port>#这里需要提供配置访问的出口IP和端口,一般情况下不需要配置
sentinel monitor mymaster 127.0.0.1 6379 2 #指定监控的主机,mymaster是个名称,主机IP,端口,当有2台哨兵认为主机挂掉之后,进行容灾切换
logfile ./sentinel.log #日志文件
sentinel auth-pass mymaster #Redis实例上是否启用了密码验证,启用的话这里需要配置密码
sentinel down-after-milliseconds mymaster 3000 #主实例多长时间无响应则认为挂掉
snetinel parallel-syncs mymaster 1 #发生容灾主备切换时,最多有一个从节点对新主节点进行数据同步
sentinel failover-timeout mymaster 180000 #故障转移的超时时间

在 bin 目录下 ./redis-sentinel ../etc/sentinel.conf 启动 sentinel

使用客户端连接哨兵,执行 info sentinel 查看哨兵配置信息

1
2
3
4
5
6
7
# Sentinel
sentinel_masters:1
sentinel_tilt:0
sentinel_running_scripts:0
sentinel_scripts_queue_length:0
sentinel_simulate_failure_flags:0
master0:name=mymaster,status=ok,address=IP:6379,slaves=1,sentinels=3