Files
doc-exports/docs/dcs/umn/dcs-ug-211202002.html
Chen, Junjie 2b90aa9fff DCS UMN 20240313 version
Reviewed-by: Mützel, Andrea <andrea.muetzel@t-systems.com>
Co-authored-by: Chen, Junjie <chenjunjie@huawei.com>
Co-committed-by: Chen, Junjie <chenjunjie@huawei.com>
2024-04-10 14:46:00 +00:00

51 lines
8.1 KiB
HTML

<a name="dcs-ug-211202002"></a><a name="dcs-ug-211202002"></a>
<h1 class="topictitle1">Predis</h1>
<div id="body32001227"><p id="dcs-ug-211202002__en-us_topic_0000001184480402_p8060118"><span class="keyword" id="dcs-ug-211202002__en-us_topic_0000001184480402_keyword1319762711391">Access a DCS Redis instance through Predis</span> on an ECS in the same VPC. For more information about how to use other Redis clients, visit <a href="https://redis.io/clients" target="_blank" rel="noopener noreferrer">the Redis official website</a>.</p>
<div class="section" id="dcs-ug-211202002__en-us_topic_0000001184480402_section21874355292"><h4 class="sectiontitle">Prerequisites</h4><ul id="dcs-ug-211202002__en-us_topic_0000001184480402_ul070064255113"><li id="dcs-ug-211202002__en-us_topic_0000001184480402_li3700942145119">A DCS Redis instance has been created and is in the <strong id="dcs-ug-211202002__en-us_topic_0000001184480402_b1763673015240">Running</strong> state.</li><li id="dcs-ug-211202002__en-us_topic_0000001184480402_li3648134612516">An ECS has been created. For details about how to create an ECS, see <a href="https://docs.otc.t-systems.com/en-us/usermanual/ecs/en-us_topic_0163572588.html" target="_blank" rel="noopener noreferrer"><em id="dcs-ug-211202002__en-us_topic_0000001184480402_i105481435145912">Elastic Cloud Server User Guide</em></a>.</li><li id="dcs-ug-211202002__en-us_topic_0000001184480402_li1618724916518">If the ECS runs the Linux OS, ensure that the PHP compilation environment has been installed on the ECS.</li></ul>
</div>
<div class="section" id="dcs-ug-211202002__en-us_topic_0000001184480402_section17228451112917"><h4 class="sectiontitle">Procedure</h4><ol id="dcs-ug-211202002__en-us_topic_0000001184480402_ol134127204302"><li id="dcs-ug-211202002__en-us_topic_0000001184480402_li1655151054317"><a name="dcs-ug-211202002__en-us_topic_0000001184480402_li1655151054317"></a><a name="en-us_topic_0000001184480402_li1655151054317"></a><span>View the IP address/domain name and port number of the DCS Redis instance to be accessed.</span><p><p id="dcs-ug-211202002__en-us_topic_0000001184480402_p165602107438">For details, see <a href="dcs-ug-0312016.html">Viewing Details of a DCS Instance</a>.</p>
</p></li><li id="dcs-ug-211202002__en-us_topic_0000001184480402_li175501736825"><span>Log in to the ECS.</span></li><li id="dcs-ug-211202002__en-us_topic_0000001184480402_li3412172043015"><span>Install the PHP development package and CLI tool. Run the following <strong id="dcs-ug-211202002__en-us_topic_0000001184480402_b155345529853330">yum</strong> command:</span><p><p id="dcs-ug-211202002__en-us_topic_0000001184480402_p72911632193010"><strong id="dcs-ug-211202002__en-us_topic_0000001184480402_b4106743113014">yum install php-devel php-common php-cli</strong></p>
</p></li><li id="dcs-ug-211202002__en-us_topic_0000001184480402_li79102583303"><span>After the installation is complete, check the version number to ensure that the installation is successful.</span><p><p id="dcs-ug-211202002__en-us_topic_0000001184480402_p12473141183120"><strong id="dcs-ug-211202002__en-us_topic_0000001184480402_b571512140316">php --version</strong></p>
</p></li><li id="dcs-ug-211202002__en-us_topic_0000001184480402_li143821348325"><span>Download the Predis package to the <strong id="dcs-ug-211202002__en-us_topic_0000001184480402_b26150486412">/usr/share/php</strong> directory.</span><p><ol type="a" id="dcs-ug-211202002__en-us_topic_0000001184480402_ol66871018163312"><li id="dcs-ug-211202002__en-us_topic_0000001184480402_li02428473330">Run the following command to download the Predis source file:<p id="dcs-ug-211202002__en-us_topic_0000001184480402_p18682164816333"><a name="dcs-ug-211202002__en-us_topic_0000001184480402_li02428473330"></a><a name="en-us_topic_0000001184480402_li02428473330"></a><strong id="dcs-ug-211202002__en-us_topic_0000001184480402_b1990617548338">wget https://github.com/predis/predis/archive/refs/tags/v2.2.2.tar.gz</strong></p>
<div class="note" id="dcs-ug-211202002__en-us_topic_0000001184480402_note1533491112343"><img src="public_sys-resources/note_3.0-en-us.png"><span class="notetitle"> </span><div class="notebody"><p id="dcs-ug-211202002__en-us_topic_0000001184480402_p12335161115343">This version is used as an example. To download Predis clients of other versions, visit the Redis or PHP official website.</p>
</div></div>
</li><li id="dcs-ug-211202002__en-us_topic_0000001184480402_li13465140163414">Run the following commands to decompress the source Predis package:<p id="dcs-ug-211202002__en-us_topic_0000001184480402_p159795473415"><a name="dcs-ug-211202002__en-us_topic_0000001184480402_li13465140163414"></a><a name="en-us_topic_0000001184480402_li13465140163414"></a><strong id="dcs-ug-211202002__en-us_topic_0000001184480402_b26249584347">tar -zxvf predis-2.2.2.tar.gz</strong></p>
</li><li id="dcs-ug-211202002__en-us_topic_0000001184480402_li199041691771">Rename the decompressed Predis directory <strong id="dcs-ug-211202002__en-us_topic_0000001184480402_b929622302018">predis</strong> and move it to <strong id="dcs-ug-211202002__en-us_topic_0000001184480402_b1781811304201">/usr/share/php/</strong>.<p id="dcs-ug-211202002__en-us_topic_0000001184480402_p29041718185"><strong id="dcs-ug-211202002__en-us_topic_0000001184480402_b116025311881">mv predis-2.2.2 predis</strong></p>
</li></ol>
</p></li><li id="dcs-ug-211202002__en-us_topic_0000001184480402_li167601915133513"><span>Edit a file used to connect to Redis.</span><p><ul id="dcs-ug-211202002__en-us_topic_0000001184480402_ul216216330355"><li id="dcs-ug-211202002__en-us_topic_0000001184480402_li171621433143514">Example of using <strong id="dcs-ug-211202002__en-us_topic_0000001184480402_b1735155511425">redis.php</strong> to connect to a single-node, master/standby, or Proxy Cluster DCS Redis instance:<pre class="screen" id="dcs-ug-211202002__en-us_topic_0000001184480402_screen860218458355">&lt;?php
require 'predis/autoload.php';
Predis\Autoloader::register();
$client = new Predis\Client([
'scheme' =&gt; 'tcp' ,
'host' =&gt; '{redis_instance_address}' ,
'port' =&gt;{port} ,
'password' =&gt; '{password}'
]);
$client-&gt;set('foo', 'bar');
$value = $client-&gt;get('foo');
echo $value;
?&gt;</pre>
</li><li id="dcs-ug-211202002__en-us_topic_0000001184480402_li191271619143614">Example code for using <strong id="dcs-ug-211202002__en-us_topic_0000001184480402_b1314143264319">redis-cluster.php</strong> to connect to Redis Cluster:<pre class="screen" id="dcs-ug-211202002__en-us_topic_0000001184480402_screen287413276361">&lt;?php
require 'predis/autoload.php';
$servers = array(
'tcp://{redis_instance_address}:{port}'
);
$options = array('cluster' =&gt; 'redis');
$client = new Predis\Client($servers, $options);
$client-&gt;set('foo', 'bar');
$value = $client-&gt;get('foo');
echo $value;
?&gt;</pre>
</li></ul>
<p id="dcs-ug-211202002__en-us_topic_0000001184480402_p4745163883612"><em id="dcs-ug-211202002__en-us_topic_0000001184480402_i55941710115420">{redis_instance_address}</em> indicates the actual IP address/domain name of the DCS instance and <em id="dcs-ug-211202002__en-us_topic_0000001184480402_i65941710145418">{port}</em> is the actual port number of DCS instance. For details about how to obtain the IP address/domain name and port, see <a href="#dcs-ug-211202002__en-us_topic_0000001184480402_li1655151054317">1</a>. Change them as required. <em id="dcs-ug-211202002__en-us_topic_0000001184480402_i127601612175418">{password}</em> indicates the password used to log in to the chosen DCS Redis instance. This password is defined during DCS Redis instance creation. If password-free access is required, delete the line that contains "password".</p>
</p></li><li id="dcs-ug-211202002__en-us_topic_0000001184480402_li549415116412"><span>Run the <strong id="dcs-ug-211202002__en-us_topic_0000001184480402_b2089518473449">php redis.php</strong> command to access the DCS instance.</span></li></ol>
</div>
</div>
<div>
<div class="familylinks">
<div class="parentlink"><strong>Parent topic:</strong> <a href="dcs-ug-211202001.html">PHP</a></div>
</div>
</div>