通过jedis远程访问redis服务器
一.jedis简介
类似于mysql数据库,一般开发都需要通过代码去访问redis服务器,对于主流的开发语言,redis提供了访问的客户端接口。

而对于Java而言,最常用的就是jedis了。
二.jedis访问redis服务器代码实现
1.创建Maven工程,添加jedis依赖如下:
<!--redis-->
<dependency>
<groupId>redis.clients</groupId>
<artifactId>jedis</artifactId>
<version>2.9.0</version>
</dependency>
</dependencies>
2.简单的代码测试
public class RedisTest {
public static void main(String[] args) {
Jedis jedis = new Jedis("192.168.2.132", 6379); //创建客户端,1p和端口号
jedis.set("name", "齐高相");
String value = jedis.get("name");
System.out.println(value);
jedis.close(); //释放连接资源
}
}
运行之后,会报错。

原因是redis在虚拟机上,默认只允许本机访问,
1.开放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.qgx.redis.RedisTest.main(RedisTest.java:9)
报错的意思是说redis在保护模式下运行,需要去掉保护。
去掉redis的自我保护
1.直接去掉自我保护(不推荐)
进入redis的配置文件
vi /usr/local/redis/redis.conf

将daemonize yes改为no
第二种 设置redis的连接密码

./bin/redis-cli
config set requirepass 123456 123456为访问密码
代码处需要认证密码。

重新运行

至此,远程访问成功!
通过jedis远程访问redis服务器的更多相关文章
- Redis服务器搭建/配置/及Jedis客户端的使用方法
摘要 Redis服务器搭建.常用参数含意说明.主从配置.以及使用Jedis客户端来操作Redis Redis服务器搭建 安装 在命令行执行下面的命令: $ wget http://download.r ...
- 外部服务器使用jedis操作redis数据库
使用maven获取jedis的包,我本来想去找jedis的jar包的,但是没找到. (maven) <dependency> <groupId>redis.client ...
- Redis服务器开启远程访问
重启 ps aux|grep redis root 32684 0.0 0.0 48452 6944 ? Ssl 21:27 0:00 ./redis-server *:6379 kill了这个进程 ...
- Spring + Jedis集成Redis(集群redis数据库)
前段时间说过单例redis数据库的方法,但是生成环境一般不会使用,基本上都是集群redis数据库,所以这里说说集群redis的代码. 1.pom.xml引入jar <!--Redis--> ...
- Java中使用Jedis操作Redis(转载)
整理 1.字符串 添加:set keyname value 查询:get keyname 拼接:append keyname value 删除:del keyname 添加多个: mset keyna ...
- (转)Java中使用Jedis操作Redis
转自http://www.cnblogs.com/liuling/p/2014-4-19-04.html 使用Java操作Redis需要jedis-2.1.0.jar,下载地址:http://file ...
- Java中使用Jedis操作Redis
使用Java操作Redis需要jedis-2.1.0.jar,下载地址:http://files.cnblogs.com/liuling/jedis-2.1.0.jar.zip 如果需要使用Redis ...
- 关于Jedis连接redis出现问题
环境说明: redis服务器系统:ubuntu ip 192.168.10.9 port 6379 两台电脑:一个作为专门的服务器,一个是开发环境,以下一顿操作皆基于开发环境. 就这样的简单的代码连接 ...
- Jedis操作Redis数据库
添加Maven依赖: <dependencies> <!-- 单元测试 --> <dependency> <groupId>junit</grou ...
随机推荐
- 阿里云ecs使用补充说明
前置文章:http://www.cnblogs.com/newbob/articles/5400495.html 阿里云ecs使用心得的一些个人补充部分 一.在yum安装node/npm的过程中出现的 ...
- CSS-动画巧用translated3d
VUE2.0单元素或组件的过渡 情景:插件从右向左滑动,巧用translate3d .food position :fixed left : 0 top :0 bottom :48px z-index ...
- python中shelve模块的使用
import shelve # 将序列化文件操作dump与load进行封装,写入文件的内容就是个大的字符串字典 s_dic = shelve.open("text/b.txt",w ...
- node.js一行一行的获取txt文件内容
node.js一行一行获取text文件代码: const readline = require('readline');//Readline是Node.js里实现标准输入输出的封装好的模块,通过这个模 ...
- java反射2
package com.wen; import java.lang.reflect.Field;import java.lang.reflect.Method; public class Test2 ...
- Go语言极速入门手册.go
Github: https://github.com/coderzh/CodeTips /* gotips_test.go: Golang速学速查速用代码手册 Source: github.com/c ...
- android开发解决Error:Execution failed for task ':app:transformDexArchiveWithExternalLibsDexMergerForDebug'. > java.lang.RuntimeException: java.lang.RuntimeException: c.....
网上常见的方法我都试过,都没能解决,偶然看到的一个方法解决了,在这了记录一下. 在App目录下的build.gradle的android{ ... ....}中添加如下代码,即可解决.(xx.xx. ...
- 我发起了一个 用 javascript 写一个 Office 的 开源项目 JS Office
用 js 写 Office, 说实在的, 把 现在已有的 各种 富文本编辑器 和 电子表格 js 库 收集起来 整合一下 就 差不多了 , 放到 前几天 那个 “ js 作为 一等公民 ...
- eclipse项目改为maven项目导致svn无法比较历史数据的解决办法
这个问题没有找到合适的答案,最终自己想出了一个解决方案,在此记录下. 问题描述 在将老的eclipse项目重构为maven项目时,我这边是新建了一个maven项目,然后将对应的数据分别放到相应的位置, ...
- 弹出的 Dialog 里,包含 Form,如何在关闭 Dialog 时,执行 resetFields(对整个表单进行重置,将所有字段值重置为初始值并移除校验结果)
做法: before-close 事件中,调用 resetFields 取消按钮事件中,调用 resetFields <el-dialog title="弹出窗口" :vis ...