• java jedis连接redis数据库实战


    1.下载jedis-5.0.0.jar 下载地址:
    https://mvnrepository.com/artifact/redis.clients/jedis/2.9.0

    2.编写java代码
    vi RedisJDBCDemol.java
    [mysql@t3-dtpoc-dtpoc-web04 liys]$ javac RedisJDBCDemol.java
      
      
      
      import redis.clients.jedis.Jedis;
     
    public class RedisJDBCDemol {
        public static void main(String[] args){

            Jedis jedis = new Jedis("10.153.119.7", 6379);
       
          jedis.auth("123456");
            System.out.println(jedis.ping());
        }
    }
    3.编译java代码,发现报错。重新下载低版本的jar包jedis-2.9.0.jar 后编译通过
    [mysql@t3-dtpoc-dtpoc-web04 liys]$ javac RedisJDBCDemol.java
    warning: /home/mysql/liys/jdk64/lib/jedis-4.4.3.jar(redis/clients/jedis/Jedis.class): major version 52 is newer than 51, the highest major version supported by this compiler.
      It is recommended that the compiler be upgraded.
      
    3.执行java文件,发现报错,
    [mysql@t3-dtpoc-dtpoc-web04 liys]$ java RedisJDBCDemol
    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.BinaryClient.connect(BinaryClient.java:96)
            at redis.clients.jedis.Connection.sendCommand(Connection.java:126)
            at redis.clients.jedis.Connection.sendCommand(Connection.java:117)
            at redis.clients.jedis.BinaryClient.auth(BinaryClient.java:564)
            at redis.clients.jedis.BinaryJedis.auth(BinaryJedis.java:2138)
            at RedisJDBCDemol.main(RedisJDBCDemol.java:8)
            

    编辑 Redis 配置文件 redis.conf 并将保护模式选项设置为“protected-mode no,重新执行java代码发现又报错:

    [mysql@t3-dtpoc-dtpoc-web04 liys]$ java RedisJDBCDemol     
    Exception in thread "main" redis.clients.jedis.exceptions.JedisDataException: ERR AUTH called without any password configured for the default user. Are you sure your configuration is correct?
            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.BinaryClient.connect(BinaryClient.java:96)
            at redis.clients.jedis.Connection.sendCommand(Connection.java:126)
            at redis.clients.jedis.Connection.sendCommand(Connection.java:117)
            at redis.clients.jedis.BinaryClient.auth(BinaryClient.java:564)
            at redis.clients.jedis.BinaryJedis.auth(BinaryJedis.java:2138)
            at RedisJDBCDemol.main(RedisJDBCDemol.java:8)    
    原来是redis server端没有设置密码,把jedis.auth("123456");去掉重新编译执行,发现连接成功
    public class RedisJDBCDemol {
        public static void main(String[] args){

            Jedis jedis = new Jedis("10.153.119.7", 6379);
       
            System.out.println(jedis.ping());
        }
    }        
            
    [mysql@t3-dtpoc-dtpoc-web04 liys]$ javac RedisJDBCDemol.java 
    [mysql@t3-dtpoc-dtpoc-web04 liys]$ java RedisJDBCDemol
    PONG

    4.写入数据并读取数据测试成功
    public class RedisJDBCDemol {
        public static void main(String[] args){

            Jedis jedis = new Jedis("10.153.119.7", 6379);
            jedis.set("name","tom");
            jedis.set("age","18");
            System.out.println(jedis.ping());      // pong
            System.out.println(jedis.get("name")); // tom
            System.out.println(jedis.get("age")); // 18

        }
    }
    [mysql@t3-dtpoc-dtpoc-web04 liys]$ javac RedisJDBCDemol.java 
    [mysql@t3-dtpoc-dtpoc-web04 liys]$ java RedisJDBCDemol       
    PONG
    tom
    18

  • 相关阅读:
    算法|每日一题|根据规则将箱子分类|注意转换数据类型
    【MindSpore易点通】数据处理之Numpy数组的轴和矢量化
    绥化学院学报杂志绥化学院学杂志社绥化学院学报编辑部2022年第9期目录
    一文教你如何使用Scan Kit快速生成带有logo的个性化二维码
    tailwindcss -原子化 CSS 框架
    ubuntu虚拟机终端使用安装脚本报错,请问如何解决
    面试官:单核 CPU 支持 Java 多线程吗?为什么?被问懵了!
    移动订货小程序哪个好 批发订货系统源码哪个好
    2024年pmp考试还有多久啊?怎么备考?
    基于Fluent求解器进行二次开发到底怎么做?
  • 原文地址:https://blog.csdn.net/liys0811/article/details/133707807