001、Redis环境部署实战
本文最后更新于 66 天前,其中的信息可能已经过时,如有错误请发送邮件到wuxianglongblog@163.com

Redis环境部署实战

一.NoSQL(Not Olny SQL)企业缓存产品介绍

    早期解决数据的存储的主流是传统的关系型数据库,典型的代表是Oracle,MySQL,PgsSQL等。

    后来随着数据量的井喷式增长,关系型数据库在PB,EB级别的存储上显得力不从心,于是出现了"关系型数据库 + NoSQL数据库"逐渐成为主流,尤其是在大数据领域,NoSQL数据库应用非常广泛。

    NoSQL通常被细分为一下几类数据库:
        缓存数据库:
            主要解决高并发读的数据库。代表产品Redis,Memcached,Tair(淘宝公司基于memcached二次研发的产品),vainish等。
        文档类数据库:
            MongoDB,Elasticsearch等。
        列存储数据库:
            HBase,OpenTSDB,Kudu等。
        图形存储数据库:
            Neo4j,FlockDB,AllegroGrap,GraphDB,InfiniteGraph,HugeGraph等。

    温馨提示:
        (1)对于数据一致性要求比较高的,我们还是会使用关系型数据库,对于数据一致性追求不是特别高或者对于性能追求高的通常会选择非关系数据库。但在实际生产环境中,我们通常会将关系型和非关系型数据库搭配一起使用。
        (2)如果说传统关系型数据库是第一代数据库架构,"传统关系型数据库 + 非关系数据库"为第二个数据库架构, 那么第三代数据架构则为"NewSQL",这种数据库可以将关系型和非关系型数据库功能揉在一起使用,据说阿里公司已经在做这样的数据库了;

1.Memcached介绍

    优点:
        高性能读写,单一数据类型,支持客户端分布式集群,一致性哈希,多核结构,多线程读写性能高。

    缺点:
        无持久化,节点故障可能出现缓存穿透,分布式需要客户端实现,跨机房数据同步困难,架构扩容复杂度高。

    应用场景:
        多核的缓存服务,更加适合于用户并发访问次数较少的应用场景。换句话说,适合多用户访问,每个用户少量的读写。

2.Redis

    优点:
        高性能读写,多数据类型支持,数据持久化,高可用架构,支持自定义虚拟内存,支持分布式分片集群,单线程读写性能极高。

    缺点:
        多线程读写较Memcached慢。所以更加适合单机多实例的环境。

    应用场景:
        单核的缓存服务,单节点下,更加适合于少量用户,多次访问的应用场景。换句话说,适合少用户访问,每个用户大量的读写。
        Redis一般是单机多实例架构,配合Redis集群出现。目前新浪微博,京东,直播类平台,游戏类等公司都会用到该缓存产品。

3.Tair

    优点:
        高性能读写,支持三种存储引擎(DDB,RDB,LDB),支持高可用,支持分布式分片集群。

    缺点:
        单机情况下,读写性能较Redis,Memcached较慢。

    应用场景:
        几乎所有淘宝业务的缓存。

4.vainish

    varnish就是一款开源轻量级的HTTP缓存服务,它提供了强大的缓存功能。

    varnish同时也是是一个轻量级的Cache和反向代理软件,varnish主要有以下几个特性:
        (1)基于内存进行缓存,重启后数据将消失(也可基于文件进行缓存);
        (2)能够精确的设置缓存时长;
        (3)具有强大的缓存管理功能;
        (4)可以根据导入Director模块,实现负载均衡;

5.Redis功能介绍

    (1)数据类型丰富;
    (2)支持持久化;
    (3)多种内存分配及回收策略;
    (4)支持事务;
    (5)消息队列,消息订阅;
    (6)支持高可用;
    (7)支持分布式分配集群;
    (8)缓存穿透/雪崩;
    (9)Redis API;

二.部署Redis环境

