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

你可能感兴趣的:(python,开发语言)