1. 程式人生 > >03.jedis連線redis 連線不上

03.jedis連線redis 連線不上

虛擬機器ip地址:192.168.8.156 埠6379

使用jedis連線redis

    public static void main(String[] args) {
        Jedis jedis = new Jedis("192.168.8.156",6379);
        System.out.println(jedis.ping());
    }

報錯:

Exception in thread "main" redis.clients.jedis.exceptions.JedisConnectionException: Failed connecting to host 192.168
.8.156:6379 at redis.clients.jedis.Connection.connect(Connection.java:207) at redis.clients.jedis.BinaryClient.connect(BinaryClient.java:97) at redis.clients.jedis.Connection.sendCommand(Connection.java:126) at redis.clients.jedis.BinaryClient.keys(BinaryClient.java:160) at redis.clients
.jedis.Client.keys(Client.java:102) at redis.clients.jedis.Jedis.keys(Jedis.java:283) at com.redis.test.redisTest.main(redisTest.java:13) Caused by: java.net.ConnectException: Connection refused: connect at java.net.DualStackPlainSocketImpl.waitForConnect(Native Method) at java.net.DualStackPlainSocketImpl
.socketConnect(DualStackPlainSocketImpl.java:75) at java.net.AbstractPlainSocketImpl.doConnect(AbstractPlainSocketImpl.java:339) at java.net.AbstractPlainSocketImpl.connectToAddress(AbstractPlainSocketImpl.java:200) at java.net.AbstractPlainSocketImpl.connect(AbstractPlainSocketImpl.java:182) at java.net.PlainSocketImpl.connect(PlainSocketImpl.java:157) at java.net.SocksSocketImpl.connect(SocksSocketImpl.java:391) at java.net.Socket.connect(Socket.java:579) at redis.clients.jedis.Connection.connect(Connection.java:184) ... 6 more

解決:
1.開啟bind 127.0.0.1 這個配置時:redis 只接收來自於該 IP 地址列表的請求,如果不進行設定,那麼將處理所有請求。
於是禁止這個配置,重啟redis。
2.重啟之後發現eclipse使用jedis連線沒有問題了, 但是繼續報錯

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:130)
    at redis.clients.jedis.Protocol.process(Protocol.java:164)
    at redis.clients.jedis.Protocol.read(Protocol.java:218)
    at redis.clients.jedis.Connection.readProtocolWithCheckingBroken(Connection.java:341)
    at redis.clients.jedis.Connection.getStatusCodeReply(Connection.java:240)
    at redis.clients.jedis.BinaryJedis.ping(BinaryJedis.java:199)
    at com.redis.test.redisTest.main(redisTest.java:12)

解決:
發現3.2後新增protected-mode配置,預設是yes,即開啟。解決方法分為兩種:1、關閉protected-mode模式 2、配置bind或者設定密碼
嘗試方法1關閉protected-mode模式
最終如下圖:

這裡寫圖片描述