Linux-安裝redis

 之所以要在工程中添加緩存,是因爲在互聯網項目中查詢功能是非常頻繁的,如果每次查詢都調用數據庫的話,會給數據庫造成很大的壓力,因此需要在用戶和數據庫之間加一層緩存,對於同樣的查詢,只查詢一遍數據庫,然後把數據保存到緩存當中,當其他用戶再訪問同樣的頁面時便可以直接從緩存中去讀取數據,這樣查詢效率將會提升非常多。同時也會大大減輕數據庫的壓力。

         下面開始安裝Redis。

第一步:下載Redis安裝版本

        這裏需要說明一點的是,新版的redis不夠穩定,建議大家還是使用舊版本的redis,比如3.0.0版本。大家可以到http://download.csdn.net/detail/u012453843/9820041這個地址下載。

第二步:將安裝包上傳到虛擬機

        爲了統一管理安裝包,我一般會在虛擬機的/usr/local目錄下新建一個software目錄,如下所示。

[html] view plain copy
  1. [root@redis ~]# mkdir /usr/local/software  
  2. [root@redis ~]#  
        先建完software目錄後,我們把redis-3.0.0.tar.gz上傳到software目錄下。
[html] view plain copy
  1. [root@redis software]# ll  
  2. 總用量 1512  
  3. -rw-r--r--. 1 root root 1547237 4月  20 09:19 redis-3.0.0.tar.gz  
  4. [root@redis software]#   

第三步:解壓

[html] view plain copy
  1. [root@redis software]# tar -zxvf redis-3.0.0.tar.gz -C /usr/local/  

第四步:安裝gcc環境

         由於redis是由C語言編寫的,它的運行需要C環境,因此我們需要先安裝gcc。安裝命令是yum install gcc-c++

第五步:編譯安裝

         我們到/usr/local/redis-3.0.0目錄下,要運行make和make install命令就要有Makefile這個文件,我們可以看到是有這個文件的。

[html] view plain copy
  1. [root@redis software]# cd /usr/local/redis-3.0.0/  
  2. [root@redis redis-3.0.0]# ll  
  3. 總用量 204  
  4. -rw-rw-r--.  1 root root 85775 2月  12 23:14 00-RELEASENOTES  
  5. -rw-rw-r--.  1 root root    53 2月  12 23:14 BUGS  
  6. -rw-rw-r--.  1 root root  1805 2月  12 23:14 CONTRIBUTING  
  7. -rw-rw-r--.  1 root root  1487 2月  12 23:14 COPYING  
  8. drwxrwxr-x.  7 root root  4096 2月  12 23:14 deps  
  9. -rw-rw-r--.  1 root root    11 2月  12 23:14 INSTALL  
  10. -rw-rw-r--.  1 root root   151 2月  12 23:14 Makefile  
  11. -rw-rw-r--.  1 root root  4223 2月  12 23:14 MANIFESTO  
  12. -rw-rw-r--.  1 root root  6834 2月  12 23:14 README.md  
  13. -rw-rw-r--.  1 root root 46695 2月  12 23:14 redis.conf  
  14. -rwxrwxr-x.  1 root root   271 2月  12 23:14 runtest  
  15. -rwxrwxr-x.  1 root root   280 2月  12 23:14 runtest-cluster  
  16. -rwxrwxr-x.  1 root root   281 2月  12 23:14 runtest-sentinel  
  17. -rw-rw-r--.  1 root root  7606 2月  12 23:14 sentinel.conf  
  18. drwxrwxr-x.  2 root root  4096 2月  12 23:14 src  
  19. drwxrwxr-x. 10 root root  4096 2月  12 23:14 tests  
  20. drwxrwxr-x.  7 root root  4096 2月  12 23:14 utils  
  21. [root@redis redis-3.0.0]#   
       先進行編譯

[html] view plain copy
  1. [root@redis redis-3.0.0]# make  
      再進行安裝

[html] view plain copy
  1. [root@redis redis-3.0.0]# make install PREFIX=/usr/local/redis  
  2. cd src && make install  
  3. make[1]: Entering directory `/usr/local/redis-3.0.0/src'  
  4.   
  5. Hint: It's a good idea to run 'make test' ;)  
  6.   
  7.     INSTALL install  
  8.     INSTALL install  
  9.     INSTALL install  
  10.     INSTALL install  
  11.     INSTALL install  
  12. make[1]: Leaving directory `/usr/local/redis-3.0.0/src'  
  13. [root@redis redis-3.0.0]#   
       安裝完之後,我們到/usr/local目錄下,可以看到redis目錄,我們進入redis目錄,在該目錄下可以看到bin目錄,進入到bin目錄可以看到redis的客戶端和服務端,如下所示。
