Redis学习:概述和安装

Redis概述

  • Redis是一个开源的key-value存储系统;
  • 和Memcached类似,它支持存储的value类型相对更多,包括string(字符串)、list(链表)、set(集合)、zset(sorted set --有序集合)和hash(哈希类型);
  • 这些数据类型都支持 push/pop、add/remove及取交集并集和差集及更丰富的操作,而且这些操作都是原子性的;
  • 在此基础上,Redis支持各种不同方式的排序;
  • memcached一样,为了保证效率,数据都是缓存在内存中;
  • 区别的是Redis 会周期性的把更新的数据写入磁盘或者把修改操作写入追加的记录文件;

多样化的数据结构存储持久化数据

  • 最新N个数据 --》 通过List实现按自然时间排序的数据
  • 排行榜、Top N --》 利用zset(有序集合)
  • 时效性的数据,比如手机验证码 --》 Expire过期
  • 计数器,秒杀 --》 原子性,自增方法INCR、DECR
  • 去除大量数据中的重复数据 --》 利用Set集合
  • 构建队列 --》 利用list集合
  • 发布订阅消息系统 --》 pub/sub模式

Redis安装

Redis官网:https://redis.io/

安装步骤
准备工作:下载安装最新版的gcc编译器

安装c语言的编译环境

yum install centos-release-gcl scl-utils-buildu
yum install -y devtoolset-8-toolchain
scl enable devtoolset-8 bash

或者

yum install gcc 在线安装

测试gcc版本:gcc --version

[root@VM-20-14-centos data]# gcc --version
gcc (GCC) 4.8.5 20150623 (Red Hat 4.8.5-44)
Copyright (C) 2015 Free Software Foundation, Inc.
This is free software; see the source for copying conditions.  There is NO
warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.

下载redis-6.2.1.tar. gz放/opt目录
解压命令: tar -zxvf redis-6.2.1.tar. gz
解压完成后进入目录: cd redis-6.2.1

在redis-6.2.1目录下执行make命令(仅编译)

注意:如果没有准备好C语言编译环境,make会报错:-Jemalloc/jemalloc.h:没有那个文件

在redis-6.2.1目录下执行make install命令(安装)

安装目录:/usr/local/bin

查看默认安装目录:

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

redis-check-aof:修复有问题的AOF文件

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

redis-sentinel:Redis集群使用

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

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

前台启动(不推荐)

前台启动,命令行窗口不能关闭,否则服务器停止

[root@VM-20-14-centos bin]# redis-server 
3196:C 23 Nov 2022 11:39:51.069 # oO0OoO0OoO0Oo Redis is starting oO0OoO0OoO0Oo
3196:C 23 Nov 2022 11:39:51.069 # Redis version=6.2.7, bits=64, commit=00000000, modified=0, pid=3196, just started
3196:C 23 Nov 2022 11:39:51.069 # Warning: no config file specified, using the default config. In order to specify a config file use redis-server /path/to/redis.conf
3196:M 23 Nov 2022 11:39:51.069 * monotonic clock: POSIX clock_gettime
3196:M 23 Nov 2022 11:39:51.070 # A key '__redis__compare_helper' was added to Lua globals which is not on the globals allow list nor listed on the deny list.
                _._                                                  
           _.-``__ ''-._                                             
      _.-``    `.  `_.  ''-._           Redis 6.2.7 (00000000/0) 64 bit
  .-`` .-```.  ```\/    _.,_ ''-._                                  
 (    '      ,       .-`  | `,    )     Running in standalone mode
 |`-._`-...-` __...-.``-._|'` _.-'|     Port: 6379
 |    `-._   `._    /     _.-'    |     PID: 3196
  `-._    `-._  `-./  _.-'    _.-'                                   
 |`-._`-._    `-.__.-'    _.-'_.-'|                                  
 |    `-._`-._        _.-'_.-'    |           https://redis.io       
  `-._    `-._`-.__.-'_.-'    _.-'                                   
 |`-._`-._    `-.__.-'    _.-'_.-'|                                  
 |    `-._`-._        _.-'_.-'    |                                  
  `-._    `-._`-.__.-'_.-'    _.-'                                   
      `-._    `-.__.-'    _.-'                                       
          `-._        _.-'                                           
              `-.__.-'                                               

3196:M 23 Nov 2022 11:39:51.070 # WARNING: The TCP backlog setting of 511 cannot be enforced because /proc/sys/net/core/somaxconn is set to the lower value of 128.
3196:M 23 Nov 2022 11:39:51.070 # Server initialized
3196:M 23 Nov 2022 11:39:51.070 # 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.
3196:M 23 Nov 2022 11:39:51.070 * Ready to accept connections

后台启动(推荐)
  • 备份redis.conf

    拷贝一份redis.conf到其他目录

    cp /opt/redis-3.2.5/redis.conf /etc/redis.conf

  • 后台启动设置daemonize no 改成yes

    修改redis.conf文件,将里面的daemonize no改成yes,让服务在后台启动

  • 启动Redis

    redis-server /etc/redis.conf

    [root@VM-20-14-centos bin]# redis-server /etc/redis.conf 
    [root@VM-20-14-centos bin]# ps -ef|grep redis
    root      5627     1  0 11:47 ?        00:00:00 redis-server 127.0.0.1:6379
    root      5650 15217  0 11:47 pts/0    00:00:00 grep --color=auto redis
    
Redis关闭

单实例关闭:redis-cli shutdown

[root@VM-20-14-centos bin]# redis-cli shutdown
[root@VM-20-14-centos bin]# ps -ef|grep redis
root      6970 15217  0 11:51 pts/0    00:00:00 grep --color=auto redis

进入终端关闭

[root@VM-20-14-centos bin]# redis-cli 
127.0.0.1:6379> shutdown
not connected> 
[root@VM-20-14-centos bin]# ps -ef|grep redis
root      7179 15217  0 11:52 pts/0    00:00:00 grep --color=auto redis

多实例关闭,指定端口关闭:redis-cli -p 6379 shutdown

[root@VM-20-14-centos bin]# redis-cli -p 6379 shutdown
[root@VM-20-14-centos bin]# ps -ef|grep redis
root      7362 15217  0 11:52 pts/0    00:00:00 grep --color=auto redis

使用kill关闭

[root@VM-20-14-centos bin]# ps -ef|grep redis
root      8036     1  0 11:54 ?        00:00:00 redis-server 127.0.0.1:6379
root      8055 15217  0 11:54 pts/0    00:00:00 grep --color=auto redis
[root@VM-20-14-centos bin]# kill -9 8036
[root@VM-20-14-centos bin]# ps -ef|grep redis
root      8169 15217  0 11:55 pts/0    00:00:00 grep --color=auto redis

Redis介绍相关知识

  • 端口6379从何而来:Alessia Merz(字母对应九宫格按键数字);
  • 默认16个数据库,类似数组下标从0开始,初始默认使用0号库;
  • 统一密码管理,所有库相同密码;
  • Redis是单线程+多路IO复用技术;
  • 与Memcache三点不同:支持多数据类型,支持持久化,单线程+多路IO复用
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值