前面一章已经把redis搭建好了,现在开始搭建集群(我用的是单机,不同的端口号)
开始用的是centos7,不知道怎么回事,怎么都不行,换成centos6.5就可以了
首先建立一个文件夹
<pre>[root@lijia231 redis-3.0.7]mkdir redis_cluster
[root@lijia231 redis-3.0.7]cd redis_cluster
[root@lijia231 redis-3.0.7]mkdir 7001 7002 7003 7004 7005 7006
</pre>
然后把redis中的redis.conf复制到这六个文件夹
修改各个文件夹中的redis.conf
<pre>port 7000 //端口7000,7002,7003
bind 本机ip //默认ip为127.0.0.1 需要改为其他节点机器可访问的ip 否则创建集群时无法访问对应的端口,无法创建集群
daemonize yes //redis后台运行
pidfile /var/run/redis_7000.pid //pidfile文件对应7000,7001,7002
cluster-enabled yes //开启集群 把注释#去掉
cluster-config-file nodes_7000.conf //集群的配置 配置文件首次启动自动生成 7000,7001,7002
cluster-node-timeout 15000 //请求超时 默认15秒,可自行设置
appendonly yes //aof日志开启 有需要就开启,它会每次写操作都记录一条日志 </pre>
好了,手动去启动每一个吧
<pre>
[root@lijia231 redis-3.0.7]# src/redis-server /usr/local/softtool/cluster_redis/7001/redis.conf
[root@lijia231 redis-3.0.7]# src/redis-server /usr/local/softtool/cluster_redis/7002/redis.conf
[root@lijia231 redis-3.0.7]# src/redis-server /usr/local/softtool/cluster_redis/7003/redis.conf
[root@lijia231 redis-3.0.7]# src/redis-server /usr/local/softtool/cluster_redis/7004/redis.conf
[root@lijia231 redis-3.0.7]# src/redis-server /usr/local/softtool/cluster_redis/7005/redis.conf
[root@lijia231 redis-3.0.7]# src/redis-server /usr/local/softtool/cluster_redis/7006/redis.conf
</pre>
然后查看
<pre>[root@lijia231 redis-3.0.7]# ps -ef|grep redis
root 7938 1 0 04:00 ? 00:00:00 src/redis-server 172.16.255.231:7001 [cluster]
root 7961 1 0 04:01 ? 00:00:00 src/redis-server 172.16.255.231:7002 [cluster]
root 7967 1 0 04:03 ? 00:00:00 src/redis-server 172.16.255.231:7003 [cluster]
root 7971 1 0 04:03 ? 00:00:00 src/redis-server 172.16.255.231:7004 [cluster]
root 7975 1 0 04:03 ? 00:00:00 src/redis-server 172.16.255.231:7005 [cluster]
root 7979 1 0 04:03 ? 00:00:00 src/redis-server 172.16.255.231:7006 [cluster] </pre>
然后去装ruby
<pre>[root@lijia231 redis-3.0.7]yum -y install ruby ruby-devel rubygems rpm-build
[root@lijia231 redis-3.0.7]# gem install redis
</pre>
最后通过redis-trib.rb去把几个节点关联
<pre>[root@lijia231 redis-3.0.7]# src/redis-trib.rb create --replicas 1 172.16.255.231:7000 172.16.255.231:7001 172.16.255.231:7002 172.16.255.231:7003 172.16.255.231:7004 172.16.255.231:7005
</pre>
默认前面三个是主,后面三个是从。
启动
<pre>[root@lijia231 redis-3.0.7]# src/redis-cli -h 172.16.255.231 -p 7001
172.16.255.231:7001> set foot bar
(error) MOVED 11286 172.16.255.231:7003
</pre>
上面是由于启动的时候没加上集群
<pre>[root@lijia231 redis-3.0.7]# src/redis-cli -c -h 172.16.255.231 -p 7001
172.16.255.231:7001> set foot bar
-> Redirected to slot [11286] located at 172.16.255.231:7003
OK
</pre>
集群建立完毕,没毛病,但是Centos7下还没解决,下次再试试。
网友评论