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>&nbsp; &nbsp; 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>&nbsp; &nbsp; <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>&nbsp; &nbsp; b.&nbsp;执行以下命令解压下载的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 &ndash;zxvf 1.3.6.tar.gz</span></p> </td> </tr> </tbody> </table> <p>&nbsp; &nbsp; 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>&nbsp; &nbsp; 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>&nbsp; &nbsp; <strong>执行结果</strong></p> <p>&nbsp; &nbsp; 执行以下命令,确认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 = [{&quot;host&quot;: &quot;<em>Domain-Name</em>&quot;, &quot;port&quot;: &quot;<em>Port</em>&quot;}]</span></p> <p><span style="font-family:Consolas">rc = StrictRedisCluster(startup_nodes=startup_nodes,password=&#39;<em>Password</em>&#39;,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(&quot;foo&quot;, &quot;pingan yun&quot;)</span></p> </td> </tr> </tbody> </table> <p>&nbsp; &nbsp; <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>&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">查看实例基本信息</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.Create_Instance">创建实例</a>),可参考<a href="https://pinganyun.com/ssr/help/database/Redis/User_Guide.Instance_Management.View_Instance">查看实例基本信息</a>查看。</p> <p>&nbsp; &nbsp; <strong>执行结果</strong></p> <p>&nbsp; &nbsp; 执行以下命令,查看刚刚向Redis实例中写入的数据,系统返回<strong>pingan yun</strong>即连接成功。&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">print(rc.get(&quot;foo&quot;))</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>
以上内容是否解决了您的问题?
请补全提交信息!
咨询·建议

电话咨询

400-151-8800

邮件咨询

cloud@pingan.com

在线客服

工单支持

解决云产品相关技术问题