2MUCH

Redis-Redis基础

2022-07-04


Redis-Redis基础

Redis概述

应用场景:

Redis安装

最好在linux环境上用,所以我就直接安装在云服务器上

前置条件:服务器要带gcc编译器,可输入gcc --version来查看是否已有;若没有,则安装,因为我的云服务器自带了,所以此步省略

下载redis包:官网redis.io去下载(因为视频里讲的是Redis6,所以我也安装这个版本,直接用视频给出的安装包)

步骤:

  1. 将redis安装包ftp到服务器的/opt

  2. tar -zxvf redis-6.2.1.tar.gz
    cd redis-6.2.1
    make # 编译
    

    image-20220704213052663

    如图表示编译好了

  3. 安装 make install

image-20220704213226577

  1. 查看安装后的文件

    [root@iZbp15qc4wmx335c268l5mZ bin]# ls -lrt /usr/local/bin
    -rwxr-xr-x 1 root root 9450208 Jul  4 21:31 redis-server
    -rwxr-xr-x 1 root root 4833352 Jul  4 21:31 redis-benchmark
    -rwxr-xr-x 1 root root 5003368 Jul  4 21:31 redis-cli
    lrwxrwxrwx 1 root root      12 Jul  4 21:31 redis-check-rdb -> redis-server
    lrwxrwxrwx 1 root root      12 Jul  4 21:31 redis-check-aof -> redis-server
    lrwxrwxrwx 1 root root      12 Jul  4 21:31 redis-sentinel -> redis-server
    

    redis-benchmark: 性能测试工具,可以在自己本子运行,看看自己本子性能如何

    redis-check-aof:修复有问题的AOF文件,rdb和aof后面讲

    redis-check-dump:修复有问题的dump.rdb文件

    redis-sentinel:Redis集群使用

    redis-server:Redis服务器启动命令

    redis-cli:客户端,操作入口

Redis启动

前台启动(不推荐)

[root@iZbp15qc4wmx335c268l5mZ bin]# redis-server
18353:C 04 Jul 2022 21:42:49.291 # oO0OoO0OoO0Oo Redis is starting oO0OoO0OoO0Oo
18353:C 04 Jul 2022 21:42:49.291 # Redis version=6.2.1, bits=64, commit=00000000, modified=0, pid=18353, just started
18353:C 04 Jul 2022 21:42:49.291 # Warning: no config file specified, using the default config. In order to specify a config file use redis-server /path/to/redis.conf
18353:M 04 Jul 2022 21:42:49.292 * monotonic clock: POSIX clock_gettime
                _._                                                  
           _.-``__ ''-._                                             
      _.-``    `.  `_.  ''-._           Redis 6.2.1 (00000000/0) 64 bit
  .-`` .-```.  ```\/    _.,_ ''-._                                   
 (    '      ,       .-`  | `,    )     Running in standalone mode
 |`-._`-...-` __...-.``-._|'` _.-'|     Port: 6379
 |    `-._   `._    /     _.-'    |     PID: 18353
  `-._    `-._  `-./  _.-'    _.-'                                   
 |`-._`-._    `-.__.-'    _.-'_.-'|                                  
 |    `-._`-._        _.-'_.-'    |           http://redis.io        
  `-._    `-._`-.__.-'_.-'    _.-'                                   
 |`-._`-._    `-.__.-'    _.-'_.-'|                                  
 |    `-._`-._        _.-'_.-'    |                                  
  `-._    `-._`-.__.-'_.-'    _.-'                                   
      `-._    `-.__.-'    _.-'                                       
          `-._        _.-'                                           
              `-.__.-'                                               

18353:M 04 Jul 2022 21:42:49.293 # WARNING: The TCP backlog setting of 511 cannot be enforced because /proc/sys/net/core/somaxconn is set to the lower value of 128.
18353:M 04 Jul 2022 21:42:49.293 # Server initialized
18353:M 04 Jul 2022 21:42:49.293 # 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.
18353:M 04 Jul 2022 21:42:49.293 * Ready to accept connections

但是关闭这个窗口后,redis就退出了,无法在后台运行,所以不推荐

后台启动

备份redis.conf文件

[root@iZbp15qc4wmx335c268l5mZ redis-6.2.1]# pwd
/opt/redis-6.2.1
[root@iZbp15qc4wmx335c268l5mZ redis-6.2.1]# cp redis.conf /etc/redis.conf

编辑/etc/redis.conf文件,把daemonize no改为daemonize yesdaemonize是用来指定redis是否要用守护线程的方式启动

启动redis:

[root@iZbp15qc4wmx335c268l5mZ bin]# redis-server /etc/redis.conf
[root@iZbp15qc4wmx335c268l5mZ bin]# ps -ef | grep redis
root     18353     1  0 21:42 ?        00:00:01 redis-server *:6379
root     18952 18494  0 21:52 pts/2    00:00:00 grep --color=auto redis

用客户端访问:

[root@iZbp15qc4wmx335c268l5mZ bin]# redis-cli
127.0.0.1:6379> ping
PONG

关闭redis:

127.0.0.1:6379> shutdown
not connected> 
quit
或者
[root@iZbp15qc4wmx335c268l5mZ bin]# redis-cli shutdown
[root@iZbp15qc4wmx335c268l5mZ bin]# ps -ef | grep redis
root     19462 18494  0 21:59 pts/2    00:00:00 grep --color=auto redis

Redis基础介绍

端口号

端口号 6379

来自女星Alessia Merz -> 对应手机9宫格的四位数

对应数据库

默认16个数据库(0-15),默认是0号库

切换数据库:

[cindy@iZbp15qc4wmx335c268l5mZ ~]$ redis-cli
127.0.0.1:6379> select 8
OK
127.0.0.1:6379[8]> select 15
OK
127.0.0.1:6379[15]> select 0
OK
127.0.0.1:6379> 

统一密码管理,所有库同样密码

单线程+多路IO复用技术