前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >Windows远程连接Redis(Linux)

Windows远程连接Redis(Linux)

作者头像
WHYBIGDATA
发布2023-01-31 14:40:45
9.1K0
发布2023-01-31 14:40:45
举报
文章被收录于专栏:WHYBIGDATA公众号同步文章

Windows远程连接Redis(Linux)

  • 1、写在前面
  • 2、配置redis.conf
  • 3、启动Redis
    • 3.1 开启redis服务
    • 3.2 启动客户端
    • 3.3 Redis命令
    • 3.4 查看Redis密码
  • 4、关闭Redis
  • 5、Java操作Redis


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的文件位置

代码语言:javascript
复制
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 时为默认值)。

代码语言:javascript
复制
zhangsan@node01:/usr/local/redis-3.2.7$ src/redis-cli -h 192.168.132.10 -a password --raw

3.3 Redis命令

代码语言:javascript
复制
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密码

  • 查看认证密码
代码语言:javascript
复制
config get requirepass
  • 修改认证密码
代码语言:javascript
复制
config set requirepass 123456 #设置redis密码

4、关闭Redis

代码语言:javascript
复制
192.168.132.10:6379> SHUTDOWN
not connected>

5、Java操作Redis

代码语言:javascript
复制
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连接失败");
        }
    }

结束!

本文参与 腾讯云自媒体同步曝光计划,分享自微信公众号。
原始发表:2022-12-10,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 WHYBIGDATA 微信公众号,前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • Windows远程连接Redis(Linux)
    • 1、写在前面
      • 2、配置redis.conf
        • 3、启动Redis
          • 3.1 开启redis服务
          • 3.2 启动客户端
          • 3.3 Redis命令
          • 3.4 查看Redis密码
        • 4、关闭Redis
          • 5、Java操作Redis
          相关产品与服务
          云数据库 Redis
          腾讯云数据库 Redis(TencentDB for Redis)是腾讯云打造的兼容 Redis 协议的缓存和存储服务。丰富的数据结构能帮助您完成不同类型的业务场景开发。支持主从热备,提供自动容灾切换、数据备份、故障迁移、实例监控、在线扩容、数据回档等全套的数据库服务。
          领券
          问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档