使用redis4.0.1和redis-cluster搭建集群并编写重启shell脚本

1.删除机器上原有的redis2.8

关闭redis-server

killall -9 redis-server

查找redis文件所在目录

which redis

删除相关文件

rm -rf redis
rm -rf redis-proxy/
rm -rf nutcracker/

删除相关命令

rm -f /usr/local/bin/redis-*

2.下载、安装redis

wget http://download.redis.io/releases/redis-4.0.1.tar.gz
tar xzf redis-4.0.1.tar.gz
mv redis-4.0.1 /usr/local/
cd /usr/local/redis-4.0.1/
make
make test

3.配置节点(新建6个以端口号命名的目录,表示6个节点,3主3从)

cd /usr/local
mkdir cluster
cd cluster/
mkdir 7000
mkdir 7001
mkdir 7002
mkdir 7003
mkdir 7004
mkdir 7005

在上面新建的每个目录下新建redis.conf文件,并写入以下内容:

port 7000
cluster-enabled yes
cluster-config-file nodes.conf
cluster-node-timeout 5000
appendonly yes
protected-mode no
vim 7000/redis.conf
vim 7001/redis.conf
vim 7002/redis.conf
vim 7003/redis.conf
vim 7004/redis.conf
vim 7005/redis.conf

为了方便使用把redis-server文件复制到/usr/local/cluster目录下

cp /usr/local/redis-4.0.1/src/redis-server  /usr/local/cluster

进入每个节点的目录,并用对应的配置文件启动redis

cd /usr/local/cluster/7000
../redis-server ./redis.conf &

先测试一下是否启动成功过,

/usr/local/redis-4.0.1/src/redis-cli -p 7000

如果能进去redis命令行表示启动成功,再去启动其他端口

cd /usr/local/cluster/7001
../redis-server ./redis.conf &
cd /usr/local/cluster/7002
../redis-server ./redis.conf &
cd /usr/local/cluster/7003
../redis-server ./redis.conf &
cd /usr/local/cluster/7004
../redis-server ./redis.conf &
cd /usr/local/cluster/7005
../redis-server ./redis.conf &

接下来启动cluster

cd /usr/local/redis-4.0.1/src/
./redis-trib.rb create --replicas 1 172.16.180.226:7000 172.16.180.226:7001 172.16.180.226:7002 172.16.180.227:7003 172.16.180.227:7004 172.16.180.227:7005

4.常见错误及解决方法

如果系统上各种依赖比较齐全的话,这时就能启动成功了,通常还可能遇到以下错误

错误1:

root@localhost src]# ./redis-trib.rb create --replicas 1 172.16.180.226:7000 172.16.180.226:7001 172.16.180.226:7002 172.16.180.227:7003 172.16.180.227:7004 172.16.180.227:7005
/usr/bin/env: ruby: No such file or directory

解决方案:

yum install ruby

使用redis4.0.1和redis-cluster搭建集群并编写重启shell脚本

错误2:

[root@localhost src]# ./redis-trib.rb create --replicas 1 172.16.180.226:7000 172.16.180.226:7001 172.16.180.226:7002 172.16.180.227:7003 172.16.180.227:7004 172.16.180.227:7005
./redis-trib.rb:24:in `require': no such file to load -- rubygems (LoadError)
from ./redis-trib.rb:24

解决方案:

yum install rubygems

使用redis4.0.1和redis-cluster搭建集群并编写重启shell脚本

错误3:

[root@localhost src]# ./redis-trib.rb creat --replicas 1 172.16.180.226:7000 172.16.180.226:7001 172.16.180.226:7002 172.16.180.227:7003 172.16.180.227:7004 172.16.180.227:7005
/usr/lib/ruby/site_ruby/1.8/rubygems/custom_require.rb:31:in `gem_original_require': no such file to load -- redis (LoadError)
from /usr/lib/ruby/site_ruby/1.8/rubygems/custom_require.rb:31:in `require'
from ./redis-trib.rb:25

使用redis4.0.1和redis-cluster搭建集群并编写重启shell脚本

解决方案:

gem install redis

使用redis4.0.1和redis-cluster搭建集群并编写重启shell脚本

yum install ruby
yum install rubygems
gem install redis

安装完以上各种依赖之后,再次执行以下命令即可成功启动cluster

./redis-trib.rb create --replicas 1 172.16.180.226:7000 172.16.180.226:7001 172.16.180.226:7002 172.16.180.227:7003 172.16.180.227:7004 172.16.180.227:7005

使用redis4.0.1和redis-cluster搭建集群并编写重启shell脚本

5.测试cluster集群是否能正常使用

/usr/local/src/redis-cli  -c -h 172.16.180.226 -p 7000

上面的命令如果缺少了“-c”会出现以下错误。

使用redis4.0.1和redis-cluster搭建集群并编写重启shell脚本

6.编写重启cluster的shell脚本

vim restart-cluster.sh

写入以下内容:

#!/bin/sh
killall -9 redis-server
#删掉以前运行的节点留下的配置信息
find /usr/local/cluster/ -name "appendonly.aof"|xargs rm -f;
find /usr/local/cluster/ -name "nodes.conf"|xargs rm -f;
find /usr/local/cluster/ -name "dump.rdb"|xargs rm -f;
#重新启动redis
cd /usr/local/cluster/7000
../redis-server ./redis.conf &
cd /usr/local/cluster/7001
../redis-server ./redis.conf &
cd /usr/local/cluster/7002
../redis-server ./redis.conf &
cd /usr/local/cluster/7003
../redis-server ./redis.conf &
cd /usr/local/cluster/7004
../redis-server ./redis.conf &
cd /usr/local/cluster/7005
../redis-server ./redis.conf &
cd /usr/local/redis-4.0.1/src/
#重新创建cluster集群
./redis-trib.rb create --replicas 1 127.0.0.1:7000 127.0.0.1:7001 127.0.0.1:7002 127.0.0.1:7003 127.0.0.1:7004 127.0.0.1:7005

shell文件保存之后需要赋予可执行权限。

chmod +x restart-cluster.sh

使用脚本重启

./restart-cluster.sh

查看是否重启成功

src/redis-cli -c -p 7000

本文参考了以下资料:

http://www.redis.cn/download.html
http://www.redis.cn/topics/cluster-tutorial.html
http://www.oschina.net/question/1259683_2133954
上一篇:洛谷P2698 花盆Flowerpot【单调队列】


下一篇:JavaScript判断类型