Redis

2020-04-04

原理

Linux安装

  1. https://redis.io/ 下载最新版本。

  2. 上传并解压redis

    1
    [root@iZ2ze7s2v0b78922wia32rZ software]# tar -zvxf redis-5.0.7.tar.gz
  3. 安装redis依赖

    1
    [root@iZ2ze7s2v0b78922wia32rZ redis-5.0.7]# yum install gcc-c++
  4. 生成redis & config

    1
    2
    3
    4
    5
    6
    7
    8
    [root@iZ2ze7s2v0b78922wia32rZ redis-5.0.7]# make & make install
    ...
    [root@iZ2ze7s2v0b78922wia32rZ redis-5.0.7]# cd utils/
    ...
    [root@iZ2ze7s2v0b78922wia32rZ utils]# cp redis_init_script /etc/init.d/
    ...
    [root@iZ2ze7s2v0b78922wia32rZ redis-5.0.7]# mkdir /usr/local/redis -p
    [root@iZ2ze7s2v0b78922wia32rZ redis-5.0.7]# cp redis.conf /usr/local/redis/
  5. 配置redis.conf

    1. 修改daemonize nodaemonize yes该属性表示redis在后台运行,而不是前端控制台运行
    2. 修改bind 127.0.0.1bind 0.0.0.0 (表示redis可以远程调用)
    3. 修改dir /usr/local/redis/workingdb (更改redis工作目录)
    4. 修改requirepass (默认密码是不启用的,启用密码并修改)
  6. 修改/etc/init.d路径下的启动脚本文件

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    [root@iZ2ze7s2v0b78922wia32rZ init.d]# vim redis_init_script 
    #!/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/bin/redis-server
    CLIEXEC=/usr/local/bin/redis-cli

    PIDFILE=/var/run/redis_${REDISPORT}.pid
    #主要是修改这个配置文件路径
    CONF="/usr/local/redis/redis.conf"
    ...
  7. 启动redis

    1
    [root@iZ2ze7s2v0b78922wia32rZ init.d]# ./redis_init_script start

配置重启自启动

  1. /etc/init.d路径下的启动脚本文件中添加#chkconfig: 22345 10 90 &#description: Start and Stop redis
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
[root@iZ2ze7s2v0b78922wia32rZ init.d]# vim redis_init_script 
#!/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

#chkconfig: 22345 10 90
#description: Start and Stop redis

REDISPORT=6379
EXEC=/usr/local/bin/redis-server
CLIEXEC=/usr/local/bin/redis-cli

PIDFILE=/var/run/redis_${REDISPORT}.pid
CONF="/usr/local/redis/redis.conf"
  1. 执行chkconfig redis_init_script on,启动配置