redis单机安装以及集搭建(redis-6.2.6)之前写过⼀篇基于redis-3.2.4版本的安装⽇记,这篇是基于redis-6.2.6改动不少,故再次记录⼀下
两台电脑10.2.5.147,10.2.5.148,都是centos7.5
本次搭建4主4从集
1.单机安装
1.1.下载安装包
直接从redis官⽹下载安装包,官⽹地址:redis.io/download
直接使⽤命令下载:wget dis.io/releases/redis-6.2.
1.2.安装编译
将安装包直接上传到了/opt⽬录
linux安装redis服务解压 tar -zxvf redis-6.2. 之后,得到⼀个redis-6.2.6的⽂件夹
个⼈⽐较喜欢将⽂件信息放在/usr/local/redis⽬录下,所以cp /opt/redis-6.2.6/* /usr/local/redis
接下来就是编译了
进⼊到/usr/local/redis⽬录,执⾏make命令
make编译报错
make[3]: 进⼊⽬录“/usr/local/redis/deps/hiredis”
cc -std=c99 -pedantic -c -O3 -fPIC -Wall -W -Wstrict-prototypes -Wwrite-strings -Wno-missing-field-initializers -g -ggdb alloc.c
make[3]: cc:命令未到
make[3]: *** [alloc.o] 错误 127
make[3]: 离开⽬录“/usr/local/redis/deps/hiredis”
make[2]: *** [hiredis] 错误 2
make[2]: 离开⽬录“/usr/local/redis/deps”
make[1]: [persist-settings] 错误 2 (忽略)
CC adlist.o
/bin/sh: cc: 未到命令
make[1]: *** [adlist.o] 错误 127
make[1]: 离开⽬录“/usr/local/redis/src”
安装更新gcc
yum isntall gcc-c++
make编译报错
致命错误:jemalloc/jemalloc.h:没有那个⽂件或⽬录
分配器allocator,如果有MALLOC 这个环境变量,会有⽤这个环境变量的去建⽴Redis。
⽽且libc 并不是默认的分配器,默认的是 jemalloc, 因为 jemalloc 被证明有更少的 fragmentation problems ⽐libc。
但是如果你⼜没有jemalloc ⽽只有 libc 当然 make 出错。所以加这么⼀个参数,运⾏如下命令:
make MALLOC=libc
make编译报错
cc: error: ../deps/hiredis/libhiredis.a: No such file or directory
cc: error: ../deps/lua/src/liblua.a: No such file or directory
cc: error: ../deps/jemalloc/lib/libjemalloc.a: No such file or directory
make: *** [redis-server] Error 1
进⼊redis下的deps下的运⾏如下命令
make lua hiredis linenoise
没有报错之后,在接下来安装redis
执⾏make install命令,最后安装结果如下
[root@localhost redis]# make install
cd src && make install
make[1]: 进⼊⽬录“/usr/local/redis/src”
Hint: It's a good idea to run 'make test' ;)
INSTALL redis-server
INSTALL redis-benchmark
INSTALL redis-cli
make[1]: 离开⽬录“/usr/local/redis/src”
安装成功
1.3 试运⾏
安装完成,需要修改如下配置,到/usr/local/redis⽬录下的f⽂件
打开⽂件,修改如下参数
允许远程连接,将下⼀⾏注释
#bind 127.0.0.1 -::1
redis默认是前台线程,改为后台启动,修改参数
daemonize yes
设置redis登录密码,到参数设置
requirepass 你的密码
启动redis:redis-server /usr/local/f
启动之后查看对应redis线程已启动,redis已可以使⽤
2.搭建集
4主4从集分布:
10.2.5.147 服务器,7010 7011 7012 7013四个节点
10.2.5.148 服务器,7015 7016 7017 7018四个节点
2.1.节点⽂件拷贝
在/usr/local/redis⽬录下新建集节点⽬录redis_cluster⽂件夹
mkdir -p /usr/local/redis/redis_cluster
在⽬录下创建4个节点⽂件夹
mkdir -p /usr/local/redis/redis_cluster/node01_7010
mkdir -p /usr/local/redis/redis_cluster/node02_7011
mkdir -p /usr/local/redis/redis_cluster/node03_7012
mkdir -p /usr/local/redis/redis_cluster/node04_7013
将redis⽂件夹中的f⽂件分别拷贝⾄上述4个⽂件夹中,并且依次修改每个⽂件,内容如下#以node01_7010节点为例
port 7010 #每个节点配置⾃⼰的端⼝号
pidfile /var/run/redis_7010.pid #pid⽂件
cluster-enabled yes #启⽤集
cluster-config-file f #集配置⽂件⾸次启动⾃动⽣成
cluster-node-timeout 15000 #超时时间
appendonly yes #aof⽇志
其他节点端⼝号依次为7011 7012 7013对应修改配置⽂件即可
修改完成后,启⽤4个节点
#启动7010节点
redis-server /usr/local/redis/redis_cluster/node01_f
#启动7011节点
redis-server /usr/local/redis/redis_cluster/node02_f
#启动7012节点
redis-server /usr/local/redis/redis_cluster/node03_f
#启动7013节点
redis-server /usr/local/redis/redis_cluster/node04_f
在10.2.5.148服务器中,同样进⾏上述操作即可,占⽤端⼝分别为7015 7016 7017 7018
2.2.安装ruby
安装2.4版本的ruby
2.3.组件集
在任意⼀台服务器中,进⾏集组建
redis-cli --cluster create 10.2.5.147:7010 10.2.5.147:7011 10.2.5.147:7012 10.2.5.147:7013 10.2.5.148:7015 10.2.5.148:7016 10.2.5.148:7017 10.2.5.148:7018 --cluster-replicas 1系统会随机选择主从关系
>>> Performing hash slots allocation on
Master[0] -> Slots 0 - 4095
Master[1] -> Slots 4096 - 8191
Master[2] -> Slots 8192 - 12287
Master[3] -> Slots 12288 - 16383
Adding replica 10.2.5.148:7017 to 10.2.5.147:7010
Adding replica 10.2.5.147:7013 to 10.2.5.148:7015
Adding replica 10.2.5.148:7018 to 10.2.5.147:7011
Adding replica 10.2.5.147:7012 to 10.2.5.148:7016
M: 988000d58f5b4c999c20172ef4ee1572ab24f22e 10.2.5.147:7010
slots:[0-4095] (4096 slots) master
M: b0d35f7d14f5c9ef1c261a6ef4cff3a09f39bd75 10.2.5.147:7011
slots:[8192-12287] (4096 slots) master
S: 738825958b81184e899b6f5a473a846392e6a9b6 10.2.5.147:7012
replicates 757e7f5f9d973acc182c9dadcd1d1f905571d43d
S: 723a484f7570dad408b7b50e525af5125b56e05a 10.2.5.147:7013
replicates 0a8d20f140315deeb649bf100793018243771394
M: 0a8d20f140315deeb649bf100793018243771394 10.2.5.148:7015
slots:[4096-8191] (4096 slots) master
M: 757e7f5f9d973acc182c9dadcd1d1f905571d43d 10.2.5.148:7016
slots:[12288-16383] (4096 slots) master
S: 59b175c9eba3c9931f14ac5fd65ad3ca1a19c140 10.2.5.148:7017
replicates 988000d58f5b4c999c20172ef4ee1572ab24f22e
S: c2927e8d368d4698b0accbc9e76216a3b5c67d12 10.2.5.148:7018
replicates b0d35f7d14f5c9ef1c261a6ef4cff3a09f39bd75
Can I set the above configuration? (type 'yes' to accept): yes
>>> Nodes configuration updated
>>> Assign a different config epoch to each node
>>> Sending CLUSTER MEET messages to join the cluster
Waiting for the cluster to join
...
>>> Performing Cluster Check (using node 10.2.5.147:7010)
M: 988000d58f5b4c999c20172ef4ee1572ab24f22e 10.2.5.147:7010
slots:[0-4095] (4096 slots) master
1 additional replica(s)
S: c2927e8d368d4698b0accbc9e76216a3b5c67d12 10.2.5.148:7018
slots: (0 slots) slave
replicates b0d35f7d14f5c9ef1c261a6ef4cff3a09f39bd75
M: 0a8d20f140315deeb649bf100793018243771394 10.2.5.148:7015
slots:[4096-8191] (4096 slots) master
1 additional replica(s)
S: 59b175c9eba3c9931f14ac5fd65ad3ca1a19c140 10.2.5.148:7017
slots: (0 slots) slave
replicates 988000d58f5b4c999c20172ef4ee1572ab24f22e
S: 723a484f7570dad408b7b50e525af5125b56e05a 10.2.5.147:7013
slots: (0 slots) slave
replicates 0a8d20f140315deeb649bf100793018243771394
M: b0d35f7d14f5c9ef1c261a6ef4cff3a09f39bd75 10.2.5.147:7011
slots:[8192-12287] (4096 slots) master
1 additional replica(s)
S: 738825958b81184e899b6f5a473a846392e6a9b6 10.2.5.147:7012
slots: (0 slots) slave
replicates 757e7f5f9d973acc182c9dadcd1d1f905571d43d
M: 757e7f5f9d973acc182c9dadcd1d1f905571d43d 10.2.5.148:7016
slots:[12288-16383] (4096 slots) master
1 additional replica(s)
[OK] All nodes agree about slots configuration.
>>> Check for
>>> Check
[OK] All 16384 slots covered.
⾄此集已经组建起来了,可以试试是否可⽤
在10.2.5.147服务器中推送消息、存储数据⾄10.2.5.148的7017节点
[root@localhost redis]# redis-cli -h 10.2.5.148 -c -p 7017
10.2.5.148:7017> PUBLISH testchannel "hello!"
(integer) 0
10.2.5.148:7017> PUBLISH testchannel "hello!"
(integer) 0
10.2.5.148:7017> SET accessToken 111111111111111111111111111
-> Redirected to slot [2275] located at 10.2.5.147:7010
OK
在10.2.5.148服务器中登录10.2.5.147的监听队列,获取数据
[root@localhost redis]# redis-cli -h 10.2.5.147 -c -p 7010 10.2.5.147:7010> SUBSCRIBE testchannel
(press Ctrl-C to quit)
1) "subscribe"
2) "testchannel"
3) (integer) 1
1) "message"
2) "testchannel"
3) "hello!"
[root@localhost redis]# redis-cli -h 10.2.5.147 -c -p 7010 10.2.5.147:7010> get accessToken "111111111111111111111111111"
完成
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。
发表评论