2 u010477161 u010477161 于 2016.02.21 16:26 提问

redis配置密码,客户端报错

没有使用密码认证的情况下客户端连redis服务器都是正常的。使用密码配置会就提示错误了

在redis 服务端修改/etc/redis.conf

requirepass myRedis

重启服务
sudo service redis restart

客户端是使用JedisPool ,其中参数password 就是myRedis
使用的是new JedisPool(config, host, Integer.parseInt(port), Integer.parseInt(timeout), password);

当我从池中获取一个Jedis的时候就会报错;
Jedis jedis = jedisPool.getResource();

具体报错的地方在以下。

redis.clients.jedis.Connection 类的 socket.connect(new InetSocketAddress(host, port), timeout);就会报
Could not get a resource from the pool

public void connect() {
    if (!isConnected()) {
        try {
            socket = new Socket();
            //->@wjw_add
            socket.setReuseAddress(true);
            socket.setKeepAlive(true);  //Will monitor the TCP connection is valid
            socket.setTcpNoDelay(true);  //Socket buffer Whetherclosed, to ensure timely delivery of data
            socket.setSoLinger(true,0);  //Control calls close () method, the underlying socket is closed immediately
            //<-@wjw_add

            socket.connect(new InetSocketAddress(host, port), timeout);
            socket.setSoTimeout(timeout);
            outputStream = new RedisOutputStream(socket.getOutputStream());
            inputStream = new RedisInputStream(socket.getInputStream());
        } catch (IOException ex) {
            throw new JedisConnectionException(ex);
        }
    }
}


       redis.clients.jedis.exceptions.JedisConnectionException: Could not get a resource from the pool
at redis.clients.util.Pool.getResource(Pool.java:42)

Caused by: redis.clients.jedis.exceptions.JedisConnectionException: java.net.ConnectException: Connection refused: connect
at redis.clients.jedis.Connection.connect(Connection.java:137)
at redis.clients.jedis.BinaryClient.connect(BinaryClient.java:65)
at redis.clients.jedis.BinaryJedis.connect(BinaryJedis.java:1706)
at redis.clients.jedis.JedisFactory.makeObject(JedisFactory.java:65)
at org.apache.commons.pool2.impl.GenericObjectPool.create(GenericObjectPool.java:819)
at org.apache.commons.pool2.impl.GenericObjectPool.borrowObject(GenericObjectPool.java:429)
at org.apache.commons.pool2.impl.GenericObjectPool.borrowObject(GenericObjectPool.java:360)
at redis.clients.util.Pool.getResource(Pool.java:40)
... 2 more
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:132)
... 9 more

2个回答

zxl78585
zxl78585   2016.02.21 17:42
已采纳

你这中错误,应该是你启动redis的时候,没有使用配置文件,或者是配置文件没有生效。redis带配置文件启动的命令是:
redis-server redis.conf
如果用这样还是无法启动,那建议您检查一下您的配置文件是否正确

u010477161
u010477161 谢谢,就是这个问题,我重启的时候没有加上redis.conf配置文件,谢谢!
接近 2 年之前 回复
u010477161
u010477161 谢谢,就是这个问题,我重启的时候没有加上redis.conf配置文件,谢谢!
接近 2 年之前 回复
u010477161
u010477161   2016.02.22 10:13

谢谢,就是这个问题,我重启的时候没有加上redis.conf配置文件,谢谢!

Csdn user default icon
上传中...
上传图片
插入图片
准确详细的回答,更有利于被提问者采纳,从而获得C币。复制、灌水、广告等回答会被删除,是时候展现真正的技术了!