本地Redis服务配置

本地Redis服务配置

要求:在虚拟机中启动redis服务,并要在windows物理机上取得链接

  • 虚拟机安装略,(结果如下)

windows工作机上装了Oracle VM VirtualBox,并在其中装好了linux系统,

linux系统版本如下:

# uname -a
Linux localhost.localdomain 3.10.0-327.el7.x86_64 #1 SMP Thu Nov 19 22:10:57 UTC 2015 x86_64 x86_64 x86_64 GNU/Linux
# ip addr 【查看虚拟机ip,其中(10.10.12.18)为本虚拟机ip】
1: lo: <LOOPBACK,UP,LOWER_UP> mtu 65536 qdisc noqueue state UNKNOWN 
    link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00
    inet 127.0.0.1/8 scope host lo
       valid_lft forever preferred_lft forever
    inet6 ::1/128 scope host 
       valid_lft forever preferred_lft forever
2: enp0s3: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP qlen 1000
    link/ether 08:00:27:a8:1c:bd brd ff:ff:ff:ff:ff:ff
    inet 10.10.12.18/24 brd 10.10.12.255 scope global dynamic enp0s3
       valid_lft 77192sec preferred_lft 77192sec
    inet6 fe80::a00:27ff:fea8:1cbd/64 scope link 
       valid_lft forever preferred_lft forever

windows中ping测试成功

C:Usershdb>ping 10.10.12.18

正在 Ping 10.10.12.18 具有 32 字节的数据:
来自 10.10.12.18 的回复: 字节=32 时间<1ms TTL=64
来自 10.10.12.18 的回复: 字节=32 时间<1ms TTL=64
来自 10.10.12.18 的回复: 字节=32 时间<1ms TTL=64
来自 10.10.12.18 的回复: 字节=32 时间=1ms TTL=64

10.10.12.18 的 Ping 统计信息:
    数据包: 已发送 = 4,已接收 = 4,丢失 = 0 (0% 丢失),
往返行程的估计时间(以毫秒为单位):
    最短 = 0ms,最长 = 1ms,平均 = 0ms

接下来问题来了,redis启动后,可以在linux中进行增删改查,但是外部windows无法取得链接

测试代码如下

public static void main(String[] args) {
    	@SuppressWarnings("resource")
		Jedis jedis = new Jedis("10.10.12.18", 6379);
    	System.out.println(jedis.ping());
}

日志如下
/*
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.

*/

解决办法:

  • 在redis.conf文件中 ,注释掉:bind 127.0.0.1
  • 在redis.conf文件中 ,把 protected-mode = yes 更改为 protected-mode = no

重启redis后,java测试代码运行成功($ src/redis-server redis.conf , 该命令可以确保更新的配置参数可以立即生效);

补充:redis默认端口6379 , 要保证linux对外开放了该端口,外部才能访问。我的虚拟机刚好默认关闭了防火墙,所以上面操作没有涉及开关防火墙或者开放指定端口的记录。

原文地址:https://www.cnblogs.com/wzk1992/p/9121466.html