本地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:\Users\hdb>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對外開放了該端口,外部才能訪問。我的虛擬機剛好默認關閉了防火牆,所以上面操作沒有涉及開關防火牆或者開放指定端口的記錄。


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM