Redis安裝部署與維護詳解

Redis安裝部署與維護詳解

轉自:http://www.open-open.com/lib/view/open1426468117367.html

Redis在linux上的安裝

步驟1:

首先從官網下在redis正式版的壓縮包redis-2.8.19.tar.gz

http://download.redis.io/releases/redis-2.8.19.tar.gz

步驟2:編譯源程序:

tar zxvf redis-2.8.19.tar.gz

?
1
[neil@neilhost Downloads]$ tar zxvf redis-2.8.19.tar.gz
?
1
[neil@neilhost Downloads]$ cd redis-2.8.19/src

MongoDB解壓之後就可以使用了,但Redis需要編譯,但是沒有配置。

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
[neil@neilhost src]$ make
.......此處是大量編譯過程,省略。可能有一些警告,不去官它們.............
    CC setproctitle.o
    CC hyperloglog.o
    CC latency.o
    CC sparkline.o
    LINK redis-server
    INSTALL redis-sentinel
    CC redis-cli.o
    LINK redis-cli
    CC redis-benchmark.o
    LINK redis-benchmark
    CC redis-check-dump.o
    LINK redis-check-dump
    CC redis-check-aof.o
    LINK redis-check-aof
 
Hint: It's a good idea to run 'make test' ;)

進入src進行安裝

?
1
2
3
4
5
6
7
8
9
10
11
[neil@neilhost src]$ sudo make install
[sudo] password for neil: 
 
 
Hint: It's a good idea to run 'make test' ;)
 
    INSTALL install
    INSTALL install
    INSTALL install
    INSTALL install
    INSTALL install

這時候,我們可以看看src下的文件:

?
1
[neil@neilhost src]$ ll

Redis安裝部署與維護詳解

可以看到此時,src文件夾下出現了一些綠色的文件,這些文件就是我們以後需要用到的命令文件。

步驟3:

移動文件,便於管理:(所有源代碼安裝的軟件都安裝在/usr/local下,如apache等)

創建兩個文件夾,bin用於存放命令,etc擁有存放配置文件。

?
1
2
[neil@neilhost src]$ sudo mkdir -p /usr/local/redis/bin
[neil@neilhost src]$ sudo mkdir -p /usr/local/redis/etc

-p是遞歸創建。

接下來,將redis-2.8.19文件夾下的redis.conf複製到/usr/local/redis/etc/

並將src目錄下的7個命令文件(綠色的),移動到/usr/local/redis/bin/

?
1
2
3
4
5
6
7
8
9
[neil@neilhost src]$ cd ..
[neil@neilhost redis-2.8.19]$ ls
00-RELEASENOTES  COPYING  Makefile   redis.conf        sentinel.conf  utils
BUGS             deps     MANIFESTO  runtest           src
CONTRIBUTING     INSTALL  README     runtest-sentinel  tests
[neil@neilhost redis-2.8.19]$ sudo mv ./redis.conf /usr/local/redis/etc/
[sudo] password for neil: 
[neil@neilhost redis-2.8.19]$ cd src
[neil@neilhost src]$ sudo mv mkreleasehdr.sh redis-benchmark redis-check-aof redis-check-dump redis-cli redis-sentinel redis-server /usr/local/redis/bin/

步驟4:啓動Redis服務:

首先進入剛纔安裝redis的目錄:

?
1
2
3
4
[neil@neilhost src]$ cd /usr/local/redis/bin
[neil@neilhost bin]$ ls
mkreleasehdr.sh  redis-check-aof   redis-cli       redis-server
redis-benchmark  redis-check-dump  redis-sentinel