1.Redis概述

    Redis是一个开源BSD许可的,内存中的数据结构存储系统,它可以用作数据库、缓存和消息中间件。

    它支持多种类型的数据结构,如字符串(strings),散列(hashes), 列表(lists), 集合(sets), 有序集合(sorted sets)与范围查询,bitmaps,hyperloglogs和地理空间(geospatial)索引半径查询。 

    Redis内置了复制(replication),LUA脚本(Lua scripting), LRU驱动事件(LRU eviction),事务(transactions)和不同级别的磁盘持久化(persistence), 并通过Redis哨兵(Sentinel)和自动分区(Cluster)提供高可用性(high availability)。

    官方文档:
        https://redis.io/

    Redis中文网站:
        http://redis.cn/

    github地址:
        https://github.com/redis/redis

2.下载Redis软件包

    官网下载地址:
        https://download.redis.io/releases

    GitHub下载地址:
        https://github.com/redis/redis/releases

    温馨提示:
        (1)截至目前2021-02-25日为止,Redis主流的社区版本依旧是3系列,尽管Redis最新稳定版本为"redis-6.2.0.tar.gz",但我们实验使用的依旧是2019年3月18日发布的"redis-3.2.13.tar.gz"版本。
        (2)如果生产环境中,的确想要使用最新版本的Redis 6系列,建议先查看其新特性,考虑是否有必要使用这些新特性。如果没有必要使用新版本的新特性可以暂时先不考虑迁移版本,尤其是已经在生产环境中已经运行的业务。

3.解压软件包

[root@redis201.oldboyedu.com ~]# ll
总用量 1528
-rw-r--r-- 1 root root 1564576 2月  25 18:50 redis-3.2.13.tar.gz
[root@redis201.oldboyedu.com ~]# 
[root@redis201.oldboyedu.com ~]# tar xf redis-3.2.13.tar.gz -C /oldboyedu/softwares/
[root@redis201.oldboyedu.com ~]# 
[root@redis201.oldboyedu.com ~]# ll /oldboyedu/softwares/redis-3.2.13/
总用量 204
-rw-rw-r--  1 root root 93478 3月  19 2019 00-RELEASENOTES
-rw-rw-r--  1 root root    53 3月  19 2019 BUGS
-rw-rw-r--  1 root root  1805 3月  19 2019 CONTRIBUTING
-rw-rw-r--  1 root root  1487 3月  19 2019 COPYING
drwxrwxr-x  7 root root   143 3月  19 2019 deps
-rw-rw-r--  1 root root    11 3月  19 2019 INSTALL
-rw-rw-r--  1 root root   151 3月  19 2019 Makefile
-rw-rw-r--  1 root root  4223 3月  19 2019 MANIFESTO
-rw-rw-r--  1 root root  6834 3月  19 2019 README.md
-rw-rw-r--  1 root root 46695 3月  19 2019 redis.conf
-rwxrwxr-x  1 root root   271 3月  19 2019 runtest
-rwxrwxr-x  1 root root   280 3月  19 2019 runtest-cluster
-rwxrwxr-x  1 root root   281 3月  19 2019 runtest-sentinel
-rw-rw-r--  1 root root  7921 3月  19 2019 sentinel.conf
drwxrwxr-x  2 root root  4096 3月  19 2019 src
drwxrwxr-x 10 root root   167 3月  19 2019 tests
drwxrwxr-x  7 root root  4096 3月  19 2019 utils
[root@redis201.oldboyedu.com ~]# 

4.创建符号链接

[root@redis201.oldboyedu.com ~]# ln -sv /oldboyedu/softwares/redis-3.2.13 /oldboyedu/softwares/redis
"/oldboyedu/softwares/redis" -> "/oldboyedu/softwares/redis-3.2.13"
[root@redis201.oldboyedu.com ~]# 
[root@redis201.oldboyedu.com ~]# ll /oldboyedu/softwares/ | grep redis
lrwxrwxrwx 1 root  root   35 2月  25 19:03 redis -> /oldboyedu/softwares/redis-3.2.13
drwxrwxr-x 6 root  root  309 3月  19 2019 redis-3.2.13
[root@redis201.oldboyedu.com ~]# 

