centos编译安装redis与错误解决

  1. 下载redis
    redis官网下载,然后选择stable下的Download右键复制下载链接,然后通过wget 链接在centos服务器上下载源码。
    centos编译安装redis与错误解决_第1张图片

  2. 编译redis
    解压缩安装包,然后将解压后的目录复制到/usr/local/下,进入该目录,make编译:

    tar -xzvf redis-6.0.6.tar.gz
    sudo mv redis-6.0.6 /usr/local/
    cd /usr/local/redis-6.0.6/
    make
    

报错:
centos编译安装redis与错误解决_第2张图片
参考这篇博文-Centos7 安装 redis 6 编译错误,据说是redis6.0.0后需要c++11支持,而centos7默认是gcc4.8.5,而c++11支持在4.9中引入,需要安装devtoolset7使用gcc7编译:
shell sudo yum install centos-release-scl devtoolset-7 scl enable devtoolset-7 bash make
3. 安装redis——sudo make install
4. 配置redis——参考Centos7安装配置redis与链接-https://blog.csdn.net/qq_39135287/article/details/83474865

  • 为了方便,在redis安装目录下创建两个目录bin(用于放置可执行文件)和etc(用于放置配置文件):
    cd /usr/local/redis-6.0.6/
    mkdir bin
    mkdir etc
    
  • 然后把配置文件拷贝到etc中cp redis.conf etc/6379.conf(文件名称与后面启动脚本里的保持一致),把src下可执行文件拷贝到bin(可进一步设置环境变量path,使在任意目录执行redis相关命令):
    在这里插入图片描述
  • 然后设置开机启动——把utils下的启动脚本拷贝到/etc/init.d/下:sudo cp utils/redis_init_script /etc/init.d/redis_6379
  • 更改启动脚本:sudo vim /etc/init.d/redis_6379
    #!/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
    EXEC=/usr/local/redis-6.0.6/bin/redis-server
    CLIEXEC=/usr/local/redis-6.0.6/bin/redis-cli
    
    PIDFILE=/var/run/redis_${REDISPORT}.pid
    #CONF="/etc/redis/${REDISPORT}.conf"
    CONF="/usr/local/redis-6.0.6/etc/${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
    
    ==>仅修改了EXEC,CLIEXEC和CONF行——EXEC指定redis-server命令的完整路径,也就是/usr/local/redis-6.0.6/bin/redis-server;CLIEXEC则指定redis_cli命令的完整路径,也即/usr/local/redis-6.0.6/bin/redis-cli;CONF指定配置文件完整路径
  • 更改配置文件:vim etc/6379.conf
    # 注释掉bind行以允许外网访问redis
    bind 127.0.0.1
    # 开启保护模式(默认开启)
    protected-mode yes
    # 设置端口号(默认6379)
    port 6379
    # 启用守护进程
    daemonize yes
    # 设定工作目录,指定本地数据库存放目录
    dir /var/redis/6379
    
    上面的工作目录还需要另外创建:sudo mkdir /var/redissudo mkdir /var/redis/6379
  • 设置开机执行redis启动脚本:sudo chkconfig redis_6379 on
  • 启动/停止redis
    service redis_6379 start
    service redis_6379 stop
    
  1. 测试redis
    进入/usr/local/redis-6.0.6/src目录执行redis-cli(如果设置了path变量无需进入src目录即可执行)
    redis_cli
    echo hello
    set a 15
    get a
    

参考:
1.https://blog.csdn.net/qq_39135287/article/details/83474865
2.https://www.limstash.com/articles/202005/1633
3.https://www.cnblogs.com/hankuikui/p/9111210.html

你可能感兴趣的:(linux,centos,redis)