之後我們啓動redis服務。啓動redis服務需要用到命令redis-server

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
[neil@neilhost bin]$ sudo ./redis-server
[sudo] password for neil: 
[13019] 15 Mar 22:34:24.568 # Warning: no config file specified, using the default config. In order to specify a config file use ./redis-server /path/to/redis.conf
[13019] 15 Mar 22:34:24.569 * Increased maximum number of open files to 10032 (it was originally set to 1024).
                _._                                                  
           _.-``__ ''-._                                             
      _.-``    `.  `_.  ''-._           Redis 2.8.19 (00000000/0) 64 bit
  .-`` .-```.  ```\/    _.,_ ''-._                                   
 (    '      ,       .-`  | `,    )     Running in stand alone mode
 |`-._`-...-` __...-.``-._|'` _.-'|     Port: 6379
 |    `-._   `._    /     _.-'    |     PID: 13019
  `-._    `-._  `-./  _.-'    _.-'                                   
 |`-._`-._    `-.__.-'    _.-'_.-'|                                  
 |    `-._`-._        _.-'_.-'    |           http://redis.io        
  `-._    `-._`-.__.-'_.-'    _.-'                                   
 |`-._`-._    `-.__.-'    _.-'_.-'|                                  
 |    `-._`-._        _.-'_.-'    |                                  
  `-._    `-._`-.__.-'_.-'    _.-'                                   
      `-._    `-.__.-'    _.-'                                       
          `-._        _.-'                                           
              `-.__.-'                                               
 
[13019] 15 Mar 22:34:24.570 # Server started, Redis version 2.8.19
[13019] 15 Mar 22:34:24.570 # 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.
[13019] 15 Mar 22:34:24.570 # 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.
[13019] 15 Mar 22:34:24.570 # WARNING: The TCP backlog setting of 511 cannot be enforced because /proc/sys/net/core/somaxconn is set to the lower value of 128.
[13019] 15 Mar 22:34:24.570 * The server is now ready to accept connections on port 6379

但是,這樣做的話,我們並沒有使用etc的下的配置文件進行啓動。

如果希望通過指定的配置文件啓動,需要在啓動時指定配置文件:

這裏我們先用ctrl+C來終止服務,然後查看redis服務是否終止乾淨了,之後通過設置配置文件來啓動服務:

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
^C[13019 | signal handler] (1426430158) Received SIGINT scheduling shutdown...
[13019] 15 Mar 22:35:58.502 # User requested shutdown...
[13019] 15 Mar 22:35:58.502 * Saving the final RDB snapshot before exiting.
[13019] 15 Mar 22:35:58.683 * DB saved on disk
[13019] 15 Mar 22:35:58.683 # Redis is now ready to exit, bye bye...
[neil@neilhost bin]$ pstree -p | grep redis
[neil@neilhost bin]$ sudo ./redis-server /usr/local/redis/etc/redis.conf
[13054] 15 Mar 22:36:47.951 * Increased maximum number of open files to 10032 (it was originally set to 1024).
                _._                                                  
           _.-``__ ''-._                                             
      _.-``    `.  `_.  ''-._           Redis 2.8.19 (00000000/0) 64 bit
  .-`` .-```.  ```\/    _.,_ ''-._                                   
 (    '      ,       .-`  | `,    )     Running in stand alone mode
 |`-._`-...-` __...-.``-._|'` _.-'|     Port: 6379
 |    `-._   `._    /     _.-'    |     PID: 13054
  `-._    `-._  `-./  _.-'    _.-'                                   
 |`-._`-._    `-.__.-'    _.-'_.-'|                                  
 |    `-._`-._        _.-'_.-'    |           http://redis.io        
  `-._    `-._`-.__.-'_.-'    _.-'                                   
 |`-._`-._    `-.__.-'    _.-'_.-'|                                  
 |    `-._`-._        _.-'_.-'    |                                  
  `-._    `-._`-.__.-'_.-'    _.-'                                   
      `-._    `-.__.-'    _.-'                                       
          `-._        _.-'                                           
              `-.__.-'                                               
 
[13054] 15 Mar 22:36:47.952 # Server started, Redis version 2.8.19
[13054] 15 Mar 22:36:47.952 # 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.
[13054] 15 Mar 22:36:47.952 # 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.
[13054] 15 Mar 22:36:47.952 # WARNING: The TCP backlog setting of 511 cannot be enforced because /proc/sys/net/core/somaxconn is set to the lower value of 128.
[13054] 15 Mar 22:36:47.952 * DB loaded from disk: 0.000 seconds
[13054] 15 Mar 22:36:47.952 * The server is now ready to accept connections on port 6379

但是,現在redis仍然是在前臺運行。

如果要後臺啓動該怎麼辦呢?

回想一下,後臺啓動mysql的方法是在後面加一個 &符號。如:

?
1
#mysql_safe --user=mysql &

後臺啓動mongodb,則在後面加上一個 --fork。具體方法請看我之前的MongDB入門系列(2)。

但是,這兩種方法都不適用於redis。redis的後臺啓動並運行需要通過配置文件中的參數設置。

Redis的配置文件中有哪些配置呢?

daemonize 如果需要在後臺運行,把該項改爲yes

pidfile 配置多個pid的地址 默認在/var/run/redis.pid

bind 綁定ip,設置後只接受來自該ip的請求

port 監聽端口,默認是6379

loglevel 分爲4個等級:debug verbose notice warning

logfile 用於配置log文件地址

databases 設置數據庫個數,默認使用的數據庫爲0

save 設置redis進行數據庫鏡像的頻率。

rdbcompression 在進行鏡像備份時,是否進行壓縮

dbfilename 鏡像備份文件的文件名

Dir 數據庫鏡像備份的文件放置路徑

Slaveof 設置數據庫爲其他數據庫的從數據庫

Masterauth 主數據庫連接需要的密碼驗證

Requriepass 設置 登陸時需要使用密碼

Maxclients 限制同時使用的客戶數量

Maxmemory 設置redis能夠使用的最大內存

Appendonly 開啓append only模式

Appendfsync 設置對appendonly.aof文件同步的頻率(對數據進行備份的第二種方式)

vm-enabled 是否開啓虛擬內存支持   (vm開頭的參數都是配置虛擬內存的)

vm-swap-file 設置虛擬內存的交換文件路徑

vm-max-memory 設置redis使用的最大物理內存大小

vm-page-size 設置虛擬內存的頁大小

vm-pages 設置交換文件的總的page數量

vm-max-threads 設置VM IO同時使用的線程數量

Glueoutputbuf 把小的輸出緩存存放在一起

hash-max-zipmap-entries 設置hash的臨界值

Activerehashing 重新hash


言歸正傳,如果需要redis後臺運行需要將daemonize由no改爲yes。

首先在超級權限下打開redis.conf。

?
1
[neil@neilhost bin]$ sudo gedit /usr/local/redis/etc/redis.conf

之後將daemonize由no改爲yes。

Redis安裝部署與維護詳解

保存退出。

之後我們再次使用配置文件啓動redis-server。

可以看到,redis是後臺啓動了,並且通過ps命令可以查看到redis正在運行。

?
1
2
3
4
5
6
7
[neil@neilhost bin]$ sudo ./redis-server /usr/local/redis/etc/redis.conf
[neil@neilhost bin]$ ps -ef | grep redis
root     13154     1  0 22:53 ?        00:00:00 ./redis-server *:6379
neil     13162  8143  0 22:54 pts/0    00:00:00 grep --color=auto redis
[neil@neilhost bin]$ pstree -p | grep redis
           |-redis-server(13154)-+-{redis-server}(13156)
           |                     `-{redis-server}(13157)


Redis服務端默認連接端口是6379.

mysql 服務端默認連接端口是3306

Mogodb 服務端默認連接端口是27017,還有28017。

在平時,我們往往需要查看6379端口是否被佔用。可以用以下命令:

?
1
2
3
4
5
6
7
8
9
[neil@neilhost bin]$ netstat -tunpl | grep 6379
(Not all processes could be identified, non-owned process info
 will not be shown, you would have to be root to see it all.)
tcp        0      0 0.0.0.0:6379            0.0.0.0:*               LISTEN      -                   
tcp6       0      0 :::6379                 :::*                    LISTEN      -                   
[neil@neilhost bin]$ sudo netstat -tunpl | grep 6379
[sudo] password for neil: 
tcp        0      0 0.0.0.0:6379            0.0.0.0:*               LISTEN      13154/./redis-serve 
tcp6       0      0 :::6379                 :::*                    LISTEN      13154/./redis-serve

注意,redis服務需要su權限才能查看,不然只能檢查到6379被某個進程佔用,但是看不到進程名稱。

至此,redis服務已經按照配置文件啓動成功!!

客戶端登陸

步驟5:

客戶端連接

?
1
2
[neil@neilhost bin]$ sudo /usr/local/redis/bin/redis-cli 
127.0.0.1:6379>


關閉Redis服務

步驟6:

停止Redis實例

我們可以使用pkill redis-server

?
1
2
3
4
5
6
7
8
9
10
[neil@neilhost bin]$ sudo pkill redis-server
[neil@neilhost bin]$ sudo netstat -tunpl | grep 6379
[neil@neilhost bin]$ 
[neil@neilhost bin]$ pstree -p | grep redis
[neil@neilhost bin]$ 
[neil@neilhost bin]$ 
[neil@neilhost bin]$ sudo /usr/local/redis/bin/redis-cli 
Could not connect to Redis at 127.0.0.1:6379: Connection refused
not connected> 
not connected> exit


關閉之後,發現6379就不再被佔用了,redis的進程也都沒有了。

客戶登陸也無法成功了。


也可以使用/usr/local/redis/bin/redis-cli shutdown,這種方法使用客戶端命令redis-cli 

?
1
2
3
4
5
6
7
8
9
10
[neil@neilhost bin]$ sudo ./redis-server /usr/local/redis/etc/redis.conf
[neil@neilhost bin]$ pstree -p | grep redis
           |-redis-server(13509)-+-{redis-server}(13511)
           |                     `-{redis-server}(13512)
[neil@neilhost bin]$ sudo /usr/local/redis/bin/redis-cli shutdown
[neil@neilhost bin]$ pstree -p | grep redis
[neil@neilhost bin]$ 
[neil@neilhost bin]$ sudo netstat -tunpl | grep 6379
[neil@neilhost bin]$ 
[neil@neilhost bin]$

還可以使用命令killall和kill -9,可以仿照我在之前博客中的關閉Mongdb的方法。

發佈了27 篇原創文章 · 獲贊 11 · 訪問量 1萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章