通过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 ...
随机推荐
- java web从入门到精通
1.Springboot配置 1.1mybatis mapper.xml所在的目录必须为resource的资源文件夹,如果xml文件在java的package里面,需要修改文件夹类型 idea修改方式 ...
- 手动清除mac的广告弹框病毒 MacOSDefender
最近在浏览亚马逊, 京东的时候, 发现会自动弹出很多广告到浏览器, 其实是中了病毒MacOSDefender. 这个病毒非常烦人, 会在你浏览电商网页的时候拼命的打开广告页面, 而且还会弹出一些提示, ...
- Stackoverflow热门问题
1. JavaScript如何重定向到其他网页 如何使用JavaScript将用户从一个网页重定向到另一个网页? 2. JavaScript闭包是如何工作的 只知道JavaScript闭包的概念,但是 ...
- google搜索引擎使用
部分引用 http://yearslater.me/2017/06/15/%E5%A6%82%E4%BD%95%E4%BD%BF%E7%94%A8%E6%90%9C%E7%B4%A2%E5%BC%95 ...
- org.springframework.orm.hibernate3.HibernateSystemException:
org.springframework.orm.hibernate3.HibernateSystemException: The database returned no natively gener ...
- MATLAB 进行五种边缘检测
自定义函数: function []=edge_detect(image_name) a=imread(image_name); I=rgb2gray(a); BW1=edge(I,'Roberts' ...
- 不同组的id列表的汇总对比
需求: 三个不同的dfs中存在不同的多个节点id,现在需要求出不同的dfs之间的节点对应关系,比如,哪些节点在某一个dfs,但是不在另一个dfs中 思路: 一. 如果是单纯计算dfs中节点数量,则可以 ...
- 小妖精的完美游戏教室——buff系统
作者:小妖精Balous,未经作者允许,任何个人与单位不得将此源代码用于商业化项目 #region buff /// <summary> /// 是否魔法免疫,魔法免疫的生物不会受到除自己 ...
- 关于微软小娜Cortana(安卓版)
关于微软小娜,大家都知道是微软发布的全球第一款个人智能助理.它“能够了解用户的喜好和习惯”,“帮助用户进行日程安排.问题回答等”.今天我就向大家介绍一下安卓版的小娜. 首先,和电脑版的小娜一样,安卓版 ...
- Linux命令行下载工具
目录 wget aria2 cURL Youtube-dl https://zh.wikipedia.org/wiki/下载管理程序比较 https://wiki.archlinux.org/inde ...