redis常见问题解决_如何解决Redis中的问题

redis常见问题解决

介绍 (Introduction)

Redis is an open-source, in-memory key-value data store. It comes with several commands that can help with troubleshooting and debugging issues. Because of Redis’s nature as an in-memory key-value store, many of these commands focus on memory management, but there are others that are valuable for providing an overview of the state of your Redis server. This tutorial will provide details on how to use some of these commands to help diagnose and resolve issues you may run into as you use Redis.

Redis是一个开源的内存中键值数据存储。 它带有几个命令,可以帮助您进行故障排除和调试。 由于Redis作为内存中键值存储的性质,因此其中许多命令都集中在内存管理上,但是还有其他一些命令对于概述Redis服务器的状态很有用。 本教程将提供有关如何使用其中一些命令来帮助诊断和解决使用Redis时可能遇到的问题的详细信息。

如何使用本指南 (How To Use This Guide)

This guide is written as a cheat sheet with self-contained examples. We encourage you to jump to any section that is relevant to the task you’re trying to complete.

本指南以备有完整示例的备忘单形式编写。 我们鼓励您跳至与您要完成的任务相关的任何部分。

The commands shown in this guide were tested on an Ubuntu 18.04 server running Redis version 4.0.9. To set up a similar environment, you can follow Step 1 of our guide on How To Install and Secure Redis on Ubuntu 18.04. We will demonstrate how these commands behave by running them with redis-cli, the Redis command line interface. Note that if you’re using a different Redis interface — Redli, for example — the exact output of certain commands may differ.

本指南中显示的命令已在运行Redis版本4.0.9的Ubuntu 18.04服务器上进行了测试。 要设置类似的环境,您可以按照我们的指南如何在Ubuntu 18.04上安装和保护Redis的 步骤1进行操作。 我们将通过使用Redis命令行界面redis-cli运行它们来演示这些命令的行为。 请注意,如果您使用其他Redis界面(例如Redli) ,则某些命令的确切输出可能会有所不同。

Alternatively, you could provision a managed Redis database instance to test these commands, but note that depending on the level of control allowed by your database provider, some commands in this guide may not work as described. To provision a DigitalOcean Managed Database, follow our Managed Databases product documentation. Then, you must either install Redli or set up a TLS tunnel in order to connect to the Managed Database over TLS.

另外,您可以提供一个托管的Redis数据库实例来测试这些命令,但是请注意,根据数据库提供者所允许的控制级别,本指南中的某些命令可能无法按所述方式工作。 要配置DigitalOcean托管数据库,请遵循我们的托管数据库产品文档 。 然后, 您必须 安装Redli 设置TLS隧道才能通过TLS连接到托管数据库。

memory usage tells you how much memory is currently being used by a single key. It takes the name of a key as an argument and outputs the number of bytes it uses:

memory usage告诉您单个键当前正在使用多少内存。 它以键的名称作为参数,并输出其使用的字节数:

  • memory usage key_meaningOfLife

    内存使用情况key_意义OfLife

   
   
Output
(integer) 42

For a more general understanding of how your Redis server is using memory, you can run the memory stats command:

为了更全面地了解您的Redis服务器如何使用内存,可以运行memory stats命令:

  • memory stats

    内存统计

This command outputs an array of memory-related metrics and their values. The following are the metrics reported by memory stats:

此命令输出与内存相关的指标及其值的数组。 以下是memory stats报告的指标:

  • peak.allocated: The peak number of bytes consumed by Redis

    peak.allocated :Redis消耗的最大字节数

  • total.allocated: The total number of bytes allocated by Redis

    total.allocated :Redis分配的总字节数

  • startup.allocated: The initial number of bytes consumed by Redis at startup

    startup.allocated :Redis在启动时消耗的初始字节数

  • replication.backlog: The size of the replication backlog, in bytes

    replication.backlog :复制积压的大小,以字节为单位

  • clients.slaves: The total size of all replica overheads (the output and query buffers and connection contexts)

    clients.slaves :所有副本开销的总大小(输出和查询缓冲区以及连接上下文)

  • clients.normal: The total size of all client overheads

    clients.normal :所有客户端开销的总大小

  • aof.buffer: The total size of the current and rewrite append-only file buffers

    aof.buffer :当前和重写的仅附加文件缓冲区的总大小

  • db.0: The overheads of the main and expiry dictionaries for each database in use on the server, reported in bytes

    db. 0 db. 0 :服务器上正在使用的每个数据库的主要字典和到期字典的开销,以字节为单位报告

  • overhead.total: The sum of all overheads used to manage Redis’s keyspace

    overhead.total :用于管理Redis密钥空间的所有间接overhead.total的总和

  • keys.count: The total number of keys stored in all the databases on the server

    keys.count :服务器上所有数据库中存储的密钥总数

  • keys.bytes-per-key: The ratio of the server’s net memory usage and keys.count

    keys.bytes-per-key :服务器的净内存使用量与keys.count

  • dataset.bytes: The size of the dataset, in bytes

    dataset.bytes :数据集的大小,以字节为单位

  • dataset.percentage: The percentage of Redis’s net memory usage taken by dataset.bytes

    dataset.percentagedataset.bytes占用的Redis的净内存使用率

  • peak.percentage: The percentage of peak.allocated taken out of total.allocated

    peak.percentage :百分比peak.allocated采取了total.allocated

  • fragmentation: The ratio of the amount of memory currently in use divided by the physical memory Redis is actually using

    fragmentation :当前使用的内存量除以Redis实际使用的物理内存的比率

