redis安装+redis集群配置+phpredis扩展安装

安装前的准备:

redis-3.0tar.gz    官网下载地址    http://redis.io/download/
以下软件或直接yum安装也可(安装步骤略)
tcl8.6.1-src.tar.gz               官网下载地址   http://sourceforge.jp/projects/sfnet_tcl/releases/
rubygems-2.4.2.zip            官网下载地址   http://rubygems.org/pages/download/

redis-3.0.0.gem                 官网下载地址   http://rubygems.org/gems/redis/versions/3.1.0

安装redis依赖包tcl

tar xzvf tcl8.6.1-src.tar.gz
cd tcl8.6.1/
cd unix &&
./configure --prefix=/usr           \
            --without-tzdata        \
            --mandir=/usr/share/man \
            $([ $(uname -m) = x86_64 ] && echo --enable-64bit) &&
make &&
sed -e "s@^\(TCL_SRC_DIR=‘\).*@\1/usr/include‘@" \
    -e "/TCL_B/s@=‘\(-L\)\?.*unix@=‘\1/usr/lib@" \
    -i tclConfig.sh

# 测试make是否成功,最好不要,要等半个多小时
make test

# root用户登录,执行下面命令
make install &&
make install-private-headers &&
ln -v -sf tclsh8.6 /usr/bin/tclsh &&
chmod -v 755 /usr/lib/libtcl8.6.so
如果没有tcl,将会报错如下:
[root@localhost redis-3.0.0-beta8]# make test
cd src && make test
make[1]: Entering directory `/root/redis-3.0.0-beta8/src‘
You need tcl 8.5 or newer in order to run the Redis test
make[1]: *** [test] Error 1
make[1]: Leaving directory `/root/redis-3.0.0-beta8/src‘
make: *** [test] Error 2
[root@localhost redis-3.0.0-beta8]#


安装redis前需要先配置下面的内核参数,否则Redis脚本在重启或停止redis时,将会报错,并且不能自动在停止服务前同步数据到磁盘上

echo 1 > /proc/sys/vm/overcommit_memory
echo vm.overcommit_memory=1 >> /etc/sysctl.conf
sysctl vm.overcommit_memory=1

实验环境模拟

192.168.1.222     三个实例

192.168.1.223     三个实例
注意,由于官网,必须至少6台服务器,所以鄙人笔记本过卡,直接用两台vm来虚拟,我用不同端口号来区分不同的redis实例即可。因为启动redis时只需要指定“该redis实例的配置文件”即可。在现实当中,如果有六台物理机,那样会更加简单部署,不会像我这么复杂,不过也不复杂,就是多copy一下配置文件而已。

redis安装

redis-3.0tar.gz    官网下载地址    http://redis.io/download/

安装过程指令如下:
$ mkdir /usr/local/redis   
$ cd /home/Happy/down   
$ wget https://github.com/antirez/redis/archive/3.0.0-rc1.tar.gz (或直接从官网下载即可)
$ tar xzf redis-3.0.tar.gz
$ cd redis-3.0
$ make PREFIX=/usr/local/redis install #安装redis到指定目录中
注意上面的最后一行,我们通过PREFIX指定了安装的目录。如果make失败,一般是你们系统中还未安装gcc,那么可以通过yum安装: 1    yum install gcc
安装完成后,继续执行make.
在安装redis成功后,你将可以在/usr/local/redis看到一个bin的目录,里面包括了以下文件:
redis-benchmark  redis-check-aof  redis-check-dump  redis-cli  redis-server

将redis做成一个服务

1.复制脚本到/etc/rc.d/init.d目录

ps: /etc/rc.d/init.d/目录下的脚本就类似与windows中的注册表,在系统启动的时候某些指定脚本将被执行
按以上步骤安装Redis时,其服务脚本位于:
/usr/local/src/redis/utils/redis_init_script
必须将其复制到/etc/rc.d/init.d的目录下:
cp /usr/local/src/redis/utils/redis_init_script /etc/rc.d/init.d/redis
将redis_init_script复制到/etc/rc.d/init.d/,同时重新命名为redis。
如果这时添加注册服务:
chkconfig --add redis
将报以下错误:
redis服务不支持chkconfig
为此,我们需要更改redis脚本。

