centOS7下redis3.0安装以及布置集群教程(单机创建多个实例调试)
转载自:https://blog.****.net/zyhlearnjava/article/details/73008054
版本说明
本教程使用redis3.0版本。3.0版本主要增加了redis集群功能。
安装的前提条件
需要安装gcc:yuminstall gcc-c++(如果你的Linux环境下已经装过了Nginx,此处可忽略,因为Nginx也需要gcc环境才能安装)
源码下载:此处提供官网下载地址(点击打开链接)
安装位置:将redis-3.0.0.tar.gz拷贝到/usr/local下(本文安装位置在/usr/local/redis)
开始安装
[[email protected] ~]# cd /usr/local/
[[email protected] local]# tar -zxvf redis-3.0.0.tar.gz
[[email protected] local]# cd redis-3.0.0/
[[email protected] redis-3.0.0]# make
[[email protected] redis-3.0.0]# make PREFIX=/usr/local/redis install
等待,要是其中某处问你yes/no或者y/n,输入yes或者y即可
测试:
[[email protected] ~]# cd /usr/local/redis//bin/
[[email protected] bin]# ./redis-server
出现:
安装成功!
接下来修改redis.conf配置文件为后端启动和集群做准备!
[[email protected] bin]# chmod 777 redis.conf
[[email protected] bin]# vim redis.conf
位置:第37行daemonize 把no改为yes,允许后端启动
位置:第633行cluster-enabled把no改为yes,集群的前提条件
:wq
搭建集群需要的环境
搭建集群需要使用到官方提供的ruby脚本。
需要安装ruby的环境。
[[email protected] ]# yum install ruby[[email protected] ]# yum install rubygems
上传redis-3.0.0.gem到/usr/local下
[[email protected] ]# gem install redis-3.0.0.gem
创建实例前的准备(文件夹)
[[email protected] local ]# mkdir redis-cluster
[[email protected] local ]#cp -r bin ../redis-cluster/redis01
以此步骤在redis-cluster下分别建立redis01到redis06总共6个文件夹,以此看做6个redis,后面将以此为基础进行调试
分别修改每一个redis0X下的redis.conf的端口分别从7001到7006
每一个redis.conf第633行cluster-enabled把no改为yes
:wq保存并退出
把创建集群的ruby脚本复制到redis-cluster目录下
[[email protected] ]# cd /usr/local/redis-3.0.0/src
[[email protected] src ]# cp *.rb /usr/local/redis-cluster/
至此文件夹的准备工作结束!
iptables里面要打开7001到7006端口!!!!!!!
编写打开6个redis实例的脚本
[[email protected] ]#cd/usr/local/redis-cluster/
[[email protected] redis-cluster]# vim startall.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 ..
:wq保存并退出
完成后redis-cluster的目录结构应该是这样的
建议把每个redis0X文件夹下的redis.conf都给大点的权限
[[email protected] redis0X]# chmod 777 redis.conf
打开6个redis实例
[[email protected] redis-cluster]# ./startall.sh
查看所有的redis
[[email protected] redis-cluster]# ps -aux|grep redis
(1)[[email protected] redis-cluster]# ./redis-trib.rb create --replicas 1 192.168.181.132:7001 192.168.181.132:7002 192.168.181.132:7003 192.168.181.132:7004 192.168.181.132:7005 192.168.181.132:7006
博主的Linux虚拟机的静态ip地址是192.168.181.132请根据自己的实际情况修改,centOS7的防火墙需要设置,网上很多,博主其他的博文里面也有提到
1、这一步估计有不少人会失败:出现这个问题
最下方是
[ERR]Node XXXXXX is not empty. Either the node already knows other nodes (check withCLUSTER NODES) or contains some key in database 0
解决办法是将每一个redis下删除生成的配置文件nodes.conf,如果不行则说明现在创建的结点包括了旧集群的结点信息,需要删除redis的持久化文件后再重启redis,比如:appendonly.aof、dump.rdb
还需要进入每一个redis清除数据:[[email protected] redis-cluster]# redis01/redis-cli -h 192.168.181.132 -p 7001 -c
192.168.181.132:7006> FLUSHALL
总共6个redis清除后重复(1)
若还是有问题:
[[email protected] redis-cluster]# ./redis-trib.rb check 192.168.181.132:7001
正常状态下应该是:
如果不是如上,可以修复这个端口
[[email protected] redis-cluster]# ./redis-trib.rb fix 192.168.181.132:7001
请检查这6个端口并修复!!!
创建成功
操作(1)成功后应该出现:
输入yes确定
出现:
调试redis集群效果
参照下图,set get 几个数据,相互之间端口会有跳转,证明集群布置成功会根据计算出的位置自己选择存储位置,而且互相之间可以访问数据
success!!!!!