Jedis远程连接redis-server出现错误解决方法

本文介绍了在使用Jedis连接Redis时遇到的连接失败问题,分析了Redis配置文件中`bind`和`protected-mode`选项的影响。通过配置`bind`指定允许连接的IP地址和调整`protected-mode`来允许远程访问,同时提到了防火墙和安全组规则对端口开放的重要性。在完成配置并重启Redis后,成功实现了远程连接。

今天用jedis远程连接redis的时候报错:

redis.clients.jedis.exceptions.JedisConnectionException: Failed connecting to host xxx.xxx.xxx.xxx:6379

连接失败,网上查到和配置文件里的某个配置有关系,所以打算去官网查看一下配置文件的官方文档

Redis is able to start without a configuration file using a built-in default configuration, however this setup is only recommended for testing and development purposes.

The proper way to configure Redis is by providing a Redis configuration file, usually called redis.conf.

The redis.conf file contains a number of directives that have a very simple format:

​ 刚开始先告诉我们,你启动redis不用配置文件也行,因为有一些自带的默认配置,但是这种方式呢仅推荐用于测试和开发。

​ 正确的方式是用配置文件redis.conf来启动redis 并给出了一些示例。

​ 我们直接在下面找到对应版本的redis配置文件,我的是5.0版本的,里面的说的非常详细,我选出了两个我们要用的参数。

################################## NETWORK #####################################

# By default, if no "bind" configuration directive is specified, Redis listens
默认情况下,如果不特别指定bid配置命令,redis会监听我网络上可以到达服务器的所有连接
# for connections from all the network interfaces available on the server.
# It is possible to listen to just one or multiple selected interfaces using
 可以用bind配置命令跟上IP地址来选择监听一个或者多个接口
# the "bind" configuration directive, followed by one or more IP addresses.
#
# Examples:
#
# bind 192.168.1.100 10.0.0.1
# bind 127.0.0.1 ::1
#
# ~~~ WARNING ~~~ If the computer running Redis is directly exposed to the
# internet, binding to all the interfaces is dangerous and will expose the
# instance to everybody on the internet. So by default we uncomment the
# following bind directive, that will force Redis to listen only into
# the IPv4 loopback interface address (this means Redis will be able to
# accept connections only from clients running into the same computer it
# is running).
如果redis直接暴露在网络上
### Redis Desktop Manager 连接不上服务器的解决方案 Redis Desktop Manager(RDM)无法连接Redis 服务器的问题可能由多种原因引起。以下是针对该问题的详细分析和解决方法: #### 1. 检查 Redis 配置文件 Redis 默认配置中,`bind` 参数通常设置为 `127.0.0.1`,这意味着 Redis 只允许本地回环地址访问,而拒绝远程连接。需要修改 Redis 配置文件 `redis.conf` 中的 `bind` 参数,将其设置为服务器的实际 IP 地址或 `0.0.0.0` 以允许所有网络接口上的连接[^2]。 ```bash # 修改 bind 参数 bind 0.0.0.0 ``` 同时,确保 `protected-mode` 被禁用,因为启用保护模式时,即使配置了远程访问,也可能导致连接失败。 ```bash # 禁用保护模式 protected-mode no ``` #### 2. 确认端口映射与防火墙设置 如果 Redis 运行在 Docker 容器中,需检查容器的端口是否正确映射到主机。例如,使用以下命令启动容器并映射端口: ```bash docker run -d --name redis-container -p 6379:6379 redis ``` 此外,还需确认 Linux 主机的防火墙规则是否允许外部访问 Redis 端口(默认为 6379)。可以使用以下命令临时关闭防火墙进行测试: ```bash systemctl stop firewalld ``` 或者添加特定的防火墙规则: ```bash firewall-cmd --zone=public --add-port=6379/tcp --permanent firewall-cmd --reload ``` #### 3. 测试 Redis 服务的可访问性 在修改配置后,重启 Redis 服务以应用更改。可以通过以下命令杀死旧进程并重新启动 Redis: ```bash ps -aux | grep redis kill <进程ID> src/redis-server redis.conf ``` 随后,使用 `telnet` 或 `nc` 命令测试从客户端到服务器的连接是否正常: ```bash telnet <服务器IP> 6379 ``` 如果连接成功,则说明网络层面没有问题[^4]。 #### 4. 验证 RDM 的连接参数 在 RDM 中配置连接时,确保输入正确的服务器地址、端口号、密码(如果设置了密码)。对于需要身份验证的 Redis 实例,必须提供正确的密码[^4]。 #### 5. 后台登录测试 通过命令行工具直接连接 Redis 服务器,验证其可用性。例如: ```bash redis-cli -h <服务器IP> -p 6379 -a <密码> ``` 如果能够成功登录并执行命令,则表明 Redis 服务本身运行正常,问题可能出在 RDM 的配置上[^5]。 #### 6. 其他注意事项 - 如果 Redis 运行在集群模式下,需在 RDM 中选择“Cluster”连接类型,并提供所有节点的地址列表。 - 对于安全考虑,建议使用 SSL/TLS 加密连接,特别是在公网环境中[^5]。 --- ### 示例代码:Java 连接 Redis 以下是一个简单的 Java 示例,展示如何通过 Jedis连接 Redis: ```java import redis.clients.jedis.Jedis; public class RedisTest { public static void main(String[] args) { try (Jedis jedis = new Jedis("服务器IP", 6379)) { jedis.auth("密码"); System.out.println("Connection successful: " + jedis.ping()); } catch (Exception e) { e.printStackTrace(); } } } ``` ---
评论 2
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值