之前我的一篇文章,也是解决Jedis连接不到redis的,但是情况不一样,之前的问题主要是防火墙的问题,但是现在看来并不是防火墙的问题,因为redis自身也有配置来限制外网的访问,所以当时也不知道为什么就可以了,今天主要以redis配置的角度来看一下,如何解决外网访问redis的问题。
首先还是描述一下问题:
public static void main(String[] args) {
Jedis jedis = new Jedis("192.168.1.118", 6380);
System.out.println(jedis);
jedis.ping();
}
上面是测试代码,下面是报错信息:
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 com.atguigu.redis.test.TestPing.main(TestPing.java:10)
Caused by: java.net.ConnectException: Connection refused: connect
at java.net.DualStackPlainSocketImpl.waitForConnect(Native Method)
at java.net.DualStackPlainSocketImpl.socketConnect(Unknown Source)
at java.net.AbstractPlainSocketImpl.doConnect(Unknown Source)
at java.net.AbstractPlainSocketImpl.connectToAddress(Unknown Source)
at java.net.AbstractPlainSocketImpl.connect(Unknown Source)
at java.net.PlainSocketImpl.connect(Unknown Source)
at java.net.SocksSocketImpl.connect(Unknown Source)
at java.net.Socket.connect(Unknown Source)
at redis.clients.jedis.Connection.connect(Connection.java:184)
... 6 more
首先,通过查看linux的防火墙,看看是否是防火墙搞的鬼:
[root@localhost ~]# systemctl status firewalld
● firewalld.service - firewalld - dynamic firewall daemon
Loaded: loaded (/usr/lib/systemd/system/firewalld.service; disabled; vendor preset: enabled)
Active: inactive (dead)
结果是这样的,inactive是不活动的意思,和active相反,这个比较好理解。
然后使用windows的telnet来试一下,上一篇关于远程连接redis的文章中没有关于telnet的信息,这次不上,
C:\Users\Administrator>telnet 192.168.1.118 6379
结果当然也是连不上了,然后就只能请教万能的百度大哥了:
原来是redis默认只能localhost登录,所以需要开启远程登录。解决方法如下:
在redis的配置文件redis.conf中,找到bind localhost注释掉。
注释掉本机,局域网内的所有计算机都能访问。
band localhost 只能本机访问,局域网内计算机不能访问。
bind 局域网IP 只能局域网内IP的机器访问, 本地localhost都无法访问。
上面是从别的地方找到的内容,虽然不是很具体,但是指明了方向,那就是redis.conf,所以
################################## NETWORK #####################################
# By default, if no "bind" configuration directive is specified, Redis listens
# for connections from all the network interfaces available on the server.
# It is possible to listen to just one or multiple selected interfaces using
# 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 lookback interface address (this means Redis will be able to
# accept connections only from clients running into the same computer it
# is running).
#
# IF YOU ARE SURE YOU WANT YOUR INSTANCE TO LISTEN TO ALL THE INTERFACES
# JUST COMMENT THE FOLLOWING LINE.
# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
bind 127.0.0.1
bind 192.168.1.118
# 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:
#
上面是redis.conf中的内容
bind 192.168.1.118是我后来添加的,也就是加了这句话之后,才可以正常的访问了。
public static void main(String[] args) {
Jedis jedis = new Jedis("192.168.1.118", 6379);
System.out.println(jedis);
String ping = jedis.ping();
System.out.println(ping);
}
redis.clients.jedis.Jedis@6ff3c5b5
PONG