redis不能远程访问

redis不能远程访问,报错

具体解决方案

java程序

运行结果


redis不能远程访问,报错

Exception in thread "main" redis.clients.jedis.exceptions.JedisConnectionException: Failed connecting to host 192.168.40.147:6379

Exception in thread "main" redis.clients.jedis.exceptions.JedisConnectionException: Failed connecting to host 192.168.40.147:6379
	at redis.clients.jedis.Connection.connect(Connection.java:204)
	at redis.clients.jedis.BinaryClient.connect(BinaryClient.java:100)
	at redis.clients.jedis.Connection.sendCommand(Connection.java:125)
	at redis.clients.jedis.BinaryClient.get(BinaryClient.java:129)
	at redis.clients.jedis.Client.get(Client.java:64)
	at redis.clients.jedis.Jedis.get(Jedis.java:179)
	at com.zhujunwei.jedis.TestJedis.main(TestJedis.java:12)
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:181)
	... 6 more

找了好久找不到原因,以为是jedis版本、redis版本、或者pool版本不匹配发现都不是。

最后在这里找到了答案https://www.cnblogs.com/machanghai/p/5497084.html

具体解决方案

将redis.conf中的bind 127.0.0.1 修改为 0.0.0.0

为什么不是注释掉(前面加#号),注释掉会出现其他错误,具体我也不清楚。

修改后效果如下

...
# IF YOU ARE SURE YOU WANT YOUR INSTANCE TO LISTEN TO ALL THE INTERFACES
# JUST COMMENT THE FOLLOWING LINE.
# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
bind 0.0.0.0

# Protected mode is a layer of security protection, in order to avoid that
# Redis instances left open on the internet are accessed and exploited.
#
# When protected mode is on and if:
#
# 1) The server is not binding explicitly to a set of addresses using the
#    "bind" directive.
# 2) No password is configured.
...

java程序

package com.zhujunwei.jedis;


import redis.clients.jedis.Jedis;

public class TestJedis {
	
	@SuppressWarnings("resource")
	public static void main(String[] args) {
//		Jedis jedis = new Jedis("192.168.40.147");
		Jedis jedis = new Jedis("192.168.40.147",6379);
		String name = jedis.get("name");
		System.out.println(name);
	}
}

运行结果

我好帅

附:Redis window版下载链接

https://github.com/MicrosoftArchive/redis/releases

posted @ 2019-02-25 07:58  雨中遐想  阅读(10)  评论(0编辑  收藏  举报  来源