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

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

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

         下面开始安装Redis。

第一步:下载Redis安装版本

        这里需要说明一点的是,新版的redis不够稳定,建议大家还是使用旧版本的redis。我这里选择的是4.0.10版本大家可以到redis4.0.10这个地址下载。

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

        为了统一管理安装包,我一般会在虚拟机的/usr/local目录下新建一个software目录,如果没有该目录请自行创建

[root@itcast07 /]# mkdir /usr/local/software

我们把redis-4.0.10.tar.gz上传到software目录下,如下所示。

   第三步:解压

 

[root@itcast07 software]# tar -zxvf redis-4.0.10.tar.gz  -C /usr/local/

第四步:安装gcc环境

         由于redis是由C语言编写的,它的运行需要C环境,因此我们需要先安装gcc。安装命令是yum install gcc-c++

第五步:编译安装

         我们到/usr/local/redis-4.0.10目录下,要运行make和make install命令就要有Makefile这个文件,我们可以看到是有这个文件的。

[root@itcast07 redis]# cd ../redis-4.0.10/
[root@itcast07 redis-4.0.10]# ll
total 308
-rw-rw-r--  1 root root 162174 Jun 13 19:02 00-RELEASENOTES
-rw-rw-r--  1 root root     53 Jun 13 19:02 BUGS
-rw-rw-r--  1 root root   1815 Jun 13 19:02 CONTRIBUTING
-rw-rw-r--  1 root root   1487 Jun 13 19:02 COPYING
drwxrwxr-x  6 root root   4096 Jul 31 17:28 deps
-rw-rw-r--  1 root root     11 Jun 13 19:02 INSTALL
-rw-rw-r--  1 root root    151 Jun 13 19:02 Makefile
-rw-rw-r--  1 root root   4223 Jun 13 19:02 MANIFESTO
-rw-rw-r--  1 root root  20543 Jun 13 19:02 README.md
-rw-rw-r--  1 root root  58766 Jun 13 19:02 redis.conf
-rwxrwxr-x  1 root root    271 Jun 13 19:02 runtest
-rwxrwxr-x  1 root root    280 Jun 13 19:02 runtest-cluster
-rwxrwxr-x  1 root root    281 Jun 13 19:02 runtest-sentinel
-rw-rw-r--  1 root root   7606 Jun 13 19:02 sentinel.conf
drwxrwxr-x  3 root root   4096 Jul 31 17:33 src
drwxrwxr-x 10 root root   4096 Jun 13 19:02 tests
drwxrwxr-x  8 root root   4096 Jun 13 19:02 utils

       先进行编译

[root@itcast07 redis-4.0.10]# make

      再进行安装

