NoSQL数据库Redis--1 - Go语言中文社区

NoSQL数据库Redis--1


文章目录

1 缓存技术

缓存cache 是为了调节速度不一致的两个或多个不同的物质的速度,置于中间,可以实现速度较快的一方加速访问速度较慢的一方的作用,比如CPU的一级、二级缓存是保存了CPU最近经常访问的数据,内存是保存CPU经常访问硬盘的数据,而且硬盘也有大小不一的缓存,甚至是物理服务器的raid 卡有也缓存,都是为了起到加速CPU 访问硬盘数据的目的,因为CPU的速度太快了,CPU需要的数据由于硬盘往往不能在短时间内满足CPU的需求,因此CPU缓存、内存、Raid 卡缓存以及硬盘缓存就在一定程度上满足了CPU的数据需求,即CPU 从缓存读取数据,从而可以大幅提高CPU的工作效率。

在这里插入图片描述
参考资料:http://www.sohu.com/a/246498483_468626

1.1 缓存 cache

1.1.1 buffer与cache

buffer:缓冲,也叫写缓冲,一般用于写操作,可以将数据先写入内存在写入磁盘,buffer 一般用于写缓冲,用于解决不同介质的速度不一致的缓冲,先将数据临时写入到里自己最近的地方,以提高写入速度,CPU会把数据先写到内存的磁盘缓冲区,然后应用就认为数据已经写入完成,然后由内核在后续的时间再写入磁盘,所以服务器突然断电会丢失内存中的部分数据。

cache:缓存,也叫读缓存,一般用于读操作,CPU读文件从内存读,如果内存没有,就先从硬盘读到内存再读到CPU,将需要频繁读取的数据放在里自己最近的缓存区域,下次读取的时候即可快速读取。

向 /proc/sys/vm/drop_caches 写入相应的修改值,会清理缓存。建议先执行sync(sync 命令将所有未写的系统缓冲区写到磁盘中,包含已修改的 i-node、已延迟的块 I/O 和读写映射文件)。执行echo 1、2、3 至 /proc/sys/vm/drop_caches, 达到不同的清理目的

如果因为是应用有像内存泄露、溢出的问题时,从swap的使用情况是可以比较快速可以判断的,但通过执行free 反而比较难查看。但核心并不会因为内存泄露等问题并没有快速清空buffer或cache(默认值是0),生产也不应该随便去改变此值。

一般情况下,应用在系统上稳定运行了,free值也会保持在一个稳定值的。当发生内存不足、应用获取不到可用内存、OOM错误等问题时,还是更应该去分析应用方面的原因,否则,清空buffer,强制腾出free的大小,可能只是把问题给暂时屏蔽了。

排除内存不足的情况外,除非是在软件开发阶段,需要临时清掉buffer,以判断应用的内存使用情况;或应用已经不再提供支持,即使应用对内存的时候确实有问题,而且无法避免的情况下,才考虑定时清空buffer。

范例:man proc

...省略...
To free pagecache, use:
echo 1 > /proc/sys/vm/drop_caches

To free dentries and inodes, use:
echo 2 > /proc/sys/vm/drop_caches

To free pagecache, dentries and inodes, use:
echo 3 > /proc/sys/vm/drop_caches

Because writing to this file is a nondestructive operation and dirty objects
are not freeable, the user should run sync(1) first.

范例:清理缓存

[root@centos8 ~]#cat /proc/sys/vm/drop_caches 
0
[root@centos8 ~]#free -h
              total        used        free      shared  buff/cache   available
Mem:          798Mi       198Mi        60Mi       0.0Ki       538Mi       468Mi
Swap:         2.0Gi       9.0Mi       2.0Gi
[root@centos8 ~]#echo 3 > /proc/sys/vm/drop_caches 
[root@centos8 ~]#free -h
              total        used        free      shared  buff/cache   available
Mem:          798Mi       247Mi       408Mi       1.0Mi       142Mi       426Mi
Swap:         2.0Gi       9.0Mi       2.0Gi

1.1.2 缓存保存位置及分层结构

互联网应用领域,提到缓存为王

  • 用户层: 浏览器DNS缓存,应用程序DNS缓存,操作系统DNS缓存客户端
  • 代理层: CDN,反向代理缓存
  • Web层: 解释器Opcache,Web服务器缓存
  • 应用层: 页面静态化
  • 数据层: 分布式缓存,数据库
  • 系统层: 操作系统cache
  • 物理层: 磁盘cache, Raid Cache

1.1.3 cache 的特性

自动过期:给缓存的数据加上有效时间,超出时间后自动过期删除

强制过期:源网站更新图片后CDN是不会更新的,需要强制图片缓存过期 ,通过CDN管理后台实现

命中率:即缓存的读取命中率

1.2 用户层缓存

1.2.1 DNS缓存

