Centos7安装redis

Centos7安装redis

Yum安装

yum install epel-release
yum install redis

常用命令

#启动
systemctl start redis.service
#重启  
systemctl restart redis.service
#关闭  
systemctl stop redis.service

配置文件

/etc/redis.conf

源码编译安装

说明:自 redis 6.0.0 之后,编译 redis 需要支持 C11 特性。Centos7 默认 gcc 版本为 4.8.5C11 特性在 4.9 中引入

yum install gcc gcc-c++

  • 需要gcc高版本的解决方案

    解决方案

    安装 Developer Toolset 7 使用 gcc 7 编译

    yum install centos-release-scl scl-utils-build #安装scl工具
    yum install devtoolset-7-gcc.x86_64 devtoolset-7-gcc-c++.x86_64 devtoolset-7-gcc-gdb-plugin.x86_64  #安装gcc7
    scl -l  #查看安装的版本
    scl enable devtoolset-7 bash  #切换版本  使用exit 退出当前scl版本的bash环境
    

使用稳定版

image-20200625113434705

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

image-20200625112935175

tar -zxvf redis-6.0.5.tar.gz -C /opt
cd /opt/redis-6.0.5
make test  #出现 All tests passed without errors!
make && make install

配置开机自启动

编写redis.conf配置文件

配置文件路径:/etc/redis.conf

daemonize是守护进程的意思

把daemonize no改成daemonize yes

image-20200625114500501

新建个redis启动脚本

注意要修改里面redis的安装路径:我这边的安装路径是/usr/local/redis/

这段代码就是redis根目录 /utils/redis_init_script 启动脚本的代码

我这边是用了install_server.sh自动生成的,所以不用改那么多东西,直接用就行

直接chmod + x /etc/init.d/redis

然后systemctl start redis_6379就可以启动了

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

EXEC=/usr/local/redis/src/redis-server
CLIEXEC=/usr/local/redis/src/redis-cli
PIDFILE=/var/run/redis_6379.pid
CONF="/etc/redis/6379.conf"
REDISPORT="6379"
###############
# SysV Init Information
# chkconfig: - 58 74
# description: redis_6379 is the redis daemon.
### BEGIN INIT INFO
# Provides: redis_6379
# 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 redis_6379
# Description: Redis daemon
### END INIT INFO


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
        ;;
    status)
        PID=$(cat $PIDFILE)
        if [ ! -x /proc/${PID} ]
        then
            echo 'Redis is not running'
        else
            echo "Redis is running ($PID)"
        fi
        ;;
    restart)
        $0 stop
        $0 start
        ;;
    *)
        echo "Please use start, stop, restart or status as first argument"
        ;;
esac

配置密码和远程访问

远程访问

在配置文件/etc/redis.conf中方的61行左右

image-20200625115124912

配置密码

在配置文件/etc/redis.conf的480行左右,找到# requirepass foobared在其下面加上一行,加上一行requirepass 123456 设置密码为123456

image-20200625114849911

注意!!!

远程连接一定要开放防火墙的端口,或者直接关闭防火墙(不建议)

# 开放6379端口
firewall-cmd --permanent --add-port=6379/tcp
#重新加载配置
firewall-cmd --reload

有可能遇到的问题

You need tcl 8.5 or newer in order to run the Redis test

解决方式

1、安装tcl

yum install tcl

编译报错系列

原文地址:https://www.cnblogs.com/cuianbing/p/13730669.html