开启运维之路之第 7 篇——RedisDesktopManager使用、Keys通用操作、Java代码操作基本的Redis

版权声明: https://blog.csdn.net/BiandanLoveyou/article/details/81298080

RedisDesktopManager下载地址:Redis桌面管理工具官方下载地址

安装好,直接双击打开。

说明:我本机的 IP 由于使用公司的 IP ,经常会变动,但不影响连接 Linux 虚拟机。

现在发现个问题,无法连接到 Redis 。

解决过程:

①Redis 在默认情况下,配置文件里并没有设置登录密码,可以查看:前提是先进入 Redis 客户端,命令查看上一篇博客。

127.0.0.1:6379> config get requirepass
1) "requirepass"
2) ""

默认情况下 requirepass 参数是空的,这就意味着你无需通过密码验证就可以连接到 redis 服务。可问题是不用密码也连接不上,这不是坑爹?

不,不多踩一些坑,是不会有进步的,特别是在技术领域。一帆风顺反而引起你的恐慌。

那就设置一下登录 Redis 的密码:config set requirepass XXXXX

127.0.0.1:6379> config set requirepass 123456
OK
127.0.0.1:6379> auth 123456
OK

说明:如果遇到下面的错误:(error) ERR Client sent AUTH, but no password is set

请先进入客户端,设置如下命令:config set requirepass XXXXX,再进行授权密码XXXXX

再验证下:

还是不行。

想一下,应该是防火墙的问题。防火墙。防火墙。防火墙。

查看防火墙状态:

127.0.0.1:6379> exit
[root@localhost redis]# firewall-cmd --state
not running

结果防火墙也没开启,因为我已经设置防火墙开机不启动了。(防火墙没开启,说明 Linux 的端口都可以访问。以前听说那些网站被病毒攻击,就是因为病毒使用了某些开放的端口,让病毒有机可乘。一般是不去访问病毒网站或者关闭病毒入侵的端口号。)

OK,那就应该是配置文件的问题了。去看下。(说明:本系列博客,在玩 Redis 的时候,已经把配置文件复制出来,放在与 Redis 解压文件的同一级目录了。原生的 redis.conf 文件并没有改动。这样一来,哪怕你玩坏了复制的那份配置文件,也不会有问题,还可以使用原生的配置文件。)

[root@localhost redis]# ls
bin  dump.rdb  redis.conf

下面是查看原生 redis.conf 配置文件的命令:

[root@localhost ~]# cd /root/redis-4.0.10
[root@localhost redis-4.0.10]# ls
00-RELEASENOTES  deps       README.md        runtest-sentinel  utils
BUGS             INSTALL    redis.conf       sentinel.conf
CONTRIBUTING     Makefile   runtest          src
COPYING          MANIFESTO  runtest-cluster  tests

OK,修改一下我们默认启动时使用的配置文件:我们之前复制了一份配置文件放在 redis 的目录下。

[root@localhost redis]# ls
bin  dump.rdb  redis.conf

现在修改这份配置文件:[root@localhost redis]# vi redis.conf 

找到 bind 127.0.0.1,添加 # 注释掉。不注释掉就只有使用 Linux 机器才能登录。然后保存退出。

这时候再连接,还是会报错!因为没有重启。然后就重启 Redis 。

可以使用 shell 脚本,也可以敲打命令:

[root@localhost ~]# cd /usr/local/redis/
[root@localhost redis]# ./bin/redis-server ./redis.conf 

OK,windows客户端管理连接成功!

这时候,我们再进入 Linux 下 Redis 的客户端,ping 一下:

[root@localhost redis]# ./bin/redis-cli
127.0.0.1:6379> ping
(error) NOAUTH Authentication required.

无法 ping 通,必须使用密码。那就设置密码。

127.0.0.1:6379> auth 123456
OK
127.0.0.1:6379> ping
PONG

