一.jedis简介

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

  https://redis.io/clients

   而对于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服务器的更多相关文章

  1. Redis服务器搭建/配置/及Jedis客户端的使用方法

    摘要 Redis服务器搭建.常用参数含意说明.主从配置.以及使用Jedis客户端来操作Redis Redis服务器搭建 安装 在命令行执行下面的命令: $ wget http://download.r ...

  2. 外部服务器使用jedis操作redis数据库

    使用maven获取jedis的包,我本来想去找jedis的jar包的,但是没找到. (maven)     <dependency> <groupId>redis.client ...

  3. Redis服务器开启远程访问

    重启  ps aux|grep redis root 32684 0.0 0.0 48452 6944 ? Ssl 21:27 0:00 ./redis-server *:6379 kill了这个进程 ...

  4. Spring + Jedis集成Redis(集群redis数据库)

    前段时间说过单例redis数据库的方法,但是生成环境一般不会使用,基本上都是集群redis数据库,所以这里说说集群redis的代码. 1.pom.xml引入jar <!--Redis--> ...

  5. Java中使用Jedis操作Redis(转载)

    整理 1.字符串 添加:set keyname value 查询:get keyname 拼接:append keyname value 删除:del keyname 添加多个: mset keyna ...

  6. (转)Java中使用Jedis操作Redis

    转自http://www.cnblogs.com/liuling/p/2014-4-19-04.html 使用Java操作Redis需要jedis-2.1.0.jar,下载地址:http://file ...

  7. Java中使用Jedis操作Redis

    使用Java操作Redis需要jedis-2.1.0.jar,下载地址:http://files.cnblogs.com/liuling/jedis-2.1.0.jar.zip 如果需要使用Redis ...

  8. 关于Jedis连接redis出现问题

    环境说明: redis服务器系统:ubuntu ip 192.168.10.9 port 6379 两台电脑:一个作为专门的服务器,一个是开发环境,以下一顿操作皆基于开发环境. 就这样的简单的代码连接 ...

  9. Jedis操作Redis数据库

    添加Maven依赖: <dependencies> <!-- 单元测试 --> <dependency> <groupId>junit</grou ...

随机推荐

  1. idea 里自动下载私服jar一直不能下载下来

    idea 里自动下载私服jar一直不能下载下来,只生成了.lastUpdated文件,检查了setting.xml文件.网络,私服,均无问题,在idea中打开Terminal窗口,在所要更新的pom. ...

  2. C语言中的二维数组

    1.二维数组的定义和引用 一. 数据类型 数组名[常量表达式1][常量表达式2]; (1)假如有个二维数组array[n][m],则行下标的取值范围0~n-1 (2)列下标的取值范围0~m-1 (3) ...

  3. Codeforces 977D: Divide by three, multiply by two(暴力)

    题意 有nnn个无序的数,对这些数进行排列,要求ai=3×ai+1a_i=3\times a_{i+1}ai​=3×ai+1​或2×ai=ai+12\times a_i=a_{i+1}2×ai​=ai ...

  4. 【bug记录】OS Lab4 踩坑记

    OS Lab4 踩坑记 Lab4在之前Lab3的基础上,增加了系统调用,难度增加了很多.而且加上注释不详细,开玩笑的指导书,自己做起来困难较大.也遇到了大大小小的bug,调试了一整天. 本文记录笔者在 ...

  5. js cookie 操作 封装

    pCookie.js (function(){ var PotatogCookie = {}; //设置cookie PotatogCookie.set = function(key, value, ...

  6. gdb 调试 python

    gdb 版本 >7 的 对python调试有特别支持,参考: https://docs.python.org/devguide/gdb.html?highlight=gdb https://bl ...

  7. 解决ubuntu输入正确用户密码重新跳到无法登录

    解决方法:我们需要将.Xauthority的拥有者改为登陆用户(或者干脆将.Xauthority删除,此法转自网上,本人未验证)开机后在登陆界面按下shift + ctrl + F1进入tty命令行终 ...

  8. Python map,filter,reduce函数

    # -*- coding:utf-8 -*- #定义一个自己的map函数list_list = [1,2,4,8,16] def my_map(func,iterable): my_list = [] ...

  9. js/php判断移动端还是PC端

    if ((navigator.userAgent.match(/(phone|pad|pod|iPhone|iPod|ios|iPad|Android|Mobile|BlackBerry|IEMobi ...

  10. python获取机器信息脚本(网上寻找的)

    获取机器信息(待测试) # -*- coding: UTF-8 -*- import psutil import json import os import socket import struct ...