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

 1 c = redisConnect((char*)redis_host, redis_port);
 2 if (c->err) {    /* Error flags, 0 when there is no error */
 3 printf("连接Redis失败: %s\n", c->errstr);
 4 exit(1);
 5 }
 6 else
 7 {
 8 printf("连接Redis成功!\n");
 9 }
10 
11 reply = (redisReply *)redisCommand(c, "AUTH %s", redis_password);
12 if (reply->type == REDIS_REPLY_ERROR) {
13 printf("Redis认证失败!\n");
14 }
15 else
16 {
17 printf("Redis认证成功!\n");
18 }
19 freeReplyObject(reply);

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

posted on 2017-02-13 17:16  帅胡  阅读(6345)  评论(0编辑  收藏  举报

导航