浏览器的DNS缓存默认为60秒,即60秒之内在访问同一个域名就不再进行DNS解析
范例:查看chrome浏览器的DNS缓存
注意:此方式新版的chrome 基于安全原因不再显示缓存信息
在浏览器的地址栏上输入:chrome://net-internals/#dns
在这里插入图片描述

1.2.2 火狐浏览器缓存

地址栏输入下面指令可以查看Firefox的缓存信息

about:cache 

1.2.3 前端技术 dns-prefetch

HTML5的新特性, 可以在html文件中进行DNS的 Prefetch
在这里插入图片描述

1.2.4 浏览器缓存过期机制

1.2.4.1 最后修改时间 last-modified

Last-Modified 是 HttpHeader 中的资源的最后修改时间,如果带有 Last-Modified ,下一次发送 Http请求时,将会发生带 If-modified-since 的 HttpHeader 。如果没有过期,将会收到 304 的响应,从缓存中读取。

浏览器请求文件时,会先获取服务器的文件的最后修改时间,再和本地浏览器缓存中的文件时间相比,如果没有发生变化就返回给浏览器304的状态码,表示没有发生变化,然后浏览器就使用的本地的缓存展示资源,如果变化,则重新再一次向服务器请求资源

此方式也需要向服务器发送请求才可以使用缓存
在这里插入图片描述

1.2.4.2 Etag标记

Etag 是 HttpHeader 中代表资源的标签,在服务器端生成。如果带有 Etag ,下一次发送带 Etag 的请求,如果 Etag 没有变化将收到 304 的响应,从缓存中读取。

基于Etag标记是否一致做判断页面是否发生过变化,比如基于Nginx 的etag on来实现。

Etag 在使用时要注意相同资源多台 Web 服务器的 Etag 的一致性
此方式也需要向服务器发送请求才可以使用缓存

1.2.4.3 过期时间 expires

以上两种都需要发送请求,即不管资源是否过期都要发送请求进行协商,这样会消耗不必要的时间,因此有了缓存的过期时间

Expire 是 HttpHeader 中代表资源的过期时间,由服务器端设置。如果带有 Expire ,则在 Expire 过期前不会发生 Http 请求,直接从缓存中读取。用户强制 F5 例外

第一次请求资源时,响应报文带有资源的过期时间,默认为30天,当前此方式使用的比较多,但是无法保证客户的时间都是准确并且一致的,因此会加入一个最大生存周期,使用用户本地的时间计算缓存数据是否超过多少天,下面的过期时间Expires:为2028年,但是缓存的最大生存周期Cache-Control: max-age=315360000,计算为天等于3650天即10年
在这里插入图片描述

1.2.4.3 混合使用和缓存刷新

通常 Last-Modified,Etag,Expire 是一起混合使用的

  • 特别是 Last-Modified 和 Expire 经常一起使用,因为 Expire 可以让浏览器完全不发起 Http 请求,而当浏览器强制 F5 的时候又有 Last-Modified ,这样就很好的达到了浏览器段缓存的效果。
  • Etag 和 Expire 一起使用时,先判断 Expire ,如果已经过期,再发起 Http 请求,如果 Etag 变化了,则返回 200 响应。如果 Etag 没有变化,则返回 304 响应。
  • Last-Modified,Etag,Expires 三个同时使用时。先判断 Expire ,然后发送 Http 请求,服务器先判断 last-modified ,再判断 Etag ,必须都没有过期,才能返回 304 响应。

缓存刷新

  • 第一次访问,获取最新数据,返回 200响应码
  • 鼠标点击二次访问 (Cache),输入地址后回车,浏览器对所有没有过期的内容直接使用本地缓存。
  • F5或点刷新按钮, 会向服务器发送请求缓存协商信息,last-modified和etag会有影响,但expires本地过期时间不受影响,无变化返回304
  • 按Ctrl+F5强制刷新,所有缓存不再使用,直接连接服务器,获取最新数据,返回200响应码

1.2.5 cookie 和 session

Cookie是访问某些网站以后在本地存储的一些网站相关的信息,下次再访问的时候减少一些步骤,比如加密后的账户名密码等信息

Cookies是服务器在客户端浏览器上存储的小段文本并随每一个请求发送至同一个服务器,是一种实现客户端保持状态的方案。

session称为会话信息,位于web服务器上,主要负责访问者与网站之间的交互,当浏览器请求http地址时,可以基于之前的session实现会话保持、session共享等。

1.3 CDN 缓存

1.3.1 什么是CDN

在这里插入图片描述
在这里插入图片描述
内容分发网络(Content Delivery Network,CDN)是建立并覆盖在承载网上,由不同区域的服务器组成的分布式网络。将源站资源缓存到全国各地的边缘服务器,利用全球调度系统使用户能够就近获取,有效降低访问延迟,降低源站压力,提升服务可用性。

