1. 程式人生 > >CentOS 7下安裝redis 4.0.6

CentOS 7下安裝redis 4.0.6

安裝redis

第一步:下載redis安裝包

wget http://download.redis.io/releases/redis-4.0.6.tar.gz

第二步:解壓壓縮包

tar -zxvf redis-4.0.6.tar.gz

第三步:yum安裝gcc依賴

yum install gcc

遇到選擇,輸入y即可

第四步:跳轉到redis解壓目錄下

cd redis-4.0.6

第五步:編譯

make

安裝,官網並沒有這個操作,執行這個的目的就是將可執行檔案拷貝到/usr/local/bin/目錄下,這樣就可以直接執行redis-server 、redis-cli 等命令了。

make install

安裝完成,檢視版本

[[email protected] redis-4.0.6]# redis-server -v
Redis server v=4.0.6 sha=00000000:0 malloc=jemalloc-4.0.3 bits=64 build=4f1a60f0053726b7

啟動 redis-server

[[email protected] redis-4.0.6]# redis-server 
9961:C 12 Dec 05:14:58.101 # oO0OoO0OoO0Oo Redis is starting oO0OoO0OoO0Oo
9961:C 12 Dec 05:14:58.101 # Redis version=4.0.6, bits=64, commit=00000000, modified=0, pid=9961, just started
9961:C 12 Dec 05:14:58.101 # Warning: no config file specified, using the default config. In order to specify a config file use redis-server /path/to/redis.conf
                _._                                                  
           _.-``__ ''-._                                             
      _.-``    `.  `_.  ''-._           Redis 4.0.6 (00000000/0) 64 bit
  .-`` .-```.  ```\/    _.,_ ''-._                                   
 (    '      ,       .-`  | `,    )     Running in standalone mode
 |`-._`-...-` __...-.``-._|'` _.-'|     Port: 6379
 |    `-._   `._    /     _.-'    |     PID: 9961
  `-._    `-._  `-./  _.-'    _.-'                                   
 |`-._`-._    `-.__.-'    _.-'_.-'|                                  
 |    `-._`-._        _.-'_.-'    |           http://redis.io        
  `-._    `-._`-.__.-'_.-'    _.-'                                   
 |`-._`-._    `-.__.-'    _.-'_.-'|                                  
 |    `-._`-._        _.-'_.-'    |                                  
  `-._    `-._`-.__.-'_.-'    _.-'                                   
      `-._    `-.__.-'    _.-'                                       
          `-._        _.-'                                           
              `-.__.-'                                               

9961:M 12 Dec 05:14:58.105 # WARNING: The TCP backlog setting of 511 cannot be enforced because /proc/sys/net/core/somaxconn is set to the lower value of 128.
9961:M 12 Dec 05:14:58.105 # Server initialized
9961:M 12 Dec 05:14:58.105 # 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.
9961:M 12 Dec 05:14:58.105 # 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.
9961:M 12 Dec 05:14:58.105 * Ready to accept connections

看到以上頁面就啟動成功啦,我們新開一個終端去連線redis。

[[email protected] ~]# redis-cli
127.0.0.1:6379> set name zhangsan
OK
127.0.0.1:6379> get name
"zhangsan"
127.0.0.1:6379> keys *
1) "name"
127.0.0.1:6379> 

可有些童鞋就疑惑了,為什麼執行redis-server後,不能退出後臺執行呢,如果關閉終端,不就連redis一起關閉了嗎?
請繼續往下看……

後臺執行

拷貝redis.conf配置檔案到/etc/redis

在剛解壓的redis根目錄執行:

[[email protected] redis-4.0.6]# mkdir /etc/redis
[[email protected] redis-4.0.6]# cp redis.conf /etc/redis/

編輯配置檔案

[[email protected] redis-4.0.6]# vim /etc/redis/6379.conf

找到daemonize no 改為daemonize yes

# By default Redis does not run as a daemon. Use 'yes' if you need it.
# Note that Redis will write a pid file in /var/run/redis.pid when daemonized.
daemonize yes

指定配置檔案啟動redis

[[email protected] redis-4.0.6]# redis-server  /etc/redis/6379.conf 
9999:C 12 Dec 05:31:11.872 # oO0OoO0OoO0Oo Redis is starting oO0OoO0OoO0Oo
9999:C 12 Dec 05:31:11.872 # Redis version=4.0.6, bits=64, commit=00000000, modified=0, pid=9999, just started
9999:C 12 Dec 05:31:11.872 # Configuration loaded
[[email protected] redis-4.0.6]# 

是不是可以後臺啟動了呢?

開機啟動

1、將redis的啟動指令碼複製一份放到/etc/init.d目錄下

[[email protected] redis-4.0.6]#  cp utils/redis_init_script /etc/init.d/redisd

2、設定redis開機自啟動

先切換到/etc/init.d目錄下

然後執行自啟命令

[[email protected] redis-4.0.6]# chkconfig redisd on
service redisd does not support chkconfig 

看結果是redisd不支援chkconfig

解決方法:

使用vim編輯redisd檔案,在第一行加入如下兩行註釋,儲存退出

# chkconfig:   2345 90 10
# description:  Redis is a persistent key-value database

註釋的意思是,redis服務必須在執行級2,3,4,5下被啟動或關閉,啟動的優先順序是90,關閉的優先順序是10。

再次執行開機自啟命令,成功

[[email protected] redis-4.0.6]# chkconfig redisd on

現在可以直接已服務的形式啟動和關閉redis了

啟動:

service redisd start 

[[email protected] redis-4.0.6]# service redisd start
Starting Redis server...
2288:C 13 Dec 13:51:38.087 # oO0OoO0OoO0Oo Redis is starting oO0OoO0OoO0Oo
2288:C 13 Dec 13:51:38.087 # Redis version=4.0.6, bits=64, commit=00000000, modified=0, pid=2288, just started
2288:C 13 Dec 13:51:38.087 # Configuration loaded

關閉:

service redisd stop

[[email protected] redis-4.0.6]# service redisd stop
Stopping ...
Redis stopped

關於bind

翻看網上的文章,此處多翻譯為“指定redis只接收來自於該IP地址的請求,如果不進行設定,那麼將處理所有請求,在生產環境中最好設定該項”。這種解釋會totally搞糊塗初學者,甚至是錯誤的。該處的英文原文為

# If you want you can bind a single interface, if the bind option is not 
# specified all the interfaces will listen for incoming connections. 
# bind 127.0.0.1

該處說明bind的是interface,也就是說是網路介面。伺服器可以有一個網路介面(通俗的說網絡卡),或者多個。打個比方說機器上有兩個網絡卡,分別為192.168.1.5 和192.168.1.6,如果bind 192.168.1.5,那麼只有該網絡卡地址接受外部請求,如果不繫結,則兩個網絡卡口都接受請求。

OK,不知道講清楚沒有,在舉一個例子。在我上面的實驗過程中,我是將bind項註釋掉了,實際上我還有一種解決方案。由於我redis伺服器的地址是 192.168.1.111 。如果我不註釋bind項,還有什麼辦法呢?我可以做如下配置:

# bind 192.168.1.111

這裡很多人會誤以為繫結的ip應該是請求來源的ip。其實不然,這裡應該繫結的是你redis伺服器本身接受請求的ip。