http://www.linuxidc.com/Linux/2016-09/135071.htmlinux
環境:CentOS 7.0 Redis 3.2.1redis
這裏我把Redis放在/home/linuxidc/software/下,因此在該目錄下執行下列命令:ruby
$ wget http://download.redis.io/releases/redis-3.2.1.tar.gz $ tar xzf redis-3.2.1.tar.gz $ cd redis-3.2.1 $ make
至此Redis已經安裝完成,首先試一下能不能把啓動:服務器
啓動命令(在/home/linuxidc/software/redis-3.2.1目錄下執行):ide
[root@localhost redis-3.2.1]# ./src/redis-server ../redis.conf
以下:函數
根據上圖中的警告信息,下邊是具體的解決方法工具
一、啓動的時候沒有設置配置文件優化
這個版本的時候須要指定,若是不指定的話,在後期修改了配置文件不會起到對應的效果ui
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
這個說的是在啓動的時候要制定配置文件,若是沒有指定的話就會按照默認的配置,所以咱們要制定具體的位置,具體命令爲:this
[root@localhost src]# ./redis-server ../redis.conf
二、啓動時報錯及解決方法
一、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. 二、WARNING: The TCP backlog setting of 511 cannot be enforced because /proc/sys/net/core/somaxconn is set to the lower value of 128.
解決方法其實按照上邊的說明就能夠解決
第一個警告兩個方式解決(overcommit_memory)
echo "vm.overcommit_memory=1" > /etc/sysctl.conf 或 vi /etcsysctl.conf
而後reboot重啓機器,重啓以後執行下邊的內容
echo 1 > /proc/sys/vm/overcommit_memory 不須要啓機器就生效
第二個警告解決
echo 511 > /proc/sys/net/core/somaxconn
其實在報錯信息的時候已經給出瞭解決的方法,按照給定的具體的方法解決便可。
三、在上述 2 中的解決方法的一些參數說明
(1)overcommit_memory參數說明:
設置內存分配策略(可選,根據服務器的實際狀況進行設置)
/proc/sys/vm/overcommit_memory
可選值:0、一、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的策略經過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最高)。
值得注意的是在3.2.0之後的新版本中引入了一種proteced mode
模式,詳見:http://redis.io/topics/security
在不修改配置文件任何內容的狀況下,有如下幾個默認的配置:
# By default, if no "bind" configuration directive is specified, Redis listens # for connections from all the network interfaces available on the server. # It is possible to listen to just one or multiple selected interfaces using # the "bind" configuration directive, followed by one or more IP addresses. # # Examples: # # bind 192.168.1.100 10.0.0.1 # bind 127.0.0.1 ::1 # # ~~~ WARNING ~~~ If the computer running Redis is directly exposed to the # internet, binding to all the interfaces is dangerous and will expose the # instance to everybody on the internet. So by default we uncomment the # following bind directive, that will force Redis to listen only into # the IPv4 lookback interface address (this means Redis will be able to # accept connections only from clients running into the same computer it # is running). # # IF YOU ARE SURE YOU WANT YOUR INSTANCE TO LISTEN TO ALL THE INTERFACES # JUST COMMENT THE FOLLOWING LINE. # ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ bind 127.0.0.1 # 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 # Require clients to issue AUTH <PASSWORD> before processing any other # commands. This might be useful in environments in which you do not trust # others with access to the host running redis-server. # # This should stay commented out for backward compatibility and because most # people do not need auth (e.g. they run their own servers). # # Warning: since Redis is pretty fast an outside user can try up to # 150k passwords per second against a good box. This means that you should # use a very strong password otherwise it will be very easy to break. # # requirepass foobared
簡單的就是:
bind 127.0.0.1 protected-mode yes # requirepass foobared
默認綁定的是127.0.01,默認開啓了:protected-mode模式,按照官方的說法,若是默認開啓了protected-mode模式在沒有配置綁定IP和密碼的狀況下,是隻容許迴環地址進行訪問的,就只容許127.0.0.1進行訪問,那咱們就在默認的配置下進行啓動,經過SSH工具在其餘機器上進行訪問,看看運行的效果:
[root@localhost redis-3.2.1]# ./src/redis-server ../redis.conf