5.编译Redis源码

    (1)安装依赖包
        [root@redis201.oldboyedu.com ~]# yum -y install gcc automake autoconf libtool make

    (2)编译Redis源码
        [root@redis201.oldboyedu.com ~]# cd /oldboyedu/softwares/redis
        [root@redis201.oldboyedu.com /oldboyedu/softwares/redis]# 
        [root@redis201.oldboyedu.com /oldboyedu/softwares/redis]# make -j 4  # 根据你的core数量指定对应的数字即可,此过程可能需要等待10秒钟左右。

        ...

            CC sparkline.o
            CC redis-check-rdb.o
            CC geo.o
            LINK redis-server
            INSTALL redis-sentinel
            CC redis-cli.o
            LINK redis-cli
            CC redis-benchmark.o
            LINK redis-benchmark
            INSTALL redis-check-rdb
            CC redis-check-aof.o
            LINK redis-check-aof

        Hint: It's a good idea to run 'make test' ;)

        make[1]: 离开目录“/oldboyedu/softwares/redis-3.2.13/src”
        [root@redis201.oldboyedu.com /oldboyedu/softwares/redis]# 

6.配置环境变量并测试

    (1)为Redis环境单独配置环境变量:
        [root@redis201.oldboyedu.com ~]# vim /etc/profile.d/redis.sh
        [root@redis201.oldboyedu.com ~]# 
        [root@redis201.oldboyedu.com ~]# cat /etc/profile.d/redis.sh
        #!/bin/bash

        export PATH=/oldboyedu/softwares/redis/src:$PATH
        [root@redis201.oldboyedu.com ~]# 

    (2)使得上一步配置的Redis环境变量生效
        [root@redis201.oldboyedu.com ~]# source /etc/profile.d/redis.sh 
        [root@redis201.oldboyedu.com ~]# 
        [root@redis201.oldboyedu.com ~]# redis-cli --version  # 如果输出该命令有提示信息说明配置生效!
        redis-cli 3.2.13
        [root@redis201.oldboyedu.com ~]# 

三.启动Redis并进行连接测试

1.后台运行Redis服务