常见的CDN服务商
百度CDN:https://cloud.baidu.com/product/cdn.html
阿里CDN:https://www.aliyun.com/product/cdn?spm=5176.8269123.416540.50.728y8n
腾讯CDN:https://www.qcloud.com/product/cdn
腾讯云CDN收费介绍:https://cloud.tencent.com/document/product/228/2949

1.3.2 用户请求CDN流程

假设您的业务源站域名为 www.test.com ,域名接入 CDN 开始使用加速服务后,当您的用户发起HTTP 请求时,实际的处理流程如下图所示:
在这里插入图片描述
详细说明如下:

  1. 用户向 www.test.com 下的某图片资源(如:1.jpg)发起请求,会先向 Local DNS 发起域名解析请求。
  2. 当 Local DNS 解析 www.test.com 时,会发现已经配置了 CNAME www.test.com.cdn.dnsv1.com ,解析请求会发送至 Tencent DNS(GSLB),GSLB 为腾讯云自主研发的调度体系,会为请求分配最佳节点 IP。
  3. Local DNS 获取 Tencent DNS 返回的解析 IP。
  4. 用户获取解析 IP。
  5. 用户向获取的 IP 发起对资源 1.jpg 的访问请求。
  6. 若该 IP 对应的节点缓存有 1.jpg,则会将数据直接返回给用户(10),此时请求结束。若该节点未缓存 1.jpg,则节点会向业务源站发起对 1.jpg 的请求(6、7、8),获取资源后,结合用户自定义配置的缓存策略,将资源缓存至节点(9),并返回给用户(10),此时请求结束。
    在这里插入图片描述

1.3.3 利用 302 实现转发请求重定向至最优服务器集群

因为中国网络较为复杂,依赖DNS就近解析的调度,仍然会存在部分请求调度失效、调度生效慢等问题。

比如:腾讯云利用在全国部署的302重定向服务器集群,能够为每一个请求实时决策最优的服务器资源,精准解决小运营商的调度问题,提升用户访问质量, 能最快地把用户引导到最优的服务器节点上,避开性能差或者异常的节点。
在这里插入图片描述

1.3.5 CDN 分层缓存

提前对静态内容进行预缓存,避免大量的请求回源,导致主站网络带宽被打满而导致数据无法更新,另外CDN可以将数据根据访问的热度不同而进行不同级别的缓存,例如:访问量最高的资源访问CDN 边缘节点的内存,其次的放在SSD或者SATA,再其次的放在云存储,这样兼顾了速度与成本。

比如: 腾讯云CDN节点,根据用户的数据冷热不同,动态的进行识别,按照cache层次进行数据的存储,在访问频率到40%-90%的数据,首先放在OC边缘节点内存cache中,提供8G-64G的数据空间存储;在访问频率到30%-50%的数据,放在OC节点SSD/SATA硬盘cache中,提供1T-15T的数据空间存猪,其他的比较冷的数据,放在云存储中,采用回源拉取的方式进行处理。这样在成本和效率中计算出最优平衡点,为客户提供服务。
在这里插入图片描述

1.3.3 CDN主要优势

CDN 有效地解决了目前互联网业务中网络层面的以下问题:

  • 用户与业务服务器地域间物理距离较远,需要进行多次网络转发,传输延时较高且不稳定。
  • 用户使用运营商与业务服务器所在运营商不同,请求需要运营商之间进行互联转发。
  • 业务服务器网络带宽、处理能力有限,当接收到海量用户请求时,会导致响应速度降低、可用性降低。
  • 利用CDN防止和抵御DDos等攻击,实现安全保护

1.4 应用层缓存

Nginx、PHP等web服务可以设置应用缓存以加速响应用户请求,另外有些解释性语言,比如:PHP/Python/Java不能直接运行,需要先编译成字节码,但字节码需要解释器解释为机器码之后才能执行,因此字节码也是一种缓存,有时候还会出现程序代码上线后字节码没有更新的现象。所以一般上线新版前,需要先将应用缓存清理,再上线新版

另外可以利用动态页面静态化技术,加速访问,比如:将访问数据库的数据的动态页面,提前用程序生成静态页面文件html.电商网站的商品介绍,评论信息非实时数据等皆可利用此技术实现

1.5 数据层缓存

分布式缓存服务

  • Redis
  • Memcached

数据库

  • MySQL 查询缓存
  • innodb缓存、MyISAM缓存

1.6 硬件缓存

1.6.1 CPU缓存

CPU缓存(L1的数据缓存和L1的指令缓存)、二级缓存、三级缓存
在这里插入图片描述
在这里插入图片描述

1.6.2 磁盘相关缓存

  • 磁盘缓存:Disk Cache
  • 磁盘阵列缓存:Raid Cache,可使用电池防止断电丢失数据

2 redis 部署与使用

2.1 redis 基础

2.1.1 关系型数据库和 NoSQL 数据库

数据库主要分为两大类:关系型数据库与 NoSQL 数据库。

