redis-4.0.11安装部署

redis的安装

解压redis的源码包:

[root@hadoop01 home]# tar -zxvf /home/redis-4.0.11.tar.gz -C /home/

[root@hadoop01 ~]# cd /home/redis-4.0.11/

执行编译安装:

[root@hadoop01 redis-4.0.11]# yum -y install gcc gcc-c++

[root@hadoop01 redis-4.0.11]# make

[root@hadoop01 redis-4.0.11]# make PREFIX=/usr/local/redis-4.0.11 install

[root@hadoop01 redis-4.0.11]# mkdir /usr/local/redis-4.0.11/etc/

[root@hadoop01 redis-4.0.11]# cp redis.conf /usr/local/redis-4.0.11/etc/

[root@hadoop01 redis-4.0.11]# cd /usr/local/redis-4.0.11/bin/

[root@hadoop01 redis-4.0.11]# cp redis-benchmark redis-cli redis-server /usr/bin/

修改配置:

[root@hadoop01 redis-4.0.11]# vi /usr/local/redis-4.0.11/etc/redis.conf

# 修改一下配置

# redis以守护进程的方式运行

# no表示不以守护进程的方式运行(会占用一个终端)

daemonize yes

# 客户端闲置多长时间后断开连接,默认为0关闭此功能  timeout 300

# 设置redis日志级别,默认级别:notice

loglevel verbose

# 设置日志文件的输出方式,如果以守护进程的方式运行redis 默认:""

# 并且日志输出设置为stdout,那么日志信息就输出到/dev/null里面去了

logfile stdout

# 设置密码授权

requirepass root

# 监听ip

bind 192.168.216.111

配置环境变量:

# 让环境变量立即生效 因为可执行脚本已经copy到/usr/bin下

source /etc/profile

开机自启脚本:

[root@hadoop01 redis-4.0.11]# vi ./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.

PATH=/usr/local/bin:/sbin:/usr/bin:/bin

REDISPORT=6379

EXEC=/usr/local/redis-4.0.11/bin/redis-server

REDIS_CLI=/usr/local/redis-4.0.11/bin/redis-cli

PIDFILE=/var/run/redis.pid

CONF="/usr/local/redis-4.0.11/etc/redis.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

    if [ "$?"="0" ]

    then

       echo "Redis is running..."

    fi

   ;;

  stop)

    if [ ! -f $PIDFILE ]

    then

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

    else

        PID=$(cat $PIDFILE)

        echo "Stopping ..."

        $REDIS_CLI -p $REDISPORT SHUTDOWN

        while [ -x ${PIDFILE} ]

       do

          echo "Waiting for Redis to shutdown ..."

          sleep 1

        done

        echo "Redis stopped"

    fi

   ;;

restart|force-reload)

    ${0} stop

    ${0} start

   ;;

*)

  echo "Usage: /etc/init.d/redis {start|stop|restart|force-reload}" >&2

    exit 1

esac

开机自启动设置:

# 复制脚本文件到init.d目录下

[root@hadoop01 redis-4.0.11]# cp redis /etc/init.d/

# 给脚本增加运行权限

[root@hadoop01 redis-4.0.11]# chmod +x /etc/init.d/redis

# 查看服务列表

[root@hadoop01 redis-4.0.11]# chkconfig --list

# 添加服务

[root@hadoop01 redis-4.0.11]# chkconfig --add redis

# 配置启动级别

[root@hadoop01 redis-4.0.11]# chkconfig --level 2345 redis on

启动测试:

[root@hadoop01 redis-4.0.11]# systemctl start redis  #或者 /etc/init.d/redis

start

[root@hadoop01 redis-4.0.11]# systemctl stop redis  #或者 /etc/init.d/redis stop

# 查看redis进程

[root@hadoop01 redis-4.0.11]# ps -el|grep redis

# 端口查看

[root@hadoop01 redis-4.0.11]# netstat -an|grep 6379

redis的主从部署

1、停止主

[root@hadoop01 redis-4.0.11]# systemctl stop redis

2、远程scp主到重

[root@hadoop01 redis-4.0.11]# scp -r ../redis-4.0.11/ hadoop02:/usr/local/

3、从配置:

[root@hadoop02 redis-4.0.11]# vi /usr/local/redis-4.0.11/etc/redis.conf

从服务器需要重新设置绑定:

bind 127.0.0.1 192.168.216.112

slaveof 192.168.216.111 6379

masterauth root

4、配置重

[root@hadoop02 redis-4.0.11]# cd /usr/local/redis-4.0.11/bin/

[root@hadoop02 bin]# cp redis-benchmark redis-cli redis-server /usr/bin/

# 让环境变量立即生效 因为可执行脚本已经copy到/usr/bin下

[root@hadoop02 redis-4.0.11]# source /etc/profile

5、从开机自启脚本编写(已从主复制)

6、从开机自启设置:

# 复制脚本文件到init.d目录下

[root@hadoop02 redis-4.0.11]# cp redis /etc/init.d/

# 给脚本增加运行权限

[root@hadoop02 redis-4.0.11]# chmod +x /etc/init.d/redis

# 查看服务列表

[root@hadoop02 redis-4.0.11]# chkconfig --list

# 添加服务

[root@hadoop02 redis-4.0.11]# chkconfig --add redis

# 配置启动级别

[root@hadoop02 redis-4.0.11]# chkconfig --level 2345 redis on

7、保证启动从服务器的时候master安装目录下的dump.rdb文件被删除。

[root@hadoop01 redis-4.0.11]# rm -rf ./dump.rdb

8、分别启动主从redis

[root@hadoop01 redis-4.0.11]# systemctl start redis

[root@hadoop02 redis-4.0.11]# systemctl start redis

9、主从测试

[root@hadoop01 redis-4.0.11]# redis-cli -h hadoop01

hadoop01:6379> auth root

OK

hadoop01:6379> info replication

# Replication

role:master

connected_slaves:1

...

hadoop01:6379> set k2 v2

OK

hadoop01:6379> get k2

"v2"

[root@hadoop02 redis-4.0.11]# redis-cli -h hadoop02

hadoop02:6379> auth root

OK

hadoop02:6379> info replication

# Replication

role:slave

master_host:192.168.216.111

...

hadoop02:6379> get k2

"v2"

hadoop02:6379> set k3 v3

(error) READONLY You can't write against a read only slave.   #从不能进行写操作

 

你可能感兴趣的:(redis)