非关系型数据库redis

Redis初始

redis中不区分字母的大小写

查看端口 ss tnlp

  • 安装
    • 编译安装(需要先配置epel源)

      yum install -y redis
      
    • 编译安装

      wget https://download.redis.io/releases/redis-6.2.6.tar.gz
      解压
      tar -zxf redis-4.0.10.tar.gz
      3.切换redis源码目录
      cd redis-4.0.10.tar.gz
      4.编译源文件
      make 
      5.编译好后,src/目录下有编译好的redis指令
      6.make install 安装到指定目录,默认在/usr/local/bin
      
  • redis可执行文件
    ./redis-benchmark //用于进行redis性能测试的工具
    ./redis-check-dump //用于修复出问题的dump.rdb文件
    ./redis-cli //redis的客户端
    ./redis-server //redis的服务端
    ./redis-check-aof //用于修复出问题的AOF文件
    ./redis-sentinel //用于集群管理
    

1、启动redis服务

  • 进入安装目录 默认/usr/local/bin
    执行./redis-server
    redis-server redis.conf
    默认情况下,redis-server会以非daemon的方式来运行,且默认服务端口为6379。
    
  • 启动redis后会默认占用终端口

2、redis连接

  • ./redis-cli
    [wusir@localhost bin]$ ./redis-cli 
    127.0.0.1:6379> ping
    PONG
    127.0.0.1:6379> 
    127.0.0.1:6379> echo 'return pong successful connection'
    "return pong successful connection
    回复pong表示连接成功
    
  • ECHO 与linux的打印效果一致

  • 127.0.0.1:6379> set name 'dog'
    OK
    127.0.0.1:6379> get name
    "dog"
    127.0.0.1:6379>
    

3、常用命令

  • 退出命令
    quit
    127.0.0.1:6379> quit
    [wusir@localhost bin]$ 
    
    
  • del删除命令
    127.0.0.1:6379> SET ZJW SG
    OK
    127.0.0.1:6379> SET QQ HHHH
    OK
    127.0.0.1:6379> DEL name
    (integer) 1
    127.0.0.1:6379> set name
    (error) ERR wrong number of arguments for 'set' command
    127.0.0.1:6379> get name
    (nil)
    127.0.0.1:6379> get zjw
    (nil)
    127.0.0.1:6379> get ZJW
    "SG"
    127.0.0.1:6379> get QQ
    "HHHH"
    127.0.0.1:6379> DEL ZJW QQ
    (integer) 2 删除一个或者多个值
    127.0.0.1:6379> 
    127.0.0.1:6379> del aaa
    (integer) 0 不存在的被忽略掉
    127.0.0.1:6379> 
    
  • exites key
    127.0.0.1:6379> set wusir 666
    OK
    127.0.0.1:6379> exists wusir
    (integer) 1
    127.0.0.1:6379> exists name
    (integer) 0
    
    
  • keys *

    查看所有的key 值

  • keys 匹配规则(正则)

    KEYs a??,

  • expire key 秒

    设置有效时间,

  • TTL key

    查看剩余有效时间,两个特殊值-2key不存在(KEY过期),-1永久有有效,不设置时间

  • pexpire key 毫秒

    设置有效毫秒

  • pttl key

    查看剩余毫秒

  • randomkey

    随机获取一个KEY,但是不删除,数据库为空返回nil

  • move key db

    将数据转移至其他的数据库

  • rename

    重命名KEY,如果不存在则报错

    rename key newkey,如果已有key,则直接覆盖

  • 127.0.0.1:6379> set name helloworld
    OK
    127.0.0.1:6379> keys *
    1) "wusir"
    2) "name"
    127.0.0.1:6379> set hh 12345
    OK
    127.0.0.1:6379> expire name 10
    (integer) 1
    127.0.0.1:6379> ttl name
    (integer) 5
    127.0.0.1:6379> ttl name
    (integer) 2
    127.0.0.1:6379> ttl name
    (integer) -2
    127.0.0.1:6379> ttl wusir
    (integer) -1
    127.0.0.1:6379> keys *
    1) "wusir"
    2) "hh"
    127.0.0.1:6379> keys h?
    1) "hh"
    127.0.0.1:6379> pexpire hh 9000
    (integer) 1
    127.0.0.1:6379> pttl hh
    (integer) 3405
    127.0.0.1:6379> pttl
    (error) ERR wrong number of arguments for 'pttl' command
    127.0.0.1:6379> pttl hh
    (integer) -2
    127.0.0.1:6379> pttl wusir
    (integer) -1
    127.0.0.1:6379> set zjw hello
    OK
    127.0.0.1:6379> keys
    (error) ERR wrong number of arguments for 'keys' command
    127.0.0.1:6379> keys *
    1) "wusir"
    2) "zjw"
    127.0.0.1:6379> set www fff
    OK
    127.0.0.1:6379> set ggg ccc
    OK
    127.0.0.1:6379> keys *
    1) "ggg"
    2) "wusir"
    3) "www"
    4) "zjw"
    127.0.0.1:6379> set key valse
    OK
    127.0.0.1:6379> randromkey
    (error) ERR unknown command `randromkey`, with args beginning with: 
    127.0.0.1:6379> randomkey
    "www"
    127.0.0.1:6379> randomkey
    "ggg"
    127.0.0.1:6379> randomkey
    "www"
    127.0.0.1:6379> 
    127.0.0.1:6379> 
    127.0.0.1:6379> randomkey
    "key"
    127.0.0.1:6379> randomkey
    "www"
    127.0.0.1:6379> move www 2
    (integer) 1
    127.0.0.1:6379> move www 2
    (integer) 0
    127.0.0.1:6379> get www
    (nil)
    127.0.0.1:6379> select 2
    OK
    127.0.0.1:6379[2]> get www
    "fff"
    127.0.0.1:6379[2]> select 3
    OK
    127.0.0.1:6379[3]> randomkey
    (nil)
    127.0.0.1:6379[3]> select 0
    OK
    127.0.0.1:6379> keys *
    1) "ggg"
    2) "key"
    3) "zjw"
    4) "wusir"
    127.0.0.1:6379> rename ggg gege
    OK
    127.0.0.1:6379> rename key kkk
    OK
    127.0.0.1:6379> keys *
    1) "kkk"
    2) "zjw"
    3) "gege"
    4) "wusir"
    127.0.0.1:6379> get kkk
    "valse"
    127.0.0.1:6379> get zjw
    "hello"
    127.0.0.1:6379> rename kkk zjw
    OK
    127.0.0.1:6379> get zjw
    "valse"
    127.0.0.1:6379> renamenx gege wusir
    (integer) 0
    127.0.0.1:6379> get wusir
    "666"
    127.0.0.1:6379> get gege
    "ccc"
    127.0.0.1:6379> keys *
    1) "zjw"
    2) "gege"
    3) "wusir"
    127.0.0.1:6379> type gege
    string
    127.0.0.1:6379> type wusir
    string
    127.0.0.1:6379> type kkkkkkk
    none
    127.0.0.1:6379> lpush alexlist dsb dsb2
    (integer) 2
    127.0.0.1:6379> type alexlist
    list
    127.0.0.1:6379> 
    
  • renamenx
    127.0.0.1:6379> keys *
    1) "ggg"
    2) "key"
    3) "zjw"
    4) "wusir"
    127.0.0.1:6379> rename ggg gege
    OK
    127.0.0.1:6379> rename key kkk
    OK
    127.0.0.1:6379> keys *
    1) "kkk"
    2) "zjw"
    3) "gege"
    4) "wusir"
    127.0.0.1:6379> get kkk
    "valse"
    127.0.0.1:6379> get zjw
    "hello"
    127.0.0.1:6379> rename kkk zjw
    OK
    127.0.0.1:6379> get zjw
    "valse"
    127.0.0.1:6379> renamenx gege wusir
    (integer) 0
    127.0.0.1:6379> 
    
    127.0.0.1:6379> get wusir
    "666"
    127.0.0.1:6379> get gege
    "ccc"
    127.0.0.1:6379> keys *
    1) "zjw"
    2) "gege"
    3) "wusir"
    127.0.0.1:6379>
    
  • type

    查看存储value的数据类型,如果键不存在返回None

    127.0.0.1:6379> type gege
    string
    127.0.0.1:6379> type wusir
    string
    127.0.0.1:6379> type kkkkkkk
    none
    127.0.0.1:6379> lpush alexlist dsb dsb2
    (integer) 2
    127.0.0.1:6379> type alexlist
    list
    127.0.0.1:6379> 
    
    
  • select 命令
    SELECT 2;
    默认到2库中;总共有16个库,编号为0到15
    切换redis的库
    
    127.0.0.1:6379> select 2
    OK
    127.0.0.1:6379[2]> select 15
    OK
    127.0.0.1:6379[15]> select 16
    (error) ERR DB index is out of range
    
    127.0.0.1:6379[15]> get name
    (nil)
    127.0.0.1:6379[15]> select 0
    OK
    127.0.0.1:6379> get name
    "dog"
    127.0.0.1:6379> 
    
    
    [wusir@localhost bin]$ ./redis-cli -n 15
    127.0.0.1:6379[15]> 
    启动的时候直接进行到对应库
    
  • info命令获取系统信息
    127.0.0.1:6379> info 不加参数显示所有
    # Server
    redis_version:6.2.6
    redis_git_sha1:00000000
    redis_git_dirty:0
    redis_build_id:c38900315f2f12fd
    redis_mode:standalone
    os:Linux 3.10.0-1160.el7.x86_64 x86_64
    arch_bits:64
    multiplexing_api:epoll
    atomicvar_api:atomic-builtin
    gcc_version:4.8.5
    process_id:8451
    process_supervised:no
    run_id:4bccf2f220d12c142d1514a61119a717b83ecb04
    tcp_port:6379
    server_time_usec:1634866336526570
    uptime_in_seconds:1048
    uptime_in_days:0
    hz:10
    configured_hz:10
    lru_clock:7476384
    executable:/usr/local/bin/./redis-server
    config_file:
    io_threads_active:0
    
    # Clients
    connected_clients:1
    cluster_connections:0
    maxclients:10000
    client_recent_max_input_buffer:24
    client_recent_max_output_buffer:0
    blocked_clients:0
    tracking_clients:0
    clients_in_timeout_table:0
    
    # Memory
    used_memory:873680
    used_memory_human:853.20K
    used_memory_rss:3014656
    used_memory_rss_human:2.88M
    used_memory_peak:931824
    used_memory_peak_human:909.98K
    used_memory_peak_perc:93.76%
    used_memory_overhead:830408
    used_memory_startup:809832
    used_memory_dataset:43272
    used_memory_dataset_perc:67.77%
    allocator_allocated:920808
    allocator_active:1187840
    allocator_resident:3702784
    total_system_memory:1907744768
    total_system_memory_human:1.78G
    used_memory_lua:37888
    used_memory_lua_human:37.00K
    used_memory_scripts:0
    used_memory_scripts_human:0B
    number_of_cached_scripts:0
    maxmemory:0
    maxmemory_human:0B
    maxmemory_policy:noeviction
    allocator_frag_ratio:1.29
    allocator_frag_bytes:267032
    allocator_rss_ratio:3.12
    allocator_rss_bytes:2514944
    rss_overhead_ratio:0.81
    rss_overhead_bytes:-688128
    mem_fragmentation_ratio:3.63
    mem_fragmentation_bytes:2183736
    mem_not_counted_for_evict:0
    mem_replication_backlog:0
    mem_clients_slaves:0
    mem_clients_normal:20504
    mem_aof_buffer:0
    mem_allocator:jemalloc-5.1.0
    active_defrag_running:0
    lazyfree_pending_objects:0
    lazyfreed_objects:0
    
    # Persistence
    loading:0
    current_cow_size:0
    current_cow_size_age:0
    current_fork_perc:0.00
    current_save_keys_processed:0
    current_save_keys_total:0
    rdb_changes_since_last_save:2
    rdb_bgsave_in_progress:0
    rdb_last_save_time:1634865288
    rdb_last_bgsave_status:ok
    rdb_last_bgsave_time_sec:-1
    rdb_current_bgsave_time_sec:-1
    rdb_last_cow_size:0
    aof_enabled:0
    aof_rewrite_in_progress:0
    aof_rewrite_scheduled:0
    aof_last_rewrite_time_sec:-1
    aof_current_rewrite_time_sec:-1
    aof_last_bgrewrite_status:ok
    aof_last_write_status:ok
    aof_last_cow_size:0
    module_fork_in_progress:0
    module_fork_last_cow_size:0
    
    # Stats
    total_connections_received:4
    total_commands_processed:19
    instantaneous_ops_per_sec:0
    total_net_input_bytes:526
    total_net_output_bytes:81517
    instantaneous_input_kbps:0.00
    instantaneous_output_kbps:0.00
    rejected_connections:0
    sync_full:0
    sync_partial_ok:0
    sync_partial_err:0
    expired_keys:0
    expired_stale_perc:0.00
    expired_time_cap_reached_count:0
    expire_cycle_cpu_milliseconds:27
    evicted_keys:0
    keyspace_hits:3
    keyspace_misses:1
    pubsub_channels:0
    pubsub_patterns:0
    latest_fork_usec:0
    total_forks:0
    migrate_cached_sockets:0
    slave_expires_tracked_keys:0
    active_defrag_hits:0
    active_defrag_misses:0
    active_defrag_key_hits:0
    active_defrag_key_misses:0
    tracking_total_keys:0
    tracking_total_items:0
    tracking_total_prefixes:0
    unexpected_error_replies:0
    total_error_replies:1
    dump_payload_sanitizations:0
    total_reads_processed:23
    total_writes_processed:19
    io_threaded_reads_processed:0
    io_threaded_writes_processed:0
    
    # Replication
    role:master
    connected_slaves:0
    master_failover_state:no-failover
    master_replid:937790b8a90dd91e03356bf4237c7e49224caefc
    master_replid2:0000000000000000000000000000000000000000
    master_repl_offset:0
    second_repl_offset:-1
    repl_backlog_active:0
    repl_backlog_size:1048576
    repl_backlog_first_byte_offset:0
    repl_backlog_histlen:0
    
    # CPU
    used_cpu_sys:1.765061
    used_cpu_user:1.087562
    used_cpu_sys_children:0.000000
    used_cpu_user_children:0.000000
    used_cpu_sys_main_thread:1.595049
    used_cpu_user_main_thread:1.214142
    
    # Modules
    
    # Errorstats
    errorstat_ERR:count=1
    
    # Cluster
    cluster_enabled:0
    
    # Keyspace
    db0:keys=1,expires=0,avg_ttl=0
    
    127.0.0.1:6379> info cpu   加上参数显示指定的系统信息
    # CPU
    used_cpu_sys:1.860890
    used_cpu_user:1.153960
    used_cpu_sys_children:0.000000
    used_cpu_user_children:0.000000
    used_cpu_sys_main_thread:1.687953
    used_cpu_user_main_thread:1.283307
    127.0.0.1:6379> 
    
    

4、redis数据类型

  • String
  • hash
  • list
  • set
  • zset 有序

image-20211022093955154

posted @ 2021-10-22 19:15  紫青宝剑  阅读(515)  评论(0编辑  收藏  举报