redis 遠程連接出錯的解決辦法


1. 配置防火牆端口

redis系統的默認端口是6379端口。

# 打開端口 $ firewall-cmd --zone=public --add-port=6379/tcp --permanent # 命令含義: # --zone #作用域 # --add-port=80/tcp #添加端口,格式為:端口/通訊協議 # --permanent #永久生效,沒有此參數重啟后失效 # 重啟防火牆 $ firewall-cmd --reload
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9

2. 關閉Redis的protected mode

# 關閉protected-mode 127.0.0.1:6379> CONFIG SET protected-mode no # 如果不關閉,則會出現這個錯誤: # Fatal error: Uncaught exception 'Predis\Response\ServerException' with message 'DENIED Redis is running in protected mode because protected mode is enabled, no bind address was specified, no authentication password is requested to clients. In this mode connections are only accepted from the loopback interface. If you want to connect from external computers to Redis you may adopt one of the following solutions: 1) Just disable protected mode sending the command 'CONFIG SET protected-mode no' from the loopback interface by connecting to Redis from the same host the server is running, however MAKE SURE Redis is not publicly accessible from internet if you do so. Use CONFIG REWRITE to make this change permanent. 2) Alternatively you can just disable the protected mode by editing the Redis configuration file, and setting the protected mode option to 'no', and then restarting the server. 3) If you started the server manually just for testing, restart it with the '--protected-mode no' option. 4) Setup a bind address or an authen in D:\www\redis\predis\Client.php on line 370
  • 1
  • 2
  • 3
  • 4
  • 5

3. 下載Predis

Predis是Redis官方推薦的PHP連接工具。

<?php require 'predis/Autoloader.php'; Predis\Autoloader::register(); $server = array( 'host' => '192.168.23.140', 'port' => 6379 ); $client = new Predis\Client($server); $client->set('foo', 'bar'); $value = $client->get('foo'); echo $value;


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM