解决php操作redis报:Fatal error: Uncaught exception 'RedisException' with message 'Redis server went away'

在学习php操作redis的过程中,遇到了这个问题:

Fatal error: Uncaught exception ‘RedisException’ with message ‘Redis server went away’ in D:\phpstudy\WWW\redis\demo.php:5 Stack trace: #0 D:\phpstudy\WWW\redis\demo.php(5): Redis->auth(‘123456’) #1 {main} thrown in D:\phpstudy\WWW\redis\demo.php on line

运行php的系统是windows,redis部署在线上阿里云服务器里。

php代码:


$redis = new Redis();
$redis->connect('39.105.39.241');//线上 阿里云服务器ip地址
$redis->auth('******');
//添加字符串操作
$redis->set('username','yewenhao');

解决方案

报这个错误的原因不是你windows系统是否装了redis服务,跟windows机器没有任何关系;
只需要修改阿里云上redis的配置文件redis.conf即可,修改内容如下:

1.注释掉bind 127.0.0.1
在这里插入图片描述
2.将 protected-mode yes 改为 protected-mode no
在这里插入图片描述
3.关闭SELinux
在这里插入图片描述
然后重启redis服务:
pkill redis-server
./bin/redis-server redis.conf
查看redis是否启动:
netstat -tunpl | grep 6379

再次查看key是否添加成功:
在这里插入图片描述
如果还不可以,再关闭linux防火墙试试;
希望对你有帮助

你可能感兴趣的:(redis,php)