ubuntu 14.04安装redis

最近公司从阿里云准备迁移到亚马逊云,为此需要在亚马逊上搭建一些基础设施.
这篇文档仅是搭建redis服务器的步骤记录.

前提条件

  • 系统版本 ubuntu14.04
  • 安全组开放访问 6379端口

安装步骤

  • 安装redis
sudo apt-get install redis-server
  • 检测是否启动

    • 检测进程
     ps -ef | grep redis
    

    可以看到:

    redis     1977     1  0 05:51 ?        00:00:00 /usr/bin/redis-server 127.0.0.1:6379       
    ubuntu    1990  1364  0 05:53 pts/0    00:00:00 grep --color=auto redis
    
    • 检测端口监听情况
      netstat -nlt|grep 6379
    

    结果如下:

    tcp        0      0 127.0.0.1:6379          0.0.0.0:*               LISTEN
    
    • 还可以通过redis的命令行工具查看其状态
    sudo /etc/init.d/redis-server status
    

    输出结果为:redis-server is running

修改配置

主要需要修改两个地方,一是绑定ip由本机改为所有,而是设置密码.为了步骤清晰,笔者将两者分开设置,建议读者全部阅读一遍后在一起修改配置文件.

  • 修改绑定地址
    从上面可以看到,安装后redis默认启动后监听127.0.0.1,也就是只可以本机访问.
    现在来修改地址使其监听所有地址:
    1. vim 打开配置文件

      sudo vim /etc/redis/redis.conf
      
    2. 找到以下位置:

       # 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
      
    3. bind 127.0.0.1 注释,即在这一句前面加个 #,修改后如下:

       # 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
      
    4. 修改保存后,重启redis:

      sudo /etc/init.d/redis-server restart
      
    5. 再来看一下端口监听情况:

      netstat -nlt|grep 6379
      

      可以看到,已经监听所有ip:

        tcp        0      0 0.0.0.0:6379            0.0.0.0:*               LISTEN     
        tcp6       0      0 :::6379                 :::*                    LISTEN
      
  • 设置密码
    1. vim 打开配置文件

      sudo vim /etc/redis/redis.conf
      
    2. 找到以下位置:

       ################################## 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 foobared
      
       # Command renaming.
      
      
    3. # requirepass foobared 前的#去掉,requirepass foobared里面的foobared 就是密码,您可以根据自自己的需要修改,修改后如下:

      ################################## 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 你的密码
      
      # Command renaming.
      
      
    4. 修改保存后,重启redis:

      sudo /etc/init.d/redis-server restart
      
    5. 使用默认安装的命令行客户端进行测试:

      redis-cli
      

      这会是您进入redis 命令行,屏幕提示:

        127.0.0.1:6379> 
      

      输入 keys *:

        127.0.0.1:6379> keys *
        (error) NOAUTH Authentication required.
      
      

      可以看到,是需要密码才能访问的.
      现在,我们退出然后使用密码再连接一次:

       127.0.0.1:6379> exit
      

      使用密码:

        redis-cli -a 你的密码
      

      输入keys *:

        127.0.0.1:6379> keys *
        (empty list or set)
      

      由于我还没有存储任何东西,所以显示空集.但可以确定,使用密码后就可以访问了.

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • 1 Redis介绍1.1 什么是NoSql为了解决高并发、高可扩展、高可用、大数据存储问题而产生的数据库解决方...
    克鲁德李阅读 5,354评论 0 36
  • 安全性 设置客户端连接后进行任何其他指令前需要使用的密码。 警告:因为redis 速度相当快,所以在一台比较好的服...
    OzanShareing阅读 1,824评论 1 7
  • 注意:部分内容转载来自网络~ 一.安装redis 1)下载redis安装包 可去官网http://redis.io...
    毕业笙_16c9阅读 469评论 0 0
  • 5/15/2017 7:06:35 PM 纵观各大组件,配置文件占据极其重要的地位。可配置化也是当下开发的一流行趋...
    爱做梦的胖子阅读 4,458评论 0 8
  • 感赏昨天我的儿子和女儿陪着我去运动、购物和看电影。左手挽着儿子,右手牵着女儿,走在路上的感觉特好。在电影院放映前,...
    旦子阅读 164评论 0 0