1.下载安装包并解压
全部版本地址:http://download.redis.io/releases
安装包下载: http://download.redis.io/releases/redis-3.2.4.tar.gz
我们这里使用3.2.4
2.编译安装
cd redis-3.2.4
make && make install
可能异常:(root用户部署不会出现该问题)
说明无权限,需要权限,修改目录当前用户权限。
异常2
MAKE hiredis
cd hiredis && make static
make[3]: 进入目录“/xxx/redis/redis-3.2.4/deps/hiredis”
gcc -std=c99 -pedantic -c -O3 -fPIC -Wall -W -Wstrict-prototypes -Wwrite-strings -g -ggdb net.c
make[3]: gcc:命令未找到
make[3]: *** [net.o] 错误 127
make[3]: 离开目录“/xxx/redis/redis-3.2.4/deps/hiredis”
make[2]: *** [hiredis] 错误 2
make[2]: 离开目录“/xxx/redis/redis-3.2.4/deps”
make[1]: [persist-settings] 错误 2 (忽略)CC adlist.o
/bin/sh: cc: 未找到命令
make[1]: *** [adlist.o] 错误 127
make[1]: 离开目录“/xxx/redis/redis-3.2.4/src”
make: *** [all] 错误 2
运行
yum -y install gcc-c++ gcc
删除原有目录,重新解压redis
3.进入src目录下将 redis-trib.rb 复制到 /usr/local/bin 目录下
cd src
cp redis-trib.rb /usr/local/bin/
4.创建redis节点,配置相关配置文件
- 在redis根目录下创建redis_cluster目录
-
mkdir redis_cluster
在 redis_cluster 目录下,创建名为7000、7001、7002的目录,并将 redis.conf 拷贝到这三个目录中
mkdir 7000 7001 7002
- 将redis.conf 复制到三个目录中
- 修改配置文件
-
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日志开启 有需要就开启,它会每次写操作都记录一条日志
另外一台服务器也是相同操作,端口不同(7003,7004,7005),cluster模式最少需要六个节点、
5.启动六个节点
第一台机器上执行
redis-server redis_cluster/7000/redis.conf
redis-server redis_cluster/7001/redis.conf
redis-server redis_cluster/7002/redis.conf另外一台机器上执行
redis-server redis_cluster/7003/redis.conf
redis-server redis_cluster/7004/redis.conf
redis-server redis_cluster/7005/redis.conf
如果遇到没有redis-server命令,可以直接进入目录启动
/usr/local/bin/redis-server
6.查看是否启动成功
[test@test 7000]$ ps -ef|grep redis
root 6931 1 0 16:37 ? 00:00:02 /usr/local/bin/redis-server 172.20.2.83:7000 [cluster]
root 6935 1 0 16:37 ? 00:00:02 /usr/local/bin/redis-server 172.20.2.83:7001 [cluster]
root 6939 1 0 16:37 ? 00:00:01 /usr/local/bin/redis-server 172.20.2.83:7002 [cluster]
7.创建集群,两个节点连接起来
Redis 官方提供了 redis-trib.rb 这个工具,就在解压目录的 src 目录中,第三步中已将它复制到 /usr/local/bin 目录中,可以直接在命令行中使用了。使用下面这个命令即可完成安装。
这个工具是用 ruby 实现的,所以需要安装 ruby。安装命令如下:
sudo yum -y install ruby ruby-devel rubygems rpm-build
sudo gem install redis --version 3.0.0 (注意要安装3.0.0版本,3.3.2版本会报错!!报错原因未知)
[root@test~]# /usr/local/bin/redis-trib.rb create --replicas 1 172.20.2.83:7000 172.20.2.83:7001 172.20.2.83:7002 172.20.2.86:7003 172.20.2.86:7004 172.20.2.86:7005
>>> Creating cluster
>>> Performing hash slots allocation on 6 nodes...
Using 3 masters:
172.20.2.86:7003
172.20.2.83:7000
172.20.2.86:7004
Adding replica 172.20.2.83:7001 to 172.20.2.86:7003
Adding replica 172.20.2.86:7005 to 172.20.2.83:7000
Adding replica 172.20.2.83:7002 to 172.20.2.86:7004
M: de87fb089bdafad79fb1078da75cf522aa192ba8 172.20.2.83:7000slots:5461-10922 (5462 slots) master
S: c72a1f5b4fff707c62f60265073a334c41829a87 172.20.2.83:7001replicates c430fc18fdb74dd955b106ef44abe2258ad457da
S: 7861067e7803d99c3844162d157283cb98d9edc9 172.20.2.83:7002replicates c72ab6cb7ddc6c3c7529f7d58ab9d9bb53ceef48
M: c430fc18fdb74dd955b106ef44abe2258ad457da 172.20.2.86:7003slots:0-5460 (5461 slots) master
M: c72ab6cb7ddc6c3c7529f7d58ab9d9bb53ceef48 172.20.2.86:7004slots:10923-16383 (5461 slots) master
S: f56b724050d2c86be6e1efe0de417cfc2821a2c8 172.20.2.86:7005replicates de87fb089bdafad79fb1078da75cf522aa192ba8
Can I set the above configuration? (type 'yes' to accept): yes
>>> Nodes configuration updated
>>> Assign a different config epoch to each node
>>> Sending CLUSTER MEET messages to join the cluster
Waiting for the cluster to join..
>>> Performing Cluster Check (using node 172.20.2.83:7000)
M: de87fb089bdafad79fb1078da75cf522aa192ba8 172.20.2.83:7000slots:5461-10922 (5462 slots) master1 additional replica(s)
M: c430fc18fdb74dd955b106ef44abe2258ad457da 172.20.2.86:7003slots:0-5460 (5461 slots) master1 additional replica(s)
M: c72ab6cb7ddc6c3c7529f7d58ab9d9bb53ceef48 172.20.2.86:7004slots:10923-16383 (5461 slots) master1 additional replica(s)
S: c72a1f5b4fff707c62f60265073a334c41829a87 172.20.2.83:7001slots: (0 slots) slavereplicates c430fc18fdb74dd955b106ef44abe2258ad457da
S: 7861067e7803d99c3844162d157283cb98d9edc9 172.20.2.83:7002slots: (0 slots) slavereplicates c72ab6cb7ddc6c3c7529f7d58ab9d9bb53ceef48
S: f56b724050d2c86be6e1efe0de417cfc2821a2c8 172.20.2.86:7005slots: (0 slots) slavereplicates de87fb089bdafad79fb1078da75cf522aa192ba8
[OK] All nodes agree about slots configuration.
>>> Check for open slots...
>>> Check slots coverage...
[OK] All 16384 slots covered.
安装成功
异常:
如果上述步骤不安装3.0.0版本报错如下:
8.测试集群是否可用
[root@test ~]# redis-cli -h 172.20.2.83 -c -p 7002
172.20.2.83:7002> set key1 value1
-> Redirected to slot [9189] located at 172.20.2.83:7000
OK
172.20.2.83:7000> get key1
"value1"
172.20.2.83:7000>
参考:
https://www.oschina.net/code/snippet_1046345_44151
http://www.cnblogs.com/wuxl360/p/5920330.html
redis超过使用内存大小:http://www.cnblogs.com/tdws/p/5727633.html
redis配置参数:http://blog.csdn.net/neubuffer/article/details/17003909