连接实例 通过Hiredis连接实例 主要介绍使用Hiredis访问GeminiDB Redis实例的方法。 前提条件 已成功创建GeminiDB Redis实例,且实例状态正常。 已创建弹性云主机,创建弹性云主机的方法,请参见《弹性云主机快速入门》中“登录弹性云主机”章节。 弹性云主机上已经安装GCC等编译工具。 操作步骤 1、获取GeminiDB Redis实例的内网IP地址或域名、端口。 内网IP地址和端口的查看方法请参见查看IP地址和端口。 内网域名获取方法请参见配置内网域名。 登录弹性云主机,具体操作请参见《弹性云主机快速入门》中“登录弹性云主机”。 使用如下命令,下载并解压Hiredis。 wget 进入到解压目录后编译安装Hiredis。 make make install 编写测试代码connRedisTst.cc。 说明 关于Hiredis的详细用法,请参考 代码如下: include include include include int main(int argc, char argv) { unsigned int j; redisContext conn; redisReply reply; if (argc err) { if (conn) { printf("Connection error: %sn", conn>errstr); redisFree(conn); } else { printf("Connection error: can't allocate redis contextn"); } exit(1); } / AUTH / reply redisCommand(conn, "AUTH %s", password); printf("AUTH: %sn", reply>str); freeReplyObject(reply); / Set / reply redisCommand(conn,"SET %s %s", "key", "hiredis test ok!"); printf("SET: %sn", reply>str); freeReplyObject(reply); / Get / reply redisCommand(conn,"GET key"); printf("GET key: %sn", reply>str); freeReplyObject(reply); / Disconnects and frees the context / redisFree(conn); return 0; } 执行如下命令进行编译。 gcc connRedis.c o connRedis I /usr/local/include/hiredis lhiredis 如果有报错,可查找hiredis.h文件路径,并修改编译命令。 编译完后得到一个可执行文件connRedis。 执行如下命令,连接GeminiDB Redis实例。 export LDLIBRARYPATH/usr/local/lib/:$LDLIBRARYPATH ./connRedis 8635 其中,以下信息需按照实际情况进行替换: 为[步骤1](