Access the Redis Instance from a Phpredis client
<p>After you create an ECS instance and a whitelist entry, install a phpredis client on the ECS instance and access the Redis instance from the client.</p>
<p><strong><span style="font-size:18px">Prerequisites</span></strong></p>
<ol>
<li>You have successfully created a Redis instance. For more information, see <a href="http://pinganyun.com/ssr/help/database/Redis/Getting_Started.Create_Redis" target="_blank">Create a Redis Instance</a>. Make sure the instance is in running status.</li>
<li>You have successfully created an ECS instance. For more information, see <a href="http://pinganyun.com/ssr/help/database/Redis/Getting_Started.Create_ECS" target="_blank">Create an ECS Instance</a>. Make sure the instance is in running status.</li>
<li>You have successfully created a whitelist entry. For more information, see <a href="http://pinganyun.com/ssr/help/database/Redis/Getting_Started.Create_Whitelist" target="_blank">Create a Whitelist Entry</a>.</li>
</ol>
<p><strong><span style="font-size:18px">Usage Guidelines</span></strong></p>
<p>This article uses a Linux-CentOS ECS instance as an example.</p>
<p><strong><span style="font-size:18px">Procedures</span></strong></p>
<p>1. Log in to the ECS instance.</p>
<p> a. Log in to the <a href="https://pinganyun.com/console/ecs/overview" target="_blank">ECS Console</a>.</p>
<p> b. In the left navigation pane, click <strong>Instance</strong> to enter the <strong>Instance Management</strong> page.</p>
<p> c. In the <strong>Operations</strong> column for the target instance, click <strong>ECS Console</strong>.</p>
<p> d. Enter the username and password.</p>
<p>2. Configure the editing environment.</p>
<p> a. Execute the following command to install compilation tools such as gcc-c++ and make:</p>
<pre>
<code>yum install gcc-c++ make</code></pre>
<p> b. Execute the following command to install the php development kit and command line tool:</p>
<pre>
<code>yum install php-devel php-common php-cli</code></pre>
<p><strong>Result</strong></p>
<p>Execute the following command to view the version of php:</p>
<pre>
<code>php --version</code></pre>
<p>3. Install the phpredis client. For more information, see <a href="http://github.com/phpredis/phpredis" target="_blank">Details</a>.</p>
<p> a. Execute the following command to download the phpredis client:</p>
<pre>
<code>wget https://pecl.php.net/get/redis-4.3.0.tgz</code></pre>
<p>or:</p>
<pre>
<code>wget https://github.com/phpredis/phpredis/archive/4.3.0.tar.gz</code></pre>
<p><img src="https://obs-cn-shanghai.yun.pingan.com/pcp-portal/20201507135105-10a7c5719660.png" style="height:20px; margin:0px; width:80px" />:</p>
<ul>
<li>Choose the phpredis version according to your business needs. This article uses phpredis 4.3.0 as an example.</li>
<li>You can download the phpredis client at https://pecl.php.net/package/redis or https://github.com/phpredis/phpredis/archive/4.3.0.tar.gz.</li>
</ul>
<p> b. Execute the following command to decompress the downloaded phpredis client file:</p>
<pre>
<code>tar –zxf redis-4.3.0.tgz</code></pre>
<p> c. Execute the following command to enter the redis-4.3.0 directory:</p>
<pre>
<code>cd redis-4.3.0</code></pre>
<p> d. Execute the following command to configure the <strong>php-config</strong> file:</p>
<pre>
<code>phpize
./configure --with-php-config=/usr/bin/php-config</code></pre>
<p><img src="https://obs-cn-shanghai.yun.pingan.com/pcp-portal/20201507135105-10a7c5719660.png" style="height:20px; margin:0px; width:80px" />: The installation methods of the phpredis client vary depending on the operating system. Execute the following command first to locate the php-config file:</p>
<pre>
<code>find / -name php-config</code></pre>
<p> e. Execute the following command to compile the phpredis client:</p>
<pre>
<code>make</code></pre>
<p> f. Execute the following command to install the phpredis client:</p>
<pre>
<code>make install</code></pre>
<p><strong>Result</strong></p>
<p>The system returns the following path when the installation succeeds.</p>
<p><img src="https://obs-cn-shanghai.yun.pingan.com/pcp-portal/20201507135311-1927b7d49a6c.png" style="height:46px; width:690px" /></p>
<p> g. Execute the following command to open the <strong>php.ini</strong> file:</p>
<pre>
<code>vim /etc/php.ini</code></pre>
<p><img src="https://obs-cn-shanghai.yun.pingan.com/pcp-portal/20201507135105-10a7c5719660.png" style="height:20px; margin:0px; width:80px" />: To obtain the location of the <strong>php.ini</strong> file, execute the find / -name php.ini command.</p>
<p> h. Add the <strong>extension</strong> configuration item to the <strong>php.ini</strong> file.</p>
<pre>
<code>extension = "/usr/lib64/php/modules/redis.so"</code></pre>
<p><img src="https://obs-cn-shanghai.yun.pingan.com/pcp-portal/20201507135105-10a7c5719660.png" style="height:20px; margin:0px; width:80px" />: To obtain the location of the <strong>redis. so</strong> file, execute the find / -name redis.so command.</p>
<p><strong>Result</strong></p>
<p>Execute the following command to verify that the phpredis client has been successfully installed:</p>
<pre>
<code>php -m |grep redis</code></pre>
<p>The system returns <strong>redis</strong>, indicating that the phpredis client has been successfully installed.</p>
<p><img src="https://obs-cn-shanghai.yun.pingan.com/pcp-portal/20201507135441-1115a236932f.png" style="height:44px; width:651px" /></p>
<p>4. Connect to the Redis instance.</p>
<p>Execute the following codes to create the <strong>phpredis-cluster.php</strong> file under any path:</p>
<pre>
<code><?php
$redis_info = "Domain-Name:Port";
$user_pwd = "Password";
$redisrc = new RedisCluster(NULL,Array($redis_info),3,3,true,$user_pwd);
$redisrc->set("welcome","pingan cloud redis");
$value = $redisrc->get("welcome");
echo $value;
$redisrc->close();
?></code></pre>
<p><img src="https://obs-cn-shanghai.yun.pingan.com/pcp-portal/20201507135105-10a7c5719660.png" style="height:20px; margin:0px; width:80px" />:</p>
<ul>
<li>Domain-Name: Access domain name of the Redis instance. For more information, see <a href="http://pinganyun.com/ssr/help/database/Redis/User_Guide.Instance_Management.View_Instance" target="_blank">View Basic Information about an Instance</a>.</li>
<li>Port: Port number of the Redis instance. For more information, see<a href="http://pinganyun.com/ssr/help/database/Redis/User_Guide.Instance_Management.View_Instance" target="_blank"> View Basic Information about an Instance</a>.</li>
<li>Password: Password of the Redis instance that has been set when you create the Redis instance. For more information, see <a href="http://pinganyun.com/ssr/help/database/Redis/User_Guide.Instance_Management.View_Instance" target="_blank">View Basic Information about an Instance</a>.</li>
</ul>
<p><strong>Result</strong></p>
<p>Execute the following command:</p>
<pre>
<code>php phpredis-cluster.php</code></pre>
<p>If the connection succeeded, the system returns pingan cloud redis.</p>
<p><img src="https://obs-cn-shanghai.yun.pingan.com/pcp-portal/20201507135652-1faf49a4976e.png" style="height:49px; width:719px" /> </p>
Did the above content solve your problem?
Yes
No
Submitted successfully! Thank you for your feedback, we will try our best to do better and better!