---
title: "Redis集群安装"
author: "fairy_xzc"
date: "2017-06-07T21:46:00Z"
tags: []
categories: ["数据库"]
slug: "redis"
draft: false
lastmod: "2017-06-07T22:39:07Z"
id: 2378272
url: "https://agilestyle.iteye.com/blog/2378272"
---

**原创转载请注明出处：[http://agilestyle.iteye.com/blog/2378272](/blog/2378272)**

 

Redis集群搭建，至少有3个节点，每个节点有一个备份节点，因此需要6台服务器

创建redis-cluster目录，拷贝redis安装目录到redis-cluster目录，修改别名，删除rdb  
![](http://dl2.iteye.com/upload/attachment/0125/3154/0b535fba-d9d0-3237-9ef1-adbe0980bbb8.png)

 

修改redis.conf，绑定IP为192.168.5.200，并且不要设置requirepass，否则将导致创建 \[ERR] Sorry, can't connect to node，被坑了两天，虚耗生命

![](http://dl2.iteye.com/upload/attachment/0125/3324/8b612e9c-b8e0-3fff-82da-eaad2a105e6f.png)  
 

修改redis.conf，端口改为7001

![](http://dl2.iteye.com/upload/attachment/0125/3156/3b324e5d-0a72-39db-a68d-fa2cc62879e0.png)  
 

开启集群

![](http://dl2.iteye.com/upload/attachment/0125/3158/79d25af4-1ebe-33d6-a2ef-2ef9a193aac4.png)  
 

保存退出，拷贝5份，修改各自的port

![](http://dl2.iteye.com/upload/attachment/0125/3160/acf3d571-1d55-3eb0-b47e-bc3836a43b29.png)  
 

拷贝redis源码文件夹下的src目录下的redis-trib.rb到redis-cluster目录

![](http://dl2.iteye.com/upload/attachment/0125/3162/5d393e36-c628-35a2-a8d6-2d478467b097.png)  
 

安装ruby和rubygems（需要连接外网）

sudo apt-get install software-properties-common

sudo apt-add-repository ppa:brightbox/ruby-ng

sudo apt-get update

sudo apt-get install ruby2.4

 

安装ruby和redis接口程序

sudo gem install redis

 

编写启动所有redis实例的脚本start-all-redis.sh

```
cd redis01/bin
./redis-server redis.conf
cd ../..
cd redis02/bin
./redis-server redis.conf
cd ../..
cd redis03/bin
./redis-server redis.conf
cd ../..
cd redis04/bin
./redis-server redis.conf
cd ../..
cd redis05/bin
./redis-server redis.conf
cd ../..
cd redis06/bin
./redis-server redis.conf
```

 

sudo chmod 744 start-all-redis.sh

./start-all-redis.sh

![](http://dl2.iteye.com/upload/attachment/0125/3332/7c792d19-e368-3613-b754-d7dd90ce3a12.png)  
 ps -ef | grep redis  
![](http://dl2.iteye.com/upload/attachment/0125/3334/b4d95d72-58c9-3e6e-b0be-360be36c4008.png)  
 

使用redis-trib.rb创建集群

./redis-trib.rb create --replicas 1 192.168.5.200:7001 192.168.5.200:7002 192.168.5.200:7003 192.168.5.200:7004 192.168.5.200:7005 192.168.5.200:7006 

![](http://dl2.iteye.com/upload/attachment/0125/3322/531dbd93-085d-3a8a-81fc-8a36c0b73924.png)  
 

使用redis-cli连接集群，需要加 -c 参数

 ./redis-cli -p 7001 -h 192.168.5.200 -c

![](http://dl2.iteye.com/upload/attachment/0125/3330/bf743afd-c519-3f3f-8fee-6a6bc53a9eca.png)  
 

 

## Reference

[https://www.brightbox.com/docs/ruby/ubuntu/#installation](https://www.brightbox.com/docs/ruby/ubuntu/#installation)