[root@redis201.oldboyedu.com ~]# ss -ntl
State       Recv-Q Send-Q                                                 Local Address:Port                                                                Peer Address:Port              
LISTEN      0      128                                                                *:22                                                                             *:*                  
LISTEN      0      128                                                             [::]:22                                                                          [::]:*                  
[root@redis201.oldboyedu.com ~]# 
[root@redis201.oldboyedu.com ~]# redis-server &
[1] 13125
13125:C 25 Feb 19:14:35.866 # Warning: no config file specified, using the default config. In order to specify a config file use redis-server /path/to/redis.conf
13125:M 25 Feb 19:14:35.867 * Increased maximum number of open files to 10032 (it was originally set to 1024).
                _._                                                  
           _.-``__ ''-._                                             
      _.-``    `.  `_.  ''-._           Redis 3.2.13 (00000000/0) 64 bit
  .-`` .-```.  ```\/    _.,_ ''-._                                   
 (    '      ,       .-`  | `,    )     Running in standalone mode
 |`-._`-...-` __...-.``-._|'` _.-'|     Port: 6379
 |    `-._   `._    /     _.-'    |     PID: 13125
  `-._    `-._  `-./  _.-'    _.-'                                   
 |`-._`-._    `-.__.-'    _.-'_.-'|                                  
 |    `-._`-._        _.-'_.-'    |           http://redis.io        
  `-._    `-._`-.__.-'_.-'    _.-'                                   
 |`-._`-._    `-.__.-'    _.-'_.-'|                                  
 |    `-._`-._        _.-'_.-'    |                                  
  `-._    `-._`-.__.-'_.-'    _.-'                                   
      `-._    `-.__.-'    _.-'                                       
          `-._        _.-'                                           
              `-.__.-'                                               

13125:M 25 Feb 19:14:35.867 # WARNING: The TCP backlog setting of 511 cannot be enforced because /proc/sys/net/core/somaxconn is set to the lower value of 128.
13125:M 25 Feb 19:14:35.868 # Server started, Redis version 3.2.13
13125:M 25 Feb 19:14:35.868 # 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.con
f and then reboot or run the command 'sysctl vm.overcommit_memory=1' for this to take effect.13125:M 25 Feb 19:14:35.868 # 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 ru
n 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.13125:M 25 Feb 19:14:35.868 * The server is now ready to accept connections on port 6379
[root@redis201.oldboyedu.com ~]# 
[root@redis201.oldboyedu.com ~]# ss -ntl
State       Recv-Q Send-Q                                                 Local Address:Port                                                                Peer Address:Port              
LISTEN      0      128                                                                *:6379                                                                           *:*                  
LISTEN      0      128                                                                *:22                                                                             *:*                  
LISTEN      0      128                                                             [::]:6379                                                                        [::]:*                  
LISTEN      0      128                                                             [::]:22                                                                          [::]:*                  
[root@redis201.oldboyedu.com ~]# 

2.连接Redis数据库

    (1)默认以字节的形式显示中文:
        [root@redis201.oldboyedu.com ~]# redis-cli 
        127.0.0.1:6379> SET name "Jason Yin"
        OK
        127.0.0.1:6379> 
        127.0.0.1:6379> GET name
        "Jason Yin"
        127.0.0.1:6379> 
        127.0.0.1:6379> SET myname "尹正杰"
        OK
        127.0.0.1:6379> 
        127.0.0.1:6379> GET myname
        "\xe5\xb0\xb9\xe6\xad\xa3\xe6\x9d\xb0"
        127.0.0.1:6379> 
        127.0.0.1:6379> QUIT 
        [root@redis201.oldboyedu.com ~]# 

    (2)我们可以直接让redis-cli命令行工具支持中文显示,只需启动时添加"--raw"选项即可,如下所示:
        [root@redis201.oldboyedu.com ~]# redis-cli --raw
        127.0.0.1:6379> 
        127.0.0.1:6379> GET name
        Jason Yin
        127.0.0.1:6379> 
        127.0.0.1:6379> GET myname
        尹正杰
        127.0.0.1:6379> 
        127.0.0.1:6379> QUIT
        [root@redis201.oldboyedu.com ~]# 

    (3)我们也可以像mysql命令行工具一样,可以直接在命令行操作redis服务,而无需进入到交互式字符界面:
        [root@redis201.oldboyedu.com ~]# redis-cli get name
        "Jason Yin"
        [root@redis201.oldboyedu.com ~]# 
        [root@redis201.oldboyedu.com ~]# redis-cli get myname
        "\xe5\xb0\xb9\xe6\xad\xa3\xe6\x9d\xb0"
        [root@redis201.oldboyedu.com ~]# 
        [root@redis201.oldboyedu.com ~]# redis-cli --raw get myname
        尹正杰
        [root@redis201.oldboyedu.com ~]# 
        [root@redis201.oldboyedu.com ~]# redis-cli set age 18
        OK
        [root@redis201.oldboyedu.com ~]# 
        [root@redis201.oldboyedu.com ~]# redis-cli get age
        "18"
        [root@redis201.oldboyedu.com ~]# 
        [root@redis201.oldboyedu.com ~]# 

3.关闭Redis

    (1)进入redis-cli交互式字符界面关闭Redis:
        [root@redis201.oldboyedu.com ~]# ss -ntl
        State       Recv-Q Send-Q                                                 Local Address:Port                                                                Peer Address:Port              
        LISTEN      0      128                                                                *:6379                                                                           *:*                  
        LISTEN      0      128                                                                *:22                                                                             *:*                  
        LISTEN      0      128                                                             [::]:6379                                                                        [::]:*                  
        LISTEN      0      128                                                             [::]:22                                                                          [::]:*                  
        [root@redis201.oldboyedu.com ~]# 
        [root@redis201.oldboyedu.com ~]# 
        [root@redis201.oldboyedu.com ~]# redis-cli 
        127.0.0.1:6379> SHUTDOWN
        13125:M 25 Feb 19:27:00.343 # User requested shutdown...
        13125:M 25 Feb 19:27:00.343 * Saving the final RDB snapshot before exiting.
        13125:M 25 Feb 19:27:00.344 * DB saved on disk
        13125:M 25 Feb 19:27:00.344 # Redis is now ready to exit, bye bye...
        not connected> 
        not connected> quit
        [root@redis201.oldboyedu.com ~]# 
        [root@redis201.oldboyedu.com ~]# ss -ntl
        State       Recv-Q Send-Q                                                 Local Address:Port                                                                Peer Address:Port              
        LISTEN      0      128                                                                *:22                                                                             *:*                  
        LISTEN      0      128                                                             [::]:22                                                                          [::]:*                  
        [root@redis201.oldboyedu.com ~]# 

    (2)直接在redis-cli字符界面安装Redis:
        [root@redis201.oldboyedu.com ~]# ss -ntl
        State       Recv-Q Send-Q                                                 Local Address:Port                                                                Peer Address:Port              
        LISTEN      0      128                                                                *:6379                                                                           *:*                  
        LISTEN      0      128                                                                *:22                                                                             *:*                  
        LISTEN      0      128                                                             [::]:6379                                                                        [::]:*                  
        LISTEN      0      128                                                             [::]:22                                                                          [::]:*                  
        [root@redis201.oldboyedu.com ~]# 
        [root@redis201.oldboyedu.com ~]# 
        [root@redis201.oldboyedu.com ~]# redis-cli shutdown
        14154:M 25 Feb 19:29:07.636 # User requested shutdown...
        14154:M 25 Feb 19:29:07.636 * Saving the final RDB snapshot before exiting.
        14154:M 25 Feb 19:29:07.638 * DB saved on disk
        14154:M 25 Feb 19:29:07.638 # Redis is now ready to exit, bye bye...
        [1]+  完成                  redis-server
        [root@redis201.oldboyedu.com ~]# 
        [root@redis201.oldboyedu.com ~]# 
        [root@redis201.oldboyedu.com ~]# ss -ntl
        State       Recv-Q Send-Q                                                 Local Address:Port                                                                Peer Address:Port              
        LISTEN      0      128                                                                *:22                                                                             *:*                  
        LISTEN      0      128                                                             [::]:22                                                                          [::]:*                  
        [root@redis201.oldboyedu.com ~]# 
谨此笔记,记录过往。凭君阅览,如能收益,莫大奢望。
暂无评论

发送评论 编辑评论


				
|´・ω・)ノ
ヾ(≧∇≦*)ゝ
(☆ω☆)
(╯‵□′)╯︵┴─┴
 ̄﹃ ̄
(/ω\)
∠( ᐛ 」∠)_
(๑•̀ㅁ•́ฅ)
→_→
୧(๑•̀⌄•́๑)૭
٩(ˊᗜˋ*)و
(ノ°ο°)ノ
(´இ皿இ`)
⌇●﹏●⌇
(ฅ´ω`ฅ)
(╯°A°)╯︵○○○
φ( ̄∇ ̄o)
ヾ(´・ ・`。)ノ"
( ง ᵒ̌皿ᵒ̌)ง⁼³₌₃
(ó﹏ò。)
Σ(っ °Д °;)っ
( ,,´・ω・)ノ"(´っω・`。)
╮(╯▽╰)╭
o(*////▽////*)q
>﹏<
( ๑´•ω•) "(ㆆᴗㆆ)
😂
😀
😅
😊
🙂
🙃
😌
😍
😘
😜
😝
😏
😒
🙄
😳
😡
😔
😫
😱
😭
💩
👻
🙌
🖕
👍
👫
👬
👭
🌚
🌝
🙈
💊
😶
🙏
🍦
🍉
😣
Source: github.com/k4yt3x/flowerhd
颜文字
Emoji
小恐龙
花!
上一篇
下一篇