phpredis连接
<p>本文主要介绍创建ECS并开通白名单后,如何在ECS上安装phpredis客户端,并通过phpredis客户端访问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. 配置编辑环境。</p>
<p> a. 执行以下命令安装gcc-c++和make等编译工具。</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-c++ make</span></p>
</td>
</tr>
</tbody>
</table>
<p> b. 执行以下命令安装php开发包与命令行工具。</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 php-devel php-common php-cli</span></p>
</td>
</tr>
</tbody>
</table>
<p><strong> 执行结果</strong></p>
<p> 执行以下命令查看php的版本号。</p>
<table border="0" cellpadding="0" cellspacing="0">
<tbody>
<tr>
<td style="background-color:#f7f8fa; vertical-align:top">
<p><span style="font-family:Consolas">php </span>--<span style="font-family:Consolas">version</span></p>
</td>
</tr>
</tbody>
</table>
<p>3. 安装phpredis客户端,<a href="https://github.com/phpredis/phpredis">详细信息</a>。</p>
<p> a. 执行以下命令下载phpredis客户端。</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://pecl.php.net/get/redis-4.3.0.tgz</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">wget https://github.com/phpredis/phpredis/archive/4.3.0.tar.gz</span></p>
</td>
</tr>
</tbody>
</table>
<p> <img src="https://obs-cn-shanghai.ocftcloud.com/pacloud/20190907111430-14acabbb96f8.png" style="height:26px; margin:0px; width:65px" />:</p>
<p> • phpredis客户端的版本您可根据实际业务情况选择,本文以4.3.0版本为例。</p>
<p> • phpredis客户端下载地址:https://pecl.php.net/package/redis 或 https://github.com/phpredis/phpredis/archive/4.3.0.tar.gz。</p>
<p> b. 执行以下命令解压下载的phpredis客户端文件。</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 –zxf redis-4.3.0.tgz</span></p>
</td>
</tr>
</tbody>
</table>
<p> c. 执行以下命令进入redis-4.3.0目录。</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-4.3.0</span></p>
</td>
</tr>
</tbody>
</table>
<p> d. 执行以下命令配置<strong><span style="font-family:Consolas">php-config</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">phpize</span></p>
<p><span style="font-family:Consolas">./configure --with-php-config=/usr/bin/php-config</span></p>
</td>
</tr>
</tbody>
</table>
<p> <img src="https://obs-cn-shanghai.ocftcloud.com/pacloud/20190907111430-14acabbb96f8.png" style="height:26px; margin:0px; width:65px" />:</p>
<p> 不同的操作系统,phpredis客户端的安装方式也不同,请先执行以下命令,确认<strong><span style="font-family:Consolas">php-config</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">find / -name php-config</span></p>
</td>
</tr>
</tbody>
</table>
<p> e. 执行以下命令编译phpredis客户端。</p>
<table border="0" cellpadding="0" cellspacing="0">
<tbody>
<tr>
<td style="background-color:#f7f8fa; vertical-align:top">
<p><span style="font-family:Consolas">make</span></p>
</td>
</tr>
</tbody>
</table>
<p> f. 执行以下命令安装phpredis客户端。</p>
<table border="0" cellpadding="0" cellspacing="0">
<tbody>
<tr>
<td style="background-color:#f7f8fa; vertical-align:top">
<p><span style="font-family:Consolas">make install</span></p>
</td>
</tr>
</tbody>
</table>
<p><strong> 执行结果</strong></p>
<p> 安装成功之后,系统返回以下路径。</p>
<p><img src="https://obs-cn-shanghai.ocftcloud.com/pacloud/20190907111833-1c6323649e46.png" style="height:46px; width:690px" /></p>
<p> g. 执行以下命令打开<span style="font-family:Consolas"><strong>php.ini</strong></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">vim /etc/php.ini</span></p>
</td>
</tr>
</tbody>
</table>
<p> <img src="https://obs-cn-shanghai.ocftcloud.com/pacloud/20190907111430-14acabbb96f8.png" style="height:26px; margin:0px; width:65px" />:<span style="font-family:Consolas"><strong>php.ini</strong></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">find / -name php.ini</span></p>
</td>
</tr>
</tbody>
</table>
<p> h. 在<span style="font-family:Consolas"><strong>php.ini</strong></span>文件中增加<strong>extension</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">extension = "/usr/lib64/php/modules/redis.so"</span></p>
</td>
</tr>
</tbody>
</table>
<p> <img src="https://obs-cn-shanghai.ocftcloud.com/pacloud/20190907111430-14acabbb96f8.png" style="height:26px; margin:0px; width:65px" />:<span style="font-family:Consolas"><strong>redis.so</strong></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">find / -name redis.so</span></p>
</td>
</tr>
</tbody>
</table>
<p><strong> 执行结果</strong></p>
<p> 执行以下命令,确认phpredis客户端安装成功。</p>
<table border="0" cellpadding="0" cellspacing="0">
<tbody>
<tr>
<td style="background-color:#f7f8fa; vertical-align:top">
<p><span style="font-family:Consolas">php -m |grep redis</span></p>
</td>
</tr>
</tbody>
</table>
<p> 系统返回<strong>redis</strong>,表示phpredis客户端安装成功。</p>
<p><img src="https://obs-cn-shanghai.ocftcloud.com/pacloud/20190907112144-1fc6a49091fd.png" style="height:44px; width:651px" /></p>
<p>4. 连接Redis实例。</p>
<p>• 连接主从实例:</p>
<p>执行以下代码,在任意路径下创建<span style="font-family:Consolas"><strong>phpredis.php</strong></span>文件。</p>
<table border="0" cellpadding="0" cellspacing="0" style="width:599px">
<tbody>
<tr>
<td style="background-color:#f7f8fa; vertical-align:top; width:595px">
<p><span style="font-family:Consolas"><?php</span></p>
<p><span style="font-family:Consolas"> $redis_host = "<em>Domain-name</em>";</span></p>
<p><span style="font-family:Consolas"> $redis_port = <em>Port</em>;</span></p>
<p><span style="font-family:Consolas"> $user_pwd = "<em>Password</em>";</span></p>
<p><span style="font-family:Consolas"> $redis = new Redis();</span></p>
<p><span style="font-family:Consolas"> if ($redis->connect($redis_host, $redis_port) == false) {</span></p>
<p><span style="font-family:Consolas"> die($redis->getLastError());</span></p>
<p><span style="font-family:Consolas"> }</span></p>
<p><span style="font-family:Consolas"> if ($redis->auth($user_pwd) == false) {</span></p>
<p><span style="font-family:Consolas"> die($redis->getLastError());</span></p>
<p><span style="font-family:Consolas"> }</span></p>
<p><span style="font-family:Consolas"> if ($redis->set("welcome", " fincloud redis ") == false) {</span></p>
<p><span style="font-family:Consolas"> die($redis->getLastError());</span></p>
<p><span style="font-family:Consolas"> }</span></p>
<p><span style="font-family:Consolas"> $value = $redis->get("welcome");</span></p>
<p><span style="font-family:Consolas"> echo $value;</span></p>
<p><span style="font-family:Consolas"> $redis->close();</span></p>
<p><span style="font-family:Consolas">?></span></p>
</td>
</tr>
</tbody>
</table>
<p> <img src="https://obs-cn-shanghai.ocftcloud.com/pacloud/20190907111430-14acabbb96f8.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> 执行以下命令,系统返回<strong>fincloud redis</strong>即连接成功。</p>
<table border="0" cellpadding="0" cellspacing="0">
<tbody>
<tr>
<td style="background-color:#f7f8fa; vertical-align:top">
<p>php phpredis.php</p>
</td>
</tr>
</tbody>
</table>
<p>• 连接集群实例:</p>
<p>执行以下代码,在任意路径下创建<span style="font-family:Consolas"><strong>phpredis-cluster.php</strong></span>文件。</p>
<table border="0" cellpadding="0" cellspacing="0" style="width:600px">
<tbody>
<tr>
<td style="background-color:#f7f8fa; vertical-align:top; width:596px">
<p><span style="font-family:Consolas"><?php</span></p>
<p><span style="font-family:Consolas"> $redis_info = "<em>Domain-Name:Port</em>";</span></p>
<p><span style="font-family:Consolas"> $user_pwd = "<em>Password</em>";</span></p>
<p><span style="font-family:Consolas"> $redisrc = new RedisCluster(NULL,Array($redis_info),3,3,true,$user_pwd);</span></p>
<p><span style="font-family:Consolas"> $redisrc->set("welcome","fincloud redis");</span></p>
<p><span style="font-family:Consolas"> $value = $redisrc->get("welcome");</span></p>
<p><span style="font-family:Consolas"> echo $value;</span></p>
<p><span style="font-family:Consolas"> $redisrc->close();</span></p>
<p><span style="font-family:Consolas">?></span></p>
</td>
</tr>
</tbody>
</table>
<p> <img src="https://obs-cn-shanghai.ocftcloud.com/pacloud/20190907111430-14acabbb96f8.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> 执行以下命令,系统返回<strong>fincloud redis</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">php </span>phpredis-cluster<span style="font-family:Consolas">.php</span></p>
</td>
</tr>
</tbody>
</table>
提交成功!非常感谢您的反馈,我们会继续努力做到更好!