1.复制redis-trib.rb命令
Reids安装包里有个集群工具,要复制到/usr/local/bin里去。
cp redis-3.2.9/src/redis-trib.rb /usr/local/bin
2.修改配置,创建节点
我们现在要搞六个节点,三主三从,
端口规定分别是7001,7002,7003,7004,7005,7006
我们先在root目录下新建一个redis_cluster目录,然后该目录下再创建6个目录,
分别是7001,7002,7003,7004,7005,7006,用来存在redis配置文件;
这里我们要使用redis集群,要先修改redis的配置文件redis.conf
mkdir redis_cluster 新建目录
复制redis.conf文件到各个目录下
我们修改下这个配置文件
vi redis_cluster/7001/redis.conf
修改一下几个
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 5000 //集群超时时间,节点超过这个时间没反应就断定是宕机
appendonly yes //存储方式,aof,将写操作记录保存到日志中
编辑后面5个配置文件,把 port ,pidfile,cluster-config-file 分别修改下即可
3:启动六个节点的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
4.创建集群
但是在使用之前 需要安装ruby,以及redis和ruby连接
yum -y install ruby ruby-devel rubygems rpm-build
gem install redis
安装gem install redis可能会出错
指定版本安装gem install redis --version3.3.3
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
redis-trib.rb create --replicas 1 192.168.39.118:7001 192.168.39.118:7002 192.168.39.118:7003 192.168.39.118:7004 192.168.39.118:7005 192.168.39.118:7006
--replicas 1 表示每个主数据库拥有的从数据库个数为1。
注意使用远程连接集群时,配置时不要用127.0.0.1,而是用本机的IP地址,否则会报错。
执行这个语句时,可能会报错。
(1)sorry,can't connect to node ip,端口等都配置正确的话,还需要将redis.conf文件中的密码注释掉 # requirepass 123456
(2)Node 127.0.0.1:7000 is not empty. 删除生成的配置文件nodes.conf,如果不行则说明现在创建的结点包括了旧集群的结点信息,需要删除redis的持久化文件后再重启redis,比如:appendonly.aof、dump.rdb,将redis.conf中定义的 cluster-config-file 所在的文件删除,重启每个节点。
redis-trib.rb check 127.0.0.1:7001
redis-cli是redis默认的客户端工具,启动时加上`-c`参数,`-p`指定端口,-h 指定host ,就可以连接到集群。
redis集密码设置:
1.密码设置
修改所有redis集群中的redis.conf文件加入:
masterauth passwd123
requirepass passwd123
2.设置密码之后如果需要使用redis-trib.rb的各种命令
如:redis-trib.rb check 127.0.0.1,则会报错ERR] Sorry, can’t connect to node 127.0.0.1:7000
解决办法:
vim /usr/lib/ruby/gems/1.8/gems/redis-3.3.0/lib/redis/client.rb,然后修改passord
class Client
DEFAULTS = {
:url => lambda { ENV["REDIS_URL"] },
:scheme => "redis",
:host => "127.0.0.1",
:port => 6379,
:path => nil,
:timeout => 5.0,
:password => "passwd123",
:db => 0,
:driver => nil,
:id => nil,
:tcp_keepalive => 0,
:reconnect_attempts => 1,
:inherit_socket => false
}
3.带密码访问集群
./redis-cli -c -p 7000 -a passwd123
4.通过find / -name client.rb找出文件位置。