1、下载安装
 
wget http://download.redis.io/redis-stable.tar.gz
 
tar -zxvf redis-stable.tar.gz
 
cd redis-stable
 
make
 
make test 检查一下是否正常,遇到2个错误
 
[root@localhost redis-stable]# make test
 
cd src && make test
 
make[1]: Entering directory `/usr/local/src/redis-stable/src'
 
which: no tclsh8.5 in (/usr/lib64/qt-3.3/bin:/usr/local/bin:/usr/bin:/bin:/usr/local/sbin:/usr/sbin:/sbin:/home/geffzhang/bin)
 
You need 'tclsh8.5' in order to run the Redis test
 
make[1]: *** [test] 错误 1
 
make[1]: Leaving directory `/usr/local/src/redis-stable/src'
 
make: *** [test] 错误 2
 
[root@localhost redis-stable]#
 
没安装tcl
 
按照官网http://www.linuxfromscratch.org/blfs/view/cvs/general/tcl.html 上的安装
 
【注】也可以用 
 yum install tcl 命令安装
 
cd redis-stable/src
 
make install
 
mkdir -p /home/redis
 
cp -pf redis-server /home/redis
 
cp -pf redis-benchmark /home/redis
 
cp -pf redis-cli /home/redis
 
cp -pf redis-check-aof /home/redis
 
cp -pf redis-sentinel /home/redis
 
make[1]: Leaving directory `/usr/local/src/redis-stable/src'
 
[root@localhost redis-stable]#
 
好了,现在redis就安装成功了。redis-sentinel作为主从切换功能不一定要安装
 
2.配置redis.conf
 
cd /home/redis
 
mkdir conf
 
cp /usr/local/src/redis.conf /home/redis/conf/redis.conf
 
redis.conf有几个重要的属性需要配置
 
bind 127.0.0.1 // 绑定地址
 
port 6379   // 端口
 
daemonize no // redis-server是否作为后台进程运用,一般改为yes
 
pidfile /var/run/redis_6379.pid // pid文件放置位置,一般可以改为REDIS_HOME安装目录下
 
logfile "" // 日志输出位置,一般可以改为REDIS_HOME安装目录下
 
save 900 1 // 15分钟有1个key更新则把内存数据存储到磁盘
 
save 300 10 // 5分钟有300个key更新则把内存数据存储到磁盘
 
save 60 10000 // // 1分钟有10000个key更新则把内存数据存储到磁盘
 
dir ./ // 内存数据存储目录, 
 一般可以改为REDIS_HOME安装目录下
 
#slaveof <masterip> <masterport> // 配置slave。默认是关闭
 
slave-read-only yes // slave作为只读,也可以配置为no,slave则可以读写
 
maxmemory 4gb // 最大内存空间.4gb标识4*1024*1024*1024字节
 
appendfsync everysec // 每秒把已经更改过的key写磁盘
 
 
 
 
3.建立目录
 
mkdir db
 
mkdir log
 
mkdir run
 
4.启动和停止redis
 
#建一个start.sh脚本
 
vi start.sh
 
#!/bin/bash
 
./redis-server ./conf/redis.conf
 
#建一个shudown.sh脚本
 
vi shutdown.sh
 
#!/bin/bash
 
./redis-cli -h 127.0.0.1 -p 6379 shutdown
 
4.搭建slave,可以把已经配置好的redis直接copy一份为redis-slave,修改redis.conf的一些参数就可以启动。
 
redis.conf的 
 slaveof改为
 
slaveof 127.0.0.1 6379
 
port改为
 
port 6378
 
 
 
 
注:到此redis基本的master-slave主从已经搭建好了。应用可以自己发送心跳到master和slave,如果master挂了可以切换到slave访问。但是这个主从还是不能很好的实现高可用。如果master挂了,slave一般为只读,很多写的场景就不可用。slave的slave-read-only也可以改成no,但是当master起来后,slave写的数据就会被清空。如果master和slave能互为主备就可以提高可用性,redis-sentinel组件就是为master-slave互为主备切换设计的。
 
在REDIS_HOME/conf目录下
 
vi sentinel.conf
 
bind 127.0.0.1
 
port 26379
 
daemonize yes
 
logfile "/home/redis/log/sentinel.log"
 
 
 
#master 6379
 
sentinel monitor master1 127.0.0.1 6379 2                #配置master名、ip、port、需要多少个sentinel才能判断[客观下线](2)
 
sentinel down-after-milliseconds master1 30000      #配置sentinel向master发出ping,最大响应时间、超过则认为主观下线
 
sentinel parallel-syncs master1 1                   #配置在进行故障转移时,运行多少个slave进行数据备份同步(越少速度越快)
 
sentinel failover-timeout master1 180000            #配置当出现failover时下一个sentinel与上一个sentinel对[同一个master监测的时间间隔](最后设置为客观下线)
 
 
 
#master 6378
 
sentinel monitor master2 127.0.0.1 7001 1
 
sentinel down-after-milliseconds master2 30000
 
sentinel parallel-syncs master2 1
 
sentinel failover-timeout master2 180000
 

 
通过./redis-sentinel ./conf/sentinel.conf 启动redis-sentinel