ZBLOG

hiredis添加IP、端口、用户、密码配置

在使用 hiredis 库进行 Redis 数据库连接时,可以通过以下方式添加 IP、端口、用户和密码配置:

  1. 配置 IP 和端口:
  1. redisContext *ctx = redisConnect("127.0.0.1", 6379); // 替换为你的 Redis 服务器 IP 和端口
  2. if (ctx == NULL || ctx->err) {
  3. if (ctx) {
  4. printf("Error: %s\n", ctx->errstr);
  5. // 处理连接错误的逻辑
  6. } else {
  7. printf("Can't allocate redis context\n");
  8. // 处理内存分配错误的逻辑
  9. }
  10. return;
  11. }
  1. 配置用户和密码:
  1. redisContext *ctx = redisConnectWithTimeout("127.0.0.1", 6379, timeout); // 替换为你的 Redis 服务器 IP 和端口
  2. if (ctx == NULL || ctx->err) {
  3. if (ctx) {
  4. printf("Error: %s\n", ctx->errstr);
  5. // 处理连接错误的逻辑
  6. } else {
  7. printf("Can't allocate redis context\n");
  8. // 处理内存分配错误的逻辑
  9. }
  10. return;
  11. }
  12. // 进行身份验证(用户名和密码)
  13. redisReply *reply = (redisReply *)redisCommand(ctx, "AUTH your_password"); // 替换为你的 Redis 密码
  14. if (reply == NULL || reply->type == REDIS_REPLY_ERROR) {
  15. printf("Authentication failed: %s\n", reply ? reply->str : "NULL");
  16. freeReplyObject(reply);
  17. return;
  18. }
  19. freeReplyObject(reply);

注意:上述代码中的 “127.0.0.1” 和 6379 是示例,需要替换为你实际使用的 Redis 服务器 IP 和端口。同时,“your_password”也需要替换为你的 Redis 密码。


内容由零声教学AI助手提供,问题来源于学员提问

本站部分文章来源于网络,版权归原作者所有,如有侵权请联系站长删除。
转载请注明出处:https://golang.0voice.com/?id=10462

分享:
扫描分享到社交APP
上一篇
下一篇
发表列表
游客 游客
此处应有掌声~
评论列表

还没有评论,快来说点什么吧~

联系我们

在线咨询: 点击这里给我发消息

微信号:3007537140

上班时间: 10:30-22:30

关注我们
x

注册

已经有帐号?