说明:这样设置授权密码的方式,在重启 Redis 之后,密码失效,又得重新设置。因此,我们设置到 Redis 的配置文件,下次启动就不需要重新设置密码了。

修改配置文件:通过输入 /requirepass 搜索密码设置,n 查找下一个,N查找上一个匹配的字符串。

[root@localhost redis]# vi redis.conf

然后修改密码:把注释去掉,修改 foobared 成你的密码。

保存,退出。这样,下次就不需要再重新授权密码了。不过,进入 Redis 客户端,都要通过密码登陆。

[root@localhost redis]# ./bin/redis-cli
127.0.0.1:6379> ping
(error) NOAUTH Authentication required.
127.0.0.1:6379> auth 123456
OK
127.0.0.1:6379> ping
PONG

还有一种方式,就是去掉 Redis 的自我保护,但这种方式安全性差,不推荐,只有在特殊的情况下使用,了解即可。

OK,回到windows的Redis管理工具

说明:Redis 默认使用 db0 这个库。里面存放着所有的数据,就是我们之前测试的。右边主要是数据的一些操作。TTL (Time To Live)的值是 -1 说明数据不会过期。我们试着在这里增加一些数据,然后去 Linux 客户端下查看:

注意,我们设置的是 hash 类型,需要根据 hash 的特性进行获取值:

OK,完美搞定!

Keys通用操作

①显示所有 keys ,包括 5 种数据类型:keys *

127.0.0.1:6379> keys *
 1) "s1"
 2) "myList3"
 3) "sr1"
 4) "n1"
 5) "h1"
 6) "c"
 7) "m"

②正则表达式: * 代表所有,? 代表一个字符

127.0.0.1:6379> keys s*
1) "s1"
2) "sr1"
3) "s2"
4) "sr2"
5) "sr3"
127.0.0.1:6379> keys s?
1) "s1"
2) "s2"

③删除 key:del key1 key2  ...

127.0.0.1:6379> del n1 c
(integer) 2

④判断 key 是否存在:1-存在,0-不存在:exists key

127.0.0.1:6379> exists n1
(integer) 0
127.0.0.1:6379> exists s1
(integer) 1

⑤对 key 重命名:rename oldName newName

127.0.0.1:6379> rename m m2
OK

⑥设置 key 的有效期:expire key time  单位:秒

127.0.0.1:6379> expire m2 120
(integer) 1

我们打开 Redis 桌面管理工具,查看 m2 的有效期

⑦查看 key 有效期:ttl key

127.0.0.1:6379> ttl m2
(integer) 4
127.0.0.1:6379> ttl m2
(integer) 1
127.0.0.1:6379> ttl m2
(integer) -2

说明:单位是秒,-1 是永久有效,-2 是已经不存在。

对不存在的 key 设置过期时间,返回 0 :

127.0.0.1:6379> expire m2 123
(integer) 0

⑧查看 key 类型:type key

127.0.0.1:6379> type s1
set
127.0.0.1:6379> type n
string

Java代码操作基本的Redis

①在 pom.xml 配置文件里添加下面的配置

<dependency>
			<groupId>redis.clients</groupId>
			<artifactId>jedis</artifactId>
			<version>2.9.0</version>
		</dependency>

或者去下载一个操作 Redis 的 jar 包,地址:操作 Redis 的 jar 包下载地址

OK,编写测试Java类:

package com.test.test;

import redis.clients.jedis.Jedis;

public class JedisTest {
    
    public static void main(String[] args){
        Jedis jedis=new Jedis();
    }

}

到这步,我们进入 Jedis 类,看下里面的构造器、实现的方法。按住 Ctrl + 鼠标左键,进入 Jedis 类。

看下常用方法:(只列举一点做引导,很多方法都是根据 Redis 客户端的方法来定义方法名的。这样可以避免方法名的转换,顾名思义,即拿即用。)

