Linux CentOS 下 Redis 的安装部署 + Redis 伪集群的搭建

redis下载 : http://download.redis.io/releases/redis-4.0.10.tar.gznode

  • 解压 编译 安装
//解压到local目录
# tar -xvf redis-4.0.10.tar.gz -C /usr/local/

//定位到解压目录
# cd /usr/local/redis-4.0.10/

//执行编译和安装,PREFIX必须大写
# make install PREFIX=/usr/local/redis
  • /usr/local/redis/bin目录下会生成以下文件,这时候就能够启动redis-server了
  • 这里写图片描述
# cd /usr/local/redis/bin
//在bin目录下执行命令
# ./redis-server
23154:C 24 Aug 05:23:47.865 # oO0OoO0OoO0Oo Redis is starting oO0OoO0OoO0Oo
23154:C 24 Aug 05:23:47.865 # Redis version=4.0.10, bits=64, commit=00000000, modified=0, pid=23154, just started 23154:C 24 Aug 05:23:47.865 # Warning: no config file specified, using the default config. In order to specify a config file use ./redis-server /path/to/redis.conf 23154:M 24 Aug 05:23:47.867 * Increased maximum number of open files to 10032 (it was originally set to 1024). _._ _.-``__ ''-._ _.-`` `. `_. ''-._ Redis 4.0.10 (00000000/0) 64 bit .-`` .-```. ```\/ _.,_ ''-._ ( ' , .-` | `, ) Running in standalone mode |`-._`-...-` __...-.``-._|'` _.-'| Port: 6379 | `-._ `._ / _.-' | PID: 23154 `-._ `-._ `-./ _.-' _.-' |`-._`-._ `-.__.-' _.-'_.-'| | `-._`-._ _.-'_.-' | http://redis.io `-._ `-._`-.__.-'_.-' _.-' |`-._`-._ `-.__.-' _.-'_.-'| | `-._`-._ _.-'_.-' | `-._ `-._`-.__.-'_.-' _.-' `-._ `-.__.-' _.-' `-._ _.-' `-.__.-' 23154:M 24 Aug 05:23:47.871 # WARNING: The TCP backlog setting of 511 cannot be enforced because /proc/sys/net/core/somaxconn is set to the lower value of 128. 23154:M 24 Aug 05:23:47.871 # Server initialized 23154:M 24 Aug 05:23:47.872 # WARNING overcommit_memory is set to 0! Background save may fail under low memory condition. To fix this issue add 'vm.overcommit_memory = 1' to /etc/sysctl.conf and then reboot or run the command 'sysctl vm.overcommit_memory=1' for this to take effect. 23154:M 24 Aug 05:23:47.874 * Ready to accept connections 
  • 这时候启动用的是默认的配置
  • bind 127.0.0.1
  • port 6379
  • daemonize nolinux

  • 另开一个远程客户端,进行客户端链接git

# cd /usr/local/redis-4.0.10/bin/
# cd ./redis-cli
127.0.0.1:6379> shutdown

//执行shutdown关闭redis服务
not connected>
  • 服务端能够经过CTRL+C 退出, 也能够在客户端执行shutdown退出
  • 设置服务后台启动方式
//拷贝一份配置文件到redis目录
# cp /usr/local/redis-4.0.10/redis.conf /usr/local/reids

//vi命令 修改守护进程 daemonize为yes
daemonized yes
//进入vi命令后能够经过 /daemonize快速定位

//在bin目录下带配置文件启动redis服务
# ./redis-server ../redsi.conf
23345:C 24 Aug 05:52:05.153 # oO0OoO0OoO0Oo Redis is starting oO0OoO0OoO0Oo
23345:C 24 Aug 05:52:05.153 # Redis version=4.0.10, bits=64, commit=00000000, modified=0, pid=23345, just started
23345:C 24 Aug 05:52:05.153 # Configuration loaded
#
  • 经过Redis Desktop Manager 0.9.5远程链接redis服务github

  • 首先确认你的Linux系统已经开放了6379端口web

  • 修改配置文件,打开requirepass的注释或者直接添加一行
requirepass 你想要的验证码
//添加 bind 服务器地址
 bind 120.0.0.1
 bind 192.168.25.120(这里写你服务器的ip地址)

//启动服务,测试远程链接
# ./redis-server ../redsi.conf

//由于配置文件添加了验证,关闭服务的时候也须要验证
# ./redis-cli -a 验证码 shutdown

这里写图片描述


Redis伪集群搭建

1.使用Ruby Version Manager (RVM) 部署Rubyredis

//在线安装RVM
# \curl -sSL https://get.rvm.io | bash -s stable
//在线部署Ruby,列出全部版本
# rvm list known
# MRI Rubies
[ruby-]1.8.6[-p420]
[ruby-]1.8.7[-head] # security released on head
[ruby-]1.9.1[-p431]
[ruby-]1.9.2[-p330]
[ruby-]1.9.3[-p551]
[ruby-]2.0.0[-p648]
[ruby-]2.1[.10]
[ruby-]2.2[.10]
[ruby-]2.3[.7]
[ruby-]2.4[.4]
[ruby-]2.5[.1]
[ruby-]2.6[.0-preview2]
ruby-head

