Python连接
<p>本文主要介绍创建ECS并开通白名单后,如何在ECS上安装Python Redis客户端,并通过Python Redis客户端访问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. 配置编辑环境。执行以下命令,安装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">yum install python</span></p>
</td>
</tr>
</tbody>
</table>
<p>3. 安装Python Redis客户端,可参考<a href="https://github.com/Grokzen/redis-py-cluster">redis-py-cluster详细信息</a>。</p>
<p> a. 执行以下命令下载Python Redis客户端。</p>
<table border="0" cellpadding="0" cellspacing="0">
<tbody>
<tr>
<td style="background-color:#f7f8fa; vertical-align:top">
<p>wget https://github.com/Grokzen/redis-py-cluster/archive/1.3.6.tar.gz</p>
</td>
</tr>
</tbody>
</table>
<p> <img src="https://pcp-portal-sca.obs-cn-shenzhen.pinganyun.com/pcp-portal-sca/20190907113032-15a9b23a9604.png" style="height:26px; margin:0px; width:65px" />:集群版redis-py-cluster的版本需要大于等于1.2.0。</p>
<p> b. 执行以下命令解压下载的Python 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 –zxvf 1.3.6.tar.gz</span></p>
</td>
</tr>
</tbody>
</table>
<p> c. 执行以下命令进入解压后的Python 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-py-cluster-1.3.6</span></p>
</td>
</tr>
</tbody>
</table>
<p> d. 执行以下命令安装Python 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">python setup.py install</span></p>
</td>
</tr>
</tbody>
</table>
<p> <strong>执行结果</strong></p>
<p> 执行以下命令,确认Python 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">phthon</span></p>
</td>
</tr>
</tbody>
</table>
<p>系统返回以下信息说明安装成功。</p>
<p><img src="https://pcp-portal-sca.obs-cn-shenzhen.pinganyun.com/pcp-portal-sca/20190907113350-1e32928693d4.png" style="height:97px; width:830px" /></p>
<p>4. 连接Redis实例。</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">from rediscluster import StrictRedisCluster</span></p>
<p><span style="font-family:Consolas">startup_nodes = [{"host": "<em>Domain-Name</em>", "port": "<em>Port</em>"}]</span></p>
<p><span style="font-family:Consolas">rc = StrictRedisCluster(startup_nodes=startup_nodes,password='<em>Password</em>',max_connections=32, max_connections_per_node=True,socket_timeout=5000,socket_connect_timeout=3000,retry_on_timeout=True )</span></p>
<p><span style="font-family:Consolas">rc.set("foo", "pingan yun")</span></p>
</td>
</tr>
</tbody>
</table>
<p> <img src="https://pcp-portal-sca.obs-cn-shenzhen.pinganyun.com/pcp-portal-sca/20190907113032-15a9b23a9604.png" style="height:26px; margin:0px; width:65px" />:</p>
<p> • <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> • <em>Port</em>:Redis实例的<strong>连接端口</strong>,可参考<a href="https://pinganyun.com/ssr/help/database/Redis/User_Guide.Instance_Management.View_Instance">查看实例基本信息</a>。</p>
<p> • <em>Password</em>:Redis实例的<strong>密码</strong>,在创建Redis实例时设置(可参考<a href="https://pinganyun.com/ssr/help/database/Redis/User_Guide.Instance_Management.Create_Instance">创建实例</a>),可参考<a href="https://pinganyun.com/ssr/help/database/Redis/User_Guide.Instance_Management.View_Instance">查看实例基本信息</a>查看。</p>
<p> <strong>执行结果</strong></p>
<p> 执行以下命令,查看刚刚向Redis实例中写入的数据,系统返回<strong>pingan yun</strong>即连接成功。 </p>
<table border="0" cellpadding="0" cellspacing="0">
<tbody>
<tr>
<td style="background-color:#f7f8fa; vertical-align:top">
<p><span style="font-family:Consolas">print(rc.get("foo"))</span></p>
</td>
</tr>
</tbody>
</table>
<p><img src="https://pcp-portal-sca.obs-cn-shenzhen.pinganyun.com/pcp-portal-sca/20190907113614-12d235779483.png" style="height:71px; width:306px" /></p>
提交成功!非常感谢您的反馈,我们会继续努力做到更好!