/**
   * Set the string value as value of the key. The string can't be longer than 1073741824 bytes (1
   * GB).
   * <p>
   * Time complexity: O(1)
   * @param key
   * @param value
   * @return Status code reply
   */
  public String set(final String key, String value) {
    checkIsInMultiOrPipeline();
    client.set(key, value);
    return client.getStatusCodeReply();
  }

/**
   * Get the value of the specified key. If the key does not exist null is returned. If the value
   * stored at key is not a string an error is returned because GET can only handle string values.
   * <p>
   * Time complexity: O(1)
   * @param key
   * @return Bulk reply
   */
  public String get(final String key) {
    checkIsInMultiOrPipeline();
    client.sendCommand(Protocol.Command.GET, key);
    return client.getBulkReply();
  }

/**
   * Test if the specified key exists. The command returns "1" if the key exists, otherwise "0" is
   * returned. Note that even keys set with an empty string as value will return "1". Time
   * complexity: O(1)
   * @param key
   * @return Boolean reply, true if the key exists, otherwise false
   */
  public Boolean exists(final String key) {
    checkIsInMultiOrPipeline();
    client.exists(key);
    return client.getIntegerReply() == 1;
  }

public Long del(String key) {
    client.del(key);
    return client.getIntegerReply();
  }

public Long expire(final String key, final int seconds) {
    checkIsInMultiOrPipeline();
    client.expire(key, seconds);
    return client.getIntegerReply();
  }

public Long ttl(final String key) {
    checkIsInMultiOrPipeline();
    client.ttl(key);
    return client.getIntegerReply();
  }


public Long hset(final String key, final String field, final String value) {
    checkIsInMultiOrPipeline();
    client.hset(key, field, value);
    return client.getIntegerReply();
  }

public String hget(final String key, final String field) {
    checkIsInMultiOrPipeline();
    client.hget(key, field);
    return client.getBulkReply();
  }

OK,我们来玩一下:

public static void main(String[] args){
        Jedis jedis=new Jedis("192.168.126.130",6379);
        jedis.set("name","Hi,girl,would you love me? 好哒");
        String name=jedis.get("name");
        System.out.println("name="+name);
        jedis.close();
    }

点击运行 main 函数:

说明了啥?需要授权密码。OK,我们设置密码:

perfect!强势搞定!不信去 Redis 桌面管理工具看看!

package com.test.test;

import redis.clients.jedis.Jedis;

import java.util.List;
import java.util.Set;

public class JedisTest {

    public static void main(String[] args){
        Jedis jedis=new Jedis("192.168.126.130",6379);
        jedis.auth("123456");

        /*************** 测试 string *******************/
        jedis.set("name","Hi,girl,would you love me? 好哒");
        String name=jedis.get("name");
        System.out.println("name="+name);

        /*************** 测试 list *******************/
        jedis.rpush("jList","What's your "," telephone ?");
        List<String> list= jedis.lrange("jList",0,-1);
        for(String str : list){
            System.out.println(str);
        }

        /*************** 测试 hash *******************/
        jedis.hset("jHash","hometown","My hometown is 流放深圳!");
        String hash=jedis.hget("jHash","hometown");
        System.out.println(hash);

        /*************** 测试 set *******************/
        jedis.sadd("jSet","北京");
        jedis.sadd("jSet","上海");
        jedis.sadd("jSet","深圳");
        Set<String> set = jedis.smembers("jSet");
        for(String str : set) {
            System.out.println(str);
        }

        /*************** 测试 zset *******************/
        jedis.zadd("jzSet",18,"小薇");
        jedis.zadd("jzSet",20,"小胖");
        jedis.zadd("jzSet",15,"小傻");
        Set<String> zset=jedis.zrange("jzSet",0,-1);
        for(String str : zset){
            System.out.println(str);
        }
        jedis.close();
    }

}

猜你喜欢

转载自blog.csdn.net/BiandanLoveyou/article/details/81298080
今日推荐