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://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>
</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://www.ocftcloud.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/andymccurdy/redis-py">redis-py详细信息</a>,集群版客户端<a href="https://github.com/Grokzen/redis-py-cluster">redis-py-cluster详细信息</a>。</p>
<p> a. 执行以下命令下载Python Redis客户端。</p>
<p> • 主从版:</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/andymccurdy/redis-py/archive/3.2.1.tar.gz</span></p>
</td>
</tr>
</tbody>
</table>
<p> <img src="https://obs-cn-shanghai.ocftcloud.com/pacloud/20191908145749-1285e88d928c.png" style="height:26px; margin:0px; width:65px" />:主从版redis-py的版本需要大于等于2.10.5。</p>
<p> • 集群版:</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://obs-cn-shanghai.ocftcloud.com/pacloud/20190907113032-15a9b23a9604.png" style="height:26px; margin:0px; width:65px" />:集群版redis-py-cluster的版本需要大于等于1.2.0。</p>
<p> b. 执行以下命令解压下载的Python Redis客户端文件。</p>
<p> • 主从版:</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 3.2.1.tar.gz</span></p>
</td>
</tr>
</tbody>
</table>
<p> • 集群版:</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>
<p> • 主从版:</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-3.2.1</span></p>
</td>
</tr>
</tbody>
</table>
<p> • 集群版:</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://obs-cn-shanghai.ocftcloud.com/pacloud/20190907113350-1e32928693d4.png" style="height:97px; width:830px" /></p>
<p>4. 连接Redis实例。</p>
<p> • 连接主从实例:</p>
<p> a. 执行以下命令,连接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 redis import StrictRedis</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">r = StrictRedis(host='<em> Domain-Name</em> ', port= port, password='<em> Password</em> ');</span></p>
</td>
</tr>
</tbody>
</table>
<p> <img src="https://obs-cn-shanghai.ocftcloud.com/pacloud/20190907113032-15a9b23a9604.png" style="height:26px; margin:0px; width:65px" />:</p>
<p><em> </em>• <em>Domain-Name</em>:Redis实例的<strong>访问域名</strong>,可参考<a href="https://www.ocftcloud.com/ssr/help/database/Redis/User_Guide.Instance_Management.View_Instance">查看实例基本信息</a>。</p>
<p><em> </em>• <em>Port</em>:Redis实例的<strong>连接端口</strong>,可参考<a href="https://www.ocftcloud.com/ssr/help/database/Redis/User_Guide.Instance_Management.View_Instance">查看实例基本信息</a>。</p>
<p><em> </em>• <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>查看。</p>
<p> b. 执行以下命令向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">r.set("foo", "fincloud redis")</span></p>
</td>
</tr>
</tbody>
</table>
<p><strong>执行结果</strong></p>
<p>执行以下命令,查看刚刚向Redis实例中写入的数据,系统返回<span style="font-family:Consolas">fincloud redis</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">print(r.get("foo"))</span></p>
</td>
</tr>
</tbody>
</table>
<p> • 连接集群实例:</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", "fincloud redis")</span></p>
</td>
</tr>
</tbody>
</table>
<p> <img src="https://obs-cn-shanghai.ocftcloud.com/pacloud/20190907113032-15a9b23a9604.png" style="height:26px; margin:0px; width:65px" />:</p>
<p><em> • Domain-Name</em>:Redis实例的<strong>访问域名</strong>,可参考<a href="https://www.ocftcloud.com/ssr/help/database/Redis/User_Guide.Instance_Management.View_Instance">查看实例基本信息</a>。</p>
<p><em> • Port</em>:Redis实例的<strong>连接端口</strong>,可参考<a href="https://www.ocftcloud.com/ssr/help/database/Redis/User_Guide.Instance_Management.View_Instance">查看实例基本信息</a>。</p>
<p><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>查看。</p>
<p><strong>执行结果</strong></p>
<p>执行以下命令,查看刚刚向Redis实例中写入的数据,系统返回<strong><span style="font-family:Consolas">fincloud redis</span></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>
提交成功!非常感谢您的反馈,我们会继续努力做到更好!