Redis-cli连接

<p>本文主要介绍创建ECS并开通白名单后,如何在ECS上安装Redis-cli客户端,并通过Redis-cli客户端访问Redis实例。Redis-cli是Redis自带的客户端,因此使用Redis-cli工具需要先安装Redis。</p> <p><span style="font-size:18px"><strong>前提条件</strong></span></p> <ol> <li>您已成功创建Redis实例,可参考<a href="https://pinganyun.com/ssr/help/database/Redis/Getting_Started.Create_Redis" target="_blank">创建Redis实例</a>,且实例处于运行中。</li> <li>您已成功创建ECS,可参考<a href="https://pinganyun.com/ssr/help/database/Redis/Getting_Started.Create_ECS" target="_blank">创建ECS</a>,且实例处于运行中。</li> <li>您已成功开通白名单,可参考<a href="https://pinganyun.com/ssr/help/database/Redis/Getting_Started.Create_Whitelist" target="_blank">开通白名单</a>。</li> </ol> <p><span style="font-size:18px"><strong>注意事项</strong></span></p> <p>ECS的操作系统请选择CentOS 7.x、RedHat Linux 7.x、Oracle Linux 7.x及以上的版本。</p> <p><span style="font-size:18px"><strong>操作步骤</strong></span></p> <p>1. 登录ECS,可参考<a href="https://pinganyun.com/ssr/help/compute/ecs/Quick_Start.Linux_Quick_Start.Login_Instance" target="_blank">登录Linux云主机</a>。</p> <p>2. 安装Redis。</p> <p><strong>&nbsp; &nbsp; 方法一</strong></p> <p>&nbsp; &nbsp; a.&nbsp;若您ECS的操作系统不包含gcc编译环境,请执行以下命令安装。</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</span></p> </td> </tr> </tbody> </table> <p>&nbsp; &nbsp; b.&nbsp;执行以下命令下载Redis安装包,<a href="https://redis.io/download" target="_blank">下载地址</a>。</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 http://download.redis.io/releases/redis-5.0.4.tar.gz</span></p> </td> </tr> </tbody> </table> <p>&nbsp; &nbsp; <img src="https://pcp-portal-sca.obs-cn-shenzhen.pinganyun.com/pcp-portal-sca/20190907105358-1012c489968b.png" style="height:26px; margin:0px; width:65px" />:Redis的版本您可根据实际业务情况选择,本文以5.0.4版本为例。</p> <p>&nbsp; &nbsp; c.&nbsp;执行以下命令,解压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">tar -xzf redis-5.0.4.tar.gz</span></p> </td> </tr> </tbody> </table> <p>&nbsp; &nbsp; d.&nbsp;执行以下命令进入Redis目录并编译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">cd redis-5.0.4</span></p> <p><span style="font-family:Consolas">make</span></p> </td> </tr> </tbody> </table> <p>&nbsp; &nbsp; <strong>执行结果</strong></p> <p>&nbsp; &nbsp; 系统提示编译成功。</p> <p>&nbsp; &nbsp; <strong>方法二</strong></p> <p>&nbsp; &nbsp; a. 若您ECS的操作系统不包含gcc编译环境,请执行以下命令安装。</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</span></p> </td> </tr> </tbody> </table> <p>&nbsp; &nbsp; b.&nbsp;执行以下命令,安装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">yum install redis</span></p> <p><span style="font-family:Consolas">systemctl start redis</span></p> <p><span style="font-family:Consolas">systemctl enable redis</span></p> </td> </tr> </tbody> </table> <p><img src="https://pcp-portal-sca.obs-cn-shenzhen.pinganyun.com/pcp-portal-sca/20190907105922-1e0363359c27.png" style="height:39px; margin:0px; width:830px" /></p> <p>&nbsp; &nbsp; <strong>执行结果</strong></p> <p>&nbsp; &nbsp; 执行以下命令,确认Redis-cli与Redis-server的连通性。</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 ping</span></p> </td> </tr> </tbody> </table> <p><img src="https://pcp-portal-sca.obs-cn-shenzhen.pinganyun.com/pcp-portal-sca/20190907110026-11b49e1c9638.png" style="height:43px; margin:0px; width:485px" /></p> <p>3. 使用Redis-cli访问Redis实例。</p> <p><strong>&nbsp; &nbsp; 方法一</strong></p> <p>&nbsp; &nbsp; a. 执行以下命令进入src目录。</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 src</span></p> </td> </tr> </tbody> </table> <p>&nbsp; &nbsp; b. 执行以下命令,通过Redis-cli客户端连接Redis实例。</p> <table border="0" cellpadding="0" cellspacing="0"> <tbody> <tr> <td style="background-color:#f7f8fa; vertical-align:top"> <p>./redis-cli -c -h <em>Domain-Name</em> -p <em>Port</em> -a <em>Password</em></p> </td> </tr> </tbody> </table> <p>&nbsp; &nbsp; &nbsp;<img src="https://pcp-portal-sca.obs-cn-shenzhen.pinganyun.com/pcp-portal-sca/20190907110242-150228fd9a6a.png" style="height:26px; margin:0px; width:65px" />:以上命令中:</p> <p>&nbsp; &nbsp; &bull;&nbsp;&nbsp;<em>Domain-Name</em>:Redis实例的<strong>访问域名</strong>,可参考<a href="https://pinganyun.com/ssr/help/database/Redis/User_Guide.Instance_Management.View_Instance" target="_blank">查看实例基本信息</a>获取。</p> <p>&nbsp; &nbsp; &bull;&nbsp;&nbsp;<em>Port</em>:Redis实例的<strong>连接端口</strong>,可参考<a href="https://pinganyun.com/ssr/help/database/Redis/User_Guide.Instance_Management.View_Instance" target="_blank">查看实例基本信息</a>获取。</p> <p>&nbsp; &nbsp; &bull;&nbsp;&nbsp;<em>Password</em>:Redis实例的<strong>密码</strong>,在创建Redis实例时设置,可参考<a href="https://pinganyun.com/ssr/help/database/Redis/User_Guide.Instance_Management.View_Instance" target="_blank">查看实例基本信息</a>查看。</p> <p>&nbsp; &nbsp; <strong>方法二</strong></p> <p>&nbsp; &nbsp; 执行以下命令,通过Redis-cli客户端连接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">redis-cli -c -h <em>Domain-Name</em> -p <em>Port</em> -a <em>Password</em></span></p> </td> </tr> </tbody> </table> <p>&nbsp; &nbsp; &nbsp;<img src="https://pcp-portal-sca.obs-cn-shenzhen.pinganyun.com/pcp-portal-sca/20190907110242-150228fd9a6a.png" style="height:26px; margin:0px; width:65px" />:以上命令中:</p> <p>&nbsp; &nbsp; &bull;&nbsp;&nbsp;<em>Domain-Name</em>:Redis实例的<strong>访问域名</strong>,可参考<a href="https://pinganyun.com/ssr/help/database/Redis/User_Guide.Instance_Management.View_Instance">查看实例基本信息</a>获取。</p> <p>&nbsp; &nbsp; &bull;&nbsp;&nbsp;<em>Port</em>:Redis实例的<strong>连接端口</strong>,可参考<a href="https://pinganyun.com/ssr/help/database/Redis/User_Guide.Instance_Management.View_Instance">查看实例基本信息</a>获取。</p> <p>&nbsp; &nbsp; &bull;&nbsp;&nbsp;<em>Password</em>:Redis实例的<strong>密码</strong>,在创建Redis实例时设置,可参考<a href="https://pinganyun.com/ssr/help/database/Redis/User_Guide.Instance_Management.View_Instance">查看实例基本信息</a>查看。</p> <p>4. 执行以下命令,向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">set foo bar</span></p> </td> </tr> </tbody> </table> <p><span style="font-size:18px"><strong>执行结果</strong></span></p> <p>执行以下命令,查看刚刚向Redis实例中写入的数据。&nbsp;</p> <table border="0" cellpadding="0" cellspacing="0"> <tbody> <tr> <td style="background-color:#f7f8fa; vertical-align:top"> <p><span style="font-family:Consolas">get foo</span></p> </td> </tr> </tbody> </table> <p><img src="https://pcp-portal-sca.obs-cn-shenzhen.pinganyun.com/pcp-portal-sca/20190907110755-1dbe51ff91e3.png" style="height:110px; margin:0px; width:604px" /></p>
以上内容是否解决了您的问题?
请补全提交信息!
咨询·建议

电话咨询

400-151-8800

邮件咨询

cloud@pingan.com

在线客服

工单支持

解决云产品相关技术问题