NoSQL数据库之Redis的介绍及安装

前端之家收集整理的这篇文章主要介绍了NoSQL数据库之Redis的介绍及安装前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
较流行的 Nosql数据库Redis数据库 了解过Nosql数据库的童鞋都知道,Nosql更注重的是对


海量数据存取的性能、分布式、 扩展性支持上, 并不需要传统关系数据库的一些特征,例如:

Schema、事务、完整SQL查询支持等等,因此在分布式环境下的性能相对与传统的关系数据库有较大的提升。
而我们的Redis就是Nosql这个大家族中的一份子,它是一个开源的使用ANSI C语言编写、支持网络、可基于内存也可
持久化的日志型、Key-Value数据库注意,Redis同MongoDB,同样是Key-Value数据库),并提供多种语言的API。
我们来看一下Redis官方(http://www.redis.io)的介绍:
Redis is an open source,advancedkey-value store.
Redis是一个开源的,高级的键值式存储数据库


It is often referred to as adata structure serversince keys can containstrings,hashes,lists,setsandsorted sets.
它通常被称为一个数据结构服务器可以包含字符串,哈希表,链表,集合有序集合。



In order to achieve its outstanding performance,Redis works with anin-memorydataset.
为了实现其出色的表现,Redis是工作在内存中的数据集。
Depending on your use case,you can persist it either by dumping the dataset todiskevery once in a while,or by appending each command to a log.
您还可以根据您的具体的使用情况,每隔一段时间或执行命令时将数据集写入到磁盘,并且添加到日志中。
Redis also supports trivial-to-setup master-slave replication,withvery fast non-blockingfirst synchronization,auto-reconnection on net split and so forth.
Redis还支持主从复制,并且配置起来很简单,第一次同步就可以无阻塞的达到极快的速度,在网络断开的时候可以自动重连等等。
Other features include a simple check-and-set mechanism,pub/suband configuration settings to make Redis behavelike a cache.
另外它的简单的检查与设置机制,发布(Publish)与订阅(Subscribe)配置设置的特性使它看起来像是一个缓存
另外我们的Redis数据库同样支持事务处理,这是MongoDB所不能及的,稍后的文章会给大家详细
介绍Redis的7种数据类型及使用方法
那我们哪些地方可以使用到Redis数据库呢?
下面我们来看下Redis的适用场景:
1、取最新N 个数据的操作
比如典型的取你网站的最新文章,通过下面方式,我们可以将最新的5000 条评论的ID 放在Redis 的List 集合中,
并将超出集合部分从数据库获取 @H_502_356@
@H_502_356@2、排行榜应用,取TOP N 操作
这个需求与上面需求的不同之处在于,前面操作以时间为权重,这个是以某个条件为权重,比如按顶的次数排序,
这时候就需要我们的sorted set 出马了,将你要排序的值设置成sortedset 的score,将具体的数据设置成相应的value,
每次只需要执行一条ZADD 命令即可。 @H_502_356@3、需要精准设定过期时间的应用
比如你可以把上面说到的sorted set 的score 值设置成过期时间的时间戳,那么就可以简单地通过过期时间排序,
定时清除过期数据了,不仅是清除Redis 中的过期数据,你完全可以把Redis 里这个过期时间当成是对数据库中数据的索引,
用Redis 来找出哪些数据需要过期删除,然后再精准地从数据库删除相应的记录。
@H_502_356@4、计数器应用
Redis 的命令都是原子性的,你可以轻松地利用INCR,DECR 命令来构建计数器系统。 @H_502_356@5、Uniq 操作,获取某段时间所有数据排重值
这个使用Redis 的set 数据结构最合适了,只需要不断地将数据往set 中扔就行了,set 意为集合,所以会自动排重。

@H_502_356@6、实时系统,反垃圾系统
通过上面说到的set 功能,你可以知道一个终端用户是否进行了某个操作,可以找到其操作的集合并进行分析统计对比等。
没有做不到,只有想不到。 @H_502_356@7、Pub/Sub 构建实时消息系统
Redis 的Pub/Sub 系统可以构建实时的消息系统,比如很多用Pub/Sub 构建的实时聊天系统的例子。 @H_502_356@8、构建队列系统
使用list 可以构建队列系统,使用sorted set 甚至可以构建有优先级的队列系统。 @H_502_356@9、缓存
这个不必说了,性能优于Memcached,数据结构更多样化。


好,下一步我们来将Redis数据库安装好:
第一步,下载Redis,可以通过官网(http://www.redis.io/download)下载最新版本的源代码包:


第二步,将下载好的源代码上传到我们的Linux主机中,并编译安装:
这里我们将redis-2.4.16.tar.gz源码包上传到我们Linux系统的/lamp目录下,然后解压,编译安装,
  1. [root@localhost lamp]# cd /lamp
  2. [root@localhost lamp]# tar zxf redis-2.4.15.tar.gz
  3. [root@localhost lamp]# cd redis-2.4.15
  4. [root@localhost redis-2.4.15]# make
  5. cd src && make all
  6. make[1]: Entering directory `/lamp/redis-2.4.15/src'

第三步,为了便于管理,我们将安装好的Redis移动到/usr/local/redis下
复制代码
[root@localhost redis-2.4.15]# ll
  • 总计 84
  • -rw-rw-r-- 1 root root 12371 06-21 21:04 00-RELEASENOTES
  • -rw-rw-r-- 1 root root55 06-21 21:04 BUGS
  • -rw-rw-r-- 1 root root 671 06-21 21:04 CONTRIBUTING
  • -rw-rw-r-- 1 root root1487 06-21 21:04 COPYING
  • drwxrwxr-x 5 root root4096 06-21 21:04 deps
  • -rw-rw-r-- 1 root root30 06-21 21:04 INSTALL
  • -rw-rw-r-- 1 root root 397 06-21 21:04 Makefile
  • -rw-rw-r-- 1 root root2813 06-21 21:04 README
  • -rw-rw-r-- 1 root root 21094 06-21 21:04 redis.conf
  • -rwxrwxr-x 1 root root 162 06-21 21:04 runtest
  • drwxrwxr-x 2 root root4096 08-01 20:35 src
  • drwxrwxr-x 8 root root4096 06-21 21:04 tests
  • drwxrwxr-x 2 root root4096 06-21 21:04 utils
  • [root@localhost redis-2.4.15]# mkdir /usr/local/redis
  • [root@localhost redis-2.4.15]# mv redis.conf src /usr/local/redis/
  • [root@localhost redis-2.4.15]# cd /usr/local/redis/
  • [root@localhost redis]# ll
  • 总计 28
  • [root@localhost redis]#

  • 第四步,启动redis服务:
    复制代码
    [root@localhost redis]# src/redis-server
  • [7113] 01 Aug 20:44:12 # Warning: no config file specified,using the default config. In order to specify a config file use 'redis-server /path/to/redis.conf'
  • [7113] 01 Aug 20:44:12 # Warning: 32 bit instance detected but no memory limit set. Setting 3.5 GB maxmemory limit with 'noeviction' policy now.
  • [7113] 01 Aug 20:44:12 * Server started,Redis version 2.4.15
  • [7113] 01 Aug 20:44:12 # 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.
  • [7113] 01 Aug 20:44:12 * The server is now ready to accept connections on port 6379
  • [7113] 01 Aug 20:44:12 - 0 clients connected (0 slaves),547464 bytes in use

  • Redis服务器的端口默认是6379,但是你会发现Redis服务会一直占用我们当前登录Linux的SESSION,那能否像MysqL
    或者是MongoDB一样在后台执行Redis进程呢, 当然可以,我们只需要更改Redis的配置文件,并且启动的时候指定配置文件即可!
    如果是一个专业的DBA,那么实例启动时会加很多参数以便是系统运行的非常稳定,这样就可能在启动时
    在Redis后面加一个参数,以指定配置文件的路径,就像MysqL一样的读取启动配置文件的方式来启动数据库
    我们来看Redis的配置文件redis.conf里面都有什么:
    复制代码
    #是否作为守护进程运行
  • daemonize yes
  • #配置 pid 的存放路径及文件名,默认为当前路径下
  • pidfile redis.pid
  • #Redis 默认监听端口
  • port 6379
  • #客户端闲置多少秒后,断开连接
  • timeout 300
  • #日志显示级别
  • loglevel verbose
  • #指定日志输出文件名,也可指定到标准输出端口
  • logfile stdout
  • #设置数据库数量,默认连接的数据库是 0,可以通过 select N 来连接不同的数据库
  • databases 16
  • #保存数据到 disk 的策略
  • #当有一条 Keys 数据被改变是,900 秒刷新到 disk 一次
  • save 900 1
  • #当有 10 条 Keys 数据被改变时,300 秒刷新到 disk 一次
  • save 300 10
  • #当有 1w 条 keys 数据被改变时,60 秒刷新到 disk 一次
  • save 60 10000
  • #当 dump .rdb 数据库的时候是否压缩数据对象
  • rdbcompression yes
  • #dump 数据库的数据保存的文件
  • dbfilename dump.rdb
  • #Redis 的工作目录
  • dir /home/falcon/redis-2.0.0/
  • ########### Replication #####################
  • #Redis 的复制配置
  • # slaveof <masterip> <masterport>
  • # masterauth <master-password>
  • ############## SECURITY ###########
  • # requirepass foobared
  • ############### LIMITS ##############
  • #最大客户端连接数
  • # maxclients 128
  • #最大内存使用率
  • # maxmemory <bytes>
  • ########## APPEND ONLY MODE #########
  • #是否开启日志功能
  • appendonly no
  • # 刷新日志到 disk 的规则
  • # appendfsync always
  • appendfsync everysec
  • # appendfsync no
  • ################ VIRTUAL MEMORY ###########
  • #是否开启 VM 功能
  • vm-enabled no
  • # vm-enabled yes
  • vm-swap-file logs/redis.swap
  • vm-max-memory 0
  • vm-page-size 32
  • vm-pages 134217728
  • vm-max-threads 4
  • ############# ADVANCED CONFIG ###############
  • glueoutputbuf yes
  • hash-max-zipmap-entries 64
  • hash-max-zipmap-value 512
  • #是否重置 Hash 表
  • activerehashing yes

  • 大家可以看到第一条就是启动后台进程启动Redis服务,将其设置为yes,就会在后台运行Redis服务啦!


    我们启动的时候来指定redis的配置文件 @H_502_356@/usr/local/redis/bin/redis-server/usr/local/redis/redis.conf

    猜你在找的NoSQL相关文章