远程连接redis时发现怎么都现连接不上而且一直报错
Exception in thread "main" redis.clients.jedis.exceptions.JedisConnectionException: java.net.ConnectException: Connection refused: connect
测试代码:
import redis.clients.jedis.Jedis;
/**
* Created by harry on 2016/10/6.
*/
public class Test {
public static void main(String[] args) {
Jedis jedis = new Jedis("121.42.152.75",6379);
System.out.println("Connection to server sucessfully");
//check whether server is running or not
System.out.println("Server is running: "+jedis.ping());
}
}
后来发现是配置的问题,默认情况下,只允许本机访问redis
vim /etc/redis/redis.conf
注释bind,将bind 127.0.0.1注释成如下形式
bind 127.0.0.1
然后重启服务器,
重启redis
sudo service redis-server restart
大功告成!
Connection to server sucessfully
Server is running: PONG