2.更改redis脚本

打开使用vi打开脚本,查看脚本信息: 1    vim /etc/rc.d/init.d/redis

看到的内容如下(下内容是更改好的信息):
#!/bin/sh  
#chkconfig: 2345 80 90  
# Simple Redis init.d script conceived to work on Linux systems  
# as it does use of the /proc filesystem.  
   
REDISPORT=6379  
EXEC=/usr/local/redis/bin/redis-server  
CLIEXEC=/usr/local/redis/bin/redis-cli  
   
PIDFILE=/var/run/redis_${REDISPORT}.pid  
CONF="/etc/redis/${REDISPORT}.conf"
   
case "$1" in  
    start)  
        if [ -f $PIDFILE ]  
        then  
                echo "$PIDFILE exists, process is already running or crashed"
        else
                echo "Starting Redis server..."
                $EXEC $CONF &  
        fi  
        ;;  
    stop)  
        if [ ! -f $PIDFILE ]  
        then  
                echo "$PIDFILE does not exist, process is not running"
        else
                PID=$(cat $PIDFILE)  
                echo "Stopping ..."
                $CLIEXEC -p $REDISPORT shutdown  
                while [ -x /proc/${PID} ]  
                do
                    echo "Waiting for Redis to shutdown ..."
                    sleep 1  
                done  
                echo "Redis stopped"
        fi  
        ;;  
    *)  
        echo "Please use start or stop as first argument"
        ;;  
esac  

和原配置文件相比:
1.原文件是没有以下第2行的内容的,1    #chkconfig: 2345 80 90
2.原文件EXEC、CLIEXEC参数,也是有所更改。
EXEC=/usr/local/redis/bin/redis-server    
CLIEXEC=/usr/local/redis/bin/redis-cli
3.redis开启的命令,以后台运行的方式执行。1    $EXEC $CONF &
ps:注意后面的那个“&”,即是将服务转到后面运行的意思,否则启动服务时,Redis服务将占据在前台,占用了主用户界面,造成其它的命令执行不了。
4.将redis配置文件拷贝到/etc/redis/${REDISPORT}.conf

mkdir /etc/redis     
cp /usr/local/src/redis/redis.conf /etc/redis/6379.conf

这样,redis服务脚本指定的CONF就存在了。默认情况下,Redis未启用认证,可以通过开启6379.conf的requirepass 指定一个验证密码。

以上操作完成后,即可注册redis服务:

设置redis 启动权限,并且开启自动启动模式

chmod 755 /etc/init.d/redis
chkconfig --add redis
chkconfig --level 345 redis on
chkconfig --list redis
启动redis
redis-server /etc/redis.conf
或者,可以用下面这条
service redis start

将Redis的命令所在目录添加到系统参数PATH中
修改profile文件:
vi /etc/profile
在最后行追加:
export PATH="$PATH:/usr/local/redis/bin"
然后马上应用这个文件:
/etc/profile  
这样就可以直接调用redis-cli的命令了,如下所示:
$ redis-cli    
redis 127.0.0.1:6379> auth superman    
OK    
redis 127.0.0.1:6379> ping    
PONG    
redis 127.0.0.1:6379>
至此,redis 就成功安装了。

编辑配置文件并开始集群设置的准备,修改下面几个参数

vim /etc/redis/6379.conf  (编辑A的配置)
daemonize yes   修改no为yes,后台运行redis
port 6379
#dbfilename dump-6379.rdb
appendonly yes
appendfilename "appendonly-6379.aof"
logfile "/usr/local/redis/redis6379.log"  日志
cluster-enabled yes
cluster-config-file /usr/local/redis/nodes-6379.conf
cluster-node-timeout 5000

