CentOS7编译安装Redis

基于 CentOS7.5操作系统编译安装Redis,在这里可以找到全部的Redis版本源代码: https://download.redis.io/releases/ ,本次编译选择的是 redis-4.0.10的版本。

下载源码并且安装

 1# 下载并解压
 2wget http://download.redis.io/releases/redis-4.0.10.tar.gz
 3tar -xzf redis-4.0.10.tar.gz
 4cd redis-4.0.10
 5
 6# 安装编译工具并编译
 7yum -y install gcc gcc-c++ kernel-devel
 8make
 9
10# 编译完成后安装
11make PREFIX=/usr/local/redis install
12mkdir /usr/local/redis/etc/
13cp redis.conf /usr/local/redis/etc/
14cd /usr/local/redis/bin/
15cp redis-benchmark redis-cli redis-server /usr/bin/

编辑配置文件 redis.conf

 1vim /usr/local/redis/etc/redis.conf
 2
 3# yes -> 守护进程的方式运行 no -> 终端开启
 4daemonize yes
 5
 6# 客户端闲置多长时间后断开连接,默认为0关闭此功能                                      
 7timeout 300
 8
 9# 设置redis日志级别,默认级别:notice                    
10loglevel verbose
11
12# 日志信息就输出到/dev/null
13logfile stdout
14
15# 设置密码
16requirepass <设置密码>
17
18# 监听ip
19bind 127.0.0.1 

配置环境变量

1vim /etc/profile
2export PATH="$PATH:/usr/local/redis/bin"
3
4source /etc/profile

编辑启动脚本 redis (主要是指定Redis配置文件的位置)

 1#!/bin/bash
 2#chkconfig: 2345 80 90
 3# Simple Redis init.d script conceived to work on Linux systems
 4# as it does use of the /proc filesystem.
 5
 6PATH=/usr/local/bin:/sbin:/usr/bin:/bin
 7REDISPORT=6379
 8EXEC=/usr/local/redis/bin/redis-server
 9REDIS_CLI=/usr/local/redis/bin/redis-cli
10   
11PIDFILE=/var/run/redis.pid
12CONF="/usr/local/redis/etc/redis.conf"
13   
14case "$1" in
15    start)
16        if [ -f $PIDFILE ]
17        then
18                echo "$PIDFILE exists, process is already running or crashed"
19        else
20                echo "Starting Redis server..."
21                $EXEC $CONF
22        fi
23        if [ "$?"="0" ] 
24        then
25              echo "Redis is running..."
26        fi
27        ;;
28    stop)
29        if [ ! -f $PIDFILE ]
30        then
31                echo "$PIDFILE does not exist, process is not running"
32        else
33                PID=$(cat $PIDFILE)
34                echo "Stopping ..."
35                $REDIS_CLI -p $REDISPORT SHUTDOWN
36                while [ -x ${PIDFILE} ]
37               do
38                    echo "Waiting for Redis to shutdown ..."
39                    sleep 1
40                done
41                echo "Redis stopped"
42        fi
43        ;;
44   restart|force-reload)
45        ${0} stop
46        ${0} start
47        ;;
48  *)
49    echo "Usage: /etc/init.d/redis {start|stop|restart|force-reload}" >&2
50        exit 1
51esac

开启开机时服务自启

 1# 复制脚本文件到init.d目录下
 2cp redis /etc/init.d/
 3
 4chmod +x /etc/init.d/redis
 5
 6# 查看服务列表
 7chkconfig --list
 8
 9# 添加服务
10chkconfig --add redis
11
12# 配置启动级别
13chkconfig --level 2345 redis on

服务开启和关闭

1systemctl start redis
2systemctl stop redis
3
4ps -el|grep redis
5
6netstat -an|grep 6379