天天看點

Redis的安裝與簡單部署

截止到本文完成這一天,redis的最穩定版本是3.2.1,其下載下傳位址:http://download.redis.io/releases/redis-3.2.1.tar.gz。直接wget一下就好了。

Redisの安裝

在目前目錄下#tar -zxvf redis-3.2.1.tar.gz之後,來到redis-3.2.1檔案夾裡,#make && make test,之後會出來一大片東西,會提示“\o/ All tests passed without errors!”,但是無法像其他軟體那樣去#make install,即使#make install還是提示hint:it's a good idea to "make test"。

進入redis檔案封包件夾把redis.conf拷貝到/usr/local/redis/etc檔案夾裡,如果沒有此檔案夾請建立。再傳回redis安裝封包件夾的/src檔案夾裡,把裡面所有的綠色腳本檔案都mv到/usr/local/redis/bin裡,來到/usr/local/redis/bin,#./redis-server,啟動伺服器程式。

這時,會有如下提示:

19183:C 29 Jun 09:35:07.086 # Warning: no config file specified, using the default config. In order to specify a config file use ./redis-server /path/to/redis.conf

意思是說沒有給redis-server一個配置檔案,需要制定一個配置檔案,于是乎,把指令更改成#./redis-server /usr/local/redis/etc/redis.conf。就會出現這樣的圖檔。

<a href="http://s2.51cto.com/wyfs02/M01/83/6A/wKioL1dzKb-Sa34yAAD11Fa7eoM743.png-wh_500x0-wm_3-wmp_4-s_4047405508.png" target="_blank"></a>

這裡面的英文我拷貝出來,然後加以翻譯。

18988:M 29 Jun 09:38:52.458 # WARNING: The TCP backlog setting of 511 cannot be enforced because /proc/sys/net/core/somaxconn is set to the lower value of 128.

警告:TCP backlog的值設定是511,這是無法啟動的,因為/proc/sys/net/core/somaxconn的設定值是128,比你的511要低。

【解決方案】echo 511 &gt; /proc/sys/net/core/somaxconn

18988:M 29 Jun 09:38:52.458 # 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.

警告:overcommit_memory這個值被設定為0!這樣的低記憶體條件背景可能會失敗,要解決這個問題,在/etc/sysctl.conf裡增加一句話'vm.overcommit_memory = 1'然後重新啟動伺服器,如果你不想重新啟動伺服器,那麼就輸入'sysctl vm.overcommit_memory=1'解決。

18988:M 29 Jun 09:38:52.458 # 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 &gt; /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.

警告:你的核心裡有THP支援功能,這個可能會和redis建立一些延遲和記憶體,要解決這個問題請以root的身份運作指令:“echo never &gt; /sys/kernel/mm/transparent_hugepage/enabled”,而且把這句話添加到/etc/rc.local裡,這樣在伺服器重新開機之後就會關上這個設定,在THP禁用的時候,redis必須被重新開機。

18988:M 29 Jun 09:38:52.458 * The server is now ready to accept connections on port 6379

把上面三個problem都解決了之後,再一次#./redis-server /usr/local/redis/etc/redis.conf,螢幕變成這樣:

<a href="http://s4.51cto.com/wyfs02/M01/83/6B/wKiom1dzL4uhrsdVAAB6zR7x4h0944.png-wh_500x0-wm_3-wmp_4-s_4261016597.png" target="_blank"></a>

提示:伺服器已經準備好從6379号端口接收連接配接了,意味着server端已經啟動成功。如果你不喜歡6379号端口,而是喜歡4444号端口,那麼就要把/usr/local/redis/etc下的redis.conf裡的端口由6379改成4444,然後#./redis-server /usr/local/redis/etc/redis.conf。

轉背景啟動

但是這樣的啟動是在前台啟動,螢幕被霸占了,其他工作都做不了了,比較煩。我們希望redis在背景啟動就好,怎麼辦呢?

#vim /usr/local/redis/etc/redis.conf。找到daemonize,把原來的no改成yes。這意味着redis是背景啟動了。

這時候再一次的以redis.conf啟動redis.server。就會發現螢幕沒有什麼變化和提示,但是使用#pstree -p|grep redis檢視一下程序就知道redis已經在背景啟動了,如圖:

<a href="http://s1.51cto.com/wyfs02/M02/83/6D/wKiom1dzNmbj4r3fAABiRML_-io784.png-wh_500x0-wm_3-wmp_4-s_2221946858.png" target="_blank"></a>

用#ps -ef的效果也在上面那張圖裡,至此整個redis的安裝和啟動過程結束。

在/usr/local/redis/bin裡有一個檔案叫redis-cli,cli是client登入的簡寫,也就是說這個是用戶端登陸的啟動檔案。

<a href="http://s2.51cto.com/wyfs02/M00/83/6C/wKioL1dzOOmRi9BQAABgSumxYqc579.png-wh_500x0-wm_3-wmp_4-s_1884263705.png" target="_blank"></a>

可見redis也是那種key-value模式的資料庫,在上面的例子裡name是key,value是Chris,name是ID,value是33664,但是一旦再一次将name指派成了99f90,以前的33664就會消失。

而且即使退出用戶端連接配接,再一次進入的話,原有的key和value也不會消失。而如果事前沒有啟動redis server,那麼redis-cli是無法連接配接的,如圖:

<a href="http://s3.51cto.com/wyfs02/M00/83/6D/wKiom1dzPATyqjdpAABLynVFZHw614.png-wh_500x0-wm_3-wmp_4-s_1132621752.png" target="_blank"></a>

Redisの退出

至于關閉redis,方法就效仿其他軟體,要麼#pkill redis-server,要麼就是#/usr/local/redis/bin/redis-cli shutdown。

 本文轉自 蘇幕遮618 51CTO部落格,原文連結:http://blog.51cto.com/chenx1242/1793895

繼續閱讀