然后分别将刚才配置好的redis.conf文件 拷贝三份出来并设置里面如下参数的值分别为不同端口:
cp /etc/redis/6379.conf  /etc/redis/6380.conf  (B的配置)
cp /etc/redis/6379.conf  /etc/redis/6381.conf  (C的配置)
vim /etc/redis/6380.conf (编辑B的配置)
daemonize yes   修改no为yes,后台运行redis
port 6380
#dbfilename dump-6380.rdb
appendonly yes
appendfilename "appendonly-6380.aof"
logfile "/usr/local/redis/redis6380.log"  日志
cluster-enabled yes
cluster-config-file /usr/local/redis/nodes-6380.conf
cluster-node-timeout 5000
vim /etc/redis/6381.conf (编辑C的配置)
daemonize yes   修改no为yes,后台运行redis
port 6381
#dbfilename dump-6381.rdb
appendonly yes
appendfilename "appendonly-6381.aof"
logfile "/usr/local/redis/redis6381.log"  日志
cluster-enabled yes
cluster-config-file /usr/local/redis/nodes-6381.conf
cluster-node-timeout 5000

为什么要设置不同的端口呢?

因为,我们要在一台vm里面,虚拟三个实例
port 端口不一样,这个是肯定要的,不然服务都在同一个端口运行,就只能说是一个实例而已
appendonly 这个是说在后台运行,不要一关闭ssh,redis就挂了所以设置成yes为后台运行redis
appendfilename   这个默认就是appendonly.aof,如果我们不在后面加一个后缀区分,那么所有服务,他们的快照都会存放到在同一个文件 appendonly.aof,那边就会数据紊乱,A,B,C,三者数据都一样,所以,我们要区分出来,让他们一个实例,一个文件。这里还有一个主意的地方,那就是 它,不支持路径指定,你不能在 appendonly.aof 加一个 例如/usr/local/redis/appendonly.aof,否则会报错,启动不了,查看日志,报错如下

[root@localhost opt]# vim /usr/local/redis/redis6379.log
*** FATAL CONFIG FILE ERROR ***
Reading the configuration file, at line 177
>>> ‘dbfilename /usr/local/redis/bin/dump-6379.rdb‘
dbfilename can‘t be a path, just a filename

编辑三个实例配置文件,把端口,对应改成非重叠的,为了更好的区分,我就按照

192.168.1.222      “实例A   6379”       “实例B  6380”        “实例C  6381”
192.168.1.223      “实例D   6382”       “实例E  6383”        “实例F  6384”
port       appendfilename       cluster-config-file      注意根据不同实例,修改成不同的值

例如,实例B的配置文件主要是
daemonize yes   修改no为yes,后台运行redis
port 6380
#dbfilename dump-6380.rdb
appendonly yes
appendfilename "appendonly-6380.aof"
logfile "/usr/local/redis/redis6380.log"  日志
cluster-enabled yes
cluster-config-file /usr/local/redis/nodes-6380.conf
cluster-node-timeout 5000

再例如,实例E的配置文件主要变化是
daemonize yes   修改no为yes,后台运行redis
port 6383
#dbfilename dump-6383.rdb
appendonly yes
appendfilename "appendonly-6383.aof"
logfile "/usr/local/redis/redis6383.log"  日志
cluster-enabled yes
cluster-config-file /usr/local/redis/nodes-6383.conf
cluster-node-timeout 5000

启动redis服务

redis-server /etc/redis/6379.conf > /usr/local/redis/redis6379.log 2>&1 &
redis-server /etc/redis/6380.conf > /usr/local/redis/redis6380.log 2>&1 &
redis-server /etc/redis/6381.conf > /usr/local/redis/redis6381.log 2>&1 &


注意,为了方便查看错误信息,我把它日志,重定向到 对应的实例名字,存放在/usr/local/redis/下面
一切如果正常的话,你随便找一个  redis6379.log  redis6380.log   redis6381.log
      例如,我找一个   6380的日志
[root@localhost ]# vim /usr/local/redis/redis6380.log
6115:M 31 Aug 12:34:28.904 * Increased maximum number of open files to 10032 (it was originally set to 1024).
6115:M 31 Aug 12:34:28.906 * No cluster configuration found, I‘m 92999f9840418a848f7b10c5bca0119e3b515fa4

                _._                                                  

           _.-``__ ‘‘-._                                             

      _.-``    `.  `_.  ‘‘-._           Redis 3.0 (00000000/0) 64 bit

  .-`` .-```.  ```\/    _.,_ ‘‘-._                                   

 (    ‘      ,       .-`  | `,    )     Running in cluster mode

 |`-._`-...-` __...-.``-._|‘` _.-‘|     Port: 6380

 |    `-._   `._    /     _.-‘    |     PID: 6115

  `-._    `-._  `-./  _.-‘    _.-‘                                   

 |`-._`-._    `-.__.-‘    _.-‘_.-‘|                                  

 |    `-._`-._        _.-‘_.-‘    |           http://redis.io        

  `-._    `-._`-.__.-‘_.-‘    _.-‘                                   

 |`-._`-._    `-.__.-‘    _.-‘_.-‘|                                  

 |    `-._`-._        _.-‘_.-‘    |                                  

  `-._    `-._`-.__.-‘_.-‘    _.-‘                                   

      `-._    `-.__.-‘    _.-‘                                       

          `-._        _.-‘                                           

              `-.__.-‘                                               


6115:M 31 Aug 12:34:28.924 # Server started, Redis version 3.0
6115:M 31 Aug 12:34:28.924 * The server is now ready to accept connections on port 6380



说明一切准备就绪,都能正常运作,就差集群了,为了更加放心,看一下是否三个实例都在正常运行
[root@localhost ]# ps aux |grep redis

root      6109  0.6  0.9 137408  9740 pts/1    Sl   12:33   0:52 redis-server *:6379
root      6115  0.6  0.9 137408  9912 pts/2    Sl   12:34   0:51 redis-server *:6380
root      6123  0.6  0.7 137408  7876 pts/2    Sl   12:35   0:50 redis-server *:6381
root      6235  0.0  0.0 103240   840 pts/2    S+   14:47   0:00 grep redis

[root@localhost]#

以上,所有步骤,在192.168.1.223,同样的做法,注意变化的就是  redis 端口的改变,以及对于配置文件名的变化(例如6382.conf,6383.conf,6384.conf等)。


那么,开始集群吧,集群之前,先表示怀疑一下[请注意,只需要在其中一台服务器操作下面内容

[root@localhost ~]# cd redis-3.0.0-beta8
[root@localhost redis-3.0.0-beta8]# cd src/
[root@localhost src]# ./redis-trib.rb create --replicas 1 192.168.1.222:6379 192.168.1.222:6780 192.168.1.222:6381 192.168.1.223:6382 192.168.1.223:6383 192.168.1.223:6384
/usr/bin/env: ruby: No such file or directory
[root@localhost src]#

你会发现,他马上报错,报错信息如下:/usr/bin/env: ruby: No such file or directory
就是说,没有ruby,      
因为
redis-trib位于Redis源码的src文件夹中, 它是一个Ruby程序, 这个程序通过向实例发送特殊命令来完成创建新集 群, 检查集群, 或者对集群进行重新分片(reshared)等工作。

既然没有ruby,那我们就yum安装一个吧
yum -y install ruby ruby-rdoc

好,装好了,继续来集群
[root@localhost src]# ./redis-trib.rb create --replicas 1 192.168.1.222:6379 192.168.1.222:6780
192.168.1.222:6381 192.168.1.223:6382 192.168.1.223:6383 192.168.1.223:6384
./redis-trib.rb:24:in `require‘: no such file to load -- rubygems (LoadError)
        from ./redis-trib.rb:24
[root@localhost src]#

你会发现,又有问题了,这是什么问题啊,不过看了一下,后面提示rubygems (LoadError)   ,嘿嘿,应该就是gem没有上去,那么装一个咯
gem下载地址,上面有          http://rubygems.org/pages/download/ 有TGZ   ZIP    GEM      GIT, 我这里下载的是zip的包

