美文网首页
Centos 6.x 上安装Redis

Centos 6.x 上安装Redis

作者: Martain | 来源:发表于2020-05-20 13:49 被阅读0次

前言

redis的编译安装是我遇到的最简单的编译安装,但是为什么要写这篇博客呢?因为今天需要在服务器上部署需要部署redis,而服务器的操作系统是centos 6.x,安装过程中遇到了一些问题,所以这里记录下来。

遇到的问题以及操作

  • centos 6.x 上的gcc版本一般是4.4.7,而编译redis需要更高的gcc版本
  • 直接启动不太方便,自定义shell脚本可用service的方式启动
  • redis conf 的配置

编译安装

官网教程

# 下载
wget http://download.redis.io/releases/redis-6.0.3.tar.gz 
# 解压
tar zxvf redis-6.0.3.tar.gz 
# 编译
cd redis-6.0.3
make
error: unrecognized command line option "-std=c11"

这个报错是因为gcc版本不够高,centos 6.x安装的gcc版本一般为4.4.7,我们需要安装更高版本的gcc

安装新版gcc

使用scl软件集(推荐)

scl软件集(Software Collections),是为了给 RHEL/CentOS 用户提供一种以方便、安全地安装和使用应用程序和运行时环境的多个(而且可能是更新的)版本的方式,同时避免把系统搞乱。

# 查看gcc版本
gcc -v
# 安装源
yum install centos-release-scl scl-utils-build
# 查看scl那些源可用
yum list all --enablerepo='centos-sclo-rh'
# 这里我们可以看到许多 devtoolset-{version}-gcc-* 的软件集,后面的版本就是gcc的大版本号
# 假设我们要安装gcc 7.x
yum install devtoolset-7-gcc
# 切换版本
scl enable devtoolset-7
# gcc命令已经使用的是新版的了

编译安装(不推荐,耗时久)

# 下载
wget http://ftp.gnu.org/gnu/gcc/gcc-7.5.0/gcc-7.5.0.tar.gz
# 解压
tar zxvf gcc-7.5.0.tar.gz
# 检查下载依赖库
cd gcc-7.5.0
./contrib/download_prerequisites
# 编译安装
mkdir build
cd build
../configure -enable-checking=release -enable-languages=c,c++ -disable-multilib
make & make install

安装新版完成之后,可以回到redis的源码文件夹make一下即可,编译文件在该目录下的src目录下

实现用service命令执行

​ 当我们将redis编译完成之后,可以使用 src/redis-server启动服务,那能不能像service xxx start这样启动redis呢?答案是肯定的,甚至service脚本都已经给我们准备好了----在源码包的utils下的redis_init_script,当然systemed的脚本也有,因为centos 6.x 还不支持systemctl,这里就没有去尝试;下面我们来改造下。

  • 复制源码包中的redis.conf/etc/redis目录下(没有就创建),并修改名为6379.conf备用
  • 复制源码包中的utils/redis_init_script/etc/init.d下,并重命名为redis
  • 修改上一步所移动的脚本(/etc/init.d/redis)的内容
#!/bin/sh
#
# Simple Redis init.d script conceived to work on Linux systems
# as it does use of the /proc filesystem.

### BEGIN INIT INFO
# Provides:     redis_6379
# Default-Start:        2 3 4 5
# Default-Stop:         0 1 6
# Short-Description:    Redis data structure server
# Description:          Redis data structure server. See https://redis.io
### END INIT INFO

REDISPORT=6379  # 这里是要启动的端口
# 这里表示redis-server的路径,可以把编译好的src文件夹移动到/usr/local/redis的bin下
EXEC=/usr/local/redis/bin/redis-server 
CLIEXEC=/usr/local/redis/bin/redis-cli
# pid的保存路径
PIDFILE=/var/run/redis_${REDISPORT}.pid
# conf的路径
CONF="/etc/redis/${REDISPORT}.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
        ;;
    stop)
        if [ ! -f $PIDFILE ]
        then
                echo "$PIDFILE does not exist, process is not running"
        else
                PID=$(cat $PIDFILE)
                echo "Stopping ..."
                $CLIEXEC -p $REDISPORT shutdown
                while [ -x /proc/${PID} ]
                do
                    echo "Waiting for Redis to shutdown ..."
                    sleep 1
                done
                echo "Redis stopped"
        fi
        ;;
    *)
        echo "Please use start or stop as first argument"
        ;;
esac
  • 完成以上步骤就可以使用service redis startservice redis stop来启动或停止redis服务了

关于配置文件

设置密码

修改 requirepass 参数

设置后台运行

daemonize 的值修改为yes

外网访问

bind127.0.0.1改成0.0.0.0

配置端口

修改port

相关文章

网友评论

      本文标题:Centos 6.x 上安装Redis

      本文链接:https://www.haomeiwen.com/subject/ahogohtx.html