1.准备工作
去官网下载好你想要安装的redis版本,下载链接
2.搭建步骤
输入命令yum install gcc-c++安装好gcc环境,将下载好的redis安装包上传到 /usr/local
解压redis安装包
tar -zxvf redis-4.0.11.tar.gz
进入解压后的目录进行编译
cd /usr/local/redis-4.0.11
make
编译这一步可以指定目录,也就是安装的指定目录
把上一步的make 写成 make PREFIX=/usr/local/redis install (在这里我指定的目录为 /usr/local/redis)
安装完毕之后 redis文件夹下面会生成一个bin目录
bin目录下有以下几个文件
复制redis.conf到bin目录下
cp /usr/local/redis-4.0.11 /usr/local/redis/bin
进入bin目录
cd /usr/local/redis/bin
编辑redis.conf
vi redis.conf
进行如下修改
daemonize yes //redis后台运行 cluster-enabled yes //开启集群 把注释#去掉 cluster-node-timeout 5000 //请求超时 设置5秒够了 appendonly yes //aof日志开启 有需要就开启,它会每次写操作都记录一条日志
创建集群目录
cd /usr/local
mkdir redis-cluster
将bin目录下的文件拷贝到集群目录下的节点目录中,共有6个目录,分别是redis01、redis02、redis03、redis04、redis05、redis06
cp -r /usr/local/redis/bin/ /usr/local/redis-cluster/redis01
cp -r /usr/local/redis/bin/ /usr/local/redis-cluster/redis02
cp -r /usr/local/redis/bin/ /usr/local/redis-cluster/redis03
cp -r /usr/local/redis/bin/ /usr/local/redis-cluster/redis04
cp -r /usr/local/redis/bin/ /usr/local/redis-cluster/redis05
cp -r /usr/local/redis/bin/ /usr/local/redis-cluster/redis06
对每个目录下的redis.conf文件进行如下修改
pidfile /var/run/redis_7001.pid //redis01下7001,redis下7002,以此类推,下同 port 7001 cluster-config-file nodes_7001.conf //集群的配置 配置文件首次启动自动生成 7001,7002,7003
在redis-cluster目录下新建一个启动脚本start-all.sh
cd redis01 ./redis-server redis.conf cd .. cd redis02 ./redis-server redis.conf cd .. cd redis03 ./redis-server redis.conf cd .. cd redis04 ./redis-server redis.conf cd .. cd redis05 ./redis-server redis.conf cd .. cd redis06 ./redis-server redis.conf cd ..
给启动脚本赋予权限
chmod +x start.sh
执行启动脚本,启动redis
./start-all.sh
将redis源码目录src下的redis-trib.rb拷贝到redis-cluster目录下
cp /usr/local/redis-4.0.11/src/redis-trib.rb /usr/local/redis-cluster
根据这个redis-trib.rb这个文件的后缀,很明显还需要安装ruby,查看是否安装ruby以及版本
ruby -version
低于2.2的版本在后续安装中会出现问题,如安装的版本低于2.2,则需要安装2.2以上的版本
具体安装步骤可以参考博主的另一篇博客:搭建redis集群的过程中遇到的问题
安装好ruby环境之后,再用 gem 这个命令来安装 redis接口
gem install redis
安装完事儿之后,执行redis-trib.rb
/usr/local/redis-cluster/redis-trib.rb create --replicas 1 192.168.25.133:7001 192.168.25.133:7002 192.168.25.133:7003 192.168.25.133:7004 192.168.25.133:7005 192.168.25.133:7006
如遇到如下提示信息Can I set the above configuration? (type 'yes' to accept):输入Y
创建成功会出现如下的信息:
>>> Creating cluster
>>> Performing hash slots allocation on 6 nodes...
Using 3 masters:
192.168.106.132:7001
192.168.106.132:7002
192.168.106.132:7003
Adding replica 192.168.106.132:7004 to 192.168.106.132:7001
Adding replica 192.168.106.132:7005 to 192.168.106.132:7002
Adding replica 192.168.106.132:7006 to 192.168.106.132:7003
M: 6c388e0cd04990b23e2e65b285b5f9c1bc996538 192.168.106.132:7001
slots:0-5460 (5461 slots) master
M: 01e9cd67978b1cad73a40e9a70000a236744cb17 192.168.106.132:7002
slots:5461-10922 (5462 slots) master
M: 72a649017fdd5d4045f9d58df8b231c2d69e6c32 192.168.106.132:7003
slots:10923-16383 (5461 slots) master
S: b90aa487cf48270df43e79af98807bb5ffabbe34 192.168.106.132:7004
replicates 6c388e0cd04990b23e2e65b285b5f9c1bc996538
S: f873f54c61dbb613df58fa4b9ff8bc3dd48e2388 192.168.106.132:7005
replicates 01e9cd67978b1cad73a40e9a70000a236744cb17
S: daa813e169580417f4410fac86a3d4ab9abda804 192.168.106.132:7006
replicates 72a649017fdd5d4045f9d58df8b231c2d69e6c32
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 192.168.106.132:7001)
M: 6c388e0cd04990b23e2e65b285b5f9c1bc996538 192.168.106.132:7001
slots:0-5460 (5461 slots) master
1 additional replica(s)
S: b90aa487cf48270df43e79af98807bb5ffabbe34 192.168.106.132:7004
slots: (0 slots) slave
replicates 6c388e0cd04990b23e2e65b285b5f9c1bc996538
M: 72a649017fdd5d4045f9d58df8b231c2d69e6c32 192.168.106.132:7003
slots:10923-16383 (5461 slots) master
1 additional replica(s)
M: 01e9cd67978b1cad73a40e9a70000a236744cb17 192.168.106.132:7002
slots:5461-10922 (5462 slots) master
1 additional replica(s)
S: daa813e169580417f4410fac86a3d4ab9abda804 192.168.106.132:7006
slots: (0 slots) slave
replicates 72a649017fdd5d4045f9d58df8b231c2d69e6c32
S: f873f54c61dbb613df58fa4b9ff8bc3dd48e2388 192.168.106.132:7005
slots: (0 slots) slave
replicates 01e9cd67978b1cad73a40e9a70000a236744cb17
[OK] All nodes agree about slots configuration.
>>> Check for open slots...
>>> Check slots coverage...
[OK] All 16384 slots covered.
若出现如下报错信息:./redis-trib.rb:885:in 'sort!' : undefined method '<=>' for:m: symbol (NoMethodError)
或出现其它的一些问题,可以参考博主另一篇博客:搭建redis集群的过程中遇到的问题