关系型数据库,是建立在关系模型基础上的数据库,其借助于集合代数等数学概念和方法来处理数据库中的数据。主流的 MySQL、Oracle、MS SQL Server 和 DB2 都属于这类传统数据库。

NoSQL 数据库,全称为 Not Only SQL,意思就是适用关系型数据库的时候就使用关系型数据库,不适用的时候也没有必要非得使用关系型数据库不可,可以考虑使用更加合适的数据存储。主要分为临时性键值存储(memcached、Redis)、永久性键值存储(ROMA、Redis)、面向文档的数据库(MongoDB、CouchDB)、面向列的数据库(Cassandra、HBase),每种 NoSQL 都有其特有的使用场景及优点。

Oracle,mysql 等传统的关系数据库非常成熟并且已大规模商用,为什么还要用 NoSQL 数据库呢?

主要是由于随着互联网发展,数据量越来越大,对性能要求越来越高,传统数据库存在着先天性的缺陷,即单机(单库)性能瓶颈,并且扩展困难。这样既有单机单库瓶颈,却又扩展困难,自然无法满足日益增长的海量数据存储及其性能要求,所以才会出现了各种不同的 NoSQL 产品,NoSQL 根本性的优势在于在云计算时代,简单、易于大规模分布式扩展,并且读写性能非常高

RDBMS和NOSQL的特点及优缺点:
在这里插入图片描述

2.1.2 redis 简介

短短几年,Redis就有了很大的用户群体,目前国内外使用的公司众多,比如:阿里,百度,新浪微博,知乎网,GitHub,Twitter 等

Redis是一个开源的、遵循BSD协议的、基于内存的而且目前比较流行的键值数据库(key-value database),是一个非关系型数据库,redis 提供将内存通过网络远程共享的一种服务,提供类似功能的还有memcached,但相比memcached,redis还提供了易扩展、高性能、具备数据持久性等功能。

Redis 在高并发、低延迟环境要求比较高的环境使用量非常广泛,目前redis在DB-Engine月排行榜http
s://db-engines.com/en/ranking 中一直比较靠前,而且一直是键值型存储类的首位
在这里插入图片描述
官网地址:https://redis.io/

2.1.3 Redis 特性

  • 速度快: 10W QPS,基于内存,C语言实现
  • 单线程
  • 持久化
  • 支持多种数据结构
  • 支持多种编程语言
  • 功能丰富: 支持Lua脚本,发布订阅,事务,pipeline等功能
  • 简单: 代码短小精悍(单机核心代码只有23000行左右),单线程开发容易,不依赖外部库,使用简单
  • 主从复制
  • 支持高可用和分布式

2.1.4 单线程

Redis 6.0版本前一直是单线程方式处理用户的请求
在这里插入图片描述
单线程为何如此快?

  • 纯内存
  • 非阻塞
  • 避免线程切换和竞态消耗
    在这里插入图片描述
    注意事项:
  • 一次只运行一条命令
  • 拒绝长(慢)命令:keys, flushall, flushdb, slow lua script, mutil/exec, operate big value(collection)
  • 其实不是单线程: 早期版本是单进程单线程,3版本后实际还有其它的线程, fysnc file descriptor,close file descriptor

2.1.5 redis 对比 memcached

  • 支持数据的持久化:可以将内存中的数据保持在磁盘中,重启redis服务或者服务器之后可以从备份文件中恢复数据到内存继续使用
  • 支持更多的数据类型:支持string(字符串)、hash(哈希数据)、list(列表)、set(集合)、zset(有序集合)
  • 支持数据的备份:可以实现类似于数据的master-slave模式的数据备份,另外也支持使用快照+AOF
  • 支持更大的value数据:memcache单个key value最大只支持1MB,而redis最大支持512MB(生产不建议超过2M,性能受影响)
  • 在Redis6版本前,Redis 是单线程,而memcached是多线程,所以单机情况下没有memcached 并发高,性能更好,但redis 支持分布式集群以实现更高的并发,单Redis实例可以实现数万并发
  • 支持集群横向扩展:基于redis cluster的横向扩展,可以实现分布式集群,大幅提升性能和数据安全性
  • 都是基于 C 语言开发

2.1.6 redis 典型应用场景

  • Session 共享:常见于web集群中的Tomcat或者PHP中多web服务器session共享
  • 缓存:数据查询、电商网站商品信息、新闻内容
  • 计数器:访问排行榜、商品浏览数等和次数相关的数值统计场景
  • 微博/微信社交场合:共同好友,粉丝数,关注,点赞评论等
  • 消息队列:ELK的日志缓存、部分业务的订阅发布系统
  • 地理位置: 基于GEO(地理信息定位),实现摇一摇,附近的人,外卖等功能
    数据更新操作流程:
    在这里插入图片描述
    数据读操作流程:
    在这里插入图片描述
    在这里插入图片描述
    在这里插入图片描述
    在这里插入图片描述
    在这里插入图片描述

2.2 Redis 安装及连接

