学习淘淘商城第二十九课(Redis的安装)

110 篇文章 383 订阅
13 篇文章 1 订阅

        上节课我们一起学习了淘淘商城大广告位的展示,这节课我们一起学习下Redis的安装。

        之所以要在工程中添加缓存,是因为在互联网项目中查询功能是非常频繁的,如果每次查询都调用数据库的话,会给数据库造成很大的压力,因此需要在用户和数据库之间加一层缓存,对于同样的查询,只查询一遍数据库,然后把数据保存到缓存当中,当其他用户再访问同样的页面时便可以直接从缓存中去读取数据,这样查询效率将会提升非常多。同时也会大大减轻数据库的压力。

         下面开始安装Redis。

第一步:下载Redis安装版本

        这里需要说明一点的是,新版的redis不够稳定,建议大家还是使用旧版本的redis,比如3.0.0版本。大家可以到http://download.csdn.net/detail/u012453843/9820041这个地址下载。

第二步:将安装包上传到虚拟机

        为了统一管理安装包,我一般会在虚拟机的/usr/local目录下新建一个software目录,如下所示。

[root@redis ~]# mkdir /usr/local/software
[root@redis ~]#
        先建完software目录后,我们把redis-3.0.0.tar.gz上传到software目录下。
[root@redis software]# ll
总用量 1512
-rw-r--r--. 1 root root 1547237 4月  20 09:19 redis-3.0.0.tar.gz
[root@redis software]# 

第三步:解压

