使用redis4.0.1和redis-cluster搭建集群并编写重启shell脚本
生活随笔
收集整理的這篇文章主要介紹了
使用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 test3.配置節點(新建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:70054.常見錯誤及解決方法
如果系統上各種依賴比較齊全的話,這時就能啟動成功了,通常還可能遇到以下錯誤
錯誤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錯誤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錯誤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解決方案:
gem install redis 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:70055.測試cluster集群是否能正常使用
/usr/local/src/redis-cli -c -h 172.16.180.226 -p 7000上面的命令如果缺少了“-c”會出現以下錯誤。
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:7005shell文件保存之后需要賦予可執行權限。
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轉載于:https://www.cnblogs.com/gumuzi/p/7285524.html
總結
以上是生活随笔為你收集整理的使用redis4.0.1和redis-cluster搭建集群并编写重启shell脚本的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 保险退保怎么退
- 下一篇: 华邦控股集团是国企吗