python 连接 Linux 的 redis 报错 (redis.exceptions.ConnectionError: Error 10061 ...由于目标计算机积极拒绝,无法连接)
文章目錄
- 未指定IP地址報錯
- 未修改redis.conf報錯
- 防火墻問題,未開放6379端口號
未指定IP地址報錯
😑
我的報錯如下:During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File “D:\wjpythonwj\redis_1.py”, line 21, in
result = rs.set(‘name’,‘itcast’)
File “D:\python3\lib\site-packages\redis\commands\core.py”, line 2127, in set
return self.execute_command(“SET”, *pieces, **options)
File “D:\python3\lib\site-packages\redis\client.py”, line 1215, in execute_command
conn = self.connection or pool.get_connection(command_name, **options)
File “D:\python3\lib\site-packages\redis\connection.py”, line 1386, in get_connection
connection.connect()
File “D:\python3\lib\site-packages\redis\connection.py”, line 620, in connect
raise ConnectionError(self._error_message(e)) redis.exceptions.ConnectionError: Error 10061 connecting to localhost:6379. 由于目標計算機積極拒絕,無法連接。.
👽學會看報錯信息非常重要,我恰恰忽略了最后一句redis.exceptions.ConnectionError: Error 10061 connecting to localhost:6379. 由于目標計算機積極拒絕,無法連接
報錯信息顯示: 拒絕連接localhost:6379
此時應該使用ip add 查看Linux的IP地址和端口號(本地虛擬機VMware是虛擬出來的一層設備,與127.0.0.1有區別!!!)
畫重點:IP地址是192.168.153.133
👏所以python(pycharm)連接Linux中的redis時需要指定IP地址為192.168.153.133
# encoding=utf-8 import redisif __name__=="__main__":ip='192.168.153.133'#創建redis的連接實例#我們在連接/花費外界資源的時候一定要注意使用trytry:rs=redis.Redis(host=ip,port=6379) #如果連接的是云服務器也可以不指定IP和端口except Exception as e:print(e)#操作string#添加set key valueresult = rs.set('name','itcast')print(result)連接線程池代碼
import redis ip = '192.168.153.133'conn_pool = redis.ConnectionPool(host=ip, port=6379) r = redis.Redis(connection_pool=conn_pool) r.set('name','zjh') print(r.get('name')) #b'zjh'未修改redis.conf報錯
vim /etc/redis/redis.conf #找到自己目錄下的redis.conf
vim查找指定內容:/ 要尋找的內容
1、接觸本地IP的綁定 bind
2、關閉保護模式protected-mode (把默認的yes改為no)
防火墻問題,未開放6379端口號
1、設置開啟某個端口(不攔截redis的6379端口)
firewall-cmd --zone=public --add-port=6379/tcp --permanent firewall-cmd --zone=public --add-port=80/tcp --permanent–zone #作用域
–add-port=80/tcp #添加端口,格式為:端口/通訊協議
–permanent #永久生效,沒有此參數重啟后失效
2、重啟防火墻
firewall-cmd --reload防火墻其他常用命令
firewall-cmd --state #查看防火墻狀態,是否是running firewall-cmd --reload #重新載入配置,比如添加規則之后,需要執行此命令 firewall-cmd --get-zones #列出支持的zone firewall-cmd --get-services #列出支持的服務,在列表中的服務是放行的 firewall-cmd --query-service ftp #查看ftp服務是否支持,返回yes或者no firewall-cmd --add-service=ftp #臨時開放ftp服務 firewall-cmd --add-service=ftp --permanent #永久開放ftp服務 firewall-cmd --remove-service=ftp --permanent #永久移除ftp服務 firewall-cmd --add-port=80/tcp --permanent #永久添加80端口 iptables -L -n #查看規則,這個命令是和iptables的相同的 man firewall-cmd #查看幫助 systemctl stop firewalld.service #停止firewall systemctl disable firewalld.service #禁止firewall開機啟動更多命令,使用 firewall-cmd --help 查看幫助文件重啟redis
[root@localhost redis]# redis-server redis.conf[root@localhost ~]# redis-server /etc/redis/redis.conf #在其他目錄指定啟動redis.conf[root@localhost redis]# redis-cli #連接redisctrl+c退出!!總結
以上是生活随笔為你收集整理的python 连接 Linux 的 redis 报错 (redis.exceptions.ConnectionError: Error 10061 ...由于目标计算机积极拒绝,无法连接)的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: PushMail初体验
- 下一篇: java数据结构与算法之平衡二叉树(AV