目录
QQ截图20190104141720.png1. 单机安装方法
1.1 下载安装包
1.2 解压到特定目录
[root@localhost 7101]# pwd
/usr/local/mredisCluster/7101
[root@localhost 7101]# ls
redis3.2.11 redis-3.2.11.tar.gz
[root@localhost 7101]# tar -zxvf redis-3.2.11.tar.gz
1.3 执行make和make install
- make
[root@localhost redis3.2.11]# make
cd src && make all
make[1]: 进入目录“/usr/local/mredisCluster/7101/redis3.2.11/src”
rm -rf redis-server redis-sentinel redis-cli redis-benchmark redis-check-rdb redis-check-aof *.o *.gcda *.gcno *.gcov redis.info lcov-html
……
……
Hint: It's a good idea to run 'make test' ;)
make[1]: 离开目录“/usr/local/mredisCluster/7101/redis3.2.11/src”
- make install
[root@localhost redis3.2.11]# make install
cd src && make install
make[1]: 进入目录“/usr/local/mredisCluster/7101/redis3.2.11/src”
Hint: It's a good idea to run 'make test' ;)
INSTALL install
INSTALL install
INSTALL install
INSTALL install
INSTALL install
make[1]: 离开目录“/usr/local/mredisCluster/7101/redis3.2.11/src”
1.4 基本配置
找到redis主目录下,本文为“/usr/local/mredisCluster/7101/redis3.2.11”的redis.conf文件,在redis.conf文件中,找到并修改如下内容
1.4.1 安全的生产环境最小配置
- 端口自定义
- 守护模式(后台运行)
- 开启保护模式
- 指定提供服务的端口
- 需要密码连接
port 7101
daemonize yes
protected-mode yes
bind ip 192.168.0.167
requirepass bamboocloud
1.4.2 开发时最省心的最小配置
- 端口默认6379
- 守护模式(后台运行)
- 关闭保护模式
- 不指定提供服务的端口
- 不需要密码连接
port 6379
daemonize yes
protected-mode no
# bind ip 127.0.0.1
# requirepass foobared
1.4.3 配置 保护模式(protected-mode)
- Redis自3.2.x后提供了保护模式,程序默认配置在保护模式下,此模式开启则必须要指定bind ip(参见 配置提供服务地址) 和 连接密码;
# 说明:保护模式yes-开启;
protected-mode yes
- 如果希望保持Redis使用最简单的配置,不设置连接密码,所以ip提供服务,则使用以下三种配置
# 说明:保护模式no-关闭;bind ip-注释掉;requirepass-注释掉
protected-mode no
# bind ip 127.0.0.1
# requirepass foobared
1.4.4 <span id="jump"> 配置提供服务地址(bind ip)</span>
原文参见:配置redis外网可访问
翻看网上的文章,此处多翻译为“指定redis只接收来自于该IP地址的请求,如果不进行设置,那么将处理所有请求,在生产环境中最好设置该项”。这种解释会totally搞糊涂初学者,甚至是错误的。
该处说明bind的是interface,也就是说是网络接口。服务器可以有一个网络接口(通俗的说网卡),或者多个。打个比方说机器上有两个网卡,分别为192.168.205.5 和192.168.205.6,如果bind 192.168.205.5,那么只有该网卡地址接受外部请求,如果不绑定,则两个网卡口都接受请求。
OK,不知道讲清楚没有,在举一个例子。在我上面的实验过程中,我是将bind项注释掉了,实际上我还有一种解决方案。由于我redis服务器的地址是 192.168.1.4 。如果我不注释bind项,还有什么办法呢?我可以做如下配置:
# bind 192.168.1.4
这里很多人会误以为绑定的ip应该是请求来源的ip。其实不然,这里应该绑定的是你redis服务器本身接受请求的ip。
该处的英文原文为
# If you want you can bind a single interface, if the bind option is not
# specified all the interfaces will listen for incoming connections.
# bind 127.0.0.1
所以bind后面的ip的意思,并不是限制客户端的地址,而是限制的服务端提供服务的地址。因为我服务器只有一个实际的物理网卡,我实际的配置就是这个网卡的地址:
#1. bind 127.0.0.1 改为实际地址
bind 192.168.0.167
1.4.5 配置端口
默认端口为6379,可根据个人情况指定(特别是集群和哨兵的时候需要指定),注意放开防火墙。
port 6379
1.4.6 配置连接密码
不需要密码时,注释掉本行内容,否则填入相应的密码
requirepass foobared
1.4.7 配置为守护模式
requirepass foobared
2. 一主两从配置
2.1 直接复制单机环境并修改基本配置
2.1.1 复制环境
新建7102和7103目录,复制redis主目录中所有内容
[root@localhost mredisCluster]# ls
7101 7102 7103
[root@localhost mredisCluster]# cd 7103
[root@localhost 7103]# cp -rf ../7101/redis3.2.11/ .
[root@localhost 7103]# ls
redis3.2.11
2.1.2 修改部分配置
修改端口为保持和文件夹命名一致
[root@localhost 7103]#
[root@localhost 7103]# vim redis3.2.11/redis.conf
port 7103
2.2 修改配置形成主从集群
2.2.1 分别修改7102和7103的redis配置
- (1)保持7101配置不变(master配置不变)
- (2)需要注意的是slaveof中键没有空格。
- (3)master配置的密码的话,需要配置masterauth
[root@localhost 7103]# pwd
/usr/local/mredisCluster/7103
[root@localhost 7103]# vim redis3.2.11/redis.conf
# slaveof <masterip> <masterport>
slaveof 192.168.0.167 7101
# masterauth <master-password>
masterauth bamboocloud
2.2.2 为redis提供日志输出
- 在redis.conf添加以下配置
logfile "/usr/local/mredisCluster/7103/7103.log"
2.2.3 主从复制排错
- 没有配置复制密码错误,显示需要认证。
110531:S 25 Dec 14:27:35.803 * MASTER <-> SLAVE sync started
110531:S 25 Dec 14:27:35.803 * Non blocking connect for SYNC fired the event.
110531:S 25 Dec 14:27:35.804 * Master replied to PING, replication can continue...
110531:S 25 Dec 14:27:35.804 * (Non critical) Master does not understand REPLCONF listening-port: -NOAUTH Authentication required.
110531:S 25 Dec 14:27:35.804 * (Non critical) Master does not understand REPLCONF capa: -NOAUTH Authentication required.
110531:S 25 Dec 14:27:35.804 * Partial resynchronization not possible (no cached master)
110531:S 25 Dec 14:27:35.804 # Unexpected reply to PSYNC from master: -NOAUTH Authentication required.
3. 新增哨兵配置
3.1 哨兵端口规划
目前主从实例为7101为master、7102和7103为slave从节点;
所以,规划三个哨兵,端口分别为7201、7202、7203;
3.2 哨兵配置
- 分配配置7101-7103 三个redis下的sentinel.conf
- 配置组名为默认的:mymaster
- 配置需要监控的主机:master的ip
- 配置权重为:1
# The valid charset is A-z 0-9 and the three characters ".-_".
sentinel monitor mymaster 192.168.0.167 7101 1
sentinel auth-pass mymaster bamboocloud
logfile "/usr/local/mredisCluster/7101/sen.log"
bind 192.168.0.167
masterauth "bamboocloud"
protected-mode yes
daemonize yes
3.3 启动哨兵
[root@localhost redis3.2.11]# ./src/redis-server sentinel.conf --sentinel
3.4 哨兵排错
3.4.1 离线的node重新加入后,与master无法通信问题
- 过程如下:
1.系统初始化7101作为master,7102、7103作为slave
2.shutdown 7101,7102倍选为master,7103继续作为slave
3.7101启动,作为slave加入节点。但是7102下的slave并未增加,且7101显示master_link_status:down
master_link_status:down
- 排错:结果
因为配置了认证,所以最开始默认了7102、7103配置了,但是7101没有配置,所以再次加入时,无法通过master的密码认证。所以连接失败。
#redis.conf的配置
masterauth bamboocloud
- 所以,敲黑板!!配置哨兵的时候,一定要想到每个node是平级的,该配置的认证都要配置好。
3.4.2 启动哨兵时,显示如下错误
*** FATAL CONFIG FILE ERROR ***
Reading the configuration file, at line 69
>>> 'sentinel monitor mymaster 192.168.0.167 7101 1'
sentinel directive while not in sentinel mode
- 排错:启动命令错误,结尾少了 --sentinel
3.5 哨兵测试
- 整体情况-错误(具体错误,排错的时候给出)
root 79041 0.0 0.1 136972 2744 pts/0 Sl 16:56 0:00 ./src/redis-server *:7203 [sentinel]
root 80313 0.0 0.1 136972 2728 pts/0 Sl 16:58 0:00 ./src/redis-server *:7202 [sentinel]
root 81751 0.0 0.1 136972 2736 pts/0 Sl 17:00 0:00 ./src/redis-server *:7201 [sentinel]
root 88103 0.0 0.0 112724 984 pts/1 S+ 17:10 0:00 grep --color=auto redis
root 111014 0.0 0.1 139020 2496 ? Ssl 11:09 0:11 ./redis-server 192.168.0.167:7101
root 118507 0.0 0.1 136968 2356 ? Ssl 14:38 0:04 ./redis3.2.11/src/redis-server 192.168.0.167:7102
root 119332 0.0 0.1 136968 2360 ? Ssl 14:39 0:04 ./src/redis-server 192.168.0.167:7103
- 整体情况-ok
root 19802 0.0 0.1 136968 2580 ? Ssl 18:43 0:03 ./src/redis-server 192.168.0.167:7201 [sentinel]
root 31755 0.0 0.1 136968 2480 ? Ssl 19:01 0:02 ./src/redis-server 192.168.0.167:7202 [sentinel]
root 32986 0.1 0.1 136968 2508 ? Ssl 19:03 0:02 ./src/redis-server 192.168.0.167:7203 [sentinel]
root 36312 0.1 0.1 139020 2716 ? Ssl 19:08 0:03 ./src/redis-server 192.168.0.167:7101
root 61598 0.0 0.2 151648 5188 pts/2 S+ 19:46 0:00 vim redis3.2.11/sentinel.conf
root 62737 0.0 0.0 112724 980 pts/1 S+ 19:48 0:00 grep --color=auto redis
root 102469 0.0 0.1 139016 3132 ? Ssl 17:31 0:05 ./src/redis-server 192.168.0.167:7102
root 119332 0.0 0.1 139016 2864 ? Ssl 14:39 0:09 ./src/redis-server 192.168.0.167:7103
- master的状态
192.168.0.167-7101:0>info replication
"# Replication
role:master
connected_slaves:2
slave0:ip=192.168.0.167,port=7102,state=online,offset=12937,lag=1
slave1:ip=192.168.0.167,port=7103,state=online,offset=12937,lag=0
master_repl_offset:12937
repl_backlog_active:1
repl_backlog_size:1048576
repl_backlog_first_byte_offset:2
repl_backlog_histlen:12936
- slave的状态
192.168.0.167-7102:0>info replication
"# Replication
role:slave
master_host:192.168.0.167
master_port:7101
master_link_status:up
master_last_io_seconds_ago:6
master_sync_in_progress:0
slave_repl_offset:13203
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
"
- 正常切换的额日志
_._
_.-``__ ''-._
_.-`` `. `_. ''-._ Redis 3.2.11 (00000000/0) 64 bit
.-`` .-```. ```\/ _.,_ ''-._
( ' , .-` | `, ) Running in sentinel mode
|`-._`-...-` __...-.``-._|'` _.-'| Port: 7201
| `-._ `._ / _.-' | PID: 19802
`-._ `-._ `-./ _.-' _.-'
|`-._`-._ `-.__.-' _.-'_.-'|
| `-._`-._ _.-'_.-' | http://redis.io
`-._ `-._`-.__.-'_.-' _.-'
|`-._`-._ `-.__.-' _.-'_.-'|
| `-._`-._ _.-'_.-' |
`-._ `-._`-.__.-'_.-' _.-'
`-._ `-.__.-' _.-'
`-._ _.-'
`-.__.-'
19802:X 25 Dec 18:43:27.143 # Sentinel ID is 1c46113935b50308d1b5c77d096c34903501fb08
19802:X 25 Dec 18:43:27.143 # +monitor master mymaster 192.168.0.167 7101 quorum 1
19802:X 25 Dec 18:43:57.182 # +sdown master mymaster 192.168.0.167 7101
19802:X 25 Dec 18:43:57.182 # +odown master mymaster 192.168.0.167 7101 #quorum 1/1
19802:X 25 Dec 18:43:57.182 # +new-epoch 17
19802:X 25 Dec 18:43:57.182 # +try-failover master mymaster 192.168.0.167 7101
19802:X 25 Dec 18:43:57.216 # +vote-for-leader 1c46113935b50308d1b5c77d096c34903501fb08 17
19802:X 25 Dec 18:43:57.216 # +elected-leader master mymaster 192.168.0.167 7101
19802:X 25 Dec 18:43:57.216 # +failover-state-select-slave master mymaster 192.168.0.167 7101
19802:X 25 Dec 18:43:57.306 # -failover-abort-no-good-slave master mymaster 192.168.0.167 7101
19802:X 25 Dec 18:43:57.373 # Next failover delay: I will not start a failover before Tue Dec 25 18:49:57 2018
20397:X 25 Dec 18:44:21.017 * Increased maximum number of open files to 10032 (it was originally set to 1024).
20397:X 25 Dec 18:44:21.017 # Creating Server TCP listening socket 192.168.0.167:7201: bind: Address already in use
21388:X 25 Dec 18:45:50.958 * Increased maximum number of open files to 10032 (it was originally set to 1024).
21388:X 25 Dec 18:45:50.958 # Creating Server TCP listening socket 192.168.0.167:7201: bind: Address already in use
19802:X 25 Dec 18:49:29.070 * +slave slave 192.168.0.167:7102 192.168.0.167 7102 @ mymaster 192.168.0.167 7101
19802:X 25 Dec 18:49:29.142 # -sdown master mymaster 192.168.0.167 7101
19802:X 25 Dec 18:49:29.142 # -odown master mymaster 192.168.0.167 7101
19802:X 25 Dec 18:49:39.169 * +slave slave 192.168.0.167:7103 192.168.0.167 7103 @ mymaster 192.168.0.167 7101
19802:X 25 Dec 19:01:31.279 * +sentinel sentinel 22dc756cc162e34b53036b97c1c067493a11dfc5 192.168.0.167 7202 @ mymaster 192.168.0.167 7101
19802:X 25 Dec 19:03:22.252 * +sentinel sentinel 1fb55102b245eeedd35007821b8bae50ba0e793d 192.168.0.167 7203 @ mymaster 192.168.0.167 7101
w^H
19802:X 25 Dec 19:06:05.291 # +new-epoch 18
19802:X 25 Dec 19:06:05.302 # +vote-for-leader 1fb55102b245eeedd35007821b8bae50ba0e793d 18
19802:X 25 Dec 19:06:05.314 # +sdown master mymaster 192.168.0.167 7101
19802:X 25 Dec 19:06:05.314 # +odown master mymaster 192.168.0.167 7101 #quorum 1/1
19802:X 25 Dec 19:06:05.314 # Next failover delay: I will not start a failover before Tue Dec 25 19:12:05 2018
19802:X 25 Dec 19:06:06.108 # +config-update-from sentinel 1fb55102b245eeedd35007821b8bae50ba0e793d 192.168.0.167 7203 @ mymaster 192.168.0.167 7101
19802:X 25 Dec 19:06:06.108 # +switch-master mymaster 192.168.0.167 7101 192.168.0.167 7102
19802:X 25 Dec 19:06:06.108 * +slave slave 192.168.0.167:7103 192.168.0.167 7103 @ mymaster 192.168.0.167 7102
19802:X 25 Dec 19:06:06.108 * +slave slave 192.168.0.167:7101 192.168.0.167 7101 @ mymaster 192.168.0.167 7102
19802:X 25 Dec 19:06:36.114 # +sdown slave 192.168.0.167:7101 192.168.0.167 7101 @ mymaster 192.168.0.167 7102
19802:X 25 Dec 19:08:19.746 # -sdown slave 192.168.0.167:7101 192.168.0.167 7101 @ mymaster 192.168.0.167 7102
19802:X 25 Dec 19:08:29.721 * +convert-to-slave slave 192.168.0.167:7101 192.168.0.167 7101 @ mymaster 192.168.0.167 7102
网友评论