Redis 客戶端安裝與遠程鏈接圖解

Linux環境:Centos 6.8 
Redis服務端版本:3.2.6 
Redis客戶端下載連接:https://redisdesktop.com/downloadlinux

省略Linux系統安裝Redis教程,網上安裝教程不少;建議用tar.gz包安裝 
Redis官網tar.gz下載地址:wget http://download.redis.io/releases/redis-3.2.6.tar.gzredis

安裝時可能遇到的問題sql

問題1:make[3]: gcc: Command not found 
解決:Centos系統執行yum install gcc;Ubuntu系統執行apt-get install gccubuntu


問題2:zmalloc.h:50:31: error: jemalloc/jemalloc.h: No such file or directory 
zmalloc.h:55:2: error: #error 「Newer version of jemalloc required」 
make[1]: * [adlist.o] Error 1 
解決:輸入make MALLOC=libc,而後從新編譯服務器

安裝完以後,進入Redis安裝目錄tcp

[root@Karle redis-3.2.6]# ll
總用量 208
-rw-rw-r--. 1 root root 80406 12月 6 2016 00-RELEASENOTES drwxr-xr-x. 2 root root 4096 1月 4 2017 bin -rw-rw-r--. 1 root root 53 12月 6 2016 BUGS -rw-rw-r--. 1 root root 1805 12月 6 2016 CONTRIBUTING -rw-rw-r--. 1 root root 1487 12月 6 2016 COPYING drwxrwxr-x. 7 root root 4096 1月 4 2017 deps drwxr-xr-x. 2 root root 4096 1月 4 2017 etc -rw-rw-r--. 1 root root 11 12月 6 2016 INSTALL -rw-rw-r--. 1 root root 151 12月 6 2016 Makefile -rw-rw-r--. 1 root root 4223 12月 6 2016 MANIFESTO -rw-rw-r--. 1 root root 6834 12月 6 2016 README.md -rw-rw-r--. 1 root root 46696 9月 10 10:06 redis.conf (Redis配置文件) -rwxrwxr-x. 1 root root 271 12月 6 2016 runtest -rwxrwxr-x. 1 root root 280 12月 6 2016 runtest-cluster -rwxrwxr-x. 1 root root 281 12月 6 2016 runtest-sentinel -rw-rw-r--. 1 root root 7606 12月 6 2016 sentinel.conf drwxrwxr-x. 2 root root 4096 9月 10 10:08 src (執行腳本) drwxrwxr-x. 10 root root 4096 12月 6 2016 tests drwxrwxr-x. 7 root root 4096 12月 6 2016 utils

進入src目錄,執行腳本 測試

[root@Karle src]# ./redis-serverui

