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 {
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。
未完待续。
Java 使用Jedis连接Redis数据库(-)的更多相关文章
- springboot 使用 jedis 连接 Redis 数据库
1. 在 pom.xml 配置文件中添加依赖 <!-- redis 依赖 --> <dependency> <groupId>org.springframework ...
- 【redis数据库学习】用JAVA连接redis数据库各种报错
最近项目中,需要用到redis数据库,然后使用Jedis让JAVA连接redis. 首先,安装redis数据库,参考的是:http://www.runoob.com/redis/redis-insta ...
- 用Jedis连接Redis
jedis中的方法名,和Redis的命令几乎一样 1.jar包,作为测试只需要一个jar 2.代码 package com; import java.util.HashMap; import java ...
- Java通过jedis操作redis缓存
package com.wodexiangce.util; import java.util.Set; import redis.clients.jedis.Jedis; /** * redis工具类 ...
- Redis入门和Java利用jedis操作redis
Redis入门和Java利用jedis操作redis Redis介绍 Redis 是完全开源的,遵守 BSD 协议,是一个高性能的 key-value 数据库. Redis 与其他 key - val ...
- 通过jedis连接redis单机成功,使用redis客户端可以连接集群,但使用JedisCluster连接redis集群一直报Could not get a resource from the pool
一,问题描述: (如题目)通过jedis连接redis单机成功,使用JedisCluster连接redis集群一直报Could not get a resource from the pool 但是使 ...
- java用JDBC连接MySQL数据库的详细知识点
想实现java用JDBC连接MySQL数据库.需要有几个准备工作: 1.下载Connector/J的库文件,下载Connector/J的官网地址:http://www.mysql.com/downlo ...
- JAVA通过JDBC连接Oracle数据库详解【转载】
JAVA通过JDBC连接Oracle数据库详解 (2011-03-15 00:10:03) 转载▼http://blog.sina.com.cn/s/blog_61da86dd0100q27w.htm ...
- Java Web工程连接MySQL数据库及Tomcat服务器页面中文乱码
Java Web工程连接MySQL数据库 一. 准备工作 1.下载连接MySQL数据库的JDBC (可以去官网下,也可以去百度云找) 2.将下载的jar文件复制到Tomcat的lib目录下 3.新建一 ...
随机推荐
- VS Code调试Golang提示Failed to continue:Check the debug console for details.
解决方法: 打开调试面板 VSCode->查看->调试 添加调试目标 在"没有调试"的下拉框中点击"添加配置.."添加目标调试配置 在" ...
- Java线程间和进程间通信
1 线程与线程间通信 1.1 基本概念以及线程与进程之间的区别联系 关于进程和线程,首先从定义上理解就有所不同: 进程是具有一定独立功能的程序.它是系统进行资源分配和调度的一个独立单位,重点在系统调度 ...
- git commit 时出现:please enter the commit message for your changes
每次准备提交前,先用 git status 看下,是不是都已暂存起来了,然后再运行提交命令 git commit: $ git commit 这种方式会启动文本编辑器以便输入本次提交的说明.(默认会启 ...
- java元注解 @Target注解用法
@Target: @Target说明了Annotation所修饰的对象范围:Annotation可被用于 packages.types(类.接口.枚举.Annotation类型).类型成员(方法.构造 ...
- Linux 下安装 storm
一:准备工作 (机器部署情况详见)这篇博客 3台安装supervisor,2台安装nimbus (1)安装jdk1.8 (2)安装zookeeper3.4.5 以上两部分安装可查看这篇博客 (3)下载 ...
- 绘图QPainter-画刷
Qt提供的画刷风格: Qt.TexturePattern 自定义图像画刷 线性渐变 QLinearGradientPattern QLinearGradient需要传入的参数为需要进行渐变的区域坐 ...
- Python(十三)python的函数重载
首先,重载函数的功能是实现参数不同情况下功能相同的函数. 函数重载的目的是解决功能相同的函数的以下问题: 1.参数的类型: 2.参数的个数: 对于情况1,函数功能呢相同,参数不同的情况. python ...
- Generative Adversarial Nets(原生GAN学习)
学习总结于国立台湾大学 :李宏毅老师 Author: Ian Goodfellow • Paper: https://arxiv.org/abs/1701.00160 • Video: https:/ ...
- word打不开怎么办?
方法一 故障描述:编辑Word文档的过程中,程序非法关闭,重新打开也是如此.即使重新安装了Office 2003,在启动Word 2003后仍然出现了异常情况.双击Word文档后,程序弹出出错对话框, ...
- kafka系列五、kafka常用java API
引入maven包 <dependency> <groupId>org.apache.kafka</groupId> <artifactId>kafka- ...