search
HomeDatabaseRedisThe use of shell for Redis operation and maintenance - statistical memory, regular backup, and benchmark testing

使用Redis shell可以完成许多有意思的操作,使用也非常的简单,下面开始介绍。

Redis-cli

redis-cli的选项非常多,除了常用的-h、-p、-a外,下面在介绍几个很常用的选项。想查看所有选项的话可以使用redis-cli --help查看。

--version查看redis版本

# redis-cli --version
redis-cli 5.0.5

可以看到我当前的版本是5.0.5

-r,-i

r是repeat的缩写,表示重复几次的意思。i是interval的缩写,表示间隔多久(单位为妙)。

比如执行ping命令,共执行3次,每隔一秒执行一次。

# redis-cli -r 3 -i 1 ping

另外我们再介绍一个redis的api info memory,它是用来统计redis内存使用情况的。

# redis-cli info memory
# Memory
used_memory:878984
used_memory_human:858.38K
used_memory_rss:3796992
used_memory_rss_human:3.62M
……

统计redis内存信息

介绍了上面的知识后,再结合shell基础,就可以写出一个统计redis内存占用的脚本了。

shell脚本如下,每5秒获取内存信息,共取6次。

#!/bin/bash
memorys=`redis-cli -r 6 -i 5  info memory | grep used_memory:\
    | awk -F: '{print int($2)}'`
total=0
for memory in $memorys
do
    echo $memory
    total=$((memory+total))
done

echo "avg:"$((total/6))

有一个地方需要注意,在awk那行,需要用int转换为数字类型,否则循环里面的递增将会出错,必须保证变量memory是数字类型的。脚本的执行结果如下:

# ./bin/redis-used-memory.sh 
883480
883504
883504
883504
883504
883504
avg:883500

分别输出6个请求的数据,最后再打印出平均值。

--rdb 保存rdb文件到本地

有个该选项,再配合定时任务,我们就可以定期备份Redis数据。

# redis-cli --rdb /data/redis/rdb/dump.rdb
SYNC sent to master, writing 721 bytes to '/data/redis/rdb/dump.rdb'
Transfer finished with success.

如果redis含有重要数据的话,那么建议定时备份数据,防止数据丢失后损失太大。

Redis-benchmark

redis有专门做基准测试的工具,我们可以用它来测试redis的性能。下面介绍几个常用选项。如想知道更多的选项的用法,使用redis-benchmark --help查看更多信息

-c(clients)

表示客户端的并发数量,默认为50.

-n(requests)

表示客户端请求总量,默认值为100000。

例如,有100个客户端,总请求数为10000.

# redis-benchmark -c 100 -n 10000

-q

只显示每秒的请求数据

redis-benchmark -c 100 -n 10000 -q 
PING_INLINE: 98039.22 requests per second
PING_BULK: 102040.82 requests per second
SET: 94339.62 requests per second
GET: 99009.90 requests per second
……

可以看到,每秒set、get等命令的性能。

-t

对指定的命令进行基准测试,比如我只想看get和set的性能如何:

# redis-benchmark -t get,set -q
SET: 98911.96 requests per second
GET: 100200.40 requests per second

以上就是redis-cli和redis-benchmark的基本使用,想要维护redis,这两个工具必须要掌握好的哦。

The above is the detailed content of The use of shell for Redis operation and maintenance - statistical memory, regular backup, and benchmark testing. For more information, please follow other related articles on the PHP Chinese website!

Statement
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Redis: Beyond SQL - The NoSQL PerspectiveRedis: Beyond SQL - The NoSQL PerspectiveMay 08, 2025 am 12:25 AM

Redis goes beyond SQL databases because of its high performance and flexibility. 1) Redis achieves extremely fast read and write speed through memory storage. 2) It supports a variety of data structures, such as lists and collections, suitable for complex data processing. 3) Single-threaded model simplifies development, but high concurrency may become a bottleneck.

Redis: A Comparison to Traditional Database ServersRedis: A Comparison to Traditional Database ServersMay 07, 2025 am 12:09 AM

Redis is superior to traditional databases in high concurrency and low latency scenarios, but is not suitable for complex queries and transaction processing. 1.Redis uses memory storage, fast read and write speed, suitable for high concurrency and low latency requirements. 2. Traditional databases are based on disk, support complex queries and transaction processing, and have strong data consistency and persistence. 3. Redis is suitable as a supplement or substitute for traditional databases, but it needs to be selected according to specific business needs.

Redis: Introduction to a Powerful In-Memory Data StoreRedis: Introduction to a Powerful In-Memory Data StoreMay 06, 2025 am 12:08 AM

Redisisahigh-performancein-memorydatastructurestorethatexcelsinspeedandversatility.1)Itsupportsvariousdatastructureslikestrings,lists,andsets.2)Redisisanin-memorydatabasewithpersistenceoptions,ensuringfastperformanceanddatasafety.3)Itoffersatomicoper

Is Redis Primarily a Database?Is Redis Primarily a Database?May 05, 2025 am 12:07 AM

Redis is primarily a database, but it is more than just a database. 1. As a database, Redis supports persistence and is suitable for high-performance needs. 2. As a cache, Redis improves application response speed. 3. As a message broker, Redis supports publish-subscribe mode, suitable for real-time communication.

Redis: Database, Server, or Something Else?Redis: Database, Server, or Something Else?May 04, 2025 am 12:08 AM

Redisisamultifacetedtoolthatservesasadatabase,server,andmore.Itfunctionsasanin-memorydatastructurestore,supportsvariousdatastructures,andcanbeusedasacache,messagebroker,sessionstorage,andfordistributedlocking.

Redis: Unveiling Its Purpose and Key ApplicationsRedis: Unveiling Its Purpose and Key ApplicationsMay 03, 2025 am 12:11 AM

Redisisanopen-source,in-memorydatastructurestoreusedasadatabase,cache,andmessagebroker,excellinginspeedandversatility.Itiswidelyusedforcaching,real-timeanalytics,sessionmanagement,andleaderboardsduetoitssupportforvariousdatastructuresandfastdataacces

Redis: A Guide to Key-Value Data StoresRedis: A Guide to Key-Value Data StoresMay 02, 2025 am 12:10 AM

Redis is an open source memory data structure storage used as a database, cache and message broker, suitable for scenarios where fast response and high concurrency are required. 1.Redis uses memory to store data and provides microsecond read and write speed. 2. It supports a variety of data structures, such as strings, lists, collections, etc. 3. Redis realizes data persistence through RDB and AOF mechanisms. 4. Use single-threaded model and multiplexing technology to handle requests efficiently. 5. Performance optimization strategies include LRU algorithm and cluster mode.

Redis: Caching, Session Management, and MoreRedis: Caching, Session Management, and MoreMay 01, 2025 am 12:03 AM

Redis's functions mainly include cache, session management and other functions: 1) The cache function stores data through memory to improve reading speed, and is suitable for high-frequency access scenarios such as e-commerce websites; 2) The session management function shares session data in a distributed system and automatically cleans it through an expiration time mechanism; 3) Other functions such as publish-subscribe mode, distributed locks and counters, suitable for real-time message push and multi-threaded systems and other scenarios.

See all articles

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

SublimeText3 Linux new version

SublimeText3 Linux new version

SublimeText3 Linux latest version

WebStorm Mac version

WebStorm Mac version

Useful JavaScript development tools