1,什么是NoSql
为了解决高并发、高可用、高可扩展,大数据存储等一系列问题而产生的数据库解决方案,就是NoSql。
NoSql,叫非关系型数据库,它的全名Not only sql。它不能替代关系型数据库,只能作为关系型数据库的一个良好补充。
Redis是使用c语言开发的一个高性能键值数据库。Redis可以通过一些键值类型来存储数据。
键值类型:
String字符类型
Hash(字典)
list列表类型
set集合类型
sortedset有序集合类型
2,redis的应用场景
缓存(数据查询、短连接、新闻内容、商品内容等等)。(最多使用)
分布式集群架构中的session分离。
聊天室的在线好友列表。
任务队列。(秒杀、抢购、12306等等)
应用排行榜。
网站访问统计。
数据过期处理(可以精确到毫秒)
3,redis 下载
官网地址:http://redis.io/
下载地址:http://download.redis.io/releases/redis-3.0.0.tar.gz
4,redis 的安装
redis的安装环境会安装到linux系统中。
第一步:安装VMware,并且在VMware中安装centos系统(参考linux教程)。
第二步:将redis的压缩包,上传到linux系统
第三步:对redis的压缩包进行解压缩
Redis解压缩之后的文件是用c语言写的源码文件
[root@itheima ~]# tar -zxf redis-3.0.0.tar.gz
第四步:安装c语言环境(安装centos之后,自带c语言环境)
[root@itheima ~]# yum install gcc-c++
第五步:编译redis源码
[root@itheima ~]# cd redis-3.0.0
[root@itheima redis-3.0.0]# make
第六步:安装redis
[root@itheima redis-3.0.0]# make install PREFIX=/usr/local/redis19
第七步:查看是否安装成功
[root@centos7 redis-3.0.0]#
[root@centos7 redis-3.0.0]# cd /usr/local/redis19/
[root@centos7 redis19]# ll
total 0
drwxr-xr-x. 2 root root 6 Oct 22 20:10 bin
[root@centos7 redis19]#
5,redis的启动
前端启动:
前端启动的命令:
[root@itheima bin]# ./redis-server
前端启动的关闭:
强制关闭:Ctrl+c
正常关闭:[root@itheima bin]# ./redis-cli shutdown
前端启动存在的问题:
一旦客户端关闭,则redis服务也停掉。
后端启动:
第一步:需要将redis解压之后的源码包中的redis.conf文件拷贝到bin目录下
[root@itheima bin]# cp /root/redis-3.0.0/redis.conf ./
第二步:修改redis.conf文件,将daemonize改为yes
先要使用vim redis.conf
daemonize yes
第三步:使用命令后端启动redis
[root@itheima bin]# ./redis-server redis.conf
第四步:查看是否启动成功
[root@centos7 redis19]# ps -ef|grep redis
root 93451 1 0 20:25 ? 00:00:00 ./bin/redis-server *:6379
root 93482 1929 0 20:25 pts/0 00:00:00 grep --color=auto redis
[root@centos7 redis19]#
[root@centos7 redis19]# ll
total 44
drwxr-xr-x. 2 root root 135 Oct 22 20:22 bin
-rw-r--r--. 1 root root 41404 Oct 22 20:24 redis.conf
[root@centos7 redis19]#
关闭后端启动的方式:
强制关闭:[root@itheima bin]# kill -9 5071
正常关闭:[root@itheima bin]# ./redis-cli shutdown
在项目中,建议使用正常关闭。
因为redis作为缓存来使用的话,将数据存储到内存中,如果使用正常关闭,则会将内存数据持久化到本地之后,再关闭。
如果是强制关闭,则不会进行持久化操作,可能会造成部分数据的丢失。
6,Redis客户端(即登录Redis操作数据)
Redis自带的客户端
启动
启动客户端命令:
[root@itheima bin]# ./redis-cli -h 127.0.0.1 -p 6379
-h:指定访问的redis服务器的ip地址
-p:指定访问的redis服务器的port端口
还可以写成:[root@itheima bin]# ./redis-cli 使用默认配置:默认的ip【127.0.0.1】,默认的port【6379】
关闭
Ctrl+c
127.0.0.1:6379> quit
7,redis 存数据取数据
[root@192 bin]# ./redis-cli -h 127.0.0.1 -p 6379
127.0.0.1:6379>
127.0.0.1:6379> set s1 111
OK
127.0.0.1:6379> get s1
"111"
127.0.0.1:6379>
选择数据库的方式:
使用select 加上数据库的下标 就可以选择指定的数据库来使用,默认下标从0开始
127.0.0.1:6379> select 15
OK
127.0.0.1:6379[15]>
8,Redis持久化方案
1-1,Rdb方式
Redis默认的方式,redis通过快照来将数据持久化到磁盘中。
1-1-1,设置持久化快照的条件
在redis.conf中修改持久化快照的条件,如下:
save 900 1
save 300 10
save 60 10000
900 秒内,数据发生1次操作,就将数据存储起来(save),300秒内,数据发生10次操作。
1-1-2,持久化文件存储的目录
在redis.conf中可以指定持久化文件存储的目录
# The filename where to dump the DB
dbfilename dump.rdb
# The working directory.
#
# The DB will be written inside this directory, with the filename specified
# above using the 'dbfilename' configuration directive.
#
# The Append Only File will also be created inside this directory.
#
# Note that you must specify a directory here, not a file name.
dir ./
1-1-3,Rdb问题
一旦redis非法关闭,那么会丢失最后一次持久化之后的数据。
如果数据不重要,则不必要关心。
如果数据不能允许丢失,那么要使用aof方式。
9,
1-1,Aof方式(操作一次,缓存一次)
Redis默认是不使用该方式持久化的。Aof方式的持久化,是操作一次redis数据库,则将操作的记录存储到aof持久化文件中。
第一步:开启aof方式的持久化方案
将redis.conf中的appendonly改为yes,即开启aof方式的持久化方案。
appendonly yes
Aof文件存储的目录和rdb方式的一样。
Aof文件存储的名称
appendfilename "appendonly.aof"
1-1-1,结论
在使用aof和rdb方式时,如果redis重启,则数据从aof文件加载。