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>&nbsp; &nbsp; 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>&nbsp; &nbsp; 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>&nbsp; &nbsp; 执行结果</strong></p> <p>&nbsp; &nbsp; 执行以下命令查看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>&nbsp; &nbsp; 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>&nbsp; &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">wget https://github.com/phpredis/phpredis/archive/4.3.0.tar.gz</span></p> </td> </tr> </tbody> </table> <p>&nbsp; &nbsp;&nbsp;<img src="https://obs-cn-shanghai.ocftcloud.com/pacloud/20190907111430-14acabbb96f8.png" style="height:26px; margin:0px; width:65px" />:</p> <p>&nbsp; &nbsp; &bull;&nbsp; phpredis客户端的版本您可根据实际业务情况选择,本文以4.3.0版本为例。</p> <p>&nbsp; &nbsp; &bull;&nbsp; phpredis客户端下载地址:https://pecl.php.net/package/redis 或 https://github.com/phpredis/phpredis/archive/4.3.0.tar.gz。</p> <p>&nbsp; &nbsp; 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 &ndash;zxf redis-4.3.0.tgz</span></p> </td> </tr> </tbody> </table> <p>&nbsp; &nbsp; 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>&nbsp; &nbsp; 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>&nbsp; &nbsp;&nbsp;<img src="https://obs-cn-shanghai.ocftcloud.com/pacloud/20190907111430-14acabbb96f8.png" style="height:26px; margin:0px; width:65px" />:</p> <p>&nbsp; &nbsp; 不同的操作系统,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>&nbsp; &nbsp; 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>&nbsp; &nbsp; 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>&nbsp; &nbsp; 执行结果</strong></p> <p>&nbsp; &nbsp; 安装成功之后,系统返回以下路径。</p> <p><img src="https://obs-cn-shanghai.ocftcloud.com/pacloud/20190907111833-1c6323649e46.png" style="height:46px; width:690px" /></p> <p>&nbsp; &nbsp; 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>&nbsp; &nbsp;&nbsp;<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>&nbsp; &nbsp; 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 = &quot;/usr/lib64/php/modules/redis.so&quot;</span></p> </td> </tr> </tbody> </table> <p>&nbsp; &nbsp;&nbsp;<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>&nbsp; &nbsp; 执行结果</strong></p> <p>&nbsp; &nbsp; 执行以下命令,确认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>&nbsp; &nbsp; 系统返回<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.&nbsp;连接Redis实例。</p> <p>&bull;&nbsp; &nbsp;连接主从实例:</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">&lt;?php</span></p> <p><span style="font-family:Consolas">&nbsp;&nbsp;&nbsp; $redis_host = &quot;<em>Domain-name</em>&quot;;</span></p> <p><span style="font-family:Consolas">&nbsp;&nbsp;&nbsp; $redis_port = <em>Port</em>;</span></p> <p><span style="font-family:Consolas">&nbsp;&nbsp;&nbsp; $user_pwd = &quot;<em>Password</em>&quot;;</span></p> <p><span style="font-family:Consolas">&nbsp;&nbsp;&nbsp; $redis = new Redis();</span></p> <p><span style="font-family:Consolas">&nbsp;&nbsp;&nbsp; if ($redis-&gt;connect($redis_host, $redis_port) == false) {</span></p> <p><span style="font-family:Consolas">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; die($redis-&gt;getLastError());</span></p> <p><span style="font-family:Consolas">&nbsp;&nbsp;&nbsp; }</span></p> <p><span style="font-family:Consolas">&nbsp;&nbsp;&nbsp; if ($redis-&gt;auth($user_pwd) == false) {</span></p> <p><span style="font-family:Consolas">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; die($redis-&gt;getLastError());</span></p> <p><span style="font-family:Consolas">&nbsp;&nbsp;&nbsp; }</span></p> <p><span style="font-family:Consolas">&nbsp;&nbsp;&nbsp; if ($redis-&gt;set(&quot;welcome&quot;, &quot; fincloud redis &quot;) == false) {</span></p> <p><span style="font-family:Consolas">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; die($redis-&gt;getLastError());</span></p> <p><span style="font-family:Consolas">&nbsp;&nbsp;&nbsp; }</span></p> <p><span style="font-family:Consolas">&nbsp;&nbsp;&nbsp; $value = $redis-&gt;get(&quot;welcome&quot;);</span></p> <p><span style="font-family:Consolas">&nbsp;&nbsp;&nbsp; echo $value;</span></p> <p><span style="font-family:Consolas">&nbsp;&nbsp;&nbsp; $redis-&gt;close();</span></p> <p><span style="font-family:Consolas">?&gt;</span></p> </td> </tr> </tbody> </table> <p>&nbsp; &nbsp;&nbsp;<img src="https://obs-cn-shanghai.ocftcloud.com/pacloud/20190907111430-14acabbb96f8.png" style="height:26px; margin:0px; width:65px" />:</p> <p><em>&nbsp; &nbsp; &bull;&nbsp; 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>&nbsp; &nbsp; &bull;&nbsp; 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>&nbsp; &nbsp; &bull;&nbsp; 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>&nbsp; &nbsp; 执行结果</strong></p> <p>&nbsp; &nbsp; 执行以下命令,系统返回<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>&bull;&nbsp; &nbsp;连接集群实例:</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">&lt;?php</span></p> <p><span style="font-family:Consolas">&nbsp;&nbsp;&nbsp; $redis_info = &quot;<em>Domain-Name:Port</em>&quot;;</span></p> <p><span style="font-family:Consolas">&nbsp;&nbsp;&nbsp; $user_pwd = &quot;<em>Password</em>&quot;;</span></p> <p><span style="font-family:Consolas">&nbsp;&nbsp;&nbsp; $redisrc = new RedisCluster(NULL,Array($redis_info),3,3,true,$user_pwd);</span></p> <p><span style="font-family:Consolas">&nbsp;&nbsp;&nbsp; $redisrc-&gt;set(&quot;welcome&quot;,&quot;fincloud redis&quot;);</span></p> <p><span style="font-family:Consolas">&nbsp;&nbsp;&nbsp; $value = $redisrc-&gt;get(&quot;welcome&quot;);</span></p> <p><span style="font-family:Consolas">&nbsp;&nbsp;&nbsp; echo $value;</span></p> <p><span style="font-family:Consolas">&nbsp;&nbsp;&nbsp; $redisrc-&gt;close();</span></p> <p><span style="font-family:Consolas">?&gt;</span></p> </td> </tr> </tbody> </table> <p>&nbsp; &nbsp;&nbsp;<img src="https://obs-cn-shanghai.ocftcloud.com/pacloud/20190907111430-14acabbb96f8.png" style="height:26px; margin:0px; width:65px" />:</p> <p><em>&nbsp; &nbsp; &bull;&nbsp; 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>&nbsp; &nbsp; &bull;&nbsp; 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>&nbsp; &nbsp; &bull;&nbsp; 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>&nbsp; &nbsp; 执行结果</strong></p> <p>&nbsp; &nbsp; 执行以下命令,系统返回<strong>fincloud redis</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">php </span>phpredis-cluster<span style="font-family:Consolas">.php</span></p> </td> </tr> </tbody> </table>
以上内容是否解决了您的问题?
请补全提交信息!
联系我们

电话咨询

400-151-8800

邮件咨询

fincloud@ocft.com

在线客服

工单支持

解决云产品相关技术问题