Connecting to an Instance Using Hiredis
This section describes how to use hiredis to access a GeminiDB Redis instance.
Prerequisites
- A GeminiDB Redis instance has been created and is running properly. For details about how to create a GeminiDB Redis instance, see Buying a Cluster Instance.
- 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.
- Run the following command to download and decompress the hiredis package.
wget https://github.com/redis/hiredis/archive/master.zip
- Go to the directory where the decompressed hiredis package is saved, and compile and install hiredis.
make
make install
- Write the test code connRedisTst.cc.
For details about how to use hiredis, see the usage description on the Redis official website.
The code is as follows:
#include <stdio.h> #include <stdlib.h> #include <string.h> #include <hiredis.h> int main(int argc, char **argv) { unsigned int j; redisContext *conn; redisReply *reply; if (argc < 3) { printf("Usage: example {instance_ip_address} 6379 {password}\n"); exit(0); } const char *hostname = argv[1]; const int port = atoi(argv[2]); const char *password = argv[3]; struct timeval timeout = { 1, 500000 }; // 1.5 seconds conn = redisConnectWithTimeout(hostname, port, timeout); if (conn == NULL || conn->err) { if (conn) { printf("Connection error: %s\n", conn->errstr); redisFree(conn); } else { printf("Connection error: can't allocate redis context\n"); } exit(1); } /* AUTH */ reply = redisCommand(conn, "AUTH %s", password); printf("AUTH: %s\n", reply->str); freeReplyObject(reply); /* Set */ reply = redisCommand(conn,"SET %s %s", "key", "hiredis test ok!"); printf("SET: %s\n", reply->str); freeReplyObject(reply); /* Get */ reply = redisCommand(conn,"GET key"); printf("GET key: %s\n", reply->str); freeReplyObject(reply); /* Disconnects and frees the context */ redisFree(conn); return 0; }
- Run the following command to perform compilation:
gcc connRedis.c -o connRedis -I /usr/local/include/hiredis -lhiredis
If an error is reported, locate the directory where the hiredis.h file is stored and modify the compilation command.
After the compilation, an executable connRedis file is obtained.
- Run the following command to connect to the instance.
export LD_LIBRARY_PATH=/usr/local/lib/:$LD_LIBRARY_PATH
./connRedis <redis_ip_address> 8635 <password>
Replace the following information based on the site requirements:
- <redis_ip_address> indicates the load balancer IP address obtained in 1.
- 8635 is the port for accessing the GeminiDB Redis instance.
- <password> indicates the password set when the instance is created.
- If the following information is displayed, the instance is successfully connected.
AUTH: OK SET: OK GET key: Hello, hiredis test ok!
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