Windows远程连接Redis(Linux)

Windows远程连接Redis(Linux)


在这里插入图片描述


1、写在前面

  • Windows版本:Windows10
  • Linux版本:Ubuntu Kylin 16.04
  • Redis版本:Redis-3.2.7
  • IDE:IntelliJ IDEA Ultimate2020.2.3
  • Redis:单机部署

2、配置redis.conf

修改redis.conf配置文件

  • 注释掉bind 127.0.0.1这一行,如下图所示:

在这里插入图片描述

  • 设置客户端连接的密码requirepass,如下图所示:

在这里插入图片描述

  • 官方说明

This should stay commented out for backward compatibility and because most people do not need auth (e.g. they run their own servers).

为了向后兼容性,这(#requirepass foobared)应该被注释掉,因为大多数人不需要身份验证(例如,他们运行自己的服务器)。

此处是个人机器的使用,直接设置即可

  • 关闭保护模式,如下图所示:

在这里插入图片描述

  • 官方说明

By default protected mode is enabled. You should disable it only if you are sure you want clients from other hosts to connect to Redis even if no authentication is configured, nor a specific set of interfaces are explicitly listed using the “bind” directive.

Protected mode是一层安全保护,旨在避免访问和利用互联网上打开的 Redis 实例。

当保护模式打开时,如果:
1) 服务器未使用 “bind” 指令显式绑定到一组地址。
2) 未配置密码。

  • 服务器仅接受来自从IPv4 和 IPv6 环回地址 127.0.0.1 和 ::1,以及来自 Unix 域套接字。

默认情况下,保护模式处于启用状态。仅当您确定希望其他主机的客户端连接到 Redis 时,才应禁用它,即使未配置身份验证,也没有使用bind指令显式列出一组特定的接口。

3、启动Redis

3.1 开启redis服务

需要指定redis.conf的文件位置

zhangsan@node01:/usr/local/redis-3.2.7$ src/redis-server ./redis.conf

后台启动设置daemonize no 改成 yes

3.2 启动客户端

启动客户端,在命令行指定Redis主机地址、认证密码,端口号默认是6379,可以不用指定

--raw参数是防止中文乱码,对Redis操作的结果使用raw格式(当 STDOUT 不是 tty 时为默认值)。

zhangsan@node01:/usr/local/redis-3.2.7$ src/redis-cli -h 192.168.132.10 -a password --raw

3.3 Redis命令

zhangsan@node01:/usr/local/redis-3.2.7$ src/redis-cli --help
redis-cli 3.2.7

Usage: redis-cli [OPTIONS] [cmd [arg [arg ...]]]
  -h <hostname>      Server hostname (default: 127.0.0.1).
  -p <port>          Server port (default: 6379).
  -s <socket>        Server socket (overrides hostname and port).
  -a <password>      Password to use when connecting to the server.
  -r <repeat>        Execute specified command N times.
  -i <interval>      When -r is used, waits <interval> seconds per command.
                     It is possible to specify sub-second times like -i 0.1.
  -n <db>            Database number.
  -x                 Read last argument from STDIN.
  -d <delimiter>     Multi-bulk delimiter in for raw formatting (default: \n).
  -c                 Enable cluster mode (follow -ASK and -MOVED redirections).
  --raw              Use raw formatting for replies (default when STDOUT is
                     not a tty).
  --no-raw           Force formatted output even when STDOUT is not a tty.
  --csv              Output in CSV format.
  --stat             Print rolling stats about server: mem, clients, ...
  --latency          Enter a special mode continuously sampling latency.
  --latency-history  Like --latency but tracking latency changes over time.
                     Default time interval is 15 sec. Change it using -i.
  --latency-dist     Shows latency as a spectrum, requires xterm 256 colors.
                     Default time interval is 1 sec. Change it using -i.
  --lru-test <keys>  Simulate a cache workload with an 80-20 distribution.
  --slave            Simulate a slave showing commands received from the master.
  --rdb <filename>   Transfer an RDB dump from remote server to local file.
  --pipe             Transfer raw Redis protocol from stdin to server.
  --pipe-timeout <n> In --pipe mode, abort with error if after sending all data.
                     no reply is received within <n> seconds.
                     Default timeout: 30. Use 0 to wait forever.
  --bigkeys          Sample Redis keys looking for big keys.
  --scan             List all keys using the SCAN command.
  --pattern <pat>    Useful with --scan to specify a SCAN pattern.
  --intrinsic-latency <sec> Run a test to measure intrinsic system latency.
                     The test will run for the specified amount of seconds.
  --eval <file>      Send an EVAL command using the Lua script at <file>.
  --ldb              Used with --eval enable the Redis Lua debugger.
  --ldb-sync-mode    Like --ldb but uses the synchronous Lua debugger, in
                     this mode the server is blocked and script changes are
                     are not rolled back from the server memory.
  --help             Output this help and exit.
  --version          Output version and exit.

3.4 查看Redis密码

  • 查看认证密码
config get requirepass
  • 修改认证密码
config set requirepass 123456 #设置redis密码

4、关闭Redis

192.168.132.10:6379> SHUTDOWN
not connected>

5、Java操作Redis

import redis.clients.jedis.Jedis;
import redis.clients.jedis.JedisPool;

import java.util.*;

public class RedisDemo {
    private static String HOST = "192.168.132.10";
    private static int PORT = 6379;
    private static String PWD = "password";
    private static Jedis jedis = null;
    private static JedisPool jedisPool = null;


    public static void main(String[] args) {
        // 1. 创建Jedis对象(两个都可以)
//        jedis = new Jedis(HOST, PORT);
        init();

        // 2. 测试
        String res = jedis.ping();
//        System.out.println(res);

    }


    /**
     * TODO 获取Jedis实例
     */
    public synchronized static Jedis getJedis() {
        try {
            if (jedisPool != null) {
                Jedis resource = jedisPool.getResource();
                return resource;
            } else {
                return null;
            }

        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }
    }

    /**
     * TODO 释放资源
     */
    public static void returnResource(final Jedis jedis) {
        if (jedis != null) {
//            jedisPool.returnResource(jedis);
            jedis.close();
            jedisPool.close();
        }
    }


    /**
     * TODO 初始化Redis连接池
     */
    public static void init() {
        if (jedis == null) {
            jedis = new Jedis(HOST, PORT);
            jedis.auth(PWD);
        }
        if (jedis != null) {
            System.out.println("Redis连接成功");
        } else {
            System.out.println("Redis连接失败");
        }
    }

结束!

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值