安装rubygems
unzip rubygems-2.4.2.zip
cd rubygems-2.4.2
ruby setup.rb

刷刷刷一大堆东西出来,不过,细心的朋友,你会发现,下面有一处非常重要的 代码
[root@localhost rubygems-2.4.2]# ruby setup.rb
RubyGems 2.4.2 installed
Installing ri documentation for rubygems-2.4.2
/usr/lib/ruby/1.8/rdoc/rdoc.rb:280: warning: conflicting chdir during another chdir block
/usr/lib/ruby/1.8/rdoc/rdoc.rb:287: warning: conflicting chdir during another chdir block
=== 2.4.2 / 2014-10-01

This release was sponsored by Ruby Central.

Bug fixes:

* RubyGems now correctly matches wildcard no_proxy hosts.  Issue #997 by
  voelzemo.
* Added support for missing git_source method in the gem dependencies API.
* Fixed handling of git gems with an alternate install directory.
* Lockfiles will no longer be truncated upon resolution errors.
* Fixed messaging for `gem owner -a`.  Issue #1004 by Aaron Patterson, Ryan
  Davis.
* Removed meaningless ensure.  Pull request #1003 by gogotanaka.
* Improved wording of --source option help.  Pull request #989 by Jason Clark.
* Empty build_info files are now ignored.  Issue #903 by Adan Alvarado.
* Gem::Installer ignores dependency checks when installing development
  dependencies.  Issue #994 by Jens Willie.
* `gem update` now continues after dependency errors.  Issue #993 by aaronchi.
* RubyGems no longer warns about semantic version dependencies for the 0.x
  range.  Issue #987 by Jeff Felchner, pull request #1006 by Hsing-Hui Hsu.
* Added minimal lock to allow multithread installation of gems.  Issue #982
  and pull request #1005 by Yorick Peterse
* RubyGems now considers prerelease dependencies as it did in earlier versions
  when --prerelease is given.  Issue #990 by Jeremy Tryba.
* Updated capitalization in README.  Issue #1010 by Ben Bodenmiller.
* Fixed activating gems from a Gemfile for default gems.  Issue #991 by khoan.
* Fixed windows stub script generation for Cygwin.  Issue #1000 by Brett
  DiFrischia.
* Allow gem bindir and ruby.exe to live in separate diretories.  Pull request
  #942 by Ian Flynn.
* Fixed handling of gemspec in gem dependencies files to match Bundler
  behavior.  Issue #1020 by Michal Papis.
* Fixed `gem update` when updating to prereleases.  Issue #1028 by Santiago
  Pastorino.
* RubyGems now fails immediately when a git reference cannot be found instead
  of spewing git errors.  Issue #1031 by Michal Papis
=== 2.4.1 / 2014-07-17

Bug fixes:

* RubyGems can now be updated on Ruby implementations that do not support
  vendordir in RbConfig::CONFIG.  Issue #974 by net1957.


=== 2.4.0 / 2014-07-16

Minor enhancements:

* The contents command now supports a --show-install-dir option that shows
  only the directory the gem is installed in.  Feature request #966 by Akinori
  MUSHA.
* Added a --build-root option to the install command for packagers.  Pull
  request #965 by Marcus R眉ckert.
* Added vendor gem support to RubyGems.  Package managers may now install gems
  in Gem.vendor_dir with the --vendor option to gem install.  Issue #943 by
  Marcus R眉ckert.

Bug fixes:


* Kernel#gem now respects the prerelease flag when activating gems.
  Previously this behavior was undefined which could lead to bugs when a
  prerelease version was unintentionally activated.  Bug #938 by Joe Ferris.
* RubyGems now prefers gems from git over installed gems.  This allows gems
  from git to override an installed gem with the same name and version.  Bug
  #944 by Thomas Kriechbaumer.
* Fixed handling of git gems in a lockfile with unversioned dependencies.  Bug
  #940 by Michael Kaiser-Nyman.
* The ruby directive in a gem dependencies file is ignored when installing.
  Bug #941 by Michael Kaiser-Nyman.
