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 设置密码

   修改redis.conf 配置文件,找到 “requirepass”  
  取消注释,在后面添加你的密码   
 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数据库(-)的更多相关文章

  1. springboot 使用 jedis 连接 Redis 数据库

    1. 在 pom.xml 配置文件中添加依赖 <!-- redis 依赖 --> <dependency> <groupId>org.springframework ...

  2. 【redis数据库学习】用JAVA连接redis数据库各种报错

    最近项目中,需要用到redis数据库,然后使用Jedis让JAVA连接redis. 首先,安装redis数据库,参考的是:http://www.runoob.com/redis/redis-insta ...

  3. 用Jedis连接Redis

    jedis中的方法名,和Redis的命令几乎一样 1.jar包,作为测试只需要一个jar 2.代码 package com; import java.util.HashMap; import java ...

  4. Java通过jedis操作redis缓存

    package com.wodexiangce.util; import java.util.Set; import redis.clients.jedis.Jedis; /** * redis工具类 ...

  5. Redis入门和Java利用jedis操作redis

    Redis入门和Java利用jedis操作redis Redis介绍 Redis 是完全开源的,遵守 BSD 协议,是一个高性能的 key-value 数据库. Redis 与其他 key - val ...

  6. 通过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 但是使 ...

  7. java用JDBC连接MySQL数据库的详细知识点

    想实现java用JDBC连接MySQL数据库.需要有几个准备工作: 1.下载Connector/J的库文件,下载Connector/J的官网地址:http://www.mysql.com/downlo ...

  8. JAVA通过JDBC连接Oracle数据库详解【转载】

    JAVA通过JDBC连接Oracle数据库详解 (2011-03-15 00:10:03) 转载▼http://blog.sina.com.cn/s/blog_61da86dd0100q27w.htm ...

  9. Java Web工程连接MySQL数据库及Tomcat服务器页面中文乱码

    Java Web工程连接MySQL数据库 一. 准备工作 1.下载连接MySQL数据库的JDBC (可以去官网下,也可以去百度云找) 2.将下载的jar文件复制到Tomcat的lib目录下 3.新建一 ...

随机推荐

  1. window netsh interface portproxy 配置转发

    系统版本 windows server2016 datacenter 1.配置443.80端口转发到其他服务器的443.80上 netsh interface portproxy add v4tov4 ...

  2. div中让内容能不换行就尽量不换行.【纯原】

    div中让内容能不换行就尽量不换行,部分左对齐,部分右对齐. <html> <head> <title>九歌·少司命</title> <style ...

  3. PHP5.5+Nginx1.9

    1. 安装Nginx:http://www.cnblogs.com/vurtne-lu/p/7010065.html 2. 编译安装 [root@zabbix opt]# wget http://cn ...

  4. golang变量声明

    func main() { var a1 int a1 = 1 var a = 1 b := 1 var c, d int c = 1 d = 1 var e, f = 1, 2 g, h := 1, ...

  5. JAVA求解全排列

    一,问题描述 给定一个字符串,求出该字符串的全排列. 比如:"abc"的全排列是:abc.acb.bac.bca.cab.cba 二,实现思路 采用递归的方式求解.每次先选定一个字 ...

  6. codeforces724G Xor-matic Number of the Graph

    本文版权归ljh2000和博客园共有,欢迎转载,但须保留此声明,并给出原文链接,谢谢合作. 本文作者:ljh2000 作者博客:http://www.cnblogs.com/ljh2000-jump/ ...

  7. C语言入门教程-(6)运算符

    1.运算符概述 运算符是一种编译器执行特定的数学或逻辑操作的符号.C语言提供了以下类型的运算符: 算术运算符 关系运算符 逻辑运算符 位运算符 赋值运算符 条件运算符 其他运算符 2.算术运算符 算术 ...

  8. JavaScript之原生接口类设计

    //接口类         var Interface =  function(name , methods){             if(arguments.length!=2){       ...

  9. USB摄像头无法正常读取问题

    opencv读取摄像头或者视频一种是早期版本的IplImage结构体,图片就存在结构体指针IplImage*中,另一种是Mat类,两者在操作上略有差异,且opencv2都兼容这两个版本,前面的博客也说 ...

  10. php 全局变量问题

    当在函数里通过require_once包含另外php文件. 而另外php文件包含了另外php文件,而该php文件的函数需要另外的php文件. 例子: installment_maintenance_s ...