CentOS7安装Redis

Redis安装

安装gcc编译器

# yum install gcc

下载redis

# wget http://download.redis.io/releases/redis-3.0.7.tar.gz

解压redis

# tar -zxvf redis-3.0.7.tar.gz

编译redis

# cd redis-3.0.7
# make MALLOC=libc

执行完成编译后,会在src目录下生成6个可执行文件,分别是redis-server、redis-cli、redis-benchmark、redis-check-aof、redis-check-dump、redis-sentinel

# find src -executable \! -name "*.*" -type f

安装redis,会将make编译生成的可执行文件拷贝到/usr/local/bin目录下

# make install

运行utils目录下的install_server.sh,该脚本会将redis安装成系统服务,并启动redis

# ./utils/install_server.sh

Redis服务查看、开启、关闭

查看redis进程

# ps aux | grep -v grep | grep redis-server

启动服务

# service redis_6379 start

停止服务

# service redis_6379 stop

连接redis服务

# redis-cli

查看redis相关信息

redis 127.0.0.1:6379> info  #查看server版本内存使用连接等信息
  
redis 127.0.0.1:6379> client list  #获取客户连接列表
  
redis 127.0.0.1:6379> client kill 127.0.0.1:33441  #终止某个客户端连接 
  
redis 127.0.0.1:6379> dbsize  #当前保存key的数量
  
redis 127.0.0.1:6379> save  #立即保存数据到硬盘
  
redis 127.0.0.1:6379> bgsave  #异步保存数据到硬盘
  
redis 127.0.0.1:6379> flushdb  #当前库中移除所有key
  
redis 127.0.0.1:6379> flushall  #移除所有key从所有库中
  
redis 127.0.0.1:6379> lastsave  #获取上次成功保存到硬盘的unix时间戳
  
redis 127.0.0.1:6379> monitor  #实时监测服务器接收到的请求
  
redis 127.0.0.1:6379> slowlog len  #查询慢查询日志条数
(integer) 3
  
redis 127.0.0.1:6379> slowlog get  #返回所有的慢查询日志,最大值取决于slowlog-max-len配置
  
redis 127.0.0.1:6379> slowlog get 2  #打印两条慢查询日志
  
redis 127.0.0.1:6379> slowlog reset  #清空慢查询日志信息

优化系统配置

增大文件描述符,支持高并发

# vi /etc/sysctl.conf

vm.overcommit_memory = 1
fs.file-max = 100000
net.core.somaxconn = 2048
net.ipv4.tcp_max_syn_backlog = 2048

运行如下命令生效

# sysctl -p

# vi /etc/security/limits.conf

*              soft     nofile          65536
*              hard     nofile          65536

设置好这些需要重启系统后生效。

redis密码设置

修改redis配置文件,找到requirepass,去掉注释并修改密码

# vi /etc/redis/6379.conf

requirepass Q1w2e3r4

集群中运用加密设置时,还需设置masterauth,不然slave 是无法进行正常复制的

requirepass Q1w2e3r4
masterauth Q1w2e3r4

Sentinel的主从切换方案

部署三台Redis服务器构成一个小的集群,主要有2个目的:

  • 高可用性:在主机挂掉后,自动故障转移,使前端服务对用户无影响。
  • 读写分离:将主机读压力分流到从机上。

集群方案

整个集群:
1台虚拟机,每台机器上3个instance

Redis Group由一组Redis Instance组成,一组Redis Instatnce有1个Master Instance,2个Slave Instance

                  Redis端口                  Sentinel端口
            -----------------------      ------------------------

server1: 6379、6380、6381    26379、26380、26381

生成redis instance

server1:运行./utils/install_server.sh,分别生成redis_6379、redis_6380、redis_6381的redis服务

修改/etc/redis/6380.conf、/etc/redis/6381.conf

slaveof 127.0.0.1 6379

重新启动redis_6380、redis_6381服务

# service redis_6380 restart
# service redis_6381 restart

复制redis编译后的目录下的sentinel.conf到redis配置文件目录下

# cp sentinel.conf /etc/redis/sentinel_26379.conf
# cp sentinel.conf /etc/redis/sentinel_26380.conf
# cp sentinel.conf /etc/redis/sentinel_26381.conf

修改每个sentinel配置文件(端口依次改为26379,26380,26381)

port 26379
daemonize yes
pidfile /var/run/sentinel_26379.pid
sentinel monitor mymaster 127.0.0.1 6379 2
sentinel down-after-milliseconds mymaster 60000
sentinel failover-timeout mymaster 180000
sentinel parallel-syncs mymaster 1

sentinel monitor <master-name> <ip> <redis-port> <quorum>  设置sentinel监控redis master

创建sentinel启动脚本(/etc/init.d/sentinel_26379、/etc/init.d/sentinel_26380、/etc/init.d/sentinel_26381),注意修改对应端口和文件

#!/bin/sh
#Configurations injected by install_sentinel below....

EXEC=/usr/local/bin/redis-sentinel
CLIEXEC=/usr/local/bin/redis-cli
PIDFILE=/var/run/sentinel_26379.pid
CONF="/etc/redis/sentinel_26379.conf"
SENTINELPORT="26379"
###############
# SysV Init Information
# chkconfig: - 58 74
# description: sentinel_26379 is the sentinel daemon.
### BEGIN INIT INFO
# Provides: sentinel_26379
# Required-Start: $network $local_fs $remote_fs
# Required-Stop: $network $local_fs $remote_fs
# Default-Start: 2 3 4 5
# Default-Stop: 0 1 6
# Should-Start: $syslog $named
# Should-Stop: $syslog $named
# Short-Description: start and stop sentinel_26379
# Description: Sentinel daemon
### END INIT INFO


case "$1" in
    start)
        if [ -f $PIDFILE ]
        then
            echo "$PIDFILE exists, process is already running or crashed"
        else
            echo "Starting Sentinel 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 $SENTINELPORT shutdown
            while [ -x /proc/${PID} ]
            do
                echo "Waiting for Sentinel to shutdown ..."
                sleep 1
            done
            echo "Sentinel stopped"
        fi
        ;;
    status)
        PID=$(cat $PIDFILE)
        if [ ! -x /proc/${PID} ]
        then
            echo 'Sentinel is not running'
        else
            echo "Sentinel is running ($PID)"
        fi
        ;;
    restart)
        $0 stop
        $0 start
        ;;
    *)
        echo "Please use start, stop, restart or status as first argument"
        ;;
esac

启动sentinel服务,并设为自启动

# chkconfig sentinel_26379 on
# chkconfig sentinel_26380 on
# chkconfig sentinel_26381 on

# service sentinel_26379 start
# service sentinel_26380 start
# service sentinel_26381 start

查看redis进程

# ps -ef | grep redis

登录sentinel实例

# redis-cli -p 26379

查看masters状态

# sentinel masters

集群方案

整个集群:
2台虚拟机,每台机器上3个instance

                    Redis端口
           --------------------------

server2: 6379,6380,6381

server3: 7379,7380,7381

生成redis instance

server2:运行./utils/install_server.sh,分别生成redis_6379、redis_6380、redis_6381的redis服务

server3:运行./utils/install_server.sh,分别生成redis_7379、redis_7380、redis_7381的redis服务

安装ruby

# yum install ruby

gem源更换国内源

# gem sources --add https://ruby.taobao.org/ --remove https://rubygems.org/
# gem sources -l

安装redis-rb库

# gem install redis

server2:修改/etc/redis/6379.conf、/etc/redis/6380.conf、/etc/redis/6381.conf(nodes-6379.conf中的数字为redis端口号)

appendonly yes
cluster-enabled yes
cluster-config-file nodes-6379.conf
cluster-node-timeout 15000

server3:修改/etc/redis/7379.conf,/etc/redis/7380.conf,/etc/redis/7381.conf

appendonly yes
cluster-enabled yes
cluster-config-file nodes-7379.conf
cluster-node-timeout 15000

复制redis编译后的目录下的src/redis-trib.rb脚本到/usr/local/bin

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

创建集群

# redis-trib.rb create --replicas 1 10.10.10.12:6379 10.10.10.12:6380 10.10.10.12:6381 10.10.10.13:7379 10.10.10.13:7380 10.10.10.13:7381

查看群集状态

# redis-trib.rb check 10.10.10.12:6379

登录集群某个节点

# redis-cli -c -p 6379 -h 10.10.10.12

 

Redis客户端

https://github.com/caoxinyu/RedisClient

posted @ 2016-04-23 18:58  Edward Guan  阅读(1213)  评论(0编辑  收藏  举报