注:我是用root账号进的系统,我的redis版本为redis-3.2.10,解压在root目录下,安装在/usr/local/redis/目录下
创建6个节点,三主三从,端口为7001,7002,7003,7004,7005,7006,以此来集群,步骤如下
1.在root目录创建一个目录为redis-cluster,并在此目录下再创建6个文件夹
[root@localhost ~]# mkdir redis-cluster
[root@localhost ~]# cd redis-cluster
[root@localhost redis-cluster]# mkdir 7001 7002 7003 7004 7005 7006
2.将root目录下的redis-3.2.10中的redis.conf 分别复制到六个刚创建的文件夹下,这里可以先复制一个conf到7001下,配置好后再复制到其他文件夹,这样配置会快些,也不容易出错
[root@localhost ~]# cp redis-3.2.10/redis.conf redis-cluster/7001/
下面可以先不复制
[root@localhost ~]# cp redis-3.2.10/redis.conf redis-cluster/7002/
[root@localhost ~]# cp redis-3.2.10/redis.conf redis-cluster/7003/
[root@localhost ~]# cp redis-3.2.10/redis.conf redis-cluster/7004/
[root@localhost ~]# cp redis-3.2.10/redis.conf redis-cluster/7005/
[root@localhost ~]# cp redis-3.2.10/redis.conf redis-cluster/7006/
3.打开conf文件,修改其中的配置
[root@localhost ~]# vi redis-cluster/7001/redis.conf
4.修改其中配置
ps:按字母 i 进入编辑模式,按esc退出编辑模式,再不可编辑模式下按/+搜索词(如/port)进行搜索,
在不可编辑模式下 按:+ wq(:wq)退出并保存
修改其中的
port 7001 // 六个配置文件分别对应7001~7006
daemonize yes //redis后台运行
pidfile /var/run/redis_7001.pid //pidfile分别对应7001~7006
cluster-enabled yes //开启集群
cluster-config-file nodes_7001.conf //保存节点配置,自动创建,自动更新,对应7001~7006
cluster-node-timeout 15000 //集群超时时间,节点超过这个时间没反应就断定宕机,我是默认,可以修改
appendonly yes //开启aof持久化,将写操作记录保存到日志中
然后保存退出,开始另外五个的配置,这里可以将这个配置的文件复制到另外五个文件夹中,
修改其中的port pidfile cluster-config-file 注意仔细对应好
5.启动六个redis服务,在此之前,如果有持久化文件,先删掉。
[root@localhost ~]# /usr/local/redis/bin/redis-server redis-cluster/7001/redis.conf
[root@localhost ~]# /usr/local/redis/bin/redis-server redis-cluster/7002/redis.conf
[root@localhost ~]# /usr/local/redis/bin/redis-server redis-cluster/7003/redis.conf
[root@localhost ~]# /usr/local/redis/bin/redis-server redis-cluster/7004/redis.conf
[root@localhost ~]# /usr/local/redis/bin/redis-server redis-cluster/7005/redis.conf
[root@localhost ~]# /usr/local/redis/bin/redis-server redis-cluster/7006/redis.conf
6.使用官方推荐的集群工具redis-trib.rb,将redis解压包中的redis-trib工具复制至usr/local/bin目录下
[root@localhost ~]# cp redis-3.2.10/src/redis-tib.rb /usr/local/bin
7.使用redis-trib.rb之前要先安装ruby
[root@localhost ~]# yum -y install ruby ruby-devel rubygems rpm-build
8.然后使用如下命令来建立连接
[root@localhost ~]# gem install redis
这里可能会报错,redis requires ruby version 2.3.0,这说明你的ruby版本过低
解决方法,贴个链接 https://blog.csdn.net/Gushiyuta/article/details/90770681
解决后,重新执行上面命令
9,创建集群
[root@localhost ~]# redis-trib.rb create --replicas 1 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 127.0.0.1:7006
如果之前的持久化文件没有删掉,这里可能会创建失败,
此时kill命令杀掉所有redis进程。使用ps -ef | grep redis 来查看进程id,然后杀掉,
然后删掉持久化文件,删掉root目录下的六个nodes-****.conf文件,
然后执行第五步、第九步操作
Can I set the above configuration? yes
10.集群成功。结束,连接
[root@localhost ~]# /usr/local/redis/bin/redis-cli -c -p 7002
-c表示集群 , -p 表示端口
11.如果想创建带密码的集群,修改集群中的所有redis.conf文件中的密码,密码要一样,然后重启六个服务即可
masterauth=你的密码
requirepass=你的密码
12.带密码的连接
[root@localhost ~]# /usr/local/redis/bin/redis-cli -c -p 7002 -a 密码
-c表示集群 , -p 表示端口 , -a表示密码认证
13.如果集群带了密码,且你需要使用redis-trib.rb工具中的命令,得先修改usr/local/rvm/gems/ruby-2.3.3/gems/redis-4.1.3/lib/redis/client.rb中的密码(路径可能不一样,用 find / -name 'client.rb' 查询,密码要和前面设置的密码一样)
14.查看集群情况命令(root目录下)
[root@localhost ~]# redis-trib.rb check 127.0.0.1:7001