# for forks use: rvm install ruby-head-<name> --url https://github.com/github/ruby.git --branch 2.2

# JRuby
jruby-1.6[.8]
jruby-1.7[.27]
jruby-9.1[.17.0]
jruby[-9.2.0.0]
jruby-head

# Rubinius
rbx-1[.4.3]
rbx-2.3[.0]
rbx-2.4[.1]
rbx-2[.5.8]
rbx-3[.100]
rbx-head

# TruffleRuby
truffleruby[-1.0.0-rc2]

# Opal
opal

# Minimalistic ruby implementation - ISO 30170:2012
mruby-1.0.0
mruby-1.1.0
mruby-1.2.0
mruby-1.3.0
mruby-1[.4.0]
mruby[-head]

# Ruby Enterprise Edition
ree-1.8.6
ree[-1.8.7][-2012.02]

# Topaz
topaz

# MagLev
maglev-1.0.0
maglev-1.1[RC1]
maglev[-1.2Alpha4]
maglev-head

# Mac OS X Snow Leopard Or Newer
macruby-0.10
macruby-0.11
macruby[-0.12]
macruby-nightly
macruby-head

# IronRuby
ironruby[-1.1.3]
ironruby-head
//安装选定的版本
# rvm install ruby-2.5
//验证版本
# ruby -v
ruby 2.5.1p57 (2018-03-29 revision 63029) [x86_64-linux]

2.安装RubyGems
安装包下载 : https://rubygems.org/rubygems/rubygems-2.7.7.tgzsql

//解压rubygems.tgz后,在解压目录内执行
# ruby setup.rb

//验证版本
# gem -v
2.7.7

3.在线安装redis.gemruby

//会安装最新版本的redis.gem
# gem install redis

4.配置多个redis服务端bash

//新建redis-cluster文件夹
# mkdir /usr/local/redis-cluster

//删除bin目录下生成的RDB快照文件和AOF文件
# rm -rf /usr/local/redis/bin/dump.rdb appendonly.aof

/* 修改redis.conf配置文件 1.注释掉bind 127.0.0.1 2.注释掉requirepass 验证码 3.开启cluster-enabled yes的注释 4.修改protected-mode 为 no */

# bind 127.0.0.1
# requirepass 验证码
cluster-enabled yes
protected-mode no

//拷贝6份redis目录到redis-cluster *集群至少须要3个redis服务,每一个reids还须要一个备份.因此须要6份*
# cp /usr/local/redis /usr/local/redis-cluster/redis01 -r
# cp /usr/local/redis /usr/local/redis-cluster/redis02 -r
# cp /usr/local/redis /usr/local/redis-cluster/redis03 -r
# cp /usr/local/redis /usr/local/redis-cluster/redis04 -r
# cp /usr/local/redis /usr/local/redis-cluster/redis05 -r
# cp /usr/local/redis /usr/local/redis-cluster/redis06 -

//修改redis01-06目录中配置文件redis.conf,更改端口号port
redis01/reids.conf  port 7001
redis02/reids.conf  port 7002
redis03/reids.conf  port 7003
redis04/reids.conf  port 7004
redis05/reids.conf  port 7005
redis01/reids.conf  port 7006

//拷贝redis解压目录src下的redis-trib.rb到redis-cluster目录下
# cp /usr/local/redis-4.0.10/src/redis-trib.rb /usr/local/redis-cluster

//新建批执行文件
# mkdir /usr/local/redis-cluster/redis_start_all.sh

//修改批执行
# vi /usr/local/redis-cluster/redis_start_all.sh
d /usr/local/redis-cluster/redis01/bin
./redis-server ../redis.conf

cd /usr/local/redis-cluster/redis02/bin
./redis-server ../redis.conf

cd /usr/local/redis-cluster/redis03/bin
./redis-server ../redis.conf

cd /usr/local/redis-cluster/redis04/bin
./redis-server ../redis.conf

cd /usr/local/redis-cluster/redis05/bin
./redis-server ../redis.conf

cd /usr/local/redis-cluster/redis06/bin
./redis-server ../redis.conf

//修改redis_start_all.sh的可执行权限
# chmod u+x /usr/local/redis-cluster/redis_start_all.sh