[root@Karle src]# ./redis-server 
2745:C 10 Sep 10:16:13.130 # Warning: no config file specified, using the default config. In order to specify a config file use ./redis-server /path/to/redis.conf
2745:M 10 Sep 10:16:13.131 * Increased maximum number of open files to 10032 (it was originally set to 1024). _._ _.-``__ ''-._ _.-`` `. `_. ''-._ Redis 3.2.6 (00000000/0) 64 bit .-`` .-```. ```\/ _.,_ ''-._ ( ' , .-` | `, ) Running in standalone mode |`-._`-...-` __...-.``-._|'` _.-'| Port: 6379 | `-._ `._ / _.-' | PID: 2745 `-._ `-._ `-./ _.-' _.-' |`-._`-._ `-.__.-' _.-'_.-'| | `-._`-._ _.-'_.-' | http://redis.io `-._ `-._`-.__.-'_.-' _.-' |`-._`-._ `-.__.-' _.-'_.-'| | `-._`-._ _.-'_.-' | `-._ `-._`-.__.-'_.-' _.-' `-._ `-.__.-' _.-' `-._ _.-' `-.__.-' 2745:M 10 Sep 10:16:13.145 # WARNING: The TCP backlog setting of 511 cannot be enforced because /proc/sys/net/core/somaxconn is set to the lower value of 128. 2745:M 10 Sep 10:16:13.145 # Server started, Redis version 3.2.6 2745:M 10 Sep 10:16:13.147 # WARNING overcommit_memory is set to 0! Background save may fail under low memory condition. To fix this issue add 'vm.overcommit_memory = 1' to /etc/sysctl.conf and then reboot or run the command 'sysctl vm.overcommit_memory=1' for this to take effect. 2745:M 10 Sep 10:16:13.147 # WARNING you have Transparent Huge Pages (THP) support enabled in your kernel. This will create latency and memory usage issues with Redis. To fix this issue run the command 'echo never > /sys/kernel/mm/transparent_hugepage/enabled' as root, and add it to your /etc/rc.local in order to retain the setting after a reboot. Redis must be restarted after THP is disabled. 2745:M 10 Sep 10:16:13.147 * DB loaded from disk: 0.000 seconds 2745:M 10 Sep 10:16:13.147 * The server is now ready to accept connections on port 6379

此時Redis服務已經能夠成功啓動了;但問題來了,界面一直停留在Redis服務窗口中,按Ctrl+C雖然能夠屏蔽服務窗口,但同時也會結束Redis服務。this

2745:M 10 Sep 10:23:04.765 # User requested shutdown… 
2745:M 10 Sep 10:23:04.765 * Saving the final RDB snapshot before exiting. 
2745:M 10 Sep 10:23:04.767 * DB saved on disk 
2745:M 10 Sep 10:23:04.767 # Redis is now ready to exit, bye bye…spa

Redis服務默認是前臺運行,須要修改成後臺運行;返回上一層目錄,修改redis.conf配置文件。找到daemonize(守護進程)配置,默認false。 
修改後:

daemonize yes

此時滿臉自信地回到src目錄執行

[root@Karle src]# ./redis-server

擦,仍是前臺運行;咋回事?什麼毛病?這是我們修改了配置文件,但沒告訴Redis讀取最新的配置文件。

啓動服務的同時讀取最新的配置文件(必須)

[root@Karle src]# ./redis-server ../redis.conf

下載RedisDesktopManager客戶端,輸入服務器IP地址,端口(缺省值:6379);點擊Test Connection按鈕測試鏈接,噢,My God,鏈接失敗!

 

什麼問題呢?緣由是Redis默認只支持本地連接,輸入進程命令查看得知(127.0.0.1:6379)

[root@Karle src]# ps -ef | grep redis 
root 5239 1 0 10:37 ? 00:00:00 ./redis-server 127.0.0.1:6379 
root 5244 2321 0 10:37 pts/0 00:00:00 grep redis

問題解決:編輯redis.conf配置文件;註釋掉61行本地連接限制以及80行配置修改成no

61 # bind 127.0.0.1 
80 protected-mode no

讀取最新配置文件並重啓,查看Redis進程狀況!我再擦,什麼鬼,都開放IP連接權限了,怎麼仍是127.0.0.1:6379!!

[root@Karle src]# ./redis-server ../redis.conf 
[root@Karle src]# ps -ef | grep redis 
root 5352 1 0 10:59 ? 00:00:00 ./redis-server 127.0.0.1:6379 
root 5367 2321 0 11:00 pts/0 00:00:00 grep redis

問題解決:先殺掉Redis進程,src目錄下依次執行

[root@Karle src]# redis-cli shutdown 
[root@Karle src]# ./redis-server ../redis.conf

再查看進程狀況:

[root@Karle src]# ps -ef | grep redis 
root 5391 1 0 11:05 ? 00:00:00 ./redis-server *:6379 
root 5395 2321 0 11:05 pts/0 00:00:00 grep redis

哇塞,*.6379,這意味着已經成功開放IP訪問權限了。萬事俱備,只欠點擊RedisDesktopManager客戶端測試連接按鈕了。好,走起。 
當你信心滿滿的時候,現實總潑你一盆冷水來清醒!連接失敗,連接失敗,我都很差意思截圖上傳了!!這又是鬧哪樣啊?還讓我活不???

冥思苦想,忽然,我想到一個詞——防火牆

[root@Karle src]# service iptables status
表格:filter
Chain INPUT (policy ACCEPT)
num  target     prot opt source               destination         
1 ACCEPT all -- 0.0.0.0/0 0.0.0.0/0 state RELATED,ESTABLISHED 2 ACCEPT icmp -- 0.0.0.0/0 0.0.0.0/0 3 ACCEPT all -- 0.0.0.0/0 0.0.0.0/0 4 ACCEPT tcp -- 0.0.0.0/0 0.0.0.0/0 state NEW tcp dpt:22 5 ACCEPT tcp -- 0.0.0.0/0 0.0.0.0/0 state NEW tcp dpt:8080 6 ACCEPT tcp -- 0.0.0.0/0 0.0.0.0/0 state NEW tcp dpt:80 7 ACCEPT tcp -- 0.0.0.0/0 0.0.0.0/0 state NEW tcp dpt:3306 8 ACCEPT tcp -- 0.0.0.0/0 0.0.0.0/0 state NEW tcp dpt:15672 9 REJECT all -- 0.0.0.0/0 0.0.0.0/0 reject-with icmp-host-prohibited

編輯Linux防火牆 
[root@Karle src]# vi /etc/sysconfig/iptables 
加入防火牆規則:-A INPUT -m state –state NEW -m tcp -p tcp –dport 6379 -j ACCEPT

[root@cthon src]# service iptables status
表格:filter
Chain INPUT (policy ACCEPT)
num  target     prot opt source               destination         
1 ACCEPT all -- 0.0.0.0/0 0.0.0.0/0 state RELATED,ESTABLISHED 2 ACCEPT icmp -- 0.0.0.0/0 0.0.0.0/0 3 ACCEPT all -- 0.0.0.0/0 0.0.0.0/0 4 ACCEPT tcp -- 0.0.0.0/0 0.0.0.0/0 state NEW tcp dpt:22 5 ACCEPT tcp -- 0.0.0.0/0 0.0.0.0/0 state NEW tcp dpt:8080 6 ACCEPT tcp -- 0.0.0.0/0 0.0.0.0/0 state NEW tcp dpt:80 7 ACCEPT tcp -- 0.0.0.0/0 0.0.0.0/0 state NEW tcp dpt:3306 8 ACCEPT tcp -- 0.0.0.0/0 0.0.0.0/0 state NEW tcp dpt:15672 9 ACCEPT tcp -- 0.0.0.0/0 0.0.0.0/0 state NEW tcp dpt:6379 10 REJECT all -- 0.0.0.0/0 0.0.0.0/0 reject-with icmp-host-prohibited

重啓Linux防火牆 
[root@Karle src]# service iptables restart

點擊Test Connection按鈕測試鏈接,鏈接成功,大功告成

鏈接成功

 

補充:在ubuntu等版本的linux系統下,不妨將redis.cconf文件中 的bind改成0.0.0.0

相關文章
相關標籤/搜索