Connecting to Redis on phpredis (PHP)
This section describes how to connect to Redis on phpredis. For more information about how to use other Redis clients, visit the Redis official website.
The operations described in this section apply only to single-node, master/standby, and Proxy Cluster instances. To use phpredis to connect to a Redis Cluster instance, see the phpredis description.
Prerequisites
- A DCS Redis instance has been created and is in the Running state.
- An ECS has been created. For details about how to create an ECS, see the Elastic Cloud Server User Guide.
- If the ECS runs the Linux OS, ensure that the GCC compilation environment has been installed on the ECS.
Connecting to Redis on phpredis
- View the IP address and port number of the DCS Redis instance to be accessed.
For details, see Viewing Details of a DCS Instance.
- Log in to the ECS.
The following uses CentOS as an example to describe how to access an instance through phpredis.
- Install GCC-C++ and Make compilation components.
yum install gcc-c++ make
- Install the PHP development package and CLI tool.
Run the following yum command to install the PHP development package:
yum install php-devel php-common php-cli
After the installation is complete, run the following command to query the PHP version and check whether the installation is successful:
php --version
- Install the phpredis client.
- Download the source phpredis package.
wget http://pecl.php.net/get/redis-5.3.7.tgz
This version is used as an example. To download phpredis clients of other versions, visit the Redis or PHP official website.
- Decompress the source phpredis package.
cd redis-5.3.7
- Command before compilation.
- Configure the php-config file.
./configure --with-php-config=/usr/bin/php-config
The location of the file varies depending on the OS and PHP installation mode. You are advised to locate the directory where the file is saved before the configuration.
find / -name php-config
- Compile and install the phpredis client.
- After the installation, add the extension configuration in the php.ini file to reference the Redis module.
vim /etc/php.ini
Add the following configuration:
extension = "/usr/lib64/php/modules/redis.so"
The redis.so file may be saved in a different directory from php.ini. Run the following command to locate the directory:
find / -name php.ini
- Save the configuration and exit. Then, run the following command to check whether the extension takes effect:
If the command output contains redis, the phpredis client environment has been set up.
- Download the source phpredis package.
- Access the DCS instance by using phpredis.
- Edit a redis.php file.
<?php $redis_host = "{redis_instance_address}"; $redis_port = {port}; $user_pwd = "{password}"; $redis = new Redis(); if ($redis->connect($redis_host, $redis_port) == false) { die($redis->getLastError()); } if ($redis->auth($user_pwd) == false) { die($redis->getLastError()); } if ($redis->set("welcome", "Hello, DCS for Redis!") == false) { die($redis->getLastError()); } $value = $redis->get("welcome"); echo $value; $redis->close(); ?>
{redis_instance_address} indicates the example IP address of the DCS instance and {port} indicates the port number of the DCS instance. For details about how to obtain the IP address and port, see 1. Change them as required. {password} 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 enabled, shield the if statement for password authentication.
- Run the php redis.php command to access the DCS instance.
- Edit a redis.php file.
Feedback
Was this page helpful?
Provide feedbackThank you very much for your feedback. We will continue working to improve the documentation.See the reply and handling status in My Cloud VOC.
For any further questions, feel free to contact us through the chatbot.
Chatbot