REmote DIctionary Server(远程字典服务器)。是完全开源免费的,用C语言编写的,遵守BSD协议;是一个高性能的(key/value)分布式内存数据库,基于内存运行并支持持久化nosql数据库,是当前最热门的nosql数据库之一,也被人们称为数据结构服务器
Redis与其他nosql数据库相比较,三个特点:
a.Redis支持数据的持久化,可以将内存中的数据保存在磁盘中,重启的时候可以再次加载进行使用
b.Redis不仅仅支持简单的key-value类型的数据,同时还提供list,set,zset,hash等数据结构的存储
c.Redis支持数据备份,即master-slave模式的数据备份
Redis能干什么:
a.内存存储和持久化:redis支持异步将内存中的数据写到硬盘上,同时不影响继续服务
b.取最新n个数据的操作,如:可以将最新的10条评论的ID放在Redis的list集合里面
c.模拟类似于HttpSession这种需要设定过期时间的功能
d.发布,订阅消息系统
e.定时器,计数器
2. Redis的安装
第一步,官网下载Redis 压缩包,地址:http://redis.io/download。stable表示稳定版.这里下载的是3.2.9版本
第二步,通过远程管理工具(这里使用的是xftp),将压缩包拷贝到Linux服务器中(这里存放到了/usr/local下面)
第三步,进入/usr/local目录,解压文件
tar -zxvf redis-3.2.9.tar.gz
此时在/usr/local中会出现一个redis-3.2.9的目录
第四步,进入redis-3.2.9目录,编译
cd redis-3.2.9 make
此时可能出现cc未找到的命令,原因是虚拟机系统中缺少gcc,安装一下gcc即可解决问题,所以需要先安装:
yum install gcc-c++
此时还可能出现 :致命错误:jemalloc/jemalloc.h:没有那个文件或目录 为解决这个问题,需要执行如下命令:
make MALLOC=libc
第五步,进入src目录,安装
make install
第六步,新增用户和组
groupadd redis useradd -c Redis Server -s /sbin/nologin -d /var/lib/redis -g redis -G root
reids参数解释: -c 用户描述信息 -s 用户执行脚本,此处为安全考虑,redis用户是不允许远程登录,故使用/sbin/nologin -d 用户home目录,此处无需在/home目录下创建redis子目录,故将其定位于/var/lib/redis空目录中 -G 扩展用户组,即表示此用户同时属于root用户组
到此为止,redis安装完成
3.redis的启动
为了方便管理,将redis文件重的redis.conf配置文件和常用命令移动到统一文件夹中,这里常用命令都自动移动到了/usr/local/bin目录中,所以这里我直接把redis-3.2.9目录中的redis.conf文件也移动到了/usr/local/bin目录下:
[root redis-3.2.9]# mv redis.conf /usr/local/bin
此时直接执行命令启动redis:
./redis-server ./redis.conf
看到如下效果:
[root bin]# ./redis-server ./redis.conf 11164:M 10 Jul 19:02:02.049 * Increased maximum number of open files to 10032 (it was originally set to 1024). _._ _.-``__ ''-._ _.-`` `. `_. ''-._ Redis 3.2.9 (00000000/0) 64 bit .-`` .-```. ```\/ _.,_ ''-._ ( ' , .-` | `, ) Running in standalone mode |`-._`-...-` __...-.``-._|'` _.-'| Port: 6379 | `-._ `._ / _.-' | PID: 11164 `-._ `-._ `-./ _.-' _.-' |`-._`-._ `-.__.-' _.-'_.-'| | `-._`-._ _.-'_.-' | http://redis.io `-._ `-._`-.__.-'_.-' _.-' |`-._`-._ `-.__.-' _.-'_.-'| | `-._`-._ _.-'_.-' | `-._ `-._`-.__.-'_.-' _.-' `-._ `-.__.-' _.-' `-._ _.-' `-.__.-' 11164:M 10 Jul 19:02:02.071 # WARNING: The TCP backlog setting of 511 cannot be enforced because /proc/sys/net/core/somaxconn is set to the lower value of 128. 11164:M 10 Jul 19:02:02.072 # Server started, Redis version 3.2.9 11164:M 10 Jul 19:02:02.072 # 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. 11164:M 10 Jul 19:02:02.073 # 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. 11164:M 10 Jul 19:02:02.073 * The server is now ready to accept connections on port 6379 ^C11164:signal-handler (1499684525) Received SIGINT scheduling shutdown... 11164:M 10 Jul 19:02:05.537 # User requested shutdown... 11164:M 10 Jul 19:02:05.537 * Saving the final RDB snapshot before exiting. 11164:M 10 Jul 19:02:05.545 * DB saved on disk 11164:M 10 Jul 19:02:05.545 * Removing the pid file. 11164:M 10 Jul 19:02:05.545 # Redis is now ready to exit, bye bye...
表示启动成功,但此时是前台运行的,我们还需要让redis服务后台运行,所以Ctrl+C退出,然后编辑redis.conf文件,把其中的daemonize属性改为yes(表明需要在后台运行,默认为no)
[root bin]# netstat -tunpl | grep 6379 [root bin]# ls dump.rdb redis-benchmark redis-check-aof redis-check-rdb redis-cli redis.conf redis-sentinel redis-server [root bin]# vi redis.conf
修改,保存退出,再次启动redis:
[root bin]# ./redis-server ./redis.conf [root bin]# netstat -tunpl | grep 6379 tcp 0 0 127.0.0.1:6379 0.0.0.0:* LISTEN 11172/./redis-serve
看到redis-serve占用了6379端口(redis的默认端口为6379).
启动redis的客户端:
[root bin]# ./redis-cli 127.0.0.1:6379> keys * (empty list or set)
启动成功!