官方下载地址:http://download.redis.io/releases/

2.2.1 yum安装redis

在centos系统上需要安装epel源

2.2.1.1 查看yum仓库redis版本

#CentOS 8 由系统源提供
[root@centos8 ~]#yum info redis
Installed Packages
Name         : redis
Version      : 5.0.3
Release      : 1.module_el8.0.0+6+ab019c03
Arch         : x86_64
Size         : 3.3 M
Source       : redis-5.0.3-1.module_el8.0.0+6+ab019c03.src.rpm
Repo         : @System
From repo    : AppStream
Summary      : A persistent key-value database
URL          : http://redis.io
License      : BSD and MIT
Description  : Redis is an advanced key-value store. It is often referred to as a data
             : structure server since keys can contain strings, hashes, lists, sets and
             : sorted sets.
             : 
             : You can run atomic operations on these types, like appending to a string;
             : incrementing the value in a hash; pushing to a list; computing set
             : intersection, union and difference; or getting the member with highest
             : ranking in a sorted set.
             : 
             : In order to achieve its outstanding performance, Redis works with an
             : in-memory dataset. Depending on your use case, you can persist it either
             : by dumping the dataset to disk every once in a while, or by appending
             : each command to a log.
             : 
             : Redis also supports trivial-to-setup master-slave replication, with very
             : fast non-blocking first synchronization, auto-reconnection on net split
             : and so forth.
             : 
             : Other features include Transactions, Pub/Sub, Lua scripting, Keys with a
             : limited time-to-live, and configuration settings to make Redis behave like
             : a cache.
             : 
             : You can use Redis from most programming languages also.

#CentOS 7 由epel源提供
[root@node7 ~]# yum info redis
Loaded plugins: fastestmirror
Loading mirror speeds from cached hostfile
 * base: mirrors.aliyun.com
 * epel: mirrors.aliyun.com
 * extras: mirrors.aliyun.com
Available Packages
Name        : redis
Arch        : x86_64
Version     : 3.2.12
Release     : 2.el7
Size        : 544 k
Repo        : epel/7/x86_64
Summary     : A persistent key-value database
URL         : http://redis.io
License     : BSD
Description : Redis is an advanced key-value store. It is often referred to as a data
            : structure server since keys can contain strings, hashes, lists, sets and
            : sorted sets.
            : 
            : You can run atomic operations on these types, like appending to a string;
            : incrementing the value in a hash; pushing to a list; computing set
            : intersection, union and difference; or getting the member with highest
            : ranking in a sorted set.
            : 
            : In order to achieve its outstanding performance, Redis works with an
            : in-memory dataset. Depending on your use case, you can persist it either
            : by dumping the dataset to disk every once in a while, or by appending
            : each command to a log.
            : 
            : Redis also supports trivial-to-setup master-slave replication, with very
            : fast non-blocking first synchronization, auto-reconnection on net split
            : and so forth.
            : 
            : Other features include Transactions, Pub/Sub, Lua scripting, Keys with a
            : limited time-to-live, and configuration settings to make Redis behave like
            : a cache.
            : 
            : You can use Redis from most programming languages also.

2.2.1.2 yum安装 redis

[root@centos8 ~]#dnf -y install redis
[root@centos8 ~]#systemctl enable --now redis

[root@centos8 ~]#ss -ntl
State       Recv-Q       Send-Q               Local Address:Port               Peer Address:Port       
LISTEN      0            128                        0.0.0.0:22                      0.0.0.0:*          
LISTEN      0            128                      127.0.0.1:6379                    0.0.0.0:*          
LISTEN      0            128                           [::]:22                         [::]:*

