java connection redis error



Exception in thread "main" redis.clients.jedis.exceptions.JedisConnectionException: java.net.ConnectException: Connection refused: connect
    at redis.clients.jedis.Connection.connect(Connection.java:207)
    at redis.clients.jedis.BinaryClient.connect(BinaryClient.java:93)
    at redis.clients.jedis.Connection.sendCommand(Connection.java:126)
    at redis.clients.jedis.Connection.sendCommand(Connection.java:121)
    at redis.clients.jedis.BinaryClient.ping(BinaryClient.java:106)
    at redis.clients.jedis.BinaryJedis.ping(BinaryJedis.java:195)
    at practice.RedisJava.main(RedisJava.java:13)
Caused by: java.net.ConnectException: Connection refused: connect
    at java.net.DualStackPlainSocketImpl.waitForConnect(Native Method)
    at java.net.DualStackPlainSocketImpl.socketConnect(DualStackPlainSocketImpl.java:85)
    at java.net.AbstractPlainSocketImpl.doConnect(AbstractPlainSocketImpl.java:350)
    at java.net.AbstractPlainSocketImpl.connectToAddress(AbstractPlainSocketImpl.java:206)
    at java.net.AbstractPlainSocketImpl.connect(AbstractPlainSocketImpl.java:188)
    at java.net.PlainSocketImpl.connect(PlainSocketImpl.java:172)
    at java.net.SocksSocketImpl.connect(SocksSocketImpl.java:392)
    at java.net.Socket.connect(Socket.java:589)
    at redis.clients.jedis.Connection.connect(Connection.java:184)
    ... 6 more
copy code

 This may be caused by two reasons. First, the port 6379 of redis may not be accessible. Please enter the command 
telnet 127.0.0.1 6379  in cmd to
see if you can access port 6379 of the redis-server machine. Turn off the firewall on the remote machine or add permission to 
log in by 1) using the root user, vi /etc/sysconfig/iptables, add a line as shown in the figure 

 

2) Enter the command service iptables restart to restart the firewall

Or you can directly use the command service iptables stop to turn off the firewall as the root user.

After the firewall is checked, if the above problems still occur, it means that redis still needs to be configured. By default, redis can only be accessed by the local machine. If you want to remotely access, you need to modify the redis.conf configuration file. 
Enter the redis.conf directory, and use the vi command to open it. After finding the bind line and modifying it, wq saves and exits, and restarts the redis-server. 

The ip that allows access is added after 
bind. Bind 127.0.0.1 means that only the machine can access it. You can add the ip that is allowed to access, or you can directly comment out this line, so that all machines can access.

New problem after solving the above problem: DENIED Redis is running in protected mode

The error message is as follows:

copy code
Exception in thread "main" redis.clients.jedis.exceptions.JedisDataException: 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 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.
    at redis.clients.jedis.Protocol.processError(Protocol.java:127)
    at redis.clients.jedis.Protocol.process(Protocol.java:161)
    at redis.clients.jedis.Protocol.read(Protocol.java:215)
    at redis.clients.jedis.Connection.readProtocolWithCheckingBroken(Connection.java:340)
    at redis.clients.jedis.Connection.getStatusCodeReply(Connection.java:239)
    at redis.clients.jedis.Jedis.set(Jedis.java:121)
    at roy.redis.test.Test.init(Test.java:13)
    at roy.redis.test.Test.main(Test.java:8)
copy code

报错信息很长,但是主要是说redis开启了protected mode,这也是Redis3.2加入的新特性,开启保护模式的redis只允许本机登录,同样设置在配置文件redis.conf中,如图 
write picture description here 
这里原来是yes代表开启了保护模式,后面可以填密码也可以填no代表关闭,我们这里选择关闭保护模式,wq保存退出后再重启redis-server

 

Exception in thread "main" redis.clients.jedis.exceptions.JedisConnectionException: java.net.ConnectException: Connection refused: connect
    at redis.clients.jedis.Connection.connect(Connection.java:207)
    at redis.clients.jedis.BinaryClient.connect(BinaryClient.java:93)
    at redis.clients.jedis.Connection.sendCommand(Connection.java:126)
    at redis.clients.jedis.Connection.sendCommand(Connection.java:121)
    at redis.clients.jedis.BinaryClient.ping(BinaryClient.java:106)
    at redis.clients.jedis.BinaryJedis.ping(BinaryJedis.java:195)
    at practice.RedisJava.main(RedisJava.java:13)
Caused by: java.net.ConnectException: Connection refused: connect
    at java.net.DualStackPlainSocketImpl.waitForConnect(Native Method)
    at java.net.DualStackPlainSocketImpl.socketConnect(DualStackPlainSocketImpl.java:85)
    at java.net.AbstractPlainSocketImpl.doConnect(AbstractPlainSocketImpl.java:350)
    at java.net.AbstractPlainSocketImpl.connectToAddress(AbstractPlainSocketImpl.java:206)
    at java.net.AbstractPlainSocketImpl.connect(AbstractPlainSocketImpl.java:188)
    at java.net.PlainSocketImpl.connect(PlainSocketImpl.java:172)
    at java.net.SocksSocketImpl.connect(SocksSocketImpl.java:392)
    at java.net.Socket.connect(Socket.java:589)
    at redis.clients.jedis.Connection.connect(Connection.java:184)
    ... 6 more
copy code

 这可能是两个原因造成的,首先可能是redis的6379端口无法访问,请先在cmd中输入命令 
telnet 127.0.0.1 6379 
看看可不可以访问redis-server 机器的6379端口,如果不能访问,需要在远程机器关掉防火墙或者添加允许通过 
1)使用root用户登录,vi /etc/sysconfig/iptables,添加如图所以一行 

 

2)输入命令service iptables restart重启防火墙

或者可以直接root用户使用命令service iptables stop关闭防火墙。

防火墙检查完后,如果还是出现上述问题,说明redis还有地方需要配置,redis默认是只有本机可以访问的,想要远程访问需要修改redis.conf配置文件。 
进入redis.conf目录,并使用vi命令打开,找到bind那行修改后,wq保存退出,重启redis-server。 

bind 后加的是允许访问的ip 
bind 127.0.0.1代表只有本机可以访问,可以将允许访问的ip加入,也可以直接注释掉这一行,这样所有机器都可以访问。

解决上述问题后出现的新问题:DENIED Redis is running in protected mode

报错信息如下:

copy code
Exception in thread "main" redis.clients.jedis.exceptions.JedisDataException: 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 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.
    at redis.clients.jedis.Protocol.processError(Protocol.java:127)
    at redis.clients.jedis.Protocol.process(Protocol.java:161)
    at redis.clients.jedis.Protocol.read(Protocol.java:215)
    at redis.clients.jedis.Connection.readProtocolWithCheckingBroken(Connection.java:340)
    at redis.clients.jedis.Connection.getStatusCodeReply(Connection.java:239)
    at redis.clients.jedis.Jedis.set(Jedis.java:121)
    at roy.redis.test.Test.init(Test.java:13)
    at roy.redis.test.Test.main(Test.java:8)
copy code

The error message is very long, but it mainly means that redis has enabled protected mode, which is also a new feature added by Redis 3.2. Redis with protected mode enabled only allows local login, which is also set in the configuration file redis.conf, as shown in the 
write picture description here 
original picture here Yes, it means the protected mode is turned on. You can fill in the password or no to turn it off. We choose to turn off the protected mode here, save wq and exit, and then restart the redis-server.

 

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325753325&siteId=291194637