美文网首页
linux安装redis 并设置为系统服务(service方式管

linux安装redis 并设置为系统服务(service方式管

作者: 一个傻乎乎的少年 | 来源:发表于2019-01-07 16:15 被阅读0次

    小明的个人博客

    1:linux下执行以下命令

    wget http://download.redis.io/releases/redis-2.8.17.tar.gz
    tar xzf redis-2.8.17.tar.gz
    cd redis-2.8.17
    make
    

    执行make命令如果出现 " jemalloc/jemalloc.h: No such file or directory"错误, 带上参数 MALLOC=libc

    make MALLOC=libc
    

    这里使用redis2.8.17为例,如果需要下载其他版本请点击我进入列表页

    2:修改配置文件

    make完成后,目录结构如下

    [root@localhost redis-2.8.17]# ll
    总用量 132
    -rw-rw-r-- 1 root root 27587 9月  19 2014 00-RELEASENOTES
    -rw-rw-r-- 1 root root    52 9月  19 2014 BUGS
    -rw-rw-r-- 1 root root  1439 9月  19 2014 CONTRIBUTING
    -rw-rw-r-- 1 root root  1487 9月  19 2014 COPYING
    drwxrwxr-x 6 root root  4096 1月   7 02:15 deps
    -rw-rw-r-- 1 root root    11 9月  19 2014 INSTALL
    -rw-rw-r-- 1 root root   151 9月  19 2014 Makefile
    -rw-rw-r-- 1 root root  4223 9月  19 2014 MANIFESTO
    -rw-rw-r-- 1 root root  4404 9月  19 2014 README
    -rw-rw-r-- 1 root root 34017 9月  19 2014 redis.conf
    -rwxrwxr-x 1 root root   271 9月  19 2014 runtest
    -rwxrwxr-x 1 root root   281 9月  19 2014 runtest-sentinel
    -rw-rw-r-- 1 root root  7110 9月  19 2014 sentinel.conf
    drwxrwxr-x 2 root root  4096 1月   7 02:20 src
    drwxrwxr-x 9 root root  4096 9月  19 2014 tests
    drwxrwxr-x 3 root root  4096 9月  19 2014 utils
    

    进入src目录,测试是否能启动成功,如下图表明启动成功

    [root@localhost redis-2.8.17]# cd src
    [root@localhost src]# ./redis-server 
    [32056] 07 Jan 02:44:37.235 # Warning: no config file specified, using the default config. In order to specify a config file use ./redis-server /path/to/redis.conf
    [32056] 07 Jan 02:44:37.235 * Increased maximum number of open files to 10032 (it was originally set to 1024).
                    _._                                                  
               _.-``__ ''-._                                             
          _.-``    `.  `_.  ''-._           Redis 2.8.17 (00000000/0) 64 bit
      .-`` .-```.  ```\/    _.,_ ''-._                                   
     (    '      ,       .-`  | `,    )     Running in stand alone mode
     |`-._`-...-` __...-.``-._|'` _.-'|     Port: 6379
     |    `-._   `._    /     _.-'    |     PID: 32056
      `-._    `-._  `-./  _.-'    _.-'                                   
     |`-._`-._    `-.__.-'    _.-'_.-'|                                  
     |    `-._`-._        _.-'_.-'    |           http://redis.io        
      `-._    `-._`-.__.-'_.-'    _.-'                                   
     |`-._`-._    `-.__.-'    _.-'_.-'|                                  
     |    `-._`-._        _.-'_.-'    |                                  
      `-._    `-._`-.__.-'_.-'    _.-'                                   
          `-._    `-.__.-'    _.-'                                       
              `-._        _.-'                                           
                  `-.__.-'                                               
    
    [32056] 07 Jan 02:44:37.243 # Server started, Redis version 2.8.17
    [32056] 07 Jan 02:44:37.243 # 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.
    [32056] 07 Jan 02:44:37.243 * DB loaded from disk: 0.000 seconds
    [32056] 07 Jan 02:44:37.243 * The server is now ready to accept connections on port 6379
    

    3:将redis使用service进行管理

    进入redis/utils目录,将redis_init_script拷贝到/etc/rc.d/init.d/目录下,具体操作如下

    [root@localhost redis-2.8.17]# cd utils/
    [root@localhost utils]# cp redis_init_script /etc/rc.d/init.d/
    

    重命名为redis

    mv redis_init_script redis
    

    修改脚本配置文件,打开redis进行修改,注意下图中注释的地方,都是需要修改的地方

    [root@localhost utils]# cd /etc/rc.d/init.d/
    [root@localhost init.d]# vim redis 
    #!/bin/sh
    #
    # chkconfig: 2345 80 90
    # Simple Redis init.d script conceived to work on Linux systems
    # as it does use of the /proc filesystem.
    
    REDISPORT=6379     //默认启动端口 可以自定义
    EXEC=/data/redis-2.8.17/src/redis-server   //这里需要自己设置redis的安装目录
    CLIEXEC=/data/redis-2.8.17/src/redis-cli   //这里需要自己设置redis的安装目录
    
    PIDFILE=/var/run/redis_${REDISPORT}.pid
    CONF="/data/redis-2.8.17/redis.conf"  //  启动配置文件,如果你的redis.conf在/etc/redis下,自行修改这儿的路径
    
    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
    

    4:将redis添加到系统服务

    chkconfig -add redis
    

    5:启动

    [root@localhost init.d]# service redis start
    Starting Redis server...
    [root@localhost init.d]# [32625] 07 Jan 02:56:09.679 * Increased maximum number of open files to 10032 (it was originally set to 1024).
                    _._                                                  
               _.-``__ ''-._                                             
          _.-``    `.  `_.  ''-._           Redis 2.8.17 (00000000/0) 64 bit
      .-`` .-```.  ```\/    _.,_ ''-._                                   
     (    '      ,       .-`  | `,    )     Running in stand alone mode
     |`-._`-...-` __...-.``-._|'` _.-'|     Port: 6379
     |    `-._   `._    /     _.-'    |     PID: 32625
      `-._    `-._  `-./  _.-'    _.-'                                   
     |`-._`-._    `-.__.-'    _.-'_.-'|                                  
     |    `-._`-._        _.-'_.-'    |           http://redis.io        
      `-._    `-._`-.__.-'_.-'    _.-'                                   
     |`-._`-._    `-.__.-'    _.-'_.-'|                                  
     |    `-._`-._        _.-'_.-'    |                                  
      `-._    `-._`-.__.-'_.-'    _.-'                                   
          `-._    `-.__.-'    _.-'                                       
              `-._        _.-'                                           
                  `-.__.-'                                               
    
    [32625] 07 Jan 02:56:09.683 # Server started, Redis version 2.8.17
    [32625] 07 Jan 02:56:09.683 # 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.
    [32625] 07 Jan 02:56:09.683 * The server is now ready to accept connections on port 6379
    

    启动成功

    6:redis设置密码

    修改redis.conf,找到requirepass字段,修改后面的值即可,保存后重启即可

    [root@localhost init.d]# cd /data/redis-2.8.17/
    [root@localhost redis-2.8.17]# vim redis.conf 
    
    ...
    ################################## SECURITY ###################################
    
    # Require clients to issue AUTH <PASSWORD> before processing any other
    # commands.  This might be useful in environments in which you do not trust
    # others with access to the host running redis-server.
    #
    # This should stay commented out for backward compatibility and because most
    # people do not need auth (e.g. they run their own servers).
    # 
    # Warning: since Redis is pretty fast an outside user can try up to
    # 150k passwords per second against a good box. This means that you should
    # use a very strong password otherwise it will be very easy to break.
    #
    requirepass testpassword    //取消注释 foobared设置成你自己的密码
    
    # Command renaming.
    #
    # It is possible to change the name of dangerous commands in a shared
    # environment. For instance the CONFIG command may be renamed into something
    # hard to guess so that it will still be available for internal-use tools
    # but not available for general clients.
    ...
    
    [root@localhost src]# ./redis-cli 
    127.0.0.1:6379> select 0
    (error) NOAUTH Authentication required.
    127.0.0.1:6379> set 2 1
    (error) NOAUTH Authentication required.
    127.0.0.1:6379> 
    

    可以看到现在想操作redis的话,已经提示需要身份验证了

    127.0.0.1:6379> auth chen19960119
    OK
    127.0.0.1:6379> set 2 2
    OK
    127.0.0.1:6379> get 2
    "2"
    127.0.0.1:6379> 
    

    输入auth 你自己设置的密码 回车进行验证

    7:redis开启远程连接

    修改redis.conf,找到bind字段,注释掉即可

    [root@localhost ~]# cd /data/redis-2.8.17/
    [root@localhost redis-2.8.17]# vim redis.conf 
    # By default Redis listens for connections from all the network interfaces
    # available on the server. It is possible to listen to just one or multiple
    # interfaces using the "bind" configuration directive, followed by one or
    # more IP addresses.
    #
    # Examples:
    #
    # bind 192.168.1.100 10.0.0.1   //注释掉
    # bind 127.0.0.1    //注释掉
    
    # Specify the path for the Unix socket that will be used to listen for
    # incoming connections. There is no default, so Redis will not listen
    # on a unix socket when not specified.
    #
    

    如果是redis3.x,则改需要设置protected-mode=no,默认是yes,这个配置也会限制远程访问

    小明的个人博客

    相关文章

      网友评论

          本文标题:linux安装redis 并设置为系统服务(service方式管

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