美文网首页
安装Redis

安装Redis

作者: 吟风者 | 来源:发表于2018-11-29 14:22 被阅读0次

    1 解压Redis

    创建一个redis的文件夹,用户存放redis文件

    mkdir /usr/local/redis
    

    通过ftp工具把下载好的redis安装包上传到redis文件夹下。

    解压:

    cd /usr/local/redis
    tar -vxzf redis-3.2.10.tar.gz
    

    解压完成

    1.png

    2 编译

    cd redis-3.2.10
    make
    

    如果编译失败,请检查是否安装了gcc环境,未安装环境,请先安装:

    apt-get install gcc
    

    清除上次编译失败的文件:

    make clean
    

    3 安装

    make install
    

    4 启动

    cd src
    ./redis-server
    

    查看是否启动

    ps -aux|grep redis
    

    测试

    ./redis-cli
    redis> set foo bar
    OK
    redis> get foo
    "bar"
    

    5 修改配置

    进入redis根目录:

    cd /etc/local/redis/ redis-3.2.10
    

    编辑配置文件:

    vim redis.conf
    

    远程访问

    注释掉bind 127.0.0.1可以使所有的ip访问redis

    若是想指定多个ip访问,但并不是全部的ip访问,可以bind

    在redis3.2之后,redis增加了protected-mode,在这个模式下,即使注释掉了bind 127.0.0.1,再访问redisd时候还是报错,如下:

    (error) DENIED Redis is running in protected mode because protected mode is enabled, no bind address 
    was specified, no authentication password is requested to clients. In this mode connections are only 
    accepted from the loopback interface. If you want to connect from external computers to Redis you may 
    adopt one of the following solutions: 1) Just disable protected mode sending the command 'CONFIG SET 
    protected-mode no' from the loopback interface by connecting to Redis from the same host the server is 
    running, however MAKE SURE Redis is not publicly accessible from internet if you do so. Use CONFIG 
    REWRITE to make this change permanent. 2) Alternatively you can just disable the protected mode by 
    editing the Redis configuration file, and setting the protected mode option to 'no', and then restarting the 
    server. 3) If you started the server manually just for testing, restart it with the '--protected-mode no' option. 4) 
    Setup a bind address or an authentication password. NOTE: You only need to do one of the above things in 
    order for the server to start accepting connections from the outside.
    

    修改办法:protected-mode no

    设置为守护进程后台启动

    修改:daemonize no

    使用redis账号访问

    默认情况下,访问Redis服务器是不需要密码的,为了增加安全性,设置Redis服务器的访问密码,取消requirepass前的注释#,并设置密码

    requirepass 123456

    1.6 设置开机启动

    在redis目录下找到 utils/redis_init_script 复制到 /etc/init.d/redis 打开文件进行修改

    cp redis_init_script /etc/init.d/redis   # 复制文件
    cd /etc/init.d/                         # 进入文件目录
    vim redis                              #  编辑配置文件redis
    

    原配置文件

    #!/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="/etc/redis/${REDISPORT}.conf"
    

    修改后:

    #!/bin/sh
    #
    # chkconfig:   2345 90 10   ---这里修改
    # description:  Redis is a persistent key-value database   ---这里修改
    
    ### BEGIN INIT INFO
    # Provides:     redis_6379
    # Required-Start:          ---这里修改
    # Required-Stop:           ---这里修改            
    # 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="/etc/redis/redis.conf"  # ---这里修改
    

    修改完配置文件后执行下面两条命令

    chmod +x /etc/init.d/redis  # 取得权限
    update-rc.d redis defaults  # 加载到系统自启动文件
    

    其他命令:

    service redis start     #启动服务 
    service redis stop      #停止服务
    service redis restart       #重启服务
    

    重启电脑,查看redis是否启动

    相关文章

      网友评论

          本文标题:安装Redis

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