本文已参与「新人创作礼」活动,一起开启掘金创作之路。
环境
代码
<?php
$redis = new Redis();
$redis->connect('redis', 6379);
var_dump('<pre>',$redis);exit;
创建 test.php,代码如上。
错误
$ php56 test.php
Fatal error: Uncaught exception 'RedisException' with message 'Connection refused' in /www/test.php:3
Stack trace:
#0 /www/test.php(3): Redis->connect('redis', 6379)
#1 {main}
thrown in /www/test.php on line 3
在执行 php 文件的时候,报了上述错误。原因是主机要连接 docker 中的 redis,要求容器必须经过 ports 把端口映射到主机,所以主机上访问 redis 的地址是 127.0.0.1:6379。
$ php56 test.php
Fatal error: Uncaught exception 'RedisException' with message 'Connection refused' in /www/test.php:3
Stack trace:
#0 /www/test.php(3): Redis->connect('127.0.0.1', 6379)
#1 {main}
thrown in /www/test.php on line 3
把 host 地址改为
127.0.0.1后再进行尝试,仍然报错。
$ php56 test.php
string(5) "<pre>"
object(Redis)#1 (0) {
}
经各种尝试后,把 host 地址改为本机的 ip 地址,成功连接 redis。