memory malloc-stats provides an internal statistics report from jemalloc, the memory allocator used by Redis on Linux systems:

memory malloc-stats提供了来自jemalloc的内部统计报告,该报告是Linux系统上Redis使用的内存分配器:

  • memory malloc-stats

    内存malloc-stats

If it seems like you’re running into memory-related issues, but parsing the output of the previous commands proves to be unhelpful, you can try running memory doctor:

如果您似乎遇到了与内存有关的问题,但是解析前面的命令的输出证明是没有帮助的,则可以尝试运行memory doctor

  • memory doctor

    记忆医生

This feature will output any memory consumption issues that it can find and suggest potential solutions.

此功能将输出它可以找到的所有内存消耗问题,并提出潜在的解决方案。

获取有关Redis实例的常规信息 (Getting General Information about Your Redis Instance)

A debugging command that isn’t directly related to memory management is monitor. This command allows you to see a constant stream of every command processed by the Redis server:

与内存管理不直接相关的调试命令是monitor 。 此命令使您可以查看Redis服务器处理的每个命令的恒定流:

  • monitor

    监控

   
   
Output
OK 1566157213.896437 [0 127.0.0.1:47740] "auth" "foobared" 1566157215.870306 [0 127.0.0.1:47740] "set" "key_1" "878"

Another command useful for debugging is info, which returns several blocks of information and statistics about the server:

另一个对调试有用的命令是info ,它返回有关服务器的多个信息和统计信息块:

  • info

    信息

   
   
Output
# Server redis_version:4.0.9 redis_git_sha1:00000000 redis_git_dirty:0 redis_build_id:9435c3c2879311f3 redis_mode:standalone os:Linux 4.15.0-52-generic x86_64 . . .

This command returns a lot of information. If you only want to see one info block, you can specify it as an argument to info:

此命令返回很多信息。 如果只想查看一个信息块,则可以将其指定为info的参数:

  • info CPU

    信息CPU

   
   
Output
# CPU used_cpu_sys:173.16 used_cpu_user:70.89 used_cpu_sys_children:0.01 used_cpu_user_children:0.04

Note that the information returned by the info command will depend on which version of Redis you’re using.

请注意, info命令返回的info将取决于您所使用的Redis版本。

使用keys命令 (Using the keys Command)

The keys command is helpful in cases where you’ve forgotten the name of a key, or perhaps you’ve created one but accidentally misspelled its name. keys looks for keys that match a pattern:

如果您忘记了某个键的名称,或者您已经创建了一个键,但又不小心拼错了它的名称,则keys命令很有用。 keys查找与模式匹配的键:

  • keys pattern

    按键模式

The following glob-style variables are supported

支持以下glob样式的变量

  • ? is a wildcard standing for any single character, so s?mmy matches sammy, sommy, and sqmmy

    ? 是代表任何单个字符的通配符,因此s?mmy匹配sammysommysqmmy

  • * is a wildcard that stands for any number of characters, including no characters at all, so sa*y matches sammy, say, sammmmmmy, and salmony

    *是一个通配符,代表任意数量的字符,根本不包含任何字符,因此sa*y匹配sammysaysammmmmmysalmony

  • You can specify two or more characters that the pattern can include by wrapping them in brackets, so s[ai]mmy will match sammy and simmy, but not summy

    您可以通过将模式包装在方括号中来指定模式可以包含的两个或多个字符,因此s[ai]mmy可以匹配sammysimmy ,但不能summy

  • To set a wildcard that disregards one or more letters, wrap them in brackets and precede them with a carrot (^), so s[^oi]mmy will match sammy and sxmmy, but not sommy or simmy

    要设置一个不考虑一个或多个字母的通配符,请将其括在方括号中,并在其前面加上一个胡萝卜( ^ ),这样s[^oi]mmy将匹配sammysxmmy ,但不sommysimmy

  • To set a wildcard that includes a range of letters, separate the beginning and end of the range with a hyphen and wrap it in brackets, so s[a-o]mmy will match sammy, skmmy, and sommy, but not srmmy

    要设置包含一定范围的字母的通配符,请使用连字符将范围的开头和结尾分隔开并用括号括起来,这样s[ao]mmy将匹配sammyskmmysommy ,而不是srmmy

Warning: The Redis documentation warns that keys should almost never be used in a production environment, since it can have a major negative impact on performance.

警告: Redis文档警告说, keys几乎切勿在生产环境中使用,因为它会对性能产生重大负面影响。

结论 (Conclusion)

This guide details a number of commands that are helpful for troubleshooting and resolving issues one might encounter as they work with Redis. If there are other related commands, arguments, or procedures you’d like to see outlined in this guide, please ask or make suggestions in the comments below.

本指南详细介绍了许多命令,这些命令可用于故障排除和解决与Redis一起使用时可能遇到的问题。 如果您想在本指南中概述其他相关的命令,参数或过程,请在下面的注释中提出疑问或提出建议。

For more information on Redis commands, see our tutorial series on How to Manage a Redis Database.

有关Redis命令的更多信息,请参阅关于如何管理Redis数据库的系列教程。

翻译自: https://www.digitalocean.com/community/cheatsheets/how-to-troubleshoot-issues-in-redis

redis常见问题解决

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值