redis使用过程(连接远端redis服务器)问题汇总

今天在使用redis做服务器时缓存时,逐步调错的过程连续出现了以下几个问题:

1、jedisPool.getResource() 该句出现连接失败错误

     使用ping命令( System.out.println(jedis.ping()) )测试连接时,报如下2和3的错。

2、redis.clients.jedis.exceptions.JedisConnectionException: Failed connecting to host 192.168.152.129:6379

3、Redis is running in protected mode because protected mode is enabled, no bind address was specified

接下来我们分析下以上问题的解决办法。

首先,我们检查一下虚拟机防火墙是否关闭:service iptables status

           关闭防火墙:service iptables stop

           关于防火墙的打开关闭可以参考:https://www.cnblogs.com/zhangmingcheng/p/6048043.html

 然后,打开6397端口:iptables -A INPUT -p tcp --dport 8001 -j ACCEPT

然后,我们分析第2、3个错误

 1)该错误的意思是,redis 连接时只能通过本地localhost (127.0.0.1)这个来链接,而不能用网络ip(192.168..)这个链接,如果用网络ip 链接会报以下的错误:

(error) 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 lookback 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 --portected-mode no option. 4) Setup a bind address or an authentication password. NOTE: You only need to do one of the above things in order for the server to start accepting connections from the outside.

2)我们需要修改配置文件../redis.conf  (路径:/redis-5.0.3/redis.conf) 

①打开配置文件把下面对应的注释掉:

# bind 127.0.0.1

②Redis默认不是以守护进程的方式运行,可以通过该配置项修改,使用yes启用守护进程,设置为no:

daemonize no

③关闭保护模式:

protected-mode no

④最后就是重启服务,让配置文件生效:

./redis-server redis.conf (指定配置文件启动)

问题:如果端口占用,证明原来的redis服务没有关闭,先关闭,再重启即可:

即: ./redis-server shutdown

        ./redis-server redis.conf 

本文借鉴:https://blog.csdn.net/Agly_Clarlie/article/details/52251746

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章