redis环境的安装

Redis环境的安装(源码安装),主要分为单机安装与集群安装,无论是单机安装还是集群安装,Redis本身的依赖是必须要有的,本文所采用的Redis版本是redis-5.0.3,所需要的依赖如下:

cpp-4.8.5-36.el7.x86_64.rpm
gcc-4.8.5-36.el7.x86_64.rpm
glibc-2.17-260.el7.x86_64.rpm
glibc-common-2.17-260.el7.x86_64.rpm
glibc-devel-2.17-260.el7.x86_64.rpm
glibc-headers-2.17-260.el7.x86_64.rpm
kernel-headers-3.10.0-957.1.3.el7.x86_64.rpm
libgcc-4.8.5-36.el7.x86_64.rpm
libgomp-4.8.5-36.el7.x86_64.rpm
libmpc-1.0.1-3.el7.x86_64.rpm
mpfr-3.1.1-4.el7.x86_64.rpm

  

其中,在安装时可以查看对应的依赖是否已经安装,查看方式rpm –qa |grep {rpmName}。

检查完依赖,如果依赖不存在,则进行安装,如果安装过程中出现依赖安装不上,可执行rpm –Uvh {rpmName} –-nodeps –force命令进行安装。

单节点环境安装:

解压压缩包, tar zxvf redis-5.0.3.tar.gz -C /opt/sourceware/目录下;
进入目录编译:cd /opt/sourceware/redis-*
编译:make
进入src目录进行安装:make install
修改redis配置文件redis.conf:
bind地址修改为实际地址;
将daemonize属性修改为yes(表名在后台运行)
port端口修改;
protected-mode修改为no(关闭保护模式)
requirepass密码设置
启动redis:redis-server &
进入客户端:redis-cli 
重启:redis-cli shutdown

集群环境安装:集群环境采用redis本身集群机制,建议采用3+台机器,具体如下:

环境规划:

机器1:192.168.1.1:8001192.168.1.1:8002
机器2:192.168.1.1:8003192.168.1.1:8004
机器3:192.168.1.1:8004192.168.1.1:8006

 

应用安装:

解压压缩包, tar zxvf redis-5.0.3.tar.gz -C /opt/sourceware/目录下;
进入目录编译:cd /opt/sourceware/redis-*
编译:make
进入src目录进行安装:make install
每台编译安装结束之后,在安装目录创建集群目录:
机器1:mkdir –p /opt/sourceware/redis-*/ redis_cluster/8001/
机器1:mkdir –p /opt/sourceware/redis-*/ redis_cluster/8002/
机器2:mkdir –p /opt/sourceware/redis-*/ redis_cluster/8003/
机器2:mkdir –p /opt/sourceware/redis-*/ redis_cluster/8004/
机器3:mkdir –p /opt/sourceware/redis-*/ redis_cluster/8005/
机器3:mkdir –p /opt/sourceware/redis-*/ redis_cluster/8006/
并将对应的redis.conf文件copy到redis_cluster/800*/中

 修改其中的信息:

port  8001 //端口按规划修改
bind 本机ip  //默认ip为127.0.0.1 需要改为其他节点机器可访问的ip 否则创建集群时无法访问对应的端口,无法创建集群
daemonize    yes   //redis后台运行
pidfile  /var/run/redis_8001.pid   //pidfile文件对应8001
cluster-enabled  yes  //开启集群  把注释#去掉
cluster-config-file  nodes_8001.conf   //集群的配置  配置文件首次启动自动生成 8001
cluster-node-timeout  15000                //请求超时  默认15秒,可自行设置
appendonly  yes                           //aof日志开启  有需要就开启,它会每次写操作都记录一条日志 

创建集群:

redis-cli --cluster create 192.168.1.1:8001 192.168.1.1:8002 192.168.1.2:8003 192.168.1.2:8004 192.168.1.3:8005 192.168.1.3:8006 --cluster-replicas 1

创建成功后,启用节点密码及集群密码,密码须一致,修改redis.conf

masterauth passwd123

requirepass passwd123

重启redis应用,并检查集群状态:

执行:redis-cli -c -p 8001 -h 127.0.0.1 
执行:cluster info
执行:cluster nodes

redis开机自启:编写redis脚本

#!/bin/bash

# chkconfig: 2345 80 90

#

# Simple Redis init.d script conceived to work on Linux systems

# as it does use of the /proc filesystem.

 

REDIS_PORT1=8001

REDIS_PORT2=8002

HOST_IP=192.168.1.1

PASS_WORD=Pass@2020

 

BIN_DIR=/usr/local/bin

COF_DIR=/opt/sourceware/redis-5.0.3/redis_cluster

 

SER_EXEC=$BIN_DIR/redis-server

CLI_EXEC=$BIN_DIR/redis-cli

 

PID_FILE1=/var/run/redis_${REDIS_PORT1}.pid

PID_FILE2=/var/run/redis_${REDIS_PORT2}.pid

 

CONF1=$COF_DIR/${REDIS_PORT1}/redis.conf

CONF2=$COF_DIR/${REDIS_PORT2}/redis.conf

 

case "$1" in

    start)

        if [ -f $PID_FILE1 ]

        then

            echo "$PID_FILE1 exists, process is already running or crashed"

        else

            echo "Starting Redis cluster server..."

           cd $COF_DIR/${REDIS_PORT1}

            $SER_EXEC $CONF1 &

            echo "启动成功..."

        fi

      

       if [ -f $PID_FILE2 ]

        then

            echo "$PID_FILE2 exists, process is already running or crashed"

        else

            echo "Starting Redis cluster server..."

           cd $COF_DIR/${REDIS_PORT2}

            $SER_EXEC $CONF2 &

            echo "启动成功..."

        fi

        ;;

    stop)

        if [ ! -f $PID_FILE1 ]

        then

            echo "$PID_FILE1 does not exist, process is not running"

        else

            PID=$(cat $PID_FILE1)

           echo "Stopping ..."

           $CLI_EXEC -p $REDIS_PORT1 -h $HOST_IP -a $PASS_WORD shutdown

           while [ -x /proc/${PID} ]

           do

              echo "Waiting for Redis cluster to shutdown ..."

              sleep 1

           done

           echo "Redis cluster stopped"

        fi

      

       if [ ! -f $PID_FILE2 ]

        then

            echo "$PID_FILE2 does not exist, process is not running"

        else

           PID=$(cat $PID_FILE2)

           echo "Stopping ..."

           $CLI_EXEC -p $REDIS_PORT2 -h $HOST_IP -a $PASS_WORD shutdown

           while [ -x /proc/${PID} ]

           do

              echo "Waiting for Redis cluster to shutdown ..."

              sleep 1

           done

           echo "Redis cluster stopped"

        fi

        ;;

    *)

        echo "Please use start or stop as first argument"

        ;;

esac

安装脚本:

1.将该文件放在/etc/init.d/目录下
2.修改该文件中的端口、IP、密码
3.执行:chkconfig --add redis
4.执行:chkconfig redis on
5.验证:service redis start|stop

附带常用命令:

redis-cli -c -p 8001 -h 127.0.0.1 -a Password@2020
cluster nodes
cluster info
redis-cli -a Password@2020 --cluster check ip:port
redis-cli -a Password@2020 --cluster fix ip:port
redis-cli -a Password@2020 --cluster add-node ip:port

 

你可能感兴趣的:(redis环境的安装)