* Added open to list of builtin commands (`gem open` now works).  Reported by
  Espen Antonsen.
* `gem open` now works with command-line editors.  Pull request #962 by Tim
  Pope.
* `gem install -g` now respects `--conservative`.  Pull request #950 by Jeremy
  Evans.
* RubyGems releases announcements now now include checksums.  Bug #939 by
  Alexander E. Fischer.
* RubyGems now expands ~ in $PATH when checking if installed executables will
  be runnable.  Pull request #945 by Alex Talker.
* Fixed `gem install -g --explain`.  Issue #947 by Luis Lavena.  Patch by
  Hsing-Hui Hsu.
* RubyGems locks less during gem activation.  Pull request #951 by Aaron
  Patterson and Justin Searls, #969 by Jeremy Tryba.
* Kernel#gem is now thread-safe.  Pull request #967 by Aaron Patterson.
* RubyGems now handles spaces in directory names for some parts of extension
  building.  Pull request #949 by Tristan Hill.
* RubyGems no longer defines an empty Date class.  Pull Request #948 by Benoit
  Daloze.
* RubyGems respects --document options for `gem update` again.  Bug 946 by
  jonforums.  Patch by Hsing-Hui Hsu.
* RubyGems generates documentation again with --ignore-dependencies.  Bug #961
  by Pulfer.
* RubyGems can install extensions across partitions now.  Pull request #970 by
  Michael Scherer.
* `-s` is now short for `--source` which resolves an ambiguity with
  --no-suggestions.  Pull request #955 by Alexander Kahn.
* Added extra test for ~> for 0.0.X versions.  Pull request #958 by Mark
  Lorenz.
* Fixed typo in gem updated help.  Pull request #952 by Per Modin.
* Clarified that the gem description should not be excessively long.  Part of
  bug #956 by Renier Morales.
* Hid documentation of outdated test_files related methods in Specification.
  Guides issue #90 by Emil Soman.
* RubyGems now falls back to the old index if the rubygems.org API fails
  during gem resolution.

------------------------------------------------------------------------------

RubyGems installed the following executables:
        /usr/bin/gem

Ruby Interactive (ri) documentation was installed. ri is kind of like man
pages for ruby libraries. You may access it like this:
  ri Classname
  ri Classname.class_method
  ri Classname#instance_metho
If you do not wish to install this documentation in the future, use the
--no-document flag, or set it as the default in your ~/.gemrc file. See
‘gem help env‘ for details.

为了方便操作rubygem,我们把它拷贝到系统环境变量当中,方便调用cp bin/gem /usr/local/bin/     

接下来,安装 redis 的api 接口,这就是为什么上面要装rubygem的道理了gem install -l redis-3.0.0.gem

redis-3.0.0.gem  上面讲到,可以到 官网  http://rubygems.org/gems/redis/versions/3.1.0  下载

一切就绪,现在再一次,来执行集群看看还有没有报错的信息
[root@localhost src]# ./redis-trib.rb create --replicas 1 192.168.1.222:6379 192.168.1.222:6380 192.168.1.222:6381 192.168.1.223:6382 192.168.1.223:6383 192.168.1.223:6384

>>> Creating cluster
Connecting to node 192.168.1.222:6379: OK
Connecting to node 192.168.1.222:6380: OK
Connecting to node 192.168.1.222:6381: OK
Connecting to node 192.168.1.223:6382: OK
Connecting to node 192.168.1.223:6383: OK
Connecting to node 192.168.1.223:6384: OK
>>> Performing hash slots allocation on 6 nodes...

Using 3 masters:
192.168.1.223:6382
192.168.1.222:6379
192.168.1.223:6383

Adding replica 192.168.1.222:6380 to 192.168.1.223:6382
Adding replica 192.168.1.223:6384 to 192.168.1.222:6379
Adding replica 192.168.1.222:6381 to 192.168.1.223:6383

M: faa2fab465017c9be2475d0cbd9ffb7b8e8267e6 192.168.1.222:6379
   slots:5461-10922 (5462 slots) master
