zoukankan      html  css  js  c++  java
  • Redis 学习之常用命令及安全机制

     

    该文使用centos6.5 64位    redis3.2.8 

     

    一、redis常用命令

    键值常用命令:
    1、 keys 返回满足pattern的所有key。
    127.0.0.1:6379> keys my*
    127.0.0.1:6379> keys *
    
    2、exits 确认key是否存在。 返回1表示存在  0表示不存在
    127.0.0.1:6379> exists name
    
    3、del :删除一个键 返回1:删除成功 0:失败
    127.0.0.1:6379> del name
    (integer) 1
    4、expire 设置key(该key必须存在)的过期时间 返回1表示设置成功 0 失败
    5、ttl 查看键的过期时间 如果该键已经过期(销毁)则返回负数
    127.0.0.1:6379> expire myset2 10
    (integer) 1
    127.0.0.1:6379> ttl myset2
    (integer) 4
    127.0.0.1:6379> ttl myset2
    (integer) -2
    6、选择数据库 
    Redis中一共有16个数据库他们分别是0-15
    select 0 表示当前数据库
    7、move 将当前数据库中的key转移到其他数据库中 返回1表示成功 0 失败
    127.0.0.1:6379> move age 1
    (integer) 1
    8、persiste:移除给定key的过期时间 返回1表示取消成功 0 失败
    127.0.0.1:6379[1]> expire age 100
    (integer) 1
    127.0.0.1:6379[1]> ttl age
    (integer) 94
    127.0.0.1:6379[1]> persist age
    (integer) 1
    127.0.0.1:6379[1]> get age
    "25"
    9、randomkey:随机数据库中的一个key
    127.0.0.1:6379[1]> randomkey
    "age"
    10、rename:重命名key
    127.0.0.1:6379[1]> keys *
    1) "age"
    127.0.0.1:6379[1]> rename age age_now
    OK
    127.0.0.1:6379[1]> keys *
    1) "age_now"
    11、返回key的数据类型
    127.0.0.1:6379> type height
    string
    127.0.0.1:6379> type myzset1
    Zset
    服务器相关命令
    1、Ping :测试链接redis是否存活 返回 PONG 表示链接存活
    127.0.0.1:6379> ping
    PONG
    2、echo :在命令行打印数据
    127.0.0.1:6379> echo hello
    "hello"
    3、select 选择数据库。Redis数据库编号是0-15,我们可以选择任意一个数据库进行数据存储
    127.0.0.1:6379> select 0
    OK
    127.0.0.1:6379> select 15
    OK
    127.0.0.1:6379[15]> select 16
    (error) ERR invalid DB index
    127.0.0.1:6379[15]>
    4、quit 退出客户端与redis服务器的连接
    5、dbsize 返回当前数据库中key的数量
    127.0.0.1:6379> dbsize
    (integer) 15
    6、info 获取redis服务的相关信息和统计
    127.0.0.1:6379> info
    7、config get 查看redis服务器相关配置参数
    127.0.0.1:6379> config get *
    127.0.0.1:6379> config get port
    1) "port"
    2) "6379"
    7、flushdb 删除当前数据库中所有的key
    127.0.0.1:6379[1]> keys *
    1) "age_now"
    127.0.0.1:6379[1]> flushdb
    OK
    127.0.0.1:6379[1]> keys *
    (empty list or set)
    8、删除所有数据库中所有的key
    127.0.0.1:6379[1]> flushall

    二、 redis安全机制

    设置redis连接密码

    Redis速度很快,所以在一台比较好的服务起下,一个外部的用户可以在1秒钟进行150k次的密码尝试连接,所以我们需要设置非常强大的密码来防止暴力破解。

    设置密码:在redis的配置文件中 添加requirepass password

    ################################## 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
    requirepass jalja

    重启redis服务器:[root@localhost bin]#  ./redis-server /usr/local/redis/etc/redis.conf

    1、测试密码是否可用:

    [root@localhost bin]# ./redis-cli

    127.0.0.1:6379> keys *

    (error) NOAUTH Authentication required.(操作被拒绝)

    使用密码(授权):auth password

    127.0.0.1:6379> auth jalja

    OK

    2、使用密码登录redis服务器:[root@localhost bin]# ./redis-cli -a jalja

     

  • 相关阅读:
    浅谈MVVM
    组装数据和页面渲染
    Vuejs实现轮播图
    Vuejs选项卡案例
    选项卡案例
    js中用来操作数组的相关的方法
    js中用来操作字符串的相关的方法
    用逗号拼接字符串,并且去掉最后一个逗号
    微信分享链接或网站文章到微信朋友圈,缩略图片不显示,该如何解决?
    Spring 的介绍和目标
  • 原文地址:https://www.cnblogs.com/jalja/p/6416309.html
Copyright © 2011-2022 走看看