//启动多个redis服务,在redis-cluster目录下执行
# ./redis_start_all.sh
23928:C 24 Aug 07:09:37.156 # oO0OoO0OoO0Oo Redis is starting oO0OoO0OoO0Oo
23928:C 24 Aug 07:09:37.156 # Redis version=4.0.10, bits=64, commit=00000000, modified=0, pid=23928, just started
23928:C 24 Aug 07:09:37.156 # Configuration loaded
23930:C 24 Aug 07:09:37.173 # oO0OoO0OoO0Oo Redis is starting oO0OoO0OoO0Oo
23930:C 24 Aug 07:09:37.174 # Redis version=4.0.10, bits=64, commit=00000000, modified=0, pid=23930, just started
23930:C 24 Aug 07:09:37.174 # Configuration loaded
23932:C 24 Aug 07:09:37.188 # oO0OoO0OoO0Oo Redis is starting oO0OoO0OoO0Oo
23932:C 24 Aug 07:09:37.188 # Redis version=4.0.10, bits=64, commit=00000000, modified=0, pid=23932, just started
23932:C 24 Aug 07:09:37.188 # Configuration loaded
23934:C 24 Aug 07:09:37.203 # oO0OoO0OoO0Oo Redis is starting oO0OoO0OoO0Oo
23934:C 24 Aug 07:09:37.203 # Redis version=4.0.10, bits=64, commit=00000000, modified=0, pid=23934, just started
23934:C 24 Aug 07:09:37.203 # Configuration loaded
23936:C 24 Aug 07:09:37.217 # oO0OoO0OoO0Oo Redis is starting oO0OoO0OoO0Oo
23936:C 24 Aug 07:09:37.217 # Redis version=4.0.10, bits=64, commit=00000000, modified=0, pid=23936, just started
23936:C 24 Aug 07:09:37.217 # Configuration loaded
23938:C 24 Aug 07:09:37.230 # oO0OoO0OoO0Oo Redis is starting oO0OoO0OoO0Oo
23938:C 24 Aug 07:09:37.231 # Redis version=4.0.10, bits=64, commit=00000000, modified=0, pid=23938, just started
23938:C 24 Aug 07:09:37.231 # Configuration loaded

5.使用ruby脚本搭建集群服务器

//在redis-cluster目录下执行

# ./redis-trib.rb create --replicas 1 192.168.25.120:7001 192.168.25.120:7002 192.168.25.120:7003 192.168.25.120:7004 192.168.25.120:7005 192.168.25.120:7006 >>> Creating cluster >>> Performing hash slots allocation on 6 nodes... Using 3 masters: 192.168.25.120:7001 192.168.25.120:7002 192.168.25.120:7003 Adding replica 192.168.25.120:7005 to 192.168.25.120:7001 Adding replica 192.168.25.120:7006 to 192.168.25.120:7002 Adding replica 192.168.25.120:7004 to 192.168.25.120:7003 >>> Trying to optimize slaves allocation for anti-affinity [WARNING] Some slaves are in the same host as their master M: 46a3cc0665de4b8cedd32559bb5b0846972f5ab6 192.168.25.120:7001 slots:0-5460 (5461 slots) master M: d10e7c79b3083f5726167e164f73469b84574609 192.168.25.120:7002 slots:5461-10922 (5462 slots) master M: 700db85f5944275988477093c84099ddccc6ffb3 192.168.25.120:7003 slots:10923-16383 (5461 slots) master S: fc11396097f8337ad17bde9496a1869932e8cbda 192.168.25.120:7004 replicates 700db85f5944275988477093c84099ddccc6ffb3 S: 57c5e3cf86738ed017668389e659edf7ba5f4fe4 192.168.25.120:7005 replicates 46a3cc0665de4b8cedd32559bb5b0846972f5ab6 S: c56a9bb7135073740229af070ab96ad500817c7d 192.168.25.120:7006 replicates d10e7c79b3083f5726167e164f73469b84574609 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 192.168.25.120:7001) M: 46a3cc0665de4b8cedd32559bb5b0846972f5ab6 192.168.25.120:7001 slots:0-5460 (5461 slots) master 1 additional replica(s) S: c56a9bb7135073740229af070ab96ad500817c7d 192.168.25.120:7006 slots: (0 slots) slave replicates d10e7c79b3083f5726167e164f73469b84574609 M: 700db85f5944275988477093c84099ddccc6ffb3 192.168.25.120:7003 slots:10923-16383 (5461 slots) master 1 additional replica(s) M: d10e7c79b3083f5726167e164f73469b84574609 192.168.25.120:7002 slots:5461-10922 (5462 slots) master 1 additional replica(s) S: 57c5e3cf86738ed017668389e659edf7ba5f4fe4 192.168.25.120:7005 slots: (0 slots) slave replicates 46a3cc0665de4b8cedd32559bb5b0846972f5ab6 S: fc11396097f8337ad17bde9496a1869932e8cbda 192.168.25.120:7004 slots: (0 slots) slave replicates 700db85f5944275988477093c84099ddccc6ffb3 [OK] All nodes agree about slots configuration. >>> Check for open slots... >>> Check slots coverage... [OK] All 16384 slots covered. 

大功告成

可能遇到的错误 [ERR] Sorry, can’t connect to node 192.168.X.X 检查redis.conf配置文件 bind是否为本身的服务器地址 protected-mode 是否为no requirepass是否是注释掉了