S: 92999f9840418a848f7b10c5bca0119e3b515fa4 192.168.1.222:6380
   replicates c13fbd7fadda1e46b133b8f87248414d9a7a0fcc
S: 5d44e7e3bf2c19fcdcc1548feef42d5d9665da0b 192.168.1.222:6381
   replicates 7d674df0ab68be0b4c6a461b0c1484b02eeb12c5
M: c13fbd7fadda1e46b133b8f87248414d9a7a0fcc 192.168.1.223:6382
   slots:0-5460 (5461 slots) master
M: 7d674df0ab68be0b4c6a461b0c1484b02eeb12c5 192.168.1.223:6383
   slots:10923-16383 (5461 slots) master
S: c4b565e9236effee3957c6f03b533dced729e39b 192.168.1.223:6384
   replicates faa2fab465017c9be2475d0cbd9ffb7b8e8267e6
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.1.222:6379)

M: faa2fab465017c9be2475d0cbd9ffb7b8e8267e6 192.168.1.222:6379

   slots:5461-10922 (5462 slots) master

M: 92999f9840418a848f7b10c5bca0119e3b515fa4 192.168.1.222:6380

   slots: (0 slots) master

   replicates c13fbd7fadda1e46b133b8f87248414d9a7a0fcc

M: 5d44e7e3bf2c19fcdcc1548feef42d5d9665da0b 192.168.1.222:6381

   slots: (0 slots) master

   replicates 7d674df0ab68be0b4c6a461b0c1484b02eeb12c5

M: c13fbd7fadda1e46b133b8f87248414d9a7a0fcc 192.168.1.223:6382

   slots:0-5460 (5461 slots) master

M: 7d674df0ab68be0b4c6a461b0c1484b02eeb12c5 192.168.1.223:6383

   slots:10923-16383 (5461 slots) master

M: c4b565e9236effee3957c6f03b533dced729e39b 192.168.1.223:6384

   slots: (0 slots) master

   replicates faa2fab465017c9be2475d0cbd9ffb7b8e8267e6

[OK] All nodes agree about slots configuration.

>>> Check for open slots...

>>> Check slots coverage...

[OK] All 16384 slots covered.

[root@localhost src]#

一切OK。

如果不相信,可以来测试一下集群

[root@localhost src]# ./redis-trib.rb check 192.168.1.223:6384
Connecting to node 192.168.1.223:6384: OK
Connecting to node 192.168.1.222:6380: OK
Connecting to node 192.168.1.222:6379: OK
Connecting to node 192.168.1.222:6381: OK
Connecting to node 192.168.1.223:6383: OK
Connecting to node 192.168.1.223:6382: OK

>>> Performing Cluster Check (using node 192.168.1.223:6384)

S: c4b565e9236effee3957c6f03b533dced729e39b 192.168.1.223:6384

   slots: (0 slots) slave

   replicates faa2fab465017c9be2475d0cbd9ffb7b8e8267e6

S: 92999f9840418a848f7b10c5bca0119e3b515fa4 192.168.1.222:6380

   slots: (0 slots) slave

   replicates c13fbd7fadda1e46b133b8f87248414d9a7a0fcc

M: faa2fab465017c9be2475d0cbd9ffb7b8e8267e6 192.168.1.222:6379

   slots:5461-10922 (5462 slots) master

   1 additional replica(s)

M: 5d44e7e3bf2c19fcdcc1548feef42d5d9665da0b 192.168.1.222:6381

   slots:10923-16383 (5461 slots) master

   1 additional replica(s)

S: 7d674df0ab68be0b4c6a461b0c1484b02eeb12c5 192.168.1.223:6383

   slots: (0 slots) slave

   replicates 5d44e7e3bf2c19fcdcc1548feef42d5d9665da0b

M: c13fbd7fadda1e46b133b8f87248414d9a7a0fcc 192.168.1.223:6382

   slots:0-5460 (5461 slots) master

   1 additional replica(s)

[OK] All nodes agree about slots configuration.

