Connecting to an Instance Using PHP
This section describes how to use PHP to access a GeminiDB Redis instance.
Prerequisites
- A GeminiDB Redis instance has been created and is in the Available status.
- An ECS is available. For details, see Purchasing an ECS.
- GNU Compiler Collection (GCC) has been installed on the ECS.
- The created ECS is in the same region, AZ, VPC, and security group as the GeminiDB Redis instance.
Procedure
- Obtain the load balancer IP address and port of the GeminiDB Redis instance that you want to access.
- For how to obtain the load balancer IP address, see Viewing the Load Balancer IP Address and Port.
- For how to obtain the port, see Viewing the Port for Accessing Each Instance Node.
- Log in to the ECS. For details, see Logging In to an ECS in Getting Started with Elastic Cloud Server.
- Install the PHP development kit and command line tool.
Run the following yum command to install the PHP development package:
yum install php-devel php-common php-cli
CentOS (Red Hat series) is used as an example. If Ubuntu (Debian series) is used, run the corresponding installation command.
- After the installation is complete, check the version number to ensure that the installation is successful.
php --version
- Install the PHP client of Redis.
- Run the following command to download the source phpredis package:
wget http://pecl.php.net/get/redis-4.1.0RC3.tgz
The preceding clients are of the latest version. You can download the phpredis client of other versions from the PHP official website.
- Run the following commands to decompress the source phpredis package:
cd redis-4.1.0RC3
- Run the following extension command before compilation:
- Run the following command to configure the php-config file:
./configure --with-php-config=/usr/bin/php-config
The PHP installation method and location depend on the operating system. Before the configuration, run the find / -name php.ini command to check the directory of the file.
- Run the following command to compile and install the phpredis client:
- After the installation, add the extension configuration in the php.ini file to reference the Redis module.
Run the following command to find the php.ini file:
vim /usr/local/php/etc/php.ini
Add the following configuration item to the php.ini file:
extension = "/usr/lib64/php/modules/redis.so"
The directories of the php.ini and redis.so files may be different. You can run the following command to query the directories.
find / -name php.ini
find / -name redis.so
- Save the configuration and exit. Then, run the following command to check whether the extension takes effect:
If redis is returned, the PHP Redis client environment has been set up.
- Run the following command to download the source phpredis package:
- Use the phpredis client to connect to the instance.
- Compile the test code redis.php.
- Connect to a GeminiDB Redis cluster using the SDK for the single-node API on PHP.
<?php // There will be security risks if the username and password used for authentication are directly written into code. Store the username and password in ciphertext in the configuration file or environment variables. // In this example, the username and password are stored in the environment variables. Before running this example, set environment variables EXAMPLE_USERNAME_ENV and EXAMPLE_PASSWORD_ENV as needed. $pwd =getenv('EXAMPLE_PASSWORD_ENV'); $redis_host = "192.xx.xx.xx"; //Enter the load balancer IP address obtained in step 1. $redis_port = 8635; $user_pwd = pwd; $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("key", "php test ok!") == false) { die($redis->getLastError()); } $value = $redis->get("key"); echo $value; $redis->close(); ?>
- Connect to the GeminiDB Redis cluster using the SDK for the cluster API on PHP.
<?php $redis_host = "192.xx.xx.xx"; //Enter the load balancer IP address obtained in step 1. $redis_port = 8635; $user_pwd = "pwd"; // Connect with read/write timeout as well as specify that phpredis should use // persistent connections to each node. $redis = new RedisCluster(NULL, Array("$redis_host:$redis_port"), 1.5, 1.5, true, $user_pwd); if ($redis->set("key", "php test ok!") == false) { die($redis->getLastError()); } $value = $redis->get("key"); echo $value; $redis->close(); ?>
- Connect to a GeminiDB Redis cluster using the SDK for the single-node API on PHP.
- Run the redis.php command to check whether the result is normal.
- Compile the test code redis.php.
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