Redis 3.2.8集群搭建

原创
2017/04/26 16:02
阅读数 60

安装并编译Redis3.2.8软件

tar zxvf redis-3.2.8.tar.gz

cd redis-3.2.8

sudo make & sudo make install

配置Redis

vi redis.conf:

port 6379

bind 0.0.0.0

daemonize yes

pidfile /var/run/redis_6379.pid

logfile "/opt/redis-3.2.8/logs/redis6379.log"

dbfilename dump6379.rdb

dir "/opt/redis-3.2.8/workDir6379"

appendonly yes

appendfilename "appendonly6379.aof"

cluster-enabled yes

cluster-config-file nodes-6379.conf

为slave拷贝一份配置文件:

cp redis.conf redis6380.conf

sed -i 's/6379/6380/' redis6380.conf

创建配置文件里所需目录:

mkdir logs

mkdir workDir6379

mkdir workDir6380

拷贝配置文件到各台服务器,并启动master和slave:(每台服务器上都要执行)

redis-server /opt/redis-3.2.8/redis.conf

redis-server /opt/redis-3.2.8/redis6380.conf

 

启动集群

安装ruby和redis gem:

sudo yum -y install ruby-devel ruby-irb ruby-libs ruby-rdoc ruby rubygems-devel rubygems

gem install redis

如果redis gem在线安装不成功,可以手动下载下来后进行本地安装:

gem install --local /opt/redis-3.3.3.gem

启动集群:

cp src/redis-trib.rb /usr/local/bin

redis-trib.rb create --replicas 1 192.168.70.103:6379 192.168.70.103:6380 192.168.70.104:6379 192.168.70.104:6380 192.168.70.105:6379 192.168.70.105:6380

验证集群

redis-cli -c

keys *

set hello world

set testkey "hello, redis cluster"

在另外一个服务器上可以取出刚才所添加的key:

redis-cli -c

keys *

get hello

get testkey

设置密码访问

连接各个redis实例,设置密码:

redis-cli -c 192.168.70.103 -p 6379 
config set masterauth password321 
config set requirepass password321 
AUTH password321
config rewrite

更改集群的ruby脚本工具里的密码:

vi /home/vagrant/.gem/ruby/gems/redis-3.3.3/lib/redis/client.rb 

require "redis/errors"
require "socket"
require "cgi"

class Redis
  class Client

    DEFAULTS = {
      :url => lambda { ENV["REDIS_URL"] },
      :scheme => "redis",
      :host => "127.0.0.1",
      :port => 6379,
      :path => nil,
      :timeout => 5.0,
      :password => "password321",
      :db => 0,
      :driver => nil,
      :id => nil,
      :tcp_keepalive => 0,
      :reconnect_attempts => 1,
      :inherit_socket => false
    }

以后访问Redis就都需要密码了:

redis-cli -c -p 6380 -a passord321

或者

AUTH password321

 

展开阅读全文
打赏
0
0 收藏
分享
加载中
更多评论
打赏
0 评论
0 收藏
0
分享
返回顶部
顶部