C++使用hiredis连接带密码的redis服务

时间:2021-01-10 07:17:32
 c = redisConnect((char*)redis_host, redis_port);
if (c->err) { /* Error flags, 0 when there is no error */
printf("连接Redis失败: %s\n", c->errstr);
exit();
}
else
{
printf("连接Redis成功!\n");
} reply = (redisReply *)redisCommand(c, "AUTH %s", redis_password);
if (reply->type == REDIS_REPLY_ERROR) {
printf("Redis认证失败!\n");
}
else
{
printf("Redis认证成功!\n");
}
freeReplyObject(reply);

就是在正常连接服务后加上auth命令传入密码