redis 连接遇到的坑

一、安装服务器

Q: redis.exceptions.ConnectionError: Error 111 connecting to localhost:6379. Connection refused.

A: redis 安装redis服务

sudo apt-get install redis-server

二、防火墙关闭

Q:redis exceptions ConnectionError Error 113 connecting  ...... no route to host

A: 关闭防火墙

sudo iptables -F

但是关闭之后,还是会出现no route to host 的问题,查询进程,发现用kill的方式杀不掉进程(会自动重启 /usr/bin/redis-server 127.0.0.1:6379)

ps -ef | grep -i redis #查看redis进程id
#杀死进程
sudo kill -9 id

 关闭redis服务

/etc/init.d/redis-server stop

重启redis:

redis-server

三、修改配置文件

重启redis后,redis.exceptions.ResponseError: DENIED Redis is running in protected mode because protected mode is enabled, no bind address was specified, no authentication password is requested to clients.

找到配置文件  /etc/redis/redis-conf

1. 将绑定的ip号注释

#bind 127.0.0.1

2. 在配置文件最后加上

protected-mode no

重启后顺利连接

 

附:

发送程序:

import time
import redis
r = redis.StrictRedis(host = "10.11.80.200", port = 6379, db=1)
#r = redis.StrictRedis(host = "127.0.0.1", port = 6379, db=1)
i = 0
while True:
    i += 1
    r.publish("first channel", "the i is " + str(i))
    print("the i is " + str(i))
    time.sleep(1)

 接收程序:

import time
import redis
r = redis.StrictRedis(host = "10.11.80.200", port = 6379, db=1)
p = r.pubsub()
p.subscribe("first channel")
for item in p.listen():		#监听状态:有消息发布了就拿过来
    if item['type'] == 'message':
        print(item['data'])

 

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值