Redis 安装

作者: 书上得来终觉浅 | 来源:发表于2018-11-05 18:16 被阅读16次

    安装Redis

    #root用户导航到安装目录
    $ cd /home/software
    #下载redis源文件,解压并编译
    $ wget http://download.redis.io/releases/redis-5.0.0.tar.gz
    $ tar xzf redis-5.0.0.tar.gz
    $ cd redis-5.0.0
    $ make
    # 查看编译后的文件目录结构(和编译前的目录结构一样)
    $ ll -h
    # 总用量 232K
    # -rw-rw-r--  1 root root  74K 10月 17 23:31 00-RELEASENOTES
    # -rw-rw-r--  1 root root   53 10月 17 23:31 BUGS
    # -rw-rw-r--  1 root root 1.9K 10月 17 23:31 CONTRIBUTING
    # -rw-rw-r--  1 root root 1.5K 10月 17 23:31 COPYING
    # drwxrwxr-x  6 root root 4.0K 11月  5 10:06 deps
    # -rw-rw-r--  1 root root   11 10月 17 23:31 INSTALL
    # -rw-rw-r--  1 root root  151 10月 17 23:31 Makefile
    # -rw-rw-r--  1 root root 4.2K 10月 17 23:31 MANIFESTO
    # -rw-rw-r--  1 root root  21K 10月 17 23:31 README.md
    # -rw-rw-r--  1 root root  61K 10月 17 23:31 redis.conf
    # -rwxrwxr-x  1 root root  275 10月 17 23:31 runtest
    # -rwxrwxr-x  1 root root  280 10月 17 23:31 runtest-cluster
    # -rwxrwxr-x  1 root root  281 10月 17 23:31 runtest-sentinel
    # -rw-rw-r--  1 root root 9.5K 10月 17 23:31 sentinel.conf
    # drwxrwxr-x  3 root root 4.0K 11月  5 10:07 src
    # drwxrwxr-x 10 root root 4.0K 10月 17 23:31 tests
    # drwxrwxr-x  8 root root 4.0K 10月 17 23:31 utils
    
    # 将Redis安装到/usr/local/bin目录中
    $ make install
    
    # 查看/usr/local/bin目录中的文件
    $ ll -h /usr/local/bin/
    # -rwxr-xr-x  1 root   root  12M 11月  5 11:35 redis-server     Redis 服务器
    # -rwxr-xr-x  1 root   root 9.2M 11月  5 11:35 redis-cli        Redis命令行客户端
    # -rwxr-xr-x  1 root   root 8.8M 11月  5 11:35 redis-benchmark  Redis性能测试工具
    # -rwxr-xr-x  1 root   root  12M 11月  5 11:35 redis-check-aof  AOF文件修复工具
    # -rwxr-xr-x  1 root   root  12M 11月  5 11:35 redis-check-rdb  RDBr文件检查工具
    # lrwxrwxrwx  1 root   root   12 11月  5 11:35 redis-sentinel -> redis-server
    

    启动/关闭redis

    启动Redis有两种方式,直接启动(开发环境)和通过脚本启动(生产环境)

    直接启动Redis

    # 直接启动Redis
    # $ redis-server #使用make install后可以这样启动Redis,因为 /usr/local/bin目录下有该执行程序
    # 可以通过 redis-server --port 6380 指定端口
    $ src/redis-server
    
    # Redis 开始启动
    #oO0OoO0OoO0Oo Redis is starting oO0OoO0OoO0Oo
    
    # Redis 已启动
    #Redis version=5.0.0, bits=64, commit=00000000, modified=0, pid=4461, just started
    
    # 警告:Redis使用默认的配置文件,使用 ./redis-server <path> 命令启动指定的配置
    # Warning: no config file specified, using the default config. In order to specify a config file use ./redis-server /path/to/redis.conf
    
    # 打开文件的最大数量增加到10032(原始设置为1024)
    # Increased maximum number of open files to 10032 (it was originally set to 1024).
    #                _._
    #           _.-``__ ''-._
    #      _.-``    `.  `_.  ''-._           Redis 5.0.0 (00000000/0) 64 bit
    #  .-`` .-```.  ```\/    _.,_ ''-._
    # (    '      ,       .-`  | `,    )     Running in standalone mode(独立模式)
    # |`-._`-...-` __...-.``-._|'` _.-'|     Port: 6379 (Redis的默认端口)
    # |    `-._   `._    /     _.-'    |     PID: 4461
    #  `-._    `-._  `-./  _.-'    _.-'
    # |`-._`-._    `-.__.-'    _.-'_.-'|
    # |    `-._`-._        _.-'_.-'    |           http://redis.io
    #  `-._    `-._`-.__.-'_.-'    _.-'
    # |`-._`-._    `-.__.-'    _.-'_.-'|
    # |    `-._`-._        _.-'_.-'    |
    #  `-._    `-._`-.__.-'_.-'    _.-'
    #      `-._    `-.__.-'    _.-'
    #          `-._        _.-'
    #              `-.__.-'
    #
    
    # 参见后面 Linux默认配置下Redis警告
    #WARNING: The TCP backlog setting of 511 cannot be enforced because /proc/sys/net/core/somaxconn is set to the lower value of 128.
    
    # 服务器初始化
    # Server initialized
    
    # 参见后面 Linux默认配置下Redis警告
    # WARNING overcommit_memory is set to 0! Background save may fail under low memory condition. To fix this issue add 'vm.overcommit_memory = 1' to /etc/sysctl.conf and then reboot or run the command 'sysctl vm.overcommit_memory=1' for this to take effect.
    
    # 参见后面 Linux默认配置下Redis警告
    #WARNING you have Transparent Huge Pages (THP) support enabled in your kernel. This will create latency and memory usage issues with Redis. To fix this issue run the command 'echo never > /sys/kernel/mm/transparent_hugepage/enabled' as root, and add it to your /etc/rc.local in order to retain the setting after a reboot. Redis must be restarted after THP is disabled.
    
    # 已准备好接收连接
    #Ready to accept connections
    

    使用脚本启动redis

    /redis/utils 工具箱目录中,有一个redis_init_script脚本文件,它是Redis启动脚本的模板,内容如下:

    #!/bin/sh
    #
    
    # redis监听端口,要与配置文件的port保持一致
    REDISPORT=6379
    # 执行程序位置,使用make install安装后,redis-server和redis-cli将被安装到该目录
    EXEC=/usr/local/bin/redis-server
    CLIEXEC=/usr/local/bin/redis-cli
    
    # pidfile的位置,以端口号作为文件的全称,pidfile防止启动多个redis进程
    PIDFILE=/var/run/redis_${REDISPORT}.pid
    #配置文件的位置
    CONF="/etc/redis/${REDISPORT}.conf"
    
    case "$1" in
        # redis_init_script start流程
        start)
            if [ -f $PIDFILE ]
            then
                    echo "$PIDFILE exists, process is already running or crashed"
            else
                    echo "Starting Redis server..."
                    $EXEC $CONF
            fi
            ;;
        # redis_init_script stop流程
        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
    

    按照上面启动脚本文件所说,我们需要将配置文件放在/etc/redis/目录下,并以端口号重命名。redis为我们准备了一个配置文件的模板,redis更目录下的redis.conf。完成后的目录是这样的:

    • /redis/utils/redis_init_script --> /etc/init.d/redis_${REDISPORT}
    • /redis/redis.conf --> /etc/redis/${REDISPORT}.conf(要与启动脚本文件中的配置一致)

    注意: 这里介绍了两个文件一个是启动脚本文件,一个是配置文件(参见后面的内容)

    修改部分配置文件(etc/redis/${REDISPORT}.conf)

    # 修改etc/redis/${REDISPORT}.conf配置文件的部分内容
    
    # 设置访问ip,这里是只允许本机访问,如果要让其他机器访问,可使用bind 0.0.0.0
    bind 127.0.0.1
    # 开启守护进程(后台运行)
    daemonize yes
    # pidfile文件(与启停脚本要一致)
    pidfile /var/run/redis_6379.pid
    # 数据库文件的地址(需要手动创建该目录)
    # mkdir -p /var/redis/6379
    dir /var/redis/6379
    

    在设置完成后,使用 /etc/init.d/redis_6379 start 启动redis,使用redis-cli连接redis数据库。

    设置开机启动

    # 将init.d目录中的redis_6379脚本设备开机启动
    chkconfig redis_6379 on
    # 检查是否为开机启动
    chkconfig --list | grep redis
    # redis_6379     0:关闭   1:关闭    2:启用    3:启用    4:启用    5:启用    6:关闭
    

    使用脚本手动启停redis

    # 启动
    $ /etc/init.d/redis_6379 start
    
    # 用脚本停止redis
    $ /etc/init.d/redis_6379 stop
    # 用户客户端停止redis
    $ redis-cli shutdown
    
    

    其他说明:

    1. pid文件:Redis进程在启动后会尝试给/var/run/redis_${REDISPORT}.pid文件加锁,只有获取该锁(写入权限(F_WRLCK))的进程才能正常启动,同时把自身的pid写入该文件中,这样就可以防止Linux系统中启动多个Redis

    2. init.d目录,在Linux中,/etc/init.d目录包含了各种服务的启停脚本(本文中我们将redis的启停脚本redis_${REDISPORT}放在了这里)

    Linux默认配置下Redis的警告问题

    要Redis正常运行,需要配置一些内核参数

    WARNING: The TCP backlog setting of 511 cannot be enforced because /proc/sys/net/core/somaxconn is set to the lower value of 128.

    TCP backlog是参数表示TCP 连接队列的大小,默认是128,redis在每秒请求数很高的环境下,为了避免客户端连接慢的问题,需要一个较高的backlog值,参考理解 Linux backlog/somaxconn 内核参数

    # 查看内核somaxconn的连接长度 默认128
    $ cat /proc/sys/net/core/somaxconn
    
    # sysctl命令可在内核运行时,动态地修改内核的运行参数,参考http://man.linuxde.net/sysctl
    # 设置somaxconn参数
    $ sysctl -w net.core.somaxconn=512
    

    WARNING overcommit_memory is set to 0! Background save may fail under low memory condition. To fix this issue add 'vm.overcommit_memory = 1' to /etc/sysctl.conf and then reboot or run the command 'sysctl vm.overcommit_memory=1' for this to take effect.

    参考:sysctl 中 vm.overcommit_memory 的含义,配置 vm.overcommit_memory 为1,可以避免数据被截断

    # 查看vm.overcommit_memory的值
    $sysctl -a |grep vm.over*
    # 设置 vm.overcommit_memory
    $sysctl -w vm.overcommit_memory=1
    

    WARNING you have Transparent Huge Pages (THP) support enabled in your kernel. This will create latency and memory usage issues with Redis. To fix this issue run the command 'echo never > /sys/kernel/mm/transparent_hugepage/enabled' as root, and add it to your /etc/rc.local in order to retain the setting after a reboot. Redis must be restarted after THP is disabled.

    取消对透明巨页内存(transparent huge pages)的支持,因为这会造成 redis 使用过程产生延时和内存访问问题。

    $echo never > /sys/kernel/mm/transparent_hugepage/enabled
    

    简要说明

    启停脚本文件:/etc/init.d/redis_6379
    配置文件:/etc/redis/6379.conf
    数据文件:/var/redis/6379
    端口:6379
    执行文件地址:/usr/local/bin

    相关文章

      网友评论

        本文标题:Redis 安装

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