1.下载redis源码包
wget http://download.redis.io/releases/redis-3.2.4.tar.gz
2.解压并安装
tar xvf redis-3.2.4.tar.gz cd redis-3.2.4make && make instal
3.将src文件夹将可执行文件拷贝到/usr/local/bin目录下,因为已经有redis2版本了,所以复制过去之后命名带上3
cp src/redis-server /usr/local/bin/redis-server3 cp src/redis-cli /usr/local/bin/redis-cli3 cp src/redis-trib.rb /usr/bin/redis-trib.rb3
4.创建节点配置文件,至少要6个节点才能创建一个集群【这里示范在同一台机器上创建6个节点来搭建集群,当然你也可以选择两台机器做成3个master和3个slave的形式】
创建6个文件夹并将redis.conf文件复制过去并修改,以7001文件配置为例,后面同步修改7001为7002-7006就可以了
mkdir -p cluster/7001 cluster/7002 cluster/7003 cluster/7004 cluster/7005 cluster/7006 cp redis.conf cluster/7001 vim cluster/7001/redis.conf
主要修改以下几处
port 7001 //端口7000,7002,7003 bind 127.0.0.1 //默认ip为127.0.0.1,所以本机可以不写,如果是多台机器需要改为其他节点机器可访问的ip 否则创建集群时无法访问对应的端口,无法创建集群 daemonize yes //redis后台运行 pidfile /var/run/redis_7001.pid //pidfile文件对应7000,7001,7002 cluster-enabled yes //开启集群 把注释#去掉 cluster-config-file nodes_7001.conf //集群的配置 配置文件首次启动自动生成 7001 cluster-node-timeout 15000 //请求超时 默认15秒,可自行设置 appendonly yes //aof日志开启 有需要就开启,它会每次写操作都记录一条日志
5.启动刚刚创建的节点
redis-server3 cluster/7001/redis.conf redis-server3 cluster/7002/redis.conf redis-server3 cluster/7003/redis.conf redis-server3 cluster/7004/redis.conf redis-server3 cluster/7005/redis.conf redis-server3 cluster/7006/redis.conf
6.查看redis节点启动情况,出现以下信息说明启动正常
ps -ef|grep redis root 6106 1 0 15:49 ? 00:00:00 redis-server3 127.0.0.1:7001 [cluster] root 6112 1 0 15:49 ? 00:00:00 redis-server3 127.0.0.1:7003 [cluster] root 6116 1 0 15:49 ? 00:00:00 redis-server3 127.0.0.1:7004 [cluster] root 6120 1 0 15:49 ? 00:00:00 redis-server3 127.0.0.1:7005 [cluster] root 6124 1 0 15:49 ? 00:00:00 redis-server3 127.0.0.1:7006 [cluster] root 6689 1 0 15:50 ? 00:00:00 redis-server3 127.0.0.1:7002 [cluster] netstat -tnlp|grep redistcp 0 0 127.0.0.1:7001 0.0.0.0:* LISTEN 6106/redis-server3 tcp 0 0 127.0.0.1:7002 0.0.0.0:* LISTEN 6689/redis-server3 tcp 0 0 127.0.0.1:7003 0.0.0.0:* LISTEN 6112/redis-server3 tcp 0 0 127.0.0.1:7004 0.0.0.0:* LISTEN 6116/redis-server3 tcp 0 0 127.0.0.1:7005 0.0.0.0:* LISTEN 6120/redis-server3 tcp 0 0 127.0.0.1:7006 0.0.0.0:* LISTEN 6124/redis-server3 tcp 0 0 127.0.0.1:17001 0.0.0.0:* LISTEN 6106/redis-server3 tcp 0 0 127.0.0.1:17002 0.0.0.0:* LISTEN 6689/redis-server3 tcp 0 0 127.0.0.1:17003 0.0.0.0:* LISTEN 6112/redis-server3 tcp 0 0 127.0.0.1:17004 0.0.0.0:* LISTEN 6116/redis-server3 tcp 0 0 127.0.0.1:17005 0.0.0.0:* LISTEN 6120/redis-server3 tcp 0 0 127.0.0.1:17006 0.0.0.0:* LISTEN 6124/redis-server3
6.创建集群
出现错误,看提示信息原来这个工具是基于ruby实现的,所以需要先安装ruby
redis-trib.rb3 create --replicas 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 /usr/share/ruby/vendor_ruby/2.0/rubygems/core_ext/kernel_require.rb:55:in `require‘: cannot load such file -- redis (LoadError) from /usr/share/ruby/vendor_ruby/2.0/rubygems/core_ext/kernel_require.rb:55:in `require‘ from /usr/local/bin/redis-trib.rb3:25:in `<main>‘
安装ruby,查看yum提供ruby的安装,那直接用yum安装就行了
yum list|grep ^ruby yum -y install ruby ruby-devel rubygems rpm-build gem install redis
重新创建集群
待续
时间: 2024-10-10 21:10:00