内存分析

<p>本文主要介绍如何通过Redis客户端查看Redis实例的内存使用情况。</p> <p><span style="font-size:18px"><strong>背景信息</strong></span></p> <p>您可以通过Argus云监控平台查看内存使用率,计算当前内存使用量。您也可以进一步通过<span style="font-family:Consolas"><strong>redis-rdb-tools</strong></span>工具对目标Redis实例的<strong>rdb</strong>文件进行分析,获取目标Redis实例的内存详细使用情况。</p> <p>同时在Redis 4.0及之后版本,您可以使用<strong>memory</strong>命令,在线分析指定key所占用的内存大小。</p> <p><span style="font-size:18px"><strong>查看Redis实例的内存使用情况</strong></span></p> <p><span style="font-size:16px"><strong>前提条件</strong></span></p> <ol> <li>您已成功创建Redis实例,可参考<a href="https://www.ocftcloud.com/ssr/help/database/Redis/Getting_Started.Create_Redis">创建Redis实例</a>,且实例处于运行中。</li> <li>您已成功创建ECS,可参考<a href="https://www.ocftcloud.com/ssr/help/database/Redis/Getting_Started.Create_ECS">创建ECS</a>,且实例处于运行中。</li> <li>您已成功开通白名单,可参考<a href="https://www.ocftcloud.com/ssr/help/database/Redis/Getting_Started.Create_Whitelist">开通白名单</a>。</li> <li>您已在ECS上成功安装Redis客户端,可参考访问Redis实例,本文以Redis-cli客户端为例,可参考<a href="https://www.ocftcloud.com/ssr/help/database/Redis/Getting_Started.Connect_Redis.Redis_cli">Redis-cli连接</a>。</li> </ol> <p><span style="font-size:16px"><strong>操作步骤</strong></span></p> <p>1. 安装<span style="font-family:Consolas"><strong>redis-rdb-tools</strong></span>工具。</p> <p>a. 执行以下命令下载<span style="font-family:Consolas"><strong>redis-rdb-tools</strong></span>安装包。</p> <table border="0" cellpadding="0" cellspacing="0"> <tbody> <tr> <td style="background-color:#f7f8fa; vertical-align:top"> <p><span style="font-family:Consolas">wget https://github.com/sripathikrishnan/redis-rdb-tools/archive/rdbtools-0.1.14.tar.gz</span></p> </td> </tr> </tbody> </table> <p>b. 执行以下命令,解压<span style="font-family:Consolas"><strong>redis-rdb-tools</strong></span>安装包。</p> <table border="0" cellpadding="0" cellspacing="0"> <tbody> <tr> <td style="background-color:#f7f8fa; vertical-align:top"> <p><span style="font-family:Consolas">tar &ndash;xzf rdbtools-0.1.14.tar.gz</span></p> </td> </tr> </tbody> </table> <p>c. 执行以下命令进入redis-rdb-tools目录。</p> <table border="0" cellpadding="0" cellspacing="0"> <tbody> <tr> <td style="background-color:#f7f8fa; vertical-align:top"> <p><span style="font-family:Consolas">cd redis-rdb-tools-rdbtools-0.1.14</span></p> </td> </tr> </tbody> </table> <p>d. 执行以下命令安装<span style="font-family:Consolas"><strong>redis-rdb-tools</strong></span>工具。</p> <table border="0" cellpadding="0" cellspacing="0"> <tbody> <tr> <td style="background-color:#f7f8fa; vertical-align:top"> <p><span style="font-family:Consolas">sudo python setup.py install</span></p> </td> </tr> </tbody> </table> <p><strong>执行结果</strong></p> <p>执行以下命令查看Python版本。</p> <table border="0" cellpadding="0" cellspacing="0"> <tbody> <tr> <td style="background-color:#f7f8fa; vertical-align:top"> <p><span style="font-family:Consolas">python --version</span></p> </td> </tr> </tbody> </table> <p>2. 安装PyPI工具,可参考:https://pip.pypa.io/en/stable/installing/。</p> <table border="0" cellpadding="0" cellspacing="0"> <tbody> <tr> <td style="background-color:#f7f8fa; vertical-align:top"> <p><span style="font-family:Consolas">wget https://bootstrap.pypa.io/get-pip.py</span></p> <p><span style="font-family:Consolas">python get-pip.py</span></p> </td> </tr> </tbody> </table> <p><img src="https://obs-cn-shanghai.ocftcloud.com/pacloud/20190907164614-11feb11a9cc6.png" style="height:26px; margin:0px; width:65px" />:Python 2的版本大于等于2.7.9,Python 3的版本大于等于3.4,由于自带PyPI工具,可以不安装。</p> <p><strong>执行结果</strong></p> <p>执行以下命令查看pip版本。</p> <table border="0" cellpadding="0" cellspacing="0"> <tbody> <tr> <td style="background-color:#f7f8fa; vertical-align:top"> <p><span style="font-family:Consolas">pip --version</span></p> </td> </tr> </tbody> </table> <p>3. 配置编辑环境。</p> <table border="0" cellpadding="0" cellspacing="0"> <tbody> <tr> <td style="background-color:#f7f8fa; vertical-align:top"> <p><span style="font-family:Consolas">yum install gcc libffi-devel python-devel openssl-devel -y</span></p> <p><span style="font-family:Consolas">pip install python-lzf</span></p> </td> </tr> </tbody> </table> <p>4. 执行以下命令获取目标Redis实例的rdb文件,并存储在当前路径下的<span style="font-family:Consolas"><strong>test.rdb</strong></span>文件下。</p> <table border="0" cellpadding="0" cellspacing="0"> <tbody> <tr> <td style="background-color:#f7f8fa; vertical-align:top"> <p><span style="font-family:Consolas">./redis-cli -h <em>Domain-name</em> -p <em>Port</em> -a <em>Password</em> --rdb test.rdb</span></p> </td> </tr> </tbody> </table> <p><img src="https://obs-cn-shanghai.ocftcloud.com/pacloud/20190907164614-11feb11a9cc6.png" style="height:26px; margin:0px; width:65px" />:</p> <ul> <li><em>Domain-Name</em>:Redis实例的<strong>访问域名</strong>,可参考<a href="https://www.ocftcloud.com/ssr/help/database/Redis/User_Guide.Instance_Management.View_Instance">查看实例基本信息</a>。</li> <li><em>Port</em>:Redis实例的<strong>连接端口</strong>,可参考<a href="https://www.ocftcloud.com/ssr/help/database/Redis/User_Guide.Instance_Management.View_Instance">查看实例基本信息</a>。</li> <li><em>Password</em>:Redis实例的<strong>密码</strong>,在创建Redis实例时设置,可参考<a href="https://www.ocftcloud.com/ssr/help/database/Redis/User_Guide.Instance_Management.Create_Instance">创建实例</a>,可参考<a href="https://www.ocftcloud.com/ssr/help/database/Redis/User_Guide.Instance_Management.View_Instance">查看实例基本信息</a>查看。</li> </ul> <p>5. 使用<span style="font-family:Consolas"><strong>redis-rdb-tools</strong></span>工具分析目标<strong><span style="font-family:Consolas">rdb</span></strong>文件<strong><span style="font-family:Consolas">test.rdb</span></strong>,并将分析结果存储在<span style="font-family:Consolas"><strong>memory.csv</strong></span>文件中。</p> <table border="0" cellpadding="0" cellspacing="0"> <tbody> <tr> <td style="background-color:#f7f8fa; vertical-align:top"> <p><span style="font-family:Consolas">rdb -c memory rdb.test &gt; memory.csv</span></p> </td> </tr> </tbody> </table> <p><span style="font-size:16px"><strong>执行结果</strong></span></p> <p>执行以下命令,查看目标Redis实例的内存使用情况。</p> <table border="0" cellpadding="0" cellspacing="0"> <tbody> <tr> <td style="background-color:#f7f8fa; vertical-align:top"> <p><span style="font-family:Consolas">cat memoyt.csv</span></p> </td> </tr> </tbody> </table> <p><img src="https://obs-cn-shanghai.ocftcloud.com/pacloud/20190907165141-1f38578190d7.png" style="height:119px; margin:0px; width:830px" /></p> <p><span style="font-size:18px"><strong>查看指定key的内存使用情况</strong></span></p> <p><span style="font-size:16px"><strong>前提条件</strong></span></p> <p>您已成功通过Redis客户端访问Redis实例,可参考<a href="https://www.ocftcloud.com/ssr/help/database/Redis/Getting_Started.Map">快速入门</a>。</p> <p><span style="font-size:16px"><strong>注意事项</strong></span></p> <p>请确保您的Redis实例版本在4.0及以上。</p> <p><span style="font-size:16px"><strong>操作步骤</strong></span></p> <p>1. 安装Redis客户端,可参考<a href="https://www.ocftcloud.com/ssr/help/database/Redis/Getting_Started.Connect_Redis.Redis_cli">访问Redis实例</a>。</p> <p>2. 登录Redis实例,执行以下命令查看指定key的内存使用情况。</p> <table border="0" cellpadding="0" cellspacing="0"> <tbody> <tr> <td style="background-color:#f7f8fa; vertical-align:top"> <p>MEMORY USAGE <em>key</em></p> </td> </tr> </tbody> </table> <p><span style="font-size:16px"><strong>执行结果</strong></span></p> <p>执行以下命令查看指定key的内存使用情况。&nbsp; &nbsp;</p> <p><img src="https://obs-cn-shanghai.ocftcloud.com/pacloud/20190907165752-1b1f287a9b17.png" style="height:71px; width:497px" /></p> <p>&nbsp;</p> <p>&nbsp;</p> <p>&nbsp;</p> <p>&nbsp;</p>
以上内容是否解决了您的问题?
请补全提交信息!
联系我们

电话咨询

400-151-8800

邮件咨询

fincloud@ocft.com

在线客服

工单支持

解决云产品相关技术问题