建一個Maven項目, pom裡加下jedis依賴, 1 2 3 4 5 <dependency> <groupId>redis.clients</groupId> <artifactId>jedis</artifactId> <version>2.9.0</version> </dependenc ...
建一個Maven項目,
pom裡加下jedis依賴,
1 2 3 4 5 |
< dependency >
< groupId >redis.clients</ groupId >
< artifactId >jedis</ artifactId >
< version >2.9.0</ version >
</ dependency >
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 |
package com.java1234.redis;
import redis.clients.jedis.Jedis;
/**
* 測試類
* @author user
*
*/
public class JedisTest {
public static void main(String[] args) {
Jedis jedis= new Jedis( "192.168.1.107" , 6379 ); // 創建客戶端 設置IP和埠
jedis.set( "name" , "java知識分享網" ); // 設置值
String value=jedis.get( "name" ); // 獲取值
System.out.println(value);
jedis.close(); // 釋放連接資源
}
}
|
測試代碼,
運行 報錯了
連接超時,
我們配置下防火牆 開一個6379埠許可權
firewall-cmd --zone=public --add-port=6379/tcp --permanent
firewall-cmd --reload
繼續運行 還是報錯 連接超時 錯誤;
我們配置下 redis配置文件
[root@localhost redis]# vi /usr/local/redis/redis.conf
這裡綁定了本機,我們把這個備註掉;
# bind 127.0.0.1
配置完後
[root@localhost redis]# ./bin/redis-cli shutdown
[root@localhost redis]# ./bin/redis-server ./redis.conf
要重啟下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:127)
at redis.clients.jedis.Protocol.process(Protocol.java:161)
at redis.clients.jedis.Protocol.read(Protocol.java:215)
at redis.clients.jedis.Connection.readProtocolWithCheckingBroken(Connection.java:340)
at redis.clients.jedis.Connection.getStatusCodeReply(Connection.java:239)
at redis.clients.jedis.Jedis.set(Jedis.java:121)
at com.java1234.redis.JedisTest.main(JedisTest.java:14)
這個是因為遠程連接redis redis自我保護 拒絕訪問;
有兩種方法 解決
第一種 直接去掉自我保護功能(不推薦)
[root@localhost redis]# vi /usr/local/redis/redis.conf
進入配置
找到 protected-mode yes
改成 no即可
編輯後 重啟redis服務,然後運行 ,結果出來了
第二種 設置redis連接密碼
進入客戶端
[root@localhost redis]# ./bin/redis-cli
127.0.0.1:6379> config set requirepass 123456
設置密碼 123456
127.0.0.1:6379> quit
[root@localhost redis]# ./bin/redis-cli
127.0.0.1:6379> auth 123456
OK
說明設置成功
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 |
package com.java1234.redis;
import redis.clients.jedis.Jedis;
/**
* 測試類
* @author user
*
*/
public class JedisTest {
public static void main(String[] args) {
Jedis jedis= new Jedis( "192.168.1.107" , 6379 ); // 創建客戶端 設置IP和埠
jedis.auth( "123456" ); // 設置密碼
jedis.set( "name" , "java知識分享網" ); // 設置值
String value=jedis.get( "name" ); // 獲取值
System.out.println(value);
jedis.close(); // 釋放連接資源
}
}
|
這樣就OK了
轉載於:http://blog.java1234.com/blog/articles/314.html
如果redis出現了關閉不了的問題或者其他問題,可以將此線程殺死,然後重新操作就可以(因為有可能操作的時候是上面某步驟出現了問題),殺死線程:
1、查看redis進程;
ps aux|grep redis
2、kill掉進程;
kill 進程id