redis 安裝: Linux 安裝redis
1)下載jar包:
使用Jedis需要以下兩個jar包:
jedis-2.8.0.jar
commons-pool2-2.4.2.jar
2)測試redis連接:
package com.venn.redis.demo; import redis.clients.jedis.Jedis; public class RedisJava { public static void main(String[] args) { //連接本地的 Redis 服務
Jedis jedis = new Jedis("10.80.248.24"); // jedis.select(1);
System.out.println("Connection to server sucessfully"); //查看服務是否運行
System.out.println("Server is running: "+jedis.ping()); } }
如果顯示
Connection to server sucessfully
Server is running: PONG
表示服務器連接正常。
3)簡單使用redis
package com.venn.redis.demo; import redis.clients.jedis.Jedis; public class RedisStringJava { public static void main(String[] args) { // 連接本地的 Redis 服務
Jedis jedis = new Jedis("10.80.248.22"); // 默認端口
//Jedis jedis = new Jedis("10.80.248.22",6379); // 指定端口
// jedis.auth("pass") // 指定密碼
System.out.println("Connection to server sucessfully"); // 設置 redis 字符串數據
jedis.set("redis", "Redis 1"); // 獲取存儲的數據並輸出
System.out.println("Stored string in redis:: " + jedis.get("redis"));
System.out.println("redis : " + jedis.get("redis"));
}
}
執行,有報錯。
4)Connection refused : connect 報錯處理
Connection to server sucessfully
Exception in thread "main" redis.clients.jedis.exceptions.JedisConnectionException: java.net.ConnectException: Connection refused: connect
at redis.clients.jedis.Connection.connect(Connection.java:164)
at redis.clients.jedis.BinaryClient.connect(BinaryClient.java:80)
at redis.clients.jedis.Connection.sendCommand(Connection.java:100)
at redis.clients.jedis.BinaryClient.select(BinaryClient.java:163)
at redis.clients.jedis.BinaryJedis.select(BinaryJedis.java:431)
at com.venn.redis.demo.RedisStringJava.main(RedisStringJava.java:11)
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:339)
at java.net.AbstractPlainSocketImpl.connectToAddress(AbstractPlainSocketImpl.java:200)
at java.net.AbstractPlainSocketImpl.connect(AbstractPlainSocketImpl.java:182)
at java.net.PlainSocketImpl.connect(PlainSocketImpl.java:172)
at java.net.SocksSocketImpl.connect(SocksSocketImpl.java:392)
at java.net.Socket.connect(Socket.java:579)
at redis.clients.jedis.Connection.connect(Connection.java:158)
... 5 more
經查,redis默認只能本地訪問
bind 127.0.0.1 # 不同版本可能是 localhost
解決:
修改啟動redis server 使用的redis.conf,注釋以上一行
5)保護模式異常
經過3修改后,redis可以在任意地址(局域網)訪問,但是redis默認沒有配置訪問密碼,這樣就有個報錯:
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:117) at redis.clients.jedis.Protocol.process(Protocol.java:151) at redis.clients.jedis.Protocol.read(Protocol.java:205) at redis.clients.jedis.Connection.readProtocolWithCheckingBroken(Connection.java:297) at redis.clients.jedis.Connection.getStatusCodeReply(Connection.java:196) at redis.clients.jedis.BinaryJedis.select(BinaryJedis.java:432) at com.venn.redis.demo.RedisStringJava.main(RedisStringJava.java:11)
英文比較好的同學,可以看到是什么意思。這里給英文不好的同學大概講一下:
報錯的大意就是:redis運行在保護模式,沒有綁定訪問地址,沒有登錄密碼認證,在這種模式下,連接只接受環回接口(loopback,一種路由接口),
下面有幾種解決辦法:
1)使用命令:“CONFIG SET protected-mode no” ,禁用保護模式。
2)修改配置文件,禁用保護模式。
3)重新啟動redis server 使用 “--protected-mode no” 參數
4) 設置一個綁定ip或設置認證密碼
當然使用4了。綁定ip,見3,修改127.0.0.1 to 你的ip
6) redis 設置密碼
requirepass myRedis
重啟redis。
7) 設置密碼后,客戶端登錄
設置密碼后,redis-cli可以正常登錄,但是不能操作。
(error) ERR operation not permitted
使用:
./redis-cli -a myReids
登錄正常。
8) 重新執行
package com.venn.redis.demo; import redis.clients.jedis.Jedis; public class RedisStringJava { public static void main(String[] args) { // 連接本地的 Redis 服務
Jedis jedis = new Jedis("10.80.248.22",6379); jedis.auth("myRedis"); // 設置密碼 System.out.println("Connection to server sucessfully"); // 設置 redis 字符串數據
jedis.set("redis", "Redis 1"); // 獲取存儲的數據並輸出
System.out.println("redis : " + jedis.get("redis")); } }
執行,返回正常
redis : redis
OK。
未完待續。