• NoSQL数据库redis


    1、RDB和AOF的优缺点

     关于RDB:

    #优点:
    ·RDB快照保存了某个时间点的数据,可以通过脚本执行redis指令bgsave(非阻塞,后台执行)或者save(会阻塞写操作,不推荐)命令自定义时间点备份,可以保留多个备份,当出现问题可以恢复到不同时间点的版本,很适合备份,并且此文件格式也支持有不少第三方工具可以进行后续的数据分析比如: 可以在最近的24小时内,每小时备份一次RDB文件,并且在每个月的每一天,也备份一个ROB文件。这样的话,即使遇上问题,也可以随时将数据集还原到不同的版本。
    ·RDB可以最大化Redis的性能,父进程在保存 RDB文件时唯一要做的就是fork出一个子进程,然后这个子进程就会处理接下来的所有保存工作,父进程无须执行任何磁盘工/0操作。
    ·RDB在大量数据,比如几个G的数据,恢复的速度比AOF的快

    #缺点
    ·不能实时保存数据,可能会丢失自上一次执行RDB备份到当前的内存数据 如果你需要尽量避免在服务器故障时丢失数据,那么RDB不适合你。虽然Redis允许你设置不同的保存点(save point)来控制保存RDB文件的频率,但是,因为ROB文件需要保存整个数据集的状态,所以它并不是一个轻松的操作。因此你可能会至少5分钟才保存一次RDB文件。在这种情况
    下,一旦发生故障停机,你就可能会丢失好几分钟的数据。
    ·当数据量非常大的时候,从父进程fork子进程进行保存至RDB文件时需要一点时间,可能是毫秒或者秒,取决于磁盘IO性能在数据集比较庞大时,fork()可能会非常耗时,造成服务器在一定时间内停止处理客户端﹔如果数据集非常巨大,并且CPU时间非常紧张的话,那么这种停止时间甚至可能会长达整整一秒或更久。虽然 AOF重写也需要进行fork(),但无论AOF重写的执行间隔有多长,数据的持久性都不会有任何损失。

    关于AOF

    #优点
    ·数据安全性相对较高,根据所使用的fsync策略(fsync是同步内存中redis所有已经修改的文件到存储设备),默认是appendfsync everysec,即每秒执行一次 fsync,在这种配置下,Redis 仍然可以保持良好的性能,并且就算发生故障停机,也最多只会丢失一秒钟的数据( fsync会在后台线程执行,所以主线程可以继续努力地处理命令请求)
    ·由于该机制对日志文件的写入操作采用的是append模式,因此在写入过程中不需要seek, 即使出现宕机现象,也不会破坏日志文件中已经存在的内容。然而如果本次操作只是写入了一半数据就出现了系统崩溃问题,不用担心,在Redis下一次启动之前,可以通过 redis-check-aof 工具来解决数据一致性的问题
    ·Redis可以在 AOF文件体积变得过大时,自动地在后台对AOF进行重写,重写后的新AOF文件包含了恢复当前数据集所需的最小命令集合。整个重写操作是绝对安全的,因为Redis在创建新 AOF文件的过程中,append模式不断的将修改数据追加到现有的 AOF文件里面,即使重写过程中发生停机,现有的 AOF文件也不会丢失。而一旦新AOF文件创建完毕,Redis就会从旧AOF文件切换到新AOF文件,并开始对新AOF文件进行追加操作。
    ·AOF包含一个格式清晰、易于理解的日志文件用于记录所有的修改操作。事实上,也可以通过该文件完成数据的重建。AOF文件有序地保存了对数据库执行的所有写入操作,这些写入操作以Redis协议的格式保存,因此 AOF文件的内容非常容易被人读懂,对文件进行分析(parse)也很轻松。导出(export)AOF文件也非常简单:举个例子,如果你不小心执行了FLUSHALL.命令,但只要AOF文件未被重写,那么只要停止服务器,移除 AOF文件末尾的FLUSHAL命令,并重启Redis ,就可以将数据集恢复到FLUSHALL执行之前的状态。
    #缺点
    ·即使有些操作是重复的也会全部记录,AOF 的文件大小要大于 RDB 格式的文件
    ·AOF 在恢复大数据集时的速度比 RDB 的恢复速度要慢
    ·根据fsync策略不同,AOF速度可能会慢于RDB
    ·bug 出现的可能性更多

    2、master和slave同步过程

     

    #在从服务器启用主从同步
    [root@slave ~]# redis-cli replicaof 10.0.0.8 6379
    OK
    #若主节点是有密码,需要执行设置密码命令
    [root@slave ~]# redis-cli config set masterauth 123456 #123456即主节点密码

    3、哨兵的使用和实现机制

    客户端通过哨兵间接连接redis服务器端
    Redis sentinel节点与普通redis没有区别,要实现读写分离依赖于客户端程序
    哨兵搭建好之后只能登陆查看哨兵信息状态,不可修改数据库内容

    实现机制

    首先实现主从复制,在主从复制的基础上搭建哨兵

    #三个哨兵配置文件内容相同
    [root@master ~]# vim /etc/redis-sentinel.conf 
    logfile "/var/log/redis/sentinel.log"  #修改log日志路径
    sentinel monitor mymaster 10.0.0.8 6379 2  #指定当前mymaster集群中master服务器,2为法定人数限制(quorum),即有几个sentinel认为主服务器down了就进行故障转移,一般此值是所有sentinel节点(一般总数是>=3的奇数,如3,5,7等)的一半以上的整数值,比如sentinel的总数是3,即3/2=1.5,取整为2,即master的ODOWN客观下线的依据
    sentinel auth-pass mymaster 1123  #mymaster集群中master的密码,注意此行要在上边行的下面
    sentinel down-after-milliseconds mymaster 3000  #(SDOWN)判断mymaster集群中所有节点的主观下线的时间,单位:毫秒,建议3000
    sentinel parallel-syncs mymaster 1   #发生故障转移后,同时向新master同步数据的slave数量,数字越小总同步时间越长,但可以减轻新master的负载压力
    sentinel failover-timeout mymaster 180000  #所有slaves指向新的master所需要的时间,单位:毫秒
    sentinel deny-scripts-reconfig yes  #禁止修改脚本
    [root@master ~]# grep -Ev "^$|^#" /etc/redis-sentinel.conf
    bind 0.0.0.0
    port 26379
    daemonize yes
    pidfile /var/run/redis-sentinel.pid
    dir /tmp
    sentinel monitor mymaster 10.0.0.8 6379 2
     sentinel auth-pass mymaster 1123
    sentinel down-after-milliseconds mymaster 3000
    sentinel parallel-syncs mymaster 1
    sentinel failover-timeout mymaster 180000
    sentinel deny-scripts-reconfig yes
    logfile /var/log/redis/sentinel.log
    
    #编辑slave1的哨兵配置文件
    [root@slave ~]# vim /etc/redis-sentinel.conf 
    [root@slave ~]# grep -Ev "^$|^#" /etc/redis-sentinel.conf
    bind 0.0.0.0
    port 26379
    daemonize yes
    pidfile /var/run/redis-sentinel.pid
    logfile "/var/log/redis/sentinel.log"
    dir /tmp
    sentinel monitor mymaster 10.0.0.8 6379 2
    sentinel auth-pass mymaster 1123
    sentinel down-after-milliseconds mymaster 3000
    sentinel parallel-syncs mymaster 1
    sentinel failover-timeout mymaster 180000
    sentinel deny-scripts-reconfig yes
    logfile /var/log/redis/sentinel.log
    
    [root@slave2 ~]# grep -Ev "^$|^#" /etc/redis-sentinel.conf
    bind 0.0.0.0
    port 26379
    daemonize yes
    pidfile /var/run/redis-sentinel.pid
    logfile ""
    dir /tmp
    sentinel monitor mymaster 10.0.0.8 6379 2
    sentinel auth-pass mymaster 1123
    sentinel down-after-milliseconds mymaster 3000
    sentinel parallel-syncs mymaster 1
    sentinel failover-timeout mymaster 180000
    sentinel deny-scripts-reconfig yes
    logfile /var/log/redis/sentinel.log

    #将哨兵服务进行启动
    [root@master ~]# systemctl enable --now redis-sentinel
    [root@slave ~]# systemctl enable --now redis-sentinel
    [root@slave2 ~]# systemctl enable --now redis-sentinel
    #在启动服务之后配置文件会自动生成下方内容,以下内容无需修改
    sentinel myid 8dfec4bcb770d9b7db958f9893c9b06e239e6bf9
    # Generated by CONFIG REWRITE
    protected-mode no
    supervised systemd
    sentinel leader-epoch mymaster 0
    sentinel known-replica mymaster 10.0.0.28 6379
    sentinel known-replica mymaster 10.0.0.18 6379
    sentinel known-sentinel mymaster 10.0.0.28 26379 db9410fd7a1a21e4ffe005f381f9cfa1bdab8669
    sentinel known-sentinel mymaster 10.0.0.18 26379 d35b6a2b224bb34e5ff49232c9f306b14affa6b8
    sentinel current-epoch 0
    #其中三个哨兵的配置文件中各自的myid必须保证唯一,如果myid相同会导致master只能识别一个哨兵
    [root@master ~]# grep "sentinel myid" /etc/redis-sentinel.conf
    sentinel myid 8dfec4bcb770d9b7db958f9893c9b06e239e6bf9
    [root@slave ~]# grep "sentinel myid" /etc/redis-sentinel.conf
    sentinel myid d35b6a2b224bb34e5ff49232c9f306b14affa6b8
    [root@slave2 ~]# grep "sentinel myid" /etc/redis-sentinel.conf
    sentinel myid db9410fd7a1a21e4ffe005f381f9cfa1bdab8669

    #如果是编译安装,启动服务通过调用配置文件进行启动服务,执行以下命令
    /apps/redis/bin/redis-sentinel /apps/redis/etc/sentinel.conf

    4、redis cluster集群创建和使用

    #在准备6台主机的情况下安装redis服务,并修改了配置文件开启了cluster的环境下
    #创建集群
    [root@centos8 ~]# redis-cli -a 1123 --cluster create 10.0.0.8:6379   10.0.0.18:6379   10.0.0.28:6379   10.0.0.38:6379   10.0.0.48:6379   10.0.0.58:6379 --cluster-replicas 1
    #模拟master故障,对应的slave节点自动提升为新的master
    #若是破坏单个master,对应的slave会变为master并继承其槽位
    #若是一组主从服务器同时down掉,想要get查找数据是查找不到的,没有数据显示,创建key也会报错,无法实现任何操作,其他节点仍然可以写入数据不影响,将全部down掉的主从服务器恢复服务,只能先恢复主服务器才能实现数据恢复,只启动服务器无法实现备份主服务器的数据,若只启动从服务器无法查看key值
    #将18主机down掉,需要相应的数秒故障转移时间
    [root@centos8 ~]# redis-cli -a 1123 -h 10.0.0.18 shutdown
    Warning: Using a password with '-a' or '-u' option on the command line interface may not be safe.
    #在18主机查看日志
    [root@centos8 ~]# tail /var/log/redis/redis.log 
    969:M 26 Oct 2020 15:38:38.228 # User requested shutdown...
    969:M 26 Oct 2020 15:38:38.228 * Saving the final RDB snapshot before exiting.
    969:M 26 Oct 2020 15:38:38.231 * DB saved on disk
    969:M 26 Oct 2020 15:38:38.231 * Removing the pid file.
    969:M 26 Oct 2020 15:38:38.231 # Redis is now ready to exit, bye bye...
    #查看状态信息
    [root@centos8 ~]# redis-cli -a 1123 --cluster info 10.0.0.8:6379
    Warning: Using a password with '-a' or '-u' option on the command line interface may not be safe.
    Could not connect to Redis at 10.0.0.18:6379: Connection refused  #18主机拒绝
    10.0.0.8:6379 (02aa5dc5...) -> 3332 keys | 5462 slots | 1 slaves.
    10.0.0.48:6379 (7c923a83...) -> 3340 keys | 5461 slots | 0 slaves. #48成为新的master
    10.0.0.58:6379 (6a64e134...) -> 3329 keys | 5461 slots | 1 slaves.
    [OK] 10001 keys in 3 masters.
    0.61 keys per slot on average.
    [root@centos8 ~]# redis-cli -a 1123 --cluster check 10.0.0.8:6379
    Warning: Using a password with '-a' or '-u' option on the command line interface may not be safe.
    Could not connect to Redis at 10.0.0.18:6379: Connection refused
    10.0.0.8:6379 (02aa5dc5...) -> 3332 keys | 5462 slots | 1 slaves.
    10.0.0.48:6379 (7c923a83...) -> 3340 keys | 5461 slots | 0 slaves.
    10.0.0.58:6379 (6a64e134...) -> 3329 keys | 5461 slots | 1 slaves.
    [OK] 10001 keys in 3 masters.
    0.61 keys per slot on average.
    >>> Performing Cluster Check (using node 10.0.0.8:6379)
    M: 02aa5dc558efac920e7c6b31c4c2ca937da5cbd1 10.0.0.8:6379
       slots:[0-5461] (5462 slots) master
       1 additional replica(s)
    S: 94ccd455754080f44009770e467243eabe02b041 10.0.0.28:6379
       slots: (0 slots) slave
       replicates 6a64e134cbfefad4de96d48dfedc46eaf8b0230b
    M: 7c923a833df17ca0e9c93e67729789da9b472e67 10.0.0.48:6379
       slots:[5462-10922] (5461 slots) master
    M: 6a64e134cbfefad4de96d48dfedc46eaf8b0230b 10.0.0.58:6379
       slots:[10923-16383] (5461 slots) master
       1 additional replica(s)
    S: 0a5b9d013b3985174ab6bda729742b51c96052d1 10.0.0.38:6379
       slots: (0 slots) slave
       replicates 02aa5dc558efac920e7c6b31c4c2ca937da5cbd1
    [OK] All nodes agree about slots configuration.
    >>> Check for open slots...
    >>> Check slots coverage...
    [OK] All 16384 slots covered.
    #查看48主机的redis状态信息
    [root@centos8 ~]# redis-cli -a 1123 -h 10.0.0.48 info replication
    Warning: Using a password with '-a' or '-u' option on the command line interface may not be safe.
    # Replication
    role:master   #主服务器
    connected_slaves:0
    master_replid:e495840fdb8c974db0c7bb10acad0aa5991396a2
    master_replid2:7041a8749c665ca0ca587a2a0e575954a2fc2b2c
    master_repl_offset:143320
    second_repl_offset:143321
    repl_backlog_active:1
    repl_backlog_size:1048576
    repl_backlog_first_byte_offset:1
    repl_backlog_histlen:143320
    
    #将28主机恢复服务
    [root@centos8 ~]# systemctl start redis #在28主机执行
    #查看看生成的配置文件,自动切换成slave节点
    [root@centos8 ~]# cat /var/lib/redis/nodes-6379.conf 
    6a64e134cbfefad4de96d48dfedc46eaf8b0230b 10.0.0.58:6379@16379 master - 0 1603697992832 7 connected 10923-16383
    0a5b9d013b3985174ab6bda729742b51c96052d1 10.0.0.38:6379@16379 slave 02aa5dc558efac920e7c6b31c4c2ca937da5cbd1 0 1603697996866 3 connected
    7c923a833df17ca0e9c93e67729789da9b472e67 10.0.0.48:6379@16379 master - 0 1603697994849 9 connected 5462-10922
    02aa5dc558efac920e7c6b31c4c2ca937da5cbd1 10.0.0.8:6379@16379 master - 0 1603697995856 1 connected 0-5461
    befc75573a2c2220c60246fd3444c5d648e9d04c 10.0.0.18:6379@16379 master,fail - 1603697918125 1603697917221 0 disconnected
    94ccd455754080f44009770e467243eabe02b041 10.0.0.28:6379@16379 myself,slave  6a64e134cbfefad4de96d48dfedc46eaf8b0230b 0 1603697916000 2 connected #变为slave
    vars currentEpoch 9 lastVoteEpoch 0
  • 相关阅读:
    企业如何在智能制造的时代保持竞争力?
    汽车行业MES系统在产品追溯方面的应用分析
    你能想象未来的MES系统是什么样吗?
    智能制造进入下半场?APS如何进行优化
    【案例】如何让阀门制造提高排产效率?APS系统帮你实现
    APS系统的现状以及与MES系统的关联
    MES被重新定义?做到这几点才算智能制造
    python部分笔记
    BUUCTF Hack World
    BUUCTF--checkin
  • 原文地址:https://www.cnblogs.com/llliuhuiii/p/13874718.html
Copyright © 2020-2023  润新知