[root@itcast07 redis-4.0.10]# make install PREFIX=/usr/local/redis
cd src && make install
make[1]: Entering directory `/usr/local/redis-4.0.10/src'
    CC Makefile.dep
make[1]: Leaving directory `/usr/local/redis-4.0.10/src'
make[1]: Entering directory `/usr/local/redis-4.0.10/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-4.0.10/src'
[root@itcast07 redis-4.0.10]# 
       安装完之后,我们到/usr/local目录下,可以看到redis目录,我们进入redis目录,在该目录下可以看到bin目录,进入到bin目录可以看到redis的客户端和服务端,如下所示。
[root@itcast07 bin]# pwd
/usr/local/redis/bin
[root@itcast07 bin]# ll
total 35480
-rwxr-xr-x 1 root root 5597950 Jul 31 17:33 redis-benchmark
-rwxr-xr-x 1 root root 8329925 Jul 31 17:33 redis-check-aof
-rwxr-xr-x 1 root root 8329925 Jul 31 17:33 redis-check-rdb
-rwxr-xr-x 1 root root 5738298 Jul 31 17:33 redis-cli
lrwxrwxrwx 1 root root      12 Jul 31 17:33 redis-sentinel -> redis-server
-rwxr-xr-x 1 root root 8329925 Jul 31 17:33 redis-server
[root@itcast07 bin]#

第六步:启动redis服务端

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

[root@itcast07 bin]#  ./redis-server
31891:C 31 Jul 17:37:19.610 # oO0OoO0OoO0Oo Redis is starting oO0OoO0OoO0Oo
31891:C 31 Jul 17:37:19.610 # Redis version=4.0.10, bits=64, commit=00000000, modified=0, pid=31891, just started
31891:C 31 Jul 17:37:19.610 # Warning: no config file specified, using the default config. In order to specify a config file use ./redis-server /path/to/redis.conf
31891:M 31 Jul 17:37:19.614 * Increased maximum number of open files to 10032 (it was originally set to 1024).
                _._
           _.-``__ ''-._
      _.-``    `.  `_.  ''-._           Redis 4.0.10 (00000000/0) 64 bit
  .-`` .-```.  ```\/    _.,_ ''-._
 (    '      ,       .-`  | `,    )     Running in standalone mode
 |`-._`-...-` __...-.``-._|'` _.-'|     Port: 6379
 |    `-._   `._    /     _.-'    |     PID: 31891
  `-._    `-._  `-./  _.-'    _.-'
 |`-._`-._    `-.__.-'    _.-'_.-'|
 |    `-._`-._        _.-'_.-'    |           http://redis.io
  `-._    `-._`-.__.-'_.-'    _.-'
 |`-._`-._    `-.__.-'    _.-'_.-'|
 |    `-._`-._        _.-'_.-'    |
  `-._    `-._`-.__.-'_.-'    _.-'
      `-._    `-.__.-'    _.-'
          `-._        _.-'
              `-.__.-'

31891:M 31 Jul 17:37:19.646 # WARNING: The TCP backlog setting of 511 cannot be enforced because /proc/sys/net/core/somaxconn is set to the lower value of 128.
31891:M 31 Jul 17:37:19.646 # Server initialized
31891:M 31 Jul 17:37:19.646 # 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.
31891:M 31 Jul 17:37:19.646 # 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.
31891:M 31 Jul 17:37:19.646 * Ready to accept connections

        第二种启动方式是使用配置文件启动(也叫后端启动),我们需要先到redis的安装目录下把redis.conf文件复制一份到/usr/local/redis/bin目录下。如下所示。

[root@itcast07 redis-4.0.10]# cp redis.conf /usr/local/redis/bin/

      下面我们需要简单修改下redis.conf文件,把daemonize的值由默认的no修改为yes,这个配置是指定是否在后台运行。如下图所示。


        开启远程访问,设置密码


 

下面使用后台启动

[root@itcast07 bin]#  ./redis-server redis.conf
32024:C 31 Jul 17:42:17.411 # oO0OoO0OoO0Oo Redis is starting oO0OoO0OoO0Oo
32024:C 31 Jul 17:42:17.411 # Redis version=4.0.10, bits=64, commit=00000000, modified=0, pid=32024, just started
32024:C 31 Jul 17:42:17.411 # Configuration loaded
[root@itcast07 bin]#

       要想查看是否正常启动了,可以使用ps -ef|grep redis或ps aux | grep redis,如下所示,可以看到已经正常启动了。

[root@itcast07 bin]# ps -ef|grep redis
root      32025      1  0 17:42 ?        00:00:00 ./redis-server 127.0.0.1:6379
root      32037  27890  0 17:42 pts/0    00:00:00 grep redis
[root@itcast07 bin]#
第七步:测试服务

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

[root@itcast07 bin]# ./redis-cli -a passwd
Warning: Using a password with '-a' option on the command line interface may not be safe.
127.0.0.1:6379> ping
PONG
127.0.0.1:6379>

       我们还可以连接其它设备上的redis,如下所示(当然,这里我还是用的本虚拟机的IP,大家可以换成其它的IP)

[root@itcast07 bin]# ./redis-cli -h 192.168.117.106 -p 6379 -a passwd
Warning: Using a password with '-a' option on the command line interface may not be safe.
192.168.117.106:6379> ping
PONG
192.168.117.106:6379>

我们还可以连接其它设备上的redis,如下所示(当然,这里我还是用的本虚拟机的IP,大家可以换成其它的IP)

给大家推荐一个redis的可视化连接工具: redis-desktop-manager大家可以点开连接redis可视化连接工具进行下载,一键安装

 

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值