Java 使用Jedis连接Redis数据库(-)
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 {
</span><span style="color: rgba(0, 0, 255, 1)">public</span> <span style="color: rgba(0, 0, 255, 1)">static</span> <span style="color: rgba(0, 0, 255, 1)">void</span><span style="color: rgba(0, 0, 0, 1)"> main(String[] args) {
</span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">连接本地的 Redis 服务</span>
Jedis jedis = <span style="color: rgba(0, 0, 255, 1)">new</span> Jedis("10.80.248.24"<span style="color: rgba(0, 0, 0, 1)">);
// 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 {
</span><span style="color: rgba(0, 0, 255, 1)">public</span> <span style="color: rgba(0, 0, 255, 1)">static</span> <span style="color: rgba(0, 0, 255, 1)">void</span><span style="color: rgba(0, 0, 0, 1)"> main(String[] args) {
</span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> 连接本地的 Redis 服务</span>
Jedis jedis = <span style="color: rgba(0, 0, 255, 1)">new</span> Jedis("10.80.248.22"<span style="color: rgba(0, 0, 0, 1)">); // 默认端口<br> //Jedis jedis = new Jedis("10.80.248.22",6379); // 指定端口<br> // jedis.auth("pass") // 指定密码</span>
System.out.println("Connection to server sucessfully"<span style="color: rgba(0, 0, 0, 1)">);
// 设置 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 报错处理
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 | 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 {
</span><span style="color: rgba(0, 0, 255, 1)">public</span> <span style="color: rgba(0, 0, 255, 1)">static</span> <span style="color: rgba(0, 0, 255, 1)">void</span><span style="color: rgba(0, 0, 0, 1)"> main(String[] args) {
</span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> 连接本地的 Redis 服务</span>
Jedis jedis = <span style="color: rgba(0, 0, 255, 1)">new</span> Jedis("10.80.248.22",6379<span style="color: rgba(0, 0, 0, 1)">);
<strong><span style="color: rgba(255, 0, 0, 1)">jedis.auth(</span></strong></span><strong><span style="color: rgba(255, 0, 0, 1)">"myRedis"</span></strong><span style="color: rgba(0, 0, 0, 1)"><strong><span style="color: rgba(255, 0, 0, 1)">); </span> // 设置密码</strong>
System.out.println(</span>"Connection to server sucessfully"<span style="color: rgba(0, 0, 0, 1)">);
</span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> 设置 redis 字符串数据</span>
jedis.set("redis", "Redis 1"<span style="color: rgba(0, 0, 0, 1)">);
</span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> 获取存储的数据并输出</span>
System.out.println("redis : " + jedis.get("redis"<span style="color: rgba(0, 0, 0, 1)">));
}
}
执行,返回正常
1 | redis : redis |
OK。
未完待续。