AWS ElastiCache Redis 无法从 redis-cli 的 Laravel nad 连接
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/47696004/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
AWS ElastiCache Redis can't connect from Laravel nad from redis-cli
提问by Bogdan Dubyk
I'm having a problem connecting to ElastiCache Redis from Laravel application installed on EC2 instance or even using redis-cli from EC2 instance.
我在从 EC2 实例上安装的 Laravel 应用程序连接到 ElastiCache Redis 或什至使用 EC2 实例中的 redis-cli 时遇到问题。
Laravel
Laravel
I tried to use predis with configurations in database.php like
我尝试在 database.php 中使用 predis 配置,例如
'redis' => [
'client' => 'predis',
'default' => [
'host' => env('REDIS_HOST', '127.0.0.1'),
'password' => env('REDIS_PASSWORD', null),
'port' => env('REDIS_PORT', 6379),
'database' => 0,
'read_write_timeout' => -1,
'timeout' => 0
],
],
and got 'Error while reading line from the server. [tcp:server here]
'
并得到 ' Error while reading line from the server. [tcp:server here]
'
I tried with phpRedis extension with same configurations only change 'client' => 'phpredis'
and got error read error on connection {"exception":"[object] (RedisException(code: 0): read error on connection at vendor/laravel/framework/src/Illuminate/Redis/Connectors/PhpRedisConnector.php:69)
我尝试使用具有相同配置的 phpRedis 扩展仅更改'client' => 'phpredis'
并出现错误read error on connection {"exception":"[object] (RedisException(code: 0): read error on connection at vendor/laravel/framework/src/Illuminate/Redis/Connectors/PhpRedisConnector.php:69)
Redis cli
Redis 命令行
Using redis cli redis-cli -h host_here -p 6379 -a password_here
I see prompt like host:6379>
but typing any command throws error Error: Connection reset by peer
使用 redis cliredis-cli -h host_here -p 6379 -a password_here
我看到提示,host:6379>
但输入任何命令都会引发错误Error: Connection reset by peer
ElastiCache Redis configurations
ElastiCache Redis 配置
My EC2 and elastic cache are in the same VPC and using telnet I can connect to redis instance
我的 EC2 和弹性缓存在同一个 VPC 中,并且使用 telnet 我可以连接到 redis 实例
~$ telnet host 6379
Trying 172.31.23.113...
Connected to host.
Escape character is '^]'.
Thanks for any help!
谢谢你的帮助!
回答by Guy Grin
I know this is pretty old but I was having the same issue myself. If anyone encounters this issue then see the solution hereand here.
我知道这已经很老了,但我自己也遇到了同样的问题。如果有人遇到此问题,请在此处和此处查看解决方案。
It seems that when you enable Encryption in-transit
in AWS Elasticache it prevents you from using redis-cli as it doesn't support TLS connections. Switching to another client should work. This answerhas a list of TLS enabled clients.
似乎当您Encryption in-transit
在 AWS Elasticache 中启用时,它会阻止您使用 redis-cli,因为它不支持 TLS 连接。切换到另一个客户端应该可以。此答案包含启用 TLS 的客户端列表。
Edit:
编辑:
Did some more digging and found that using stunnel
you can wrap your connection of redis-cli with ssl. Hereis a guide for doing it.
回答by CenterOrbit
Related: Laravel + Redis Cache via SSL?
To which I've answered here: https://stackoverflow.com/a/48876398/663058
我在这里回答过:https: //stackoverflow.com/a/48876398/663058
Relevant details below:
相关详情如下:
Since you have clustering andTLS then you'll need a different config entirely:
由于您拥有集群和TLS,因此您将需要完全不同的配置:
'redis' => [
'client' => 'predis',
'cluster' => env('REDIS_CLUSTER', false),
// Note! for single redis nodes, the default is defined here.
// keeping it here for clusters will actually prevent the cluster config
// from being used, it'll assume single node only.
//'default' => [
// ...
//],
// #pro-tip, you can use the Cluster config even for single instances!
'clusters' => [
'default' => [
[
'scheme' => env('REDIS_SCHEME', 'tcp'),
'host' => env('REDIS_HOST', 'localhost'),
'password' => env('REDIS_PASSWORD', null),
'port' => env('REDIS_PORT', 6379),
'database' => env('REDIS_DATABASE', 0),
],
],
'options' => [ // Clustering specific options
'cluster' => 'redis', // This tells Redis Client lib to follow redirects (from cluster)
]
],
'options' => [
'parameters' => [ // Parameters provide defaults for the Connection Factory
'password' => env('REDIS_PASSWORD', null), // Redirects need PW for the other nodes
'scheme' => env('REDIS_SCHEME', 'tcp'), // Redirects also must match scheme
],
'ssl' => ['verify_peer' => false], // Since we dont have TLS cert to verify
]
]
Explaining the above:
解释以上内容:
'client' => 'predis'
: This specifies the PHP Library Redis driver to use (predis).'cluster' => 'redis'
: This tells Predis to assume server-side clustering. Which just means "follow redirects" (e.g.-MOVED
responses). When running with a cluster, a node will respond with a-MOVED
to the node that you must ask for a specific key.- If you don't have this enabled with Redis Clusters, Laravel will throw a
-MOVED
exception 1/ntimes, nbeing the number of nodes in Redis cluster (it'll get lucky and ask the right node every once in awhile)
- If you don't have this enabled with Redis Clusters, Laravel will throw a
'clusters' => [...]
: Specifies a list of nodes, but setting just a 'default' and pointing it to the AWS 'Configuration endpoint'will let it find any/all other nodes dynamically (recommended for Elasticache, because you don't know when nodes are comin' or goin').'options'
: For Laravel, can be specified at the top-level, cluster-level, and node option. (they get combined in Illuminate before being passed off to Predis)'parameters'
: These 'override' the default connection settings/assumptions that Predis uses for new connections. Since we set them explicitly for the 'default' connection, these aren't used. But for a cluster setup, they are critical. A 'master' node may send back a redirect (-MOVED
) and unless the parameters are set forpassword
andscheme
it'll assume defaults, and that new connection to the new node will fail.
'client' => 'predis'
:这指定要使用的 PHP 库 Redis 驱动程序 (predis)。'cluster' => 'redis'
:这告诉 Predis 假设服务器端集群。这只是意味着“跟随重定向”(例如-MOVED
响应)。与集群一起运行时,节点将响应-MOVED
您必须请求特定密钥的节点。- 如果您没有在 Redis Clusters 中启用此功能,Laravel 将抛出
-MOVED
1/ n次异常,n是 Redis 集群中的节点数(它会很幸运并每隔一段时间询问正确的节点)
- 如果您没有在 Redis Clusters 中启用此功能,Laravel 将抛出
'clusters' => [...]
:指定节点列表,但仅设置一个“默认”并将其指向AWS 的“配置端点”将使其动态查找任何/所有其他节点(推荐用于 Elasticache,因为您不知道节点何时到来)或去')。'options'
:对于Laravel,可以在top-level、cluster-level、node选项中指定。(在传递给 Predis 之前,它们在 Illuminate 中组合在一起)'parameters'
:这些“覆盖” Predis 用于新连接的默认连接设置/假设。由于我们为“默认”连接明确设置了它们,因此不使用它们。但对于集群设置,它们至关重要。“主”节点可能会发回重定向 (-MOVED
) 并且除非设置了参数password
并且scheme
它将采用默认值,否则到新节点的新连接将失败。
回答by Milind Patel
If you are using predis as client.
如果您使用 predis 作为客户端。
Then you can change Redis connection in config/database.php
然后你可以在 config/database.php 中更改 Redis 连接
'redis' => [
'client' => 'predis',
'default' => [
'scheme' => 'tls',
'host' => env('REDIS_HOST', '127.0.0.1'),
'password' => env('REDIS_PASSWORD', null),
'port' => env('REDIS_PORT', 6379),
'database' => 0,
],
],
You can add 'scheme' in that.
您可以在其中添加“方案”。
I am using Laravel 5.8 And then everything works great within Laravel.
我正在使用 Laravel 5.8 然后在 Laravel 中一切正常。
But yes, as Redis doesn't provide TLS connection so redis-cli will still not work.
但是是的,由于 Redis 不提供 TLS 连接,因此 redis-cli 仍然无法工作。