Centos 7.5安装 Redis 5.0.0

1 我的环境

 1.1 linux(腾讯云)

CentOS Linux release 7.5.1804 (Core)

 1.2 Redis

Redis 5.0.0

2 下载

官网

官网下载地址

3 上传

 3.1 使用xftp上传到指定目录,我的目录为

/app/tool

4 解压到安装目录

mkdir /usr/local/redis

tar zxf redis-5.0.0.tar.gz -C /usr/local/redis/

5 安装 gcc 依赖

yum -y install gcc

6 编译

 6.1 进入redis解压目录

cd /usr/local/redis/redis-5.0.0

 6.2 编译

make MALLOC=libc

7 安装

 7.1 进入redis/src下

cd /usr/local/redis/redis-5.0.0/src

 7.2 执行安装

make install

8 配置(后台启动,设置密码,外部访问)

 8.1 修改配置文件

vim /usr/local/redis/redis-5.0.0/redis.conf

进入vim 输入 / 再按火车用来搜索关键字

比如如下命令会定位到文件中出现 bind 的位置并会将关键词用颜色区分。

/bind

 8.2 后台启动

搜索 daemonize no 改为 daemonize yes

daemonize yes

 8.3 设置密码

搜索 requirepass foobared 添加 requirepass 你设置的密码

requirepass foobared

 8.4 外部访问

搜索 bind 127.0.0.1 然后注释掉

# bind 127.0.0.1

9 启动,停止和重启

 9.1 启动

  • 在任意目录执行

    redis-server /usr/local/redis/redis-5.0.0/redis.conf

  • 启动成功界面和进程

    ps aux | grep redis

  • 连接redis
    在任意目录下执行 redis-cli 即可连接

    redis -cli

auth 密码

 9.2 停止

  • 未设置密码时

    redis-cli shutdown

  • 设置密码后,执行以下命令

    redis-cli -a 密码

  • 再执行停止和退出连接

    shutdown
    not connected> exit

 9.3 重启

一般重启都是杀掉进程,再启动redis,这里就介绍一下步骤,当然也可以执行上面的停止再启动

ps aux | grep redis

[root@VM_0_9_centos /]# ps aux | grep redis
root     29228  0.0  0.1 144004  2016 ?        Ssl  14:22   0:00 redis-server *:6379
root     29259  0.0  0.0 112704   972 pts/2    S+   14:22   0:00 grep --color=auto redis

kill -9 29228

ps aux | grep redis

[root@VM_0_9_centos /]# ps aux | grep redis
root     29538  0.0  0.0 112704   968 pts/2    S+   14:25   0:00 grep --color=auto redis

10 开机启动(可以忽略)

 10.1 复制redis服务文件 redis_init_script

cp /usr/local/redis/redis-5.0.0/utils/redis_init_script /etc/init.d/redis

 10.2 在 etc 下创建 redis 文件夹

mkdir /etc/redis

 10.3 复制redis配置文件 redis.conf 并重命名为6379.conf

cp /usr/local/redis/redis-5.0.0/redis.conf /etc/redis/

mv /etc/redis/redis.conf 6379.conf

 10.3 修改redis服务文件

vim /etc/init.d/redis

  • 在/etc/init.d/redis文件的头部添加下面两行注释代码,也就是在文件中#!/bin/sh的下方添加
# chkconfig: 2345 10 90  
# description: Start and Stop redis
  • 指定redis的安装路径和pid文件路径
REDISPORT=6379
EXEC=/usr/local/redis/redis-5.0.0/src/redis-server  # 指定的执行路径
CLIEXEC=/usr/local/redis/redis-5.0.0/src/redis-cli

PIDFILE=/var/run/redis_${REDISPORT}.pid
CONF="/etc/redis/${REDISPORT}.conf" # 这里就是上面重命名的配置文件
  • 若设置了密码,则需要修改stop脚本
stop)
        if [ ! -f $PIDFILE ]
        then
                echo "$PIDFILE does not exist, process is not running"
        else
                PID=$(cat $PIDFILE)
                echo "Stopping ..."
                $CLIEXEC -a 你的密码 -p $REDISPORT shutdown
                while [ -x /proc/${PID} ]
                do
                    echo "Waiting for Redis to shutdown ..."
                    sleep 1
                done
                echo "Redis stopped"
        fi
        ;;
  • 停止输出结果
Stopping ...
Warning: Using a password with '-a' or '-u' option on the command line interface may not be safe.
Redis stopped

 10.4 现在的redis命令

  • 打开redis命令

    service redis start

  • 关闭redis命令

    service redis stop

  • 设为开机启动

    chkconfig redis on

  • 设为开机关闭

    chkconfig redis off

猜你喜欢

转载自www.cnblogs.com/hsbt2333/p/9929377.html