一、系統環境
1.1、服務器環境
序號 | 節點名稱(hostname) | 服務器版本 | Linux版本 | IP地址 |
---|---|---|---|---|
1 | node1 | CentOS Linux release 7.3.1611 (Core) | Linux version 3.10.0-514.el7.x86_64 | 192.168.1.51 |
2 | node2 | CentOS Linux release 7.3.1611 (Core) | Linux version 3.10.0-514.el7.x86_64 | 192.168.1.243 |
3 | node3 | CentOS Linux release 7.3.1611 (Core) | Linux version 3.10.0-514.el7.x86_64 | 192.168.1.252 |
這裏準備了三臺機器,方便單機的時候搭建和搭建集羣,這裏使用的SSH界面工具是Secure CRT。
1.2、其他軟件版本和下載地址
序號 | 軟件 | 版本 | 下載地址 |
---|---|---|---|
1 | Redis | 5.0.4 | http://download.redis.io/releases/redis-5.0.4.tar.gz |
2 | Secure CRT | scrt-x64.8.5.3.1867 | https://www.vandyke.com/cgi-bin/releases.php?product=securecrt |
2 | WinSCP | WinSCP-5.15 | https://winscp.net/eng/download.php |
1.3、修改主機hostname
因爲這個是已經事先做好的了,爲了告訴大家如何實現的,命令如下:
[xuliugen@node1 ~]$ hostnamectl set-hostname node1
[xuliugen@node1 ~]$ hostname
node1
分別把三臺機器都設置了。
1.4、修改hosts
使用命令進行修改:
vim /etc/hosts
添加hosts內容爲:
192.168.1.51 node1
192.168.1.243 node2
192.168.1.252 node3
分別修改三臺機器的hosts
1.5、關閉防火牆
下述的操作過程中,始終是關閉了防火牆的,關閉的命令如下:
centos 7:
systemctl stop firewalld.service #停止
systemctl disable firewalld.service #禁用
centos 7之前的版本:
service iptables stop #停止
chkconfig iptables off #禁用
如果只是想開啓某一個端口,例如:6379的話,可以搜索一下具體的配置過程,這裏不再累述。
二、Redis單機部署
注意:單機部署的時候,只使用服務器node1,即IP:192.168.1.51
2.1、安裝Redis
將Redis安裝包下載到/home/xuliugen/software
目錄下,依次執行命令:
$ wget http://download.redis.io/releases/redis-5.0.4.tar.gz
$ tar xzf redis-5.0.4.tar.gz
$ cd redis-5.0.4
$ make
2.1.1、問題1:
如果你的安裝過程比較順利的話,應該是可以直接成功的,我的安裝出現如下錯誤:
[root@node1 redis-5.0.4]# make
cd src && make all
make[1]: Entering directory `/home/xuliugen/software/redis-5.0.4/src'
CC adlist.o
/bin/sh: cc: command not found
make[1]: *** [adlist.o] Error 127
make[1]: Leaving directory `/home/xuliugen/software/redis-5.0.4/src'
make: *** [all] Error 2
[root@node1 redis-5.0.4]#
經搜索得知是因爲沒有安裝gcc
,使用命令安裝gcc:yum install gcc
一路回車即可!
然後再次執行make
,如果出現如下錯誤的話:
2.1.2、問題2
[root@node1 redis-5.0.4]# make
cd src && make all
make[1]: Entering directory `/home/xuliugen/software/redis-5.0.4/src'
CC adlist.o
In file included from adlist.c:34:0:
zmalloc.h:50:31: fatal error: jemalloc/jemalloc.h: No such file or directory
#include <jemalloc/jemalloc.h>
^
compilation terminated.
make[1]: *** [adlist.o] Error 1
make[1]: Leaving directory `/home/xuliugen/software/redis-5.0.4/src'
make: *** [all] Error 2
[root@node1 redis-5.0.4]#
原因是jemalloc重載了Linux下的ANSI C的malloc和free函數。解決辦法:make時添加參數。
make MALLOC=libc
發現運行OK了!
如果大家還遇到了其他問題,直接把錯誤的信息扔到百度進行搜索,基本都是可以解決的哈!
2.2、啓動Redis
至此Redis已經安裝完成,首先試一下能不能把啓動:
啓動命令(在/home/xuliugen/software/redis-5.0.4目錄下執行):
[root@node1 src]# pwd
/home/xuliugen/software/redis-5.0.4/src
[root@node1 src]# ./redis-server ../redis.conf
2.3、連接Redis
三、Redis可視化圖形工具介紹
目前市面上有很多關於Redis的可視化操作界面,如:Redis Desktop Manager、等,
3.1、Redis Desktop Manager
官網下載:https://redisdesktop.com/download
github地址:https://github.com/uglide/RedisDesktopManager/releases
不過Redis Desktop Manager目前只有Linux版本的是免費的,Windows和Mac的安裝包都是收費的,或者可以提一個Pull Request然後可以免費試用一年。
安裝和使用比較簡單,這裏不在贅述!
另外,Window和Mac平臺都支持通過編譯安裝的方式使用,有興趣的可以瞭解一下:
http://docs.redisdesktop.com/en/latest/install/
3.2、Redis Client
GitHub地址:https://github.com/caoxinyu/RedisClient
下載地址:https://github.com/caoxinyu/RedisClient/releases
下載完之後,直接安裝即可!
3.3、其他客戶端可視化工具
- FastoRedis,https://fastoredis.com
- PyRedisAdmin
- Redis Studio
- Redis Live
- TreeNMS for Redis, memcached,http://www.treesoft.cn/dms.html
四、使用Redis Client連接Redis
4.1、設置Redis外網可訪問
值得注意的是在3.2.0以後的新版本中引入了一種proteced mode 模式,詳見:http://redis.io/topics/security
在不修改配置文件任何內容的情況下,有以下幾個默認的配置,簡單的就是:
bind 127.0.0.1
protected-mode yes
# requirepass foobared
默認綁定的是127.0.01,默認開啓了:protected-mode模式,按照官方的說法,如果默認開啓了protected-mode模式在沒有配置綁定IP和密碼的情況下,是隻允許迴環地址進行訪問的,就只允許127.0.0.1進行訪問,那我們就在默認的配置下進行啓動,通過SSH工具在其他機器上進行訪問,看看運行的效果:
很顯然是沒有辦法訪問到,在3.2.0以前的版本中可以將綁定的IP進行修改爲本機IP,例如我運行Redis的服務器IP爲192.168.1.51,那我的配置爲0.0.0.0,順便指定Redis的密碼,# requirepass foobared
將這一行去掉註釋,選擇自己喜歡的密碼,重啓服務即可正常訪問。
通過外網訪問Redis可能會遇到這個問題,Redis protected-mode 是3.2 之後加入的新特性,在redis.conf的註釋中,我們可以瞭解到,他的具體作用和啓用條件:
# Protected mode is a layer of security protection, in order to avoid that
# Redis instances left open on the internet are accessed and exploited.
#
# When protected mode is on and if:
#
# 1) The server is not binding explicitly to a set of addresses using the
# "bind" directive.
# 2) No password is configured.
#
# The server only accepts connections from clients connecting from the
# IPv4 and IPv6 loopback addresses 127.0.0.1 and ::1, and from Unix domain
# sockets.
#
# 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 yes
可以看到 protected-mode 是爲了禁止公網訪問redis cache,加強redis安全的。它啓用的條件,有兩個:
1) 沒有bind IP ;
2) 沒有設置訪問密碼;
如果啓用了,則只能夠通過lookback ip(127.0.0.1)訪問Redis cache,如果從外網訪問,則會返回相應的錯誤信息,就是上圖中的信息。
因此在新的版本中,應該配置綁定IP和訪問密碼,這樣的話纔不會報錯誤,在Redis的一個論壇中,老外也探討了這個問題,可以參考:https://www.reddit.com/r/redis/comments/3zv85m/new_security_feature_redis_protected_mode/
最終修改配置如下:
bind 192.168.1.51
protected-mode yes
requirepass redis_pass_123456
然後鏈接之後,就可以查看了!
五、其他常見問題
5.1、啓動的時候沒有設置配置文件
這個版本的時候需要指定,如果不指定的話,在後期修改了配置文件不會起到對應的效果
11292:C 25 Jul 13:13:58.034 # Warning: no config file specified, using
the default config. In order to specify a config file use
./redis-server /path/to/redis.conf
這個說的是在啓動的時候要制定配置文件,如果沒有指定的話就會按照默認的配置,因此我們要制定具體的位置。
5.2、啓動時報錯及解決方法
1、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.
2、WARNING: The TCP backlog setting of 511 cannot be enforced because
/proc/sys/net/core/somaxconn is set to the lower value of 128.
解決方法其實按照上邊的說明就可以解決:
5.2.1、第一個警告兩個方式解決(overcommit_memory)
echo "vm.overcommit_memory=1" > /etc/sysctl.conf 或 vi /etcsysctl.conf
然後reboot重啓機器,重啓之後執行下邊的內容
echo 1 > /proc/sys/vm/overcommit_memory 不需要啓機器就生效
5.2.2、第二個警告解決
echo 511 > /proc/sys/net/core/somaxconn
其實在報錯信息的時候已經給出瞭解決的方法,按照給定的具體的方法解決即可。
5.2.3、在上述 2 中的解決方法的一些參數說明
(1)overcommit_memory參數說明:
設置內存分配策略(可選,根據服務器的實際情況進行設置)
/proc/sys/vm/overcommit_memory
可選值:0、1、2。
0, 表示內核將檢查是否有足夠的可用內存供應用進程使用;如果有足夠的可用內存,內存申請允許;否則,內存申請失敗,並把錯誤返回給應用進程。
1, 表示內核允許分配所有的物理內存,而不管當前的內存狀態如何。
2, 表示內核允許分配超過所有物理內存和交換空間總和的內存
注意:redis在dump數據的時候,會fork出一個子進程,理論上child進程所佔用的內存和parent是一樣的,比如parent佔用 的內存爲8G,這個時候也要同樣分配8G的內存給child,如果內存無法負擔,往往會造成redis服務器的down機或者IO負載過高,效率下降。所 以這裏比較優化的內存分配策略應該設置爲 1(表示內核允許分配所有的物理內存,而不管當前的內存狀態如何)。
(2)這裏又涉及到Overcommit和OOM。
什麼是Overcommit和OOM,在Unix中,當一個用戶進程使用malloc()函數申請內存時,假如返回值是NULL,則這個進程知道當前沒有可用內存空間,就會做相應的處理工作。許多進程會打印錯誤信息並退出。
Linux使用另外一種處理方式,它對大部分申請內存的請求都回復”yes”,以便能跑更多更大的程序。因爲申請內存後,並不會馬上使用內存。這種技術叫做Overcommit。
當內存不足時,會發生OOM killer(OOM=out-of-memory)。它會選擇殺死一些進程(用戶態進程,不是內核線程),以便釋放內存。
(3)Overcommit的策略
Linux下overcommit有三種策略(Documentation/vm/overcommit-accounting):
啓發式策略。合理的overcommit會被接受,不合理的overcommit會被拒絕。
任何overcommit都會被接受。
當系統分配的內存超過swap+N%*物理RAM(N%由vm.overcommit_ratio決定)時,會拒絕commit。
overcommit的策略通過vm.overcommit_memory設置。
overcommit的百分比由vm.overcommit_ratio設置。
echo 2 > /proc/sys/vm/overcommit_memory
echo 80 > /proc/sys/vm/overcommit_ratio
當oom-killer發生時,linux會選擇殺死哪些進程選擇進程的函數是oom_badness函數(在mm/oom_kill.c中),該函數會計算每個進程的點數(0~1000)。點數越高,這個進程越有可能被殺死。每個進程的點數跟oom_score_adj有關,而且oom_score_adj可以被設置(-1000最低,1000最高)。