>>> Check for open slots...

>>> Check slots coverage...

[OK] All 16384 slots covered.

[root@localhost src]#

注意哦,我是直接在192.168.1.222,测试到192.168.1.223当中的6384这个实例了

从上面,我们也不难看出

redis_master   是    
                             192.168.1.222:6379
                             192.168.1.222:6381
                             192.168.1.223:6382
redis_salve    是

                             192.168.1.223:6384
                             192.168.1.222:6380
                             192.168.1.223:6383

实验结果
任何一个实例,都能写入,查询,不存在主备关系,redis会自动通过内部算法,选举,不需要你去关心

做一个集群小测试

在 192.168.1.222 ,连接 192.168.1.223 的实例D 6382 ,写入数据 zqtsx
[root@localhost src]# redis-cli -h 192.168.1.223 -p 6382 -c

192.168.1.223:6382> set zqtsx mynameiszqtsx

-> Redirected to slot [11570] located at 192.168.1.223:6383

OK

Redis会自己重定向到   6383 ,也就是 E 当中,换句话说,6382和6382是主备关系,其中6383是6382的 salver 服务器

好,那我们来 192.168.1.223 里面的 6484,也就是 F当中读取 zqtsx的值
[root@localhost opt]# redis-cli -h 192.168.1.223 -p 6384 -c
192.168.1.223:6384> get zqtsx

-> Redirected to slot [11570] located at 192.168.1.223:6383

"mynameiszqtsx"

读取到了,它 会自动映射到 6383 去读取


现在疑问来了,我就把6383干掉,看看是什么样的情况
[root@localhost ]# ps aux |grep redis

root     28922  0.2  0.9 137408  9752 pts/2    Sl   03:30   0:06 redis-server *:6382 [cluster]    
root     28928  0.2  0.9 137408  9596 pts/2    Sl   03:31   0:06 redis-server *:6383 [cluster]    
root     28932  0.2  0.7 137408  7884 pts/2    Sl   03:32   0:05 redis-server *:6384 [cluster]    
root     28973  0.0  0.0 103232   876 pts/2    S+   04:18   0:00 grep redis

[root@localhost opt]# kill -9 28928
[root@localhost opt]# ps aux |grep redis
root     28922  0.2  0.9 137408  9752 pts/2    Sl   03:30   0:06 redis-server *:6382 [cluster]    
root     28932  0.2  0.7 137408  7884 pts/2    Sl   03:32   0:05 redis-server *:6384 [cluster]    
root     28975  0.0  0.0 103232   876 pts/2    S+   04:19   0:00 grep redis
[2]-  Killed                  redis-server /etc/redis/6383.conf > /usr/local/redis/redis6383.log 2>&1

很明显,6383已经被干掉了

[root@localhost src]# redis-cli -h 192.168.1.223 -p 6383 -c
Could not connect to Redis at 192.168.1.223:6383: Connection refused
not connected> get zqtsx
Could not connect to Redis at 192.168.1.223:6383: Connection refused
not connected>
[root@localhost src]# redis-cli -h 192.168.1.223 -p 6384 -c
192.168.1.223:6384> get zqtsx
-> Redirected to slot [11570] located at 192.168.1.222:6381
"mynameiszqtsx"
192.168.1.222:6381>

虽然,在6383,建立不了链接,但是不影响我数据的读取,在6381一样可以获取到zqtsx的key值


phpredis扩展安装

phpredis下载地址:https://github.com/nicolasff/phpredis

unzip phpredis-master.zip
cd phpredis-master

/usr/local/php/bin/phpize
./configure --with-php-config=/usr/local/php/bin/php-config
make
make install
vi /usr/local/php/etc/php.ini
加入 extension=redis.so

apache下重启httpd或apache,NGINX下重启php-fpm即可


郑重声明:本站内容如果来自互联网及其他传播媒体,其版权均属原媒体及文章作者所有。转载目的在于传递更多信息及用于网络分享,并不代表本站赞同其观点和对其真实性负责,也不构成任何其他建议。