[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这个文件,我们可以看到是有这个文件的。

[root@redis software]# cd /usr/local/redis-3.0.0/
[root@redis redis-3.0.0]# ll
总用量 204
-rw-rw-r--.  1 root root 85775 2月  12 23:14 00-RELEASENOTES
-rw-rw-r--.  1 root root    53 2月  12 23:14 BUGS
-rw-rw-r--.  1 root root  1805 2月  12 23:14 CONTRIBUTING
-rw-rw-r--.  1 root root  1487 2月  12 23:14 COPYING
drwxrwxr-x.  7 root root  4096 2月  12 23:14 deps
-rw-rw-r--.  1 root root    11 2月  12 23:14 INSTALL
-rw-rw-r--.  1 root root   151 2月  12 23:14 Makefile
-rw-rw-r--.  1 root root  4223 2月  12 23:14 MANIFESTO
-rw-rw-r--.  1 root root  6834 2月  12 23:14 README.md
-rw-rw-r--.  1 root root 46695 2月  12 23:14 redis.conf
-rwxrwxr-x.  1 root root   271 2月  12 23:14 runtest
-rwxrwxr-x.  1 root root   280 2月  12 23:14 runtest-cluster
-rwxrwxr-x.  1 root root   281 2月  12 23:14 runtest-sentinel
-rw-rw-r--.  1 root root  7606 2月  12 23:14 sentinel.conf
drwxrwxr-x.  2 root root  4096 2月  12 23:14 src
drwxrwxr-x. 10 root root  4096 2月  12 23:14 tests
drwxrwxr-x.  7 root root  4096 2月  12 23:14 utils
[root@redis redis-3.0.0]# 
       先进行编译

[root@redis redis-3.0.0]# make
      再进行安装

[root@redis redis-3.0.0]# make install PREFIX=/usr/local/redis
cd src && make install
make[1]: Entering directory `/usr/local/redis-3.0.0/src'

Hint: It's a good idea to run 'make test' ;)

    INSTALL install
    INSTALL install
    INSTALL install
    INSTALL install
    INSTALL install
make[1]: Leaving directory `/usr/local/redis-3.0.0/src'
[root@redis redis-3.0.0]# 
       安装完之后,我们到/usr/local目录下,可以看到redis目录,我们进入redis目录,在该目录下可以看到bin目录,进入到bin目录可以看到redis的客户端和服务端,如下所示。
[root@redis local]# ll
总用量 52
drwxr-xr-x. 2 root root 4096 9月  23 2011 bin
drwxr-xr-x. 2 root root 4096 9月  23 2011 etc
drwxr-xr-x. 2 root root 4096 9月  23 2011 games
drwxr-xr-x. 2 root root 4096 9月  23 2011 include
drwxr-xr-x. 2 root root 4096 9月  23 2011 lib
drwxr-xr-x. 2 root root 4096 9月  23 2011 lib64
drwxr-xr-x. 2 root root 4096 9月  23 2011 libexec
drwxr-xr-x. 3 root root 4096 4月  20 18:20 redis
drwxrwxr-x. 6 root root 4096 2月  12 23:14 redis-3.0.0
drwxr-xr-x. 2 root root 4096 9月  23 2011 sbin
drwxr-xr-x. 5 root root 4096 4月   7 01:13 share
drwxr-xr-x. 2 root root 4096 4月  20 17:38 software
drwxr-xr-x. 2 root root 4096 9月  23 2011 src
[root@redis local]# cd redis
[root@redis redis]# ll
总用量 4
drwxr-xr-x. 2 root root 4096 4月  20 18:20 bin
[root@redis redis]# cd bin
[root@redis bin]# ll
总用量 26348
-rwxr-xr-x. 1 root root 5580303 4月  20 18:20 redis-benchmark
-rwxr-xr-x. 1 root root   22177 4月  20 18:20 redis-check-aof
-rwxr-xr-x. 1 root root 7829962 4月  20 18:20 redis-check-rdb
-rwxr-xr-x. 1 root root 5709171 4月  20 18:20 redis-cli
lrwxrwxrwx. 1 root root      12 4月  20 18:20 redis-sentinel -> redis-server
-rwxr-xr-x. 1 root root 7829962 4月  20 18:20 redis-server
[root@redis bin]# 
第六步:启动redis服务端

        第一种启动方式是直接使用./redis-server命令启动,如下所示(可以看到默认端口是6379),只不过这种启动会占用终端,如果我们按Ctrl+C就会停止redis-server。

[root@redis bin]# ./redis-server 
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
15618:M 20 Apr 18:26:36.664 * Increased maximum number of open files to 10032 (it was originally set to 1024).
                _._                                                  
           _.-``__ ''-._                                             
      _.-``    `.  `_.  ''-._           Redis 3.2.8 (00000000/0) 64 bit
  .-`` .-```.  ```\/    _.,_ ''-._                                   
 (    '      ,       .-`  | `,    )     Running in standalone mode
 |`-._`-...-` __...-.``-._|'` _.-'|     Port: 6379
 |    `-._   `._    /     _.-'    |     PID: 15618
  `-._    `-._  `-./  _.-'    _.-'                                   
 |`-._`-._    `-.__.-'    _.-'_.-'|                                  
 |    `-._`-._        _.-'_.-'    |           http://redis.io        
  `-._    `-._`-.__.-'_.-'    _.-'                                   
 |`-._`-._    `-.__.-'    _.-'_.-'|                                  
 |    `-._`-._        _.-'_.-'    |                                  
  `-._    `-._`-.__.-'_.-'    _.-'                                   
      `-._    `-.__.-'    _.-'                                       
          `-._        _.-'                                           
              `-.__.-'                                               

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.
15618:M 20 Apr 18:26:36.710 # Server started, Redis version 3.2.8
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.
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目录下。如下所示。
[root@redis redis-3.0.0]# ll
总用量 204
-rw-rw-r--.  1 root root 85775 2月  12 23:14 00-RELEASENOTES
-rw-rw-r--.  1 root root    53 2月  12 23:14 BUGS
-rw-rw-r--.  1 root root  1805 2月  12 23:14 CONTRIBUTING
-rw-rw-r--.  1 root root  1487 2月  12 23:14 COPYING
drwxrwxr-x.  7 root root  4096 4月  20 18:15 deps
-rw-rw-r--.  1 root root    11 2月  12 23:14 INSTALL
-rw-rw-r--.  1 root root   151 2月  12 23:14 Makefile
-rw-rw-r--.  1 root root  4223 2月  12 23:14 MANIFESTO
-rw-rw-r--.  1 root root  6834 2月  12 23:14 README.md
-rw-rw-r--.  1 root root 46695 2月  12 23:14 redis.conf
-rwxrwxr-x.  1 root root   271 2月  12 23:14 runtest
-rwxrwxr-x.  1 root root   280 2月  12 23:14 runtest-cluster
-rwxrwxr-x.  1 root root   281 2月  12 23:14 runtest-sentinel
-rw-rw-r--.  1 root root  7606 2月  12 23:14 sentinel.conf
drwxrwxr-x.  2 root root  4096 4月  20 18:17 src
drwxrwxr-x. 10 root root  4096 2月  12 23:14 tests
drwxrwxr-x.  7 root root  4096 2月  12 23:14 utils
[root@redis redis-3.0.0]# cp redis.conf /usr/local/redis/bin/
[root@redis redis-3.0.0]# cd /usr/local/redis/bin
[root@redis bin]# ll
总用量 26400
-rw-r--r--. 1 root root      76 4月  20 18:30 dump.rdb
-rwxr-xr-x. 1 root root 5580303 4月  20 18:20 redis-benchmark
-rwxr-xr-x. 1 root root   22177 4月  20 18:20 redis-check-aof
-rwxr-xr-x. 1 root root 7829962 4月  20 18:20 redis-check-rdb
-rwxr-xr-x. 1 root root 5709171 4月  20 18:20 redis-cli
-rw-r--r--. 1 root root   46695 4月  20 18:32 redis.conf
lrwxrwxrwx. 1 root root      12 4月  20 18:20 redis-sentinel -> redis-server
-rwxr-xr-x. 1 root root 7829962 4月  20 18:20 redis-server
[root@redis bin]# 
      下面我们需要简单修改下redis.conf文件,把daemonize的值由默认的no修改为yes,这个配置是指定是否在后台运行。如下图所示。


     

     下面使用后台启动

[root@redis bin]# ./redis-server redis.conf
       要想查看是否正常启动了,可以使用ps -ef|grep redis或ps aux | grep redis,如下所示,可以看到已经正常启动了。

[root@redis bin]# ps -ef|grep redis
root     17591     1  0 18:39 ?        00:00:00 ./redis-server 127.0.0.1:6379
root     17849  1524  0 18:41 pts/0    00:00:00 grep redis
[root@redis bin]#
第七步:测试服务

       使用./redis-cli连接上redis服务,然后使用ping命令,如果返回的是PONG,说明连接没问题。

[root@redis bin]# ./redis-cli 
127.0.0.1:6379> ping
PONG
127.0.0.1:6379>
       我们还可以连接其它设备上的redis,如下所示(当然,这里我还是用的本虚拟机的IP,大家可以换成其它的IP)
[root@redis bin]# ./redis-cli -h 192.168.156.11 -p 6379
192.168.156.11:6379> ping
PONG
192.168.156.11:6379>



   

 











    

  • 2
    点赞
  • 8
    收藏
    觉得还不错? 一键收藏
  • 2
    评论
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值