[root@centos8 ~]#pstree -p | grep redis
           |-redis-server(16812)-+-{redis-server}(16813)
           |                     |-{redis-server}(16814)
           |                     `-{redis-server}(16815)

[root@centos8 ~]#redis-cli 
127.0.0.1:6379> ping
PONG
127.0.0.1:6379> info
# Server
redis_version:5.0.3
redis_git_sha1:00000000
redis_git_dirty:0
redis_build_id:8c0bf22bfba82c8f
redis_mode:standalone
os:Linux 4.18.0-80.el8.x86_64 x86_64
arch_bits:64
multiplexing_api:epoll
atomicvar_api:atomic-builtin
gcc_version:8.2.1
process_id:16812
run_id:2af1a9195554cb7d268b5e782b58e3a23135c418
tcp_port:6379
uptime_in_seconds:89
uptime_in_days:0
hz:10
configured_hz:10
lru_clock:9451885
executable:/usr/bin/redis-server
config_file:/etc/redis.conf

# Clients
connected_clients:1
client_recent_max_input_buffer:2
client_recent_max_output_buffer:0
blocked_clients:0

# Memory
used_memory:853896
used_memory_human:833.88K
used_memory_rss:13012992
used_memory_rss_human:12.41M
used_memory_peak:853896
used_memory_peak_human:833.88K
used_memory_peak_perc:100.12%
used_memory_overhead:840694
used_memory_startup:791000
used_memory_dataset:13202
used_memory_dataset_perc:20.99%
allocator_allocated:1477016
allocator_active:1806336
allocator_resident:8712192
total_system_memory:836886528
total_system_memory_human:798.12M
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.22
allocator_frag_bytes:329320
allocator_rss_ratio:4.82
allocator_rss_bytes:6905856
rss_overhead_ratio:1.49
rss_overhead_bytes:4300800
mem_fragmentation_ratio:16.03
mem_fragmentation_bytes:12201096
mem_not_counted_for_evict:0
mem_replication_backlog:0
mem_clients_slaves:0
mem_clients_normal:49694
mem_aof_buffer:0
mem_allocator:jemalloc-5.1.0
active_defrag_running:0
lazyfree_pending_objects:0

# Persistence
loading:0
rdb_changes_since_last_save:0
rdb_bgsave_in_progress:0
rdb_last_save_time:1603287316
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

# Stats
total_connections_received:1
total_commands_processed:2
instantaneous_ops_per_sec:0
total_net_input_bytes:45
total_net_output_bytes:11475
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
evicted_keys:0
keyspace_hits:0
keyspace_misses:0
pubsub_channels:0
pubsub_patterns:0
latest_fork_usec: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

# Replication
role:master
connected_slaves:0
master_replid:cc6c17d57ae2848acfa6c93c116b3fb7e9bc305b
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:0.113862
used_cpu_user:0.015991
used_cpu_sys_children:0.000000
used_cpu_user_children:0.000000

# Cluster
cluster_enabled:0

# Keyspace
127.0.0.1:6379> 

2.2.2 编译安装 redis

下载当前最新release版本 redis 源码包
网站:http://download.redis.io/releases/
在这里插入图片描述

2.2.2.1 编译安装

官方的安装方法:

https://redis.io/download

范例:编译安装过程

#安装依赖包
[root@centos8 ~]#yum -y install make gcc tcl

#下载源码包
[root@centos8 ~]#wget http://download.redis.io/releases/redis-5.0.9.tar.gz
[root@centos8 ~]#tar xf redis-5.0.9.tar.gz

#编译安装
[root@centos8 ~]#cd redis-5.0.9/
[root@centos8 redis-5.0.9]#cd src/
[root@centos8 src]#make
[root@centos8 src]#make PREFIX=/apps/redis install
 
配置变量
[root@centos8 src]#echo "PATH=/apps/redis/bin:$PATH" > /etc/profile.d/redis.sh
[root@centos8 src]#. /etc/profile.d/redis.sh

#目录结构
[root@centos8 src]#tree /apps/redis/
/apps/redis/
└── bin
    ├── redis-benchmark
    ├── redis-check-aof
    ├── redis-check-rdb
    ├── redis-cli
    ├── redis-sentinel -> redis-server
    └── redis-server

1 directory, 6 files

#准备相关目录和文件
[root@centos8 ~]#mkdir /apps/redis/{etc,log,data,run}
[root@centos8 ~]#cp redis-5.0.9/redis.conf /apps/redis/etc/

2.2.2.2 前台启动 redis

redis-server 是redis 服务器程序

[root@centos8 ~]#redis-server --help
Usage: ./redis-server [/path/to/redis.conf] [options]
       ./redis-server - (read config from stdin)
       ./redis-server -v or --version
       ./redis-server -h or --help
       ./redis-server --test-memory <megabytes>

Examples:
       ./redis-server (run the server with default conf)
       ./redis-server /etc/redis/6379.conf
       ./redis-server --port 7777
       ./redis-server --port 7777 --replicaof 127.0.0.1 8888
       ./redis-server /etc/myredis.conf --loglevel verbose

Sentinel mode:
       ./redis-server /etc/sentinel.conf --sentinel

前台启动 redis

[root@centos8 ~]#redis-server /apps/redis/etc/redis.conf 
16418:C 22 Oct 2020 10:43:29.813 # oO0OoO0OoO0Oo Redis is starting oO0OoO0OoO0Oo
16418:C 22 Oct 2020 10:43:29.813 # Redis version=5.0.9, bits=64, commit=00000000, modified=0, pid=16418, just started
16418:C 22 Oct 2020 10:43:29.813 # Configuration loaded
16418:M 22 Oct 2020 10:43:29.814 * Increased maximum number of open files to 10032 (it was originally set to 1024).
                _._                                                  
           _.-``__ ''-._                                             
      _.-``    `.  `_.  ''-._           Redis 5.0.9 (00000000/0) 64 bit
  .-`` .-```.  ```/    _.,_ ''-._                                   
 (    '      ,       .-`  | `,    )     Running in standalone mode
 |`-._`-...-` __...-.``-._|'` _.-'|     Port: 6379
 |    `-._   `._    /     _.-'    |     PID: 16418
  `-._    `-._  `-./  _.-'    _.-'                                   
 |`-._`-._    `-.__.-'    _.-'_.-'|                                  
 |    `-._`-._        _.-'_.-'    |           http://redis.io        
  `-._    `-._`-.__.-'_.-'    _.-'                                   
 |`-._`-._    `-.__.-'    _.-'_.-'|                                  
 |    `-._`-._        _.-'_.-'    |                                  
  `-._    `-._`-.__.-'_.-'    _.-'                                   
      `-._    `-.__.-'    _.-'                                       
          `-._        _.-'                                           
              `-.__.-'                                               

16418:M 22 Oct 2020 10:43:29.815 # WARNING: The TCP backlog setting of 511 cannot be enforced because /proc/sys/net/core/somaxconn is set to the lower value of 128.
16418:M 22 Oct 2020 10:43:29.815 # Server initialized
16418:M 22 Oct 2020 10:43:29.815 # 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.
16418:M 22 Oct 2020 10:43:29.815 # WARNING you have Transparent Huge Pages (THP) support enabled in your kernel. This will create latency and memory usage issues with Redis. To fix this issue run the command 'echo madvise > /sys/kernel/mm/transparent_hugepage/enabled' as root, and add it to your /etc/rc.local in order to retain the setting after a reboot. Redis must be restarted after THP is disabled (set to 'madvise' or 'never').
16418:M 22 Oct 2020 10:43:29.815 * Loading RDB produced by version 6.0.8
16418:M 22 Oct 2020 10:43:29.815 * RDB age 603 seconds
16418:M 22 Oct 2020 10:43:29.815 * RDB memory usage when created 0.77 Mb
16418:M 22 Oct 2020 10:43:29.815 * DB loaded from disk: 0.000 seconds
16418:M 22 Oct 2020 10:43:29.815 * Ready to accept connections

[root@centos8 ~]#ss -ntl
State       Recv-Q       Send-Q               Local Address:Port               Peer Address:Port       
LISTEN      0            128                        0.0.0.0:22                      0.0.0.0:*          
LISTEN      0            128                      127.0.0.1:6379                    0.0.0.0:*          
LISTEN      0            128                           [::]:22                         [::]:*

范例:开启多实例

#安装依赖包
[root@centos8 ~]#yum -y install make gcc tcl

#下载源码包
[root@centos8 ~]#wget http://download.redis.io/releases/redis-5.0.9.tar.gz
[root@centos8 ~]#tar xf redis-5.0.9.tar.gz

#编译安装
[root@centos8 ~]#cd redis-5.0.9/
[root@centos8 redis-5.0.9]#cd deps
[root@centos8 deps]#make hiredis lua jemalloc linenoise
[root@centos8 deps]#cd ../src
[root@centos8 src]#make
[root@centos8 src]#make PREFIX=/apps/redis/6379 install
 
配置变量
[root@centos8 src]#echo "PATH=/apps/redis/6379/bin:$PATH" > /etc/profile.d/redis.sh
[root@centos8 src]#. /etc/profile.d/redis.sh

#目录结构
[root@centos8 src]#tree /apps/redis/6379
/apps/redis/
└── bin
    ├── redis-benchmark
    ├── redis-check-aof
    ├── redis-check-rdb
    ├── redis-cli
    ├── redis-sentinel -> redis-server
    └── redis-server

1 directory, 6 files

#准备相关目录和文件
[root@centos8 ~]#mkdir /apps/redis/6379/{etc,log,data,run}
[root@centos8 ~]#cp redis-5.0.9/redis.conf /apps/redis/6379/etc/

#前台启动redis 6379
[root@centos8 redis]#redis-server --port 6379
17024:C 22 Oct 2020 11:09:08.749 # oO0OoO0OoO0Oo Redis is starting oO0OoO0OoO0Oo
17024:C 22 Oct 2020 11:09:08.749 # Redis version=5.0.9, bits=64, commit=00000000, modified=0, pid=17024, just started
17024:C 22 Oct 2020 11:09:08.749 # Configuration loaded
17024:M 22 Oct 2020 11:09:08.750 * Increased maximum number of open files to 10032 (it was originally set to 1024).
                _._                                                  
           _.-``__ ''-._                                             
      _.-``    `.  `_.  ''-._           Redis 5.0.9 (00000000/0) 64 bit
  .-`` .-```.  ```/    _.,_ ''-._                                   
 (    '      ,       .-`  | `,    )     Running in standalone mode
 |`-._`-...-` __...-.``-._|'` _.-'|     Port: 6379
 |    `-._   `._    /     _.-'    |     PID: 17024
  `-._    `-._  `-./  _.-'    _.-'                                   
 |`-._`-._    `-.__.-'    _.-'_.-'|                                  
 |    `-._`-._        _.-'_.-'    |           http://redis.io        
  `-._    `-._`-.__.-'_.-'    _.-'                                   
 |`-._`-._    `-.__.-'    _.-'_.-'|                                  
 |    `-._`-._        _.-'_.-'    |                                  
  `-._    `-._`-.__.-'_.-'    _.-'                                   
      `-._    `-.__.-'    _.-'                                       
          `-._        _.-'                                           
              `-.__.-'                                               

17024:M 22 Oct 2020 11:09:08.751 # WARNING: The TCP backlog setting of 511 cannot be enforced because /proc/sys/net/core/somaxconn is set to the lower value of 128.
17024:M 22 Oct 2020 11:09:08.751 # Server initialized
17024:M 22 Oct 2020 11:09:08.751 # 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.
17024:M 22 Oct 2020 11:09:08.751 # WARNING you have Transparent Huge Pages (THP) support enabled in your kernel. This will create latency and memory usage issues with Redis. To fix this issue run the command 'echo madvise > /sys/kernel/mm/transparent_hugepage/enabled' as root, and add it to your /etc/rc.local in order to retain the setting after a reboot. Redis must be restarted after THP is disabled (set to 'madvise' or 'never').
17024:M 22 Oct 2020 11:09:08.751 * Ready to accept connections
[root@centos8 ~]#ss -ntl
State       Recv-Q       Send-Q               Local Address:Port               Peer Address:Port       
LISTEN      0            128                        0.0.0.0:22                      0.0.0.0:*          
LISTEN      0            128                        0.0.0.0:6379                    0.0.0.0:*          
LISTEN      0            128                           [::]:22                         [::]:*          
LISTEN      0            128                           [::]:6379                       [::]:* 
[root@centos8 ~]#redis-cli -p 6379
127.0.0.1:6379>

#为6380准备相关目录和文件
[root@centos8 ~]#mkdir /apps/redis/6380
[root@centos8 ~]#cp -ar /apps/redis/6379/* /apps/redis/6380/
[root@centos8 ~]#tree -d /apps/redis/
/apps/redis/
├── 6379
│   ├── bin
│   ├── data
│   ├── etc
│   ├── log
│   └── run
└── 6380
    ├── bin
    ├── data
    ├── etc
    ├── log
    └── run

12 directories

[root@centos8 ~]#vim /apps/redis/6380/etc/redis.conf 
port 6380

#前端启动6380
[root@centos8 ~]#redis-server --port 6380
17191:C 22 Oct 2020 11:17:28.354 # oO0OoO0OoO0Oo Redis is starting oO0OoO0OoO0Oo
17191:C 22 Oct 2020 11:17:28.354 # Redis version=5.0.9, bits=64, commit=00000000, modified=0, pid=17191, just started
17191:C 22 Oct 2020 11:17:28.354 # Configuration loaded
17191:M 22 Oct 2020 11:17:28.355 * Increased maximum number of open files to 10032 (it was originally set to 1024).
                _._                                                  
           _.-``__ ''-._                                             
      _.-``    `.  `_.  ''-._           Redis 5.0.9 (00000000/0) 64 bit
  .-`` .-```.  ```/    _.,_ ''-._                                   
 (    '      ,       .-`  | `,    )     Running in standalone mode
 |`-._`-...-` __...-.``-._|'` _.-'|     Port: 6380
 |    `-._   `._    /     _.-'    |     PID: 17191
  `-._    `-._  `-./  _.-'    _.-'                                   
 |`-._`-._    `-.__.-'    _.-'_.-'|                                  
 |    `-._`-._        _.-'_.-'    |           http://redis.io        
  `-._    `-._`-.__.-'_.-'    _.-'                                   
 |`-._`-._    `-.__.-'    _.-'_.-'|                                  
 |    `-._`-._        _.-'_.-'    |                                  
  `-._    `-._`-.__.-'_.-'    _.-'                                   
      `-._    `-.__.-'    _.-'                                       
          `-._        _.-'                                           
              `-.__.-'                                               

17191:M 22 Oct 2020 11:17:28.355 # WARNING: The TCP backlog setting of 511 cannot be enforced because /proc/sys/net/core/somaxconn is set to the lower value of 128.
17191:M 22 Oct 2020 11:17:28.355 # Server initialized
17191:M 22 Oct 2020 11:17:28.355 # 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.
17191:M 22 Oct 2020 11:17:28.355 # WARNING you have Transparent Huge Pages (THP) support enabled in your kernel. This will create latency and memory usage issues with Redis. To fix this issue run the command 'echo madvise > /sys/
                        
版权声明:本文来源CSDN,感谢博主原创文章,遵循 CC 4.0 by-sa 版权协议,转载请附上原文出处链接和本声明。
原文链接:https://blog.csdn.net/kingdom_xu/article/details/109193812
站方申明:本站部分内容来自社区用户分享,若涉及侵权,请联系站方删除。

0 条评论

请先 登录 后评论

官方社群

GO教程

猜你喜欢