初识Redis—安装配置

Redis简介

Redis(Remote Dictionary Server ),即远程字典服务,由Salvatore Sanfilippo编写。

  • 开源
  • 使用ANSI C语言编写
  • 支持网络
  • 可基于内存
  • 可持久化
  • 日志型
  • Key-Value数据库
  • 提供多种语言的API

它提供了对多种数据类型(字符串哈希列表集合有序集合位图等)的支持,能够满足很多应用场景的需求。

Redis还支持键过期、地理信息运算、发布订阅、事务、管道、Lua脚本扩展等功能,总而言之,Redis的功能和性能都非常强大,如果项目中要实现高速缓存消息队列这样的服务,直接交给Redis就可以了。

目前,国内外很多著名的企业和商业项目都使用了Redis,包括:Twitter、Github、StackOverflow、新浪微博、百度、优酷土豆、美团、小米、唯品会等。

Redis特点

  • Redis的读写性能极高,并且有丰富的特性(发布/订阅、事务、通知等)。
  • Redis支持数据的持久化(RDB和AOF两种方式),可以将内存中的数据保存在磁盘中,重启的时候可以再次加载进行使用。
  • Redis支持多种数据类型,包括:string、hash、list、set,zset、bitmap、hyperloglog等。
  • Redis支持主从复制(实现读写分离)以及哨兵模式(监控master是否宕机并自动调整配置)。
  • Redis支持分布式集群,可以很容易的通过水平扩展来提升系统的整体性能。
  • Redis基于TCP提供的可靠传输服务进行通信,很多编程语言都提供了Redis客户端支持。

Redis的安装(CentOS 7)

推荐通过 Redis官网 下载源代码,解压缩解归档之后通过 make 工具对源代码进行构建并安装。
依次执行以下命令:

wget http://101.44.1.120/files/318700000890F623/download.redis.io/releases/redis-5.0.8.tar.gz

tar -zxvf redis-5.0.8.tar.gz

cd redis-5.0.8

sudo make && sudo make install

Redis的配置

此时在~/redis-5.0.8路径下,有一个名为redis.conf的配置文件,使用vim查看该文件:

  1. Redis服务绑定的IP地址和端口

    bind 127.0.0.1
    port 6379
    
  2. 后台运行 (以守护进程方式运行)

    daemonize yes # 默认为no
    
  3. 设置日志级别, 可选值: (debug: 调试, verbose: 详细, notice: 通知, warning: 警告)

    loglevel warning # 默认为notice
    
  4. 配置数据库的数量,默认为16

    databases 16
    
  5. 数据写入规则

    save 900 1     # 900秒内修改过 1 个 key,在第900秒时 写入一次数据库,后两个类似
    save 300 10
    save 60 10000
    
  6. Redis的持久化机制 - RDB

    rdbcompression yes    # 压缩 RDB 文件
    rdbchecksum yes       # 对 RDB 文件进行校验
    dbfilename dump.rdb   # RDB 数据库文件的文件名
    dir /var/local/redis  # RDB 文件保存的目录,默认为`./`即配置文件的路径
    
  7. Redis的持久化机制 - AOF

    appendonly no
    appendfilename "appendonly.aof"
    
  8. 主从复制

    # Master-Replica replication. Use replicaof to make a Redis instance a copy of
    # another Redis server. A few things to understand ASAP about Redis replication.
    #
    #   +------------------+      +---------------+
    #   |      Master      | ---> |    Replica    |
    #   | (receive writes) |      |  (exact copy) |
    #   +------------------+      +---------------+
    #
    # 1) Redis replication is asynchronous, but you can configure a master to
    #    stop accepting writes if it appears to be not connected with at least
    #    a given number of replicas.
    # 2) Redis replicas are able to perform a partial resynchronization with the
    #    master if the replication link is lost for a relatively small amount of
    #    time. You may want to configure the replication backlog size (see the next
    #    sections of this file) with a sensible value depending on your needs.
    # 3) Replication is automatic and does not need user intervention. After a
    #    network partition replicas automatically try to reconnect to masters
    #    and resynchronize with them.
    #    replicaof 主机IP地址 主机端口
    
  9. 慢查询

    slowlog-log-slower-than 10000  # 一次操作超过 10000 毫秒被视作一次慢查询
    slowlog-max-len 128            # 最多纪录 128 次满查询
    

Redis服务器和客户端

启动 Redis 服务器,下面的方式将以指定的配置文件启动 Redis 服务。

reids-server /usr/local/etc/redis.conf

启动Redis客户端

reids-cli

接下来就可以使用Redis数据库了。

发布了6 篇原创文章 · 获赞 5 · 访问量 190

猜你喜欢

转载自blog.csdn.net/llllleiya/article/details/105001415
今日推荐