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>  
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值