Redis-cli连接
<p class="shortdesc">本文以与Redis实例在同一网络域的ECS作为Redis
客户端为例,介绍创建ECS并开通白名单后,如何在ECS上安装Redis-cli客户端,并通过Redis-cli客户端访问Redis实例。Redis-cli是Redis自带的客户端,因此使用Redis-cli工具需要先安装Redis。</p>
<section class="section prereq" id="CLI__prereq_dp4_jvq_gsb"><div class="tasklabel"><h2 class="doc-tairway">前提条件</h2></div>
<ol class="ol" id="CLI__ol_rlw_jvq_gsb">
<li class="li">您已成功创建Redis实例,可参考<a class="xref" href="https://pacloud.paic.com.cn/ssr/help/database/Redis/User_Guide.Instance_Management.Create_Instance.general_instance" target="_blank" rel="external noopener">创建Redis实例</a>,且实例处于运行中。</li>
<li class="li">您已成功创建ECS,可参考<a class="xref" href="https://pacloud.paic.com.cn/ssr/help/database/Redis/Getting_Started.Create_ECS" target="_blank" rel="external noopener">创建ECS</a>,且实例处于运行中。</li>
<li class="li">您已成功开通白名单,可参考<a class="xref" href="https://pacloud.paic.com.cn/ssr/help/database/Redis/Getting_Started.Create_Whitelist" target="_blank" rel="external noopener">开通白名单</a>。<div class="note important note_important" id="CLI__note_ll1_wqq_qsb"><span class="note__title">重要:</span>
<p class="p">ECS的操作系统请选择CentOS 7.x、RedHat Linux 7.x、Oracle Linux 7.x及以上的版本。</p>
</div></li>
</ol>
</section>
<section><div class="tasklabel"><h2 class="doc-tairway">操作步骤</h2></div><ol class="ol steps"><li class="li step stepexpand">
<span class="ph cmd" id="CLI__linuxC">登录ECS,可参考<a class="xref" href="/ssr/help/compute/ecs/manual.Instance.Login_Linux_Instance.Console_Remote_Login" target="_blank" rel="external noopener">登录Linux云主机</a>。</span>
</li><li class="li step stepexpand">
<span class="ph cmd">安装Redis。</span>
<div class="itemgroup info">
<p class="p"><strong class="ph b">方法一:</strong></p>
<ol class="ol" type="a" id="CLI__ol_sqr_v2x_gsb">
<li class="li">
<p class="p">若您ECS的操作系统不包含gcc编译环境,请执行以下命令安装。</p>
<pre class="pre codeblock"><code>yum install gcc</code></pre>
</li>
<li class="li">
<p class="p">执行以下命令下载Redis安装包,<a class="xref" href="https://redis.io/download" target="_blank" rel="external noopener">下载地址</a>。</p>
<pre class="pre codeblock"><code>wget http://download.redis.io/releases/redis-5.0.4.tar.gz</code></pre>
<div class="note important note_important"><span class="note__title">重要:</span> Redis的版本您可根据实际业务情况选择,本文以5.0.4版本为例。</div>
</li>
<li class="li">
<p class="p">执行以下命令,解压Redis安装包。</p>
<pre class="pre codeblock"><code>tar -xzf redis-5.0.4.tar.gz</code></pre>
</li>
<li class="li">
<p class="p">执行以下命令进入Redis目录并编译Redis。</p>
<pre class="pre codeblock"><code>cd redis-5.0.4
make</code></pre>
</li>
</ol>
<p class="p"><strong class="ph b">执行结果</strong></p>
<p class="p">系统提示编译成功。</p>
<p class="p"><strong class="ph b">方法二: </strong></p>
<ol class="ol" type="a" id="CLI__ol_ggz_z2x_gsb">
<li class="li">
<p class="p">若您ECS的操作系统不包含gcc编译环境,请执行以下命令安装。</p>
<pre class="pre codeblock"><code>yum install gcc</code></pre>
</li>
<li class="li">
<p class="p">执行以下命令,安装Redis。</p>
<pre class="pre codeblock"><code>yum install redis
systemctl start redis
systemctl enable redis
</code></pre>
<p class="p"><img class="image" id="CLI__image_axk_qrx_gsb" src="https://obs-cn-shanghai.fincloud.pinganyun.com/pacloud/20222411190036-17a6332f927c.png" width="800"></p>
</li>
</ol>
<p class="p"><strong class="ph b">执行结果</strong></p>
<p class="p">执行以下命令,确认Redis-cli与Redis-server的连通性。</p>
<pre class="pre codeblock"><code>redis-cli ping</code></pre>
<p class="p"><img class="image" id="CLI__image_w1n_rrx_gsb" src="https://obs-cn-shanghai.fincloud.pinganyun.com/pacloud/20222411190036-17cf98e698f9.png"></p>
</div>
</li><li class="li step stepexpand">
<span class="ph cmd">使用Redis-cli访问Redis实例。</span>
<div class="itemgroup info">
<p class="p">
<strong class="ph b">方法一</strong>:</p>
<ol class="ol" type="a" id="CLI__ol_n1d_gfx_gsb">
<li class="li">
<p class="p">执行以下命令进入src目录。</p>
<pre class="pre codeblock" id="CLI__codeblock_gbq_frq_qsb"><code>cd src</code></pre>
</li>
<li class="li">
<p class="p">执行以下命令,通过Redis-cli客户端连接Redis实例。</p>
<pre class="pre codeblock" id="CLI__codeblock_hbq_frq_qsb"><code>./redis-cli -h <em class="ph i">Domain-Name</em> -p <em class="ph i">Port</em> -a <em class="ph i">Password</em></code></pre>
<ul class="ul" id="CLI__ul_uct_frq_qsb">
<li class="li">
<p class="p">如果是集群实例,请执行:</p>
<pre class="pre codeblock" id="CLI__codeblock_vct_frq_qsb"><code>./redis-cli -c -h <em class="ph i">Domain-Name</em> -p <em class="ph i">Port</em> -a <em class="ph i">Password</em></code></pre>
</li>
<li class="li">以上命令中:<ul class="ul" id="CLI__ul_wct_frq_qsb">
<li class="li">
<em class="ph i">Domain-Name</em>:Redis实例的<span class="ph uicontrol">访问域名</span><p class="p" id="CLI__looksl">可参考<a class="xref" href="https://pacloud.paic.com.cn/ssr/help/database/Redis/User_Guide.Instance_Management.View_Instance" target="_blank" rel="external noopener">查看实例基本信息获取</a>。</p></li>
<li class="li">
<em class="ph i">Port</em>:Redis实例的<span class="ph uicontrol">连接端口</span>,<p class="p">可参考<a class="xref" href="https://pacloud.paic.com.cn/ssr/help/database/Redis/User_Guide.Instance_Management.View_Instance" target="_blank" rel="external noopener">查看实例基本信息获取</a>。</p></li>
<li class="li">
<em class="ph i">Password</em>:Redis实例的<span class="ph uicontrol">密码</span>,在创建Redis实例时设置,可参考<a class="xref" href="https://pacloud.paic.com.cn/ssr/help/database/Redis/User_Guide.Instance_Management.Create_Instance.general_instance" target="_blank" rel="external noopener">创建Redis实例</a>和<a class="xref" href="https://pacloud.paic.com.cn/ssr/help/database/Redis/User_Guide.Instance_Management.View_Instance" target="_blank" rel="external noopener">查看实例基本信息获取</a>。</li>
</ul></li>
</ul>
</li>
</ol>
</div>
<div class="itemgroup info">
<p class="p">
<strong class="ph b">方法二</strong>:</p>
<p class="p">执行以下命令,通过Redis-cli客户端连接Redis实例。</p>
<pre class="pre codeblock" id="CLI__codeblock_ibq_frq_qsb"><code>redis-cli -h <em class="ph i">Domain-Name</em> -p <em class="ph i">Port</em> -a <em class="ph i">Password</em></code></pre>
<ul class="ul" id="CLI__ul_uct_mfx_gsb">
<li class="li">
<p class="p">如果是集群实例,请执行:</p>
<pre class="pre codeblock" id="CLI__codeblock_kbq_frq_qsb"><code>redis-cli -c -h <em class="ph i">Domain-Name</em> -p <em class="ph i">Port</em> -a <em class="ph i">Password</em></code></pre>
</li>
<li class="li">以上命令中:<ul class="ul" id="CLI__ul_o41_j5q_qsb">
<li class="li">
<em class="ph i">Domain-Name</em>:Redis实例的<span class="ph uicontrol">访问域名</span><p class="p" id="CLI__d6e235">可参考<a class="xref" href="https://pacloud.paic.com.cn/ssr/help/database/Redis/User_Guide.Instance_Management.View_Instance" target="_blank" rel="external noopener">查看实例基本信息获取</a>。</p></li>
<li class="li">
<em class="ph i">Port</em>:Redis实例的<span class="ph uicontrol">连接端口</span>,<p class="p">可参考<a class="xref" href="https://pacloud.paic.com.cn/ssr/help/database/Redis/User_Guide.Instance_Management.View_Instance" target="_blank" rel="external noopener">查看实例基本信息获取</a>。</p></li>
<li class="li">
<em class="ph i">Password</em>:Redis实例的<span class="ph uicontrol">密码</span>,在创建Redis实例时设置,可参考<a class="xref" href="https://pacloud.paic.com.cn/ssr/help/database/Redis/User_Guide.Instance_Management.Create_Instance.general_instance" target="_blank" rel="external noopener">创建Redis实例</a>和<a class="xref" href="https://pacloud.paic.com.cn/ssr/help/database/Redis/User_Guide.Instance_Management.View_Instance" target="_blank" rel="external noopener">查看实例基本信息获取</a>。</li>
</ul></li>
</ul>
</div>
</li><li class="li step stepexpand">
<span class="ph cmd">执行以下命令,向Redis实例中写入数据。</span>
<div class="itemgroup info">
<pre class="pre codeblock"><code>set foo bar</code></pre>
</div>
</li></ol></section>
<section class="section result" id="CLI__result_gfq_zv2_ksb"><div class="tasklabel"><h2 class="doc-tairway">执行结果</h2></div>
<p class="p">执行以下命令,查看刚刚向Redis实例中写入的数据。</p>
<pre class="pre codeblock"><code>get foo</code></pre>
<p class="p"><img class="image" id="CLI__image_ozv_5rx_gsb" src="https://obs-cn-shanghai.fincloud.pinganyun.com/pacloud/20222411190036-13511f0d92ef.png"></p>
</section>
提交成功!非常感谢您的反馈,我们会继续努力做到更好!