[html] view plain copy
  1. [root@redis local]# ll  
  2. 總用量 52  
  3. drwxr-xr-x. 2 root root 4096 9月  23 2011 bin  
  4. drwxr-xr-x. 2 root root 4096 9月  23 2011 etc  
  5. drwxr-xr-x. 2 root root 4096 9月  23 2011 games  
  6. drwxr-xr-x. 2 root root 4096 9月  23 2011 include  
  7. drwxr-xr-x. 2 root root 4096 9月  23 2011 lib  
  8. drwxr-xr-x. 2 root root 4096 9月  23 2011 lib64  
  9. drwxr-xr-x. 2 root root 4096 9月  23 2011 libexec  
  10. drwxr-xr-x. 3 root root 4096 4月  20 18:20 redis  
  11. drwxrwxr-x. 6 root root 4096 2月  12 23:14 redis-3.0.0  
  12. drwxr-xr-x. 2 root root 4096 9月  23 2011 sbin  
  13. drwxr-xr-x. 5 root root 4096 4月   7 01:13 share  
  14. drwxr-xr-x. 2 root root 4096 4月  20 17:38 software  
  15. drwxr-xr-x. 2 root root 4096 9月  23 2011 src  
  16. [root@redis local]# cd redis  
  17. [root@redis redis]# ll  
  18. 總用量 4  
  19. drwxr-xr-x. 2 root root 4096 4月  20 18:20 bin  
  20. [root@redis redis]# cd bin  
  21. [root@redis bin]# ll  
  22. 總用量 26348  
  23. -rwxr-xr-x. 1 root root 5580303 4月  20 18:20 redis-benchmark  
  24. -rwxr-xr-x. 1 root root   22177 4月  20 18:20 redis-check-aof  
  25. -rwxr-xr-x. 1 root root 7829962 4月  20 18:20 redis-check-rdb  
  26. -rwxr-xr-x. 1 root root 5709171 4月  20 18:20 redis-cli  
  27. lrwxrwxrwx. 1 root root      12 4月  20 18:20 redis-sentinel -> redis-server  
  28. -rwxr-xr-x. 1 root root 7829962 4月  20 18:20 redis-server  
  29. [root@redis bin]#   
第六步:啓動redis服務端

        第一種啓動方式是直接使用./redis-server命令啓動,如下所示(可以看到默認端口是6379),只不過這種啓動會佔用終端,如果我們按Ctrl+C就會停止redis-server。

