Docker Dnmp链接Redis

WechatIMG7.jpeg

环境

代码

1
2
3
4
<?php
$redis = new Redis();
$redis->connect('redis', 6379);
var_dump('<pre>',$redis);exit;

创建 test.php,代码如上。

错误

1
2
3
4
5
6
$ 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。

1
2
3
4
5
6
$ 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后再进行尝试,仍然报错。

1
2
3
4
$ php56 test.php
string(5) "<pre>"
object(Redis)# 1 (0) {
}

经各种尝试后,把 host 地址改为本机的 ip 地址,成功连接 redis。

-------------本文结束感谢您的阅读-------------
0%