Windows远程连接Redis(Linux)
创始人
2024-03-26 19:52:40

Windows远程连接Redis(Linux)

文章目录

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

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.7Usage: redis-cli [OPTIONS] [cmd [arg [arg ...]]]-h       Server hostname (default: 127.0.0.1).-p           Server port (default: 6379).-s         Server socket (overrides hostname and port).-a       Password to use when connecting to the server.-r         Execute specified command N times.-i       When -r is used, waits  seconds per command.It is possible to specify sub-second times like -i 0.1.-n             Database number.-x                 Read last argument from STDIN.-d      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 isnot 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   Simulate a cache workload with an 80-20 distribution.--slave            Simulate a slave showing commands received from the master.--rdb    Transfer an RDB dump from remote server to local file.--pipe             Transfer raw Redis protocol from stdin to server.--pipe-timeout  In --pipe mode, abort with error if after sending all data.no reply is received within  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     Useful with --scan to specify a SCAN pattern.--intrinsic-latency  Run a test to measure intrinsic system latency.The test will run for the specified amount of seconds.--eval       Send an EVAL command using the Lua script at .--ldb              Used with --eval enable the Redis Lua debugger.--ldb-sync-mode    Like --ldb but uses the synchronous Lua debugger, inthis mode the server is blocked and script changes areare 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连接失败");}}

结束!

相关内容

热门资讯

埃菲尔铁塔在哪 中国仿建埃菲尔... 2019年4月26日,广西南宁市,街头惊现一座巨型山寨版埃菲尔铁塔,高约20米,白色塔身,造型逼真,...
苗族的传统节日 贵州苗族节日有... 【岜沙苗族芦笙节】岜沙,苗语叫“分送”,距从江县城7.5公里,是世界上最崇拜树木并以树为神的枪手部落...
北京的名胜古迹 北京最著名的景... 北京从元代开始,逐渐走上帝国首都的道路,先是成为大辽朝五大首都之一的南京城,随着金灭辽,金代从海陵王...
长白山自助游攻略 吉林长白山游... 昨天介绍了西坡的景点详细请看链接:一个人的旅行,据说能看到长白山天池全凭运气,您的运气如何?今日介绍...
世界上最漂亮的人 世界上最漂亮... 此前在某网上,选出了全球265万颜值姣好的女性。从这些数量庞大的女性群体中,人们投票选出了心目中最美...
脚上的穴位图 脚面经络图对应的... 人体穴位作用图解大全更清晰直观的标注了各个人体穴位的作用,包括头部穴位图、胸部穴位图、背部穴位图、胳...
应用未安装解决办法 平板应用未... ---IT小技术,每天Get一个小技能!一、前言描述苹果IPad2居然不能安装怎么办?与此IPad不...
猫咪吃了塑料袋怎么办 猫咪误食... 你知道吗?塑料袋放久了会长猫哦!要说猫咪对塑料袋的喜爱程度完完全全可以媲美纸箱家里只要一有塑料袋的响...
demo什么意思 demo版本... 618快到了,各位的小金库大概也在准备开闸放水了吧。没有小金库的,也该向老婆撒娇卖萌服个软了,一切只...
埃菲尔铁塔在哪 中国仿建埃菲尔... 2019年4月26日,广西南宁市,街头惊现一座巨型山寨版埃菲尔铁塔,高约20米,白色塔身,造型逼真,...
苗族的传统节日 贵州苗族节日有... 【岜沙苗族芦笙节】岜沙,苗语叫“分送”,距从江县城7.5公里,是世界上最崇拜树木并以树为神的枪手部落...
北京的名胜古迹 北京最著名的景... 北京从元代开始,逐渐走上帝国首都的道路,先是成为大辽朝五大首都之一的南京城,随着金灭辽,金代从海陵王...
长白山自助游攻略 吉林长白山游... 昨天介绍了西坡的景点详细请看链接:一个人的旅行,据说能看到长白山天池全凭运气,您的运气如何?今日介绍...
世界上最漂亮的人 世界上最漂亮... 此前在某网上,选出了全球265万颜值姣好的女性。从这些数量庞大的女性群体中,人们投票选出了心目中最美...
猫咪吃了塑料袋怎么办 猫咪误食... 你知道吗?塑料袋放久了会长猫哦!要说猫咪对塑料袋的喜爱程度完完全全可以媲美纸箱家里只要一有塑料袋的响...
应用未安装解决办法 平板应用未... ---IT小技术,每天Get一个小技能!一、前言描述苹果IPad2居然不能安装怎么办?与此IPad不...
脚上的穴位图 脚面经络图对应的... 人体穴位作用图解大全更清晰直观的标注了各个人体穴位的作用,包括头部穴位图、胸部穴位图、背部穴位图、胳...
demo什么意思 demo版本... 618快到了,各位的小金库大概也在准备开闸放水了吧。没有小金库的,也该向老婆撒娇卖萌服个软了,一切只...