[html] view plain copy
  1. [root@redis bin]# ./redis-server   
  2. 15618:C 20 Apr 18:26:36.662 # Warning: no config file specified, using the default config. In order to specify a config file use ./redis-server /path/to/redis.conf  
  3. 15618:M 20 Apr 18:26:36.664 * Increased maximum number of open files to 10032 (it was originally set to 1024).  
  4.                 _._                                                    
  5.            _.-``__ ''-._                                               
  6.       _.-``    `.  `_.  ''-._           Redis 3.2.8 (00000000/0) 64 bit  
  7.   .-`` .-```.  ```\/    _.,_ ''-._                                     
  8.  (    '      ,       .-`  | `,    )     Running in standalone mode  
  9.  |`-._`-...-` __...-.``-._|'` _.-'|     Port: 6379  
  10.  |    `-._   `._    /     _.-'    |     PID: 15618  
  11.   `-._    `-._  `-./  _.-'    _.-'                                     
  12.  |`-._`-._    `-.__.-'    _.-'_.-'|                                    
  13.  |    `-._`-._        _.-'_.-'    |           http://redis.io          
  14.   `-._    `-._`-.__.-'_.-'    _.-'                                     
  15.  |`-._`-._    `-.__.-'    _.-'_.-'|                                    
  16.  |    `-._`-._        _.-'_.-'    |                                    
  17.   `-._    `-._`-.__.-'_.-'    _.-'                                     
  18.       `-._    `-.__.-'    _.-'                                         
  19.           `-._        _.-'                                             
  20.               `-.__.-'                                                 
  21.   
  22. 15618:M 20 Apr 18:26:36.710 # WARNING: The TCP backlog setting of 511 cannot be enforced because /proc/sys/net/core/somaxconn is set to the lower value of 128.  
  23. 15618:M 20 Apr 18:26:36.710 # Server started, Redis version 3.2.8  
  24. 15618:M 20 Apr 18:26:36.714 # 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.  
  25. 15618:M 20 Apr 18:26:36.728 * The server is now ready to accept connections on port 6379  
        第二種啓動方式是使用配置文件啓動(也叫後端啓動),我們需要先到redis的安裝目錄下把redis.conf文件複製一份到/usr/local/redis/bin目錄下。如下所示。
[html] view plain copy
  1. [root@redis redis-3.0.0]# ll  
  2. 總用量 204  
  3. -rw-rw-r--.  1 root root 85775 2月  12 23:14 00-RELEASENOTES  
  4. -rw-rw-r--.  1 root root    53 2月  12 23:14 BUGS  
  5. -rw-rw-r--.  1 root root  1805 2月  12 23:14 CONTRIBUTING  
  6. -rw-rw-r--.  1 root root  1487 2月  12 23:14 COPYING  
  7. drwxrwxr-x.  7 root root  4096 4月  20 18:15 deps  
  8. -rw-rw-r--.  1 root root    11 2月  12 23:14 INSTALL  
  9. -rw-rw-r--.  1 root root   151 2月  12 23:14 Makefile  
  10. -rw-rw-r--.  1 root root  4223 2月  12 23:14 MANIFESTO  
  11. -rw-rw-r--.  1 root root  6834 2月  12 23:14 README.md  
  12. -rw-rw-r--.  1 root root 46695 2月  12 23:14 redis.conf  
  13. -rwxrwxr-x.  1 root root   271 2月  12 23:14 runtest  
  14. -rwxrwxr-x.  1 root root   280 2月  12 23:14 runtest-cluster  
  15. -rwxrwxr-x.  1 root root   281 2月  12 23:14 runtest-sentinel  
  16. -rw-rw-r--.  1 root root  7606 2月  12 23:14 sentinel.conf  
  17. drwxrwxr-x.  2 root root  4096 4月  20 18:17 src  
  18. drwxrwxr-x. 10 root root  4096 2月  12 23:14 tests  
  19. drwxrwxr-x.  7 root root  4096 2月  12 23:14 utils  
  20. [root@redis redis-3.0.0]# cp redis.conf /usr/local/redis/bin/  
  21. [root@redis redis-3.0.0]# cd /usr/local/redis/bin  
  22. [root@redis bin]# ll  
  23. 總用量 26400  
  24. -rw-r--r--. 1 root root      76 4月  20 18:30 dump.rdb  
  25. -rwxr-xr-x. 1 root root 5580303 4月  20 18:20 redis-benchmark  
  26. -rwxr-xr-x. 1 root root   22177 4月  20 18:20 redis-check-aof  
  27. -rwxr-xr-x. 1 root root 7829962 4月  20 18:20 redis-check-rdb  
  28. -rwxr-xr-x. 1 root root 5709171 4月  20 18:20 redis-cli  
  29. -rw-r--r--. 1 root root   46695 4月  20 18:32 redis.conf  
  30. lrwxrwxrwx. 1 root root      12 4月  20 18:20 redis-sentinel -> redis-server  
  31. -rwxr-xr-x. 1 root root 7829962 4月  20 18:20 redis-server  
  32. [root@redis bin]#   
      下面我們需要簡單修改下redis.conf文件,把daemonize的值由默認的no修改爲yes,這個配置是指定是否在後臺運行。如下圖所示。


     

     下面使用後臺啓動

[html] view plain copy
  1. [root@redis bin]# ./redis-server redis.conf  
       要想查看是否正常啓動了,可以使用ps -ef|grep redis或ps aux | grep redis,如下所示,可以看到已經正常啓動了。

[html] view plain copy
  1. [root@redis bin]# ps -ef|grep redis  
  2. root     17591     1  0 18:39 ?        00:00:00 ./redis-server 127.0.0.1:6379  
  3. root     17849  1524  0 18:41 pts/0    00:00:00 grep redis  
  4. [root@redis bin]#  
第七步:測試服務

       使用./redis-cli連接上redis服務,然後使用ping命令,如果返回的是PONG,說明連接沒問題。

[html] view plain copy
  1. [root@redis bin]# ./redis-cli   
  2. 127.0.0.1:6379> ping  
  3. PONG  
  4. 127.0.0.1:6379>  
       我們還可以連接其它設備上的redis,如下所示(當然,這裏我還是用的本虛擬機的IP,大家可以換成其它的IP)
[html] view plain copy
  1. [root@redis bin]# ./redis-cli -h 192.168.156.11 -p 6379  
  2. 192.168.156.11:6379> ping  
  3. PONG  
  4. 192.168.156.11:6379>  
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章