之所以要在工程中添加缓存,是因为在互联网项目中查询功能是非常频繁的,如果每次查询都调用数据库的话,会给数据库造成很大的压力,因此需要在用户和数据库之间加一层缓存,对于同样的查询,只查询一遍数据库,然后把数据保存到缓存当中,当其他用户再访问同样的页面时便可以直接从缓存中去读取数据,这样查询效率将会提升非常多。同时也会大大减轻数据库的压力。
下面开始安装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 ~]#
- [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/
由于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]#
- [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-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
- [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]#
下面使用后台启动
- [root@redis bin]# ./redis-server redis.conf
- [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>
- [root@redis bin]# ./redis-cli -h 192.168.156.11 -p 6379
- 192.168.156.11:6379> ping
- PONG
- 192.168.156.11:6379>