【问题标题】:Cannot connect Redis Cluster in Elasticache to PHP using phpredis library无法使用 phpredis 库将 Elasticache 中的 Redis 集群连接到 PHP
【发布时间】:2018-01-22 20:33:15
【问题描述】:

我能够使用 ec2 实例(在 aws 文档中给出)连接到 elasticache 中的 redis 集群,并且能够添加和获取键、值。但是当我尝试在同一个 ec2 实例上通过 phpredis 连接时,我没有收到错误,也没有数据。请帮我解决一下这个。对于这个特定问题,互联网上没有太多信息。我能够连接到在同一个 ec2 实例上运行的 redis,但不能连接到 elasticache。如果我能得到一些关于如何除了更改主机(redis 集群的端点)之外的示例。 谢谢

【问题讨论】:

  • 您是说您有一个在 Elasticache 中创建的 Redis 集群和一个运行 PHP 的单独 EC2 实例,您想访问该 Elasticache 集群吗?你的 PHPRedis 连接语句是什么样的?
  • 感谢您的回复,是的,但相同的 vpc。我用过phpredis。尽管我可以在同一个 ec2 实例中使用命令行访问 elasticache,但它能够连接到本地的 redis,但不能连接到 elasticache。 $redis = 新的 Redis(); $redis->pconnect('{redis elasticache的端点}',6379);

标签: php redis amazon-elasticache


【解决方案1】:
  1. 使用Predis库。

  2. 使用 Predis 在集群模式下连接到 Redis ElastiCache Endpoint,请参见下面的示例。

    try{ 
        // Put your AWS ElastiCache Configuration Endpoint here.
        $servers  = ['aliceredis.8xyzwu.clustercfg.euw2.cache.amazonaws.com:6379'];
        // Tell client to use 'cluster' mode.
        $options  = ['cluster' => 'redis'];
        // Create your redis client
        $redis = new Predis\Client($servers, $options); 
    
        // Do something you want:
        // Set the expiration for 7 seconds
        $redis->set("tm", "I have data for 7s.");
        $redis->expire("tm", 7);
        $ttl = $redis->ttl("tm"); // will be 7 seconds
    
        // Print out value of the key 'tm'
        var_dump(array("msg"=>"Successfully connected to Redis Cluster.", "val"=>$redis->get("tm"))) ;
    
    }
    catch(Exception $ex){ 
        echo ('Error: ' . $ex->getMessage() ); // output error message.
    }
    

【讨论】:

  • 嗨,坦率地说,我已经检查了这段代码,但是当我们给出错误端点时它没有给出异常,仍然运行 get 命令任何想法
猜你喜欢
  • 2020-03-31
  • 2019-09-12
  • 2021-03-16
  • 2015-11-21
  • 1970-01-01
  • 1970-01-01
  • 2019-02-02
  • 2018-11-01
  • 2022-11-13
相关资源
最近更新 更多