Redis数据库(二)

1. Redis数据库持久化

  redis提供了两种数据备份方式,一种是RDB,另外一种是AOF,以下将详细介绍这两种备份策略。

面试:

Redis数据库(二)

1.1  配置文件详解备份方式

[root@localhost ~]# vim /usr/local/reids/redis.conf
#   save ""
save 900 1                 #900秒内发生1次数据更改就写入Redis当中
save 300 10                #300秒内发生10次数据更改就写入Redis当中
save 60 10000              #60秒内发生10000次数据更改就写入Redis当中
                           #save值可以自定义更改,一般设置为5分钟执行一次,三者只要触发一个就写入Redis文件当中,若想关闭掉RBD备份,只需注释掉  此处的save即可。

#储存文件的绝对路径=dir+dbfilename
# The filename where to dump the DB
dbfilename dump.rdb
……
# Note that you must specify a directory here, not a file name.
dir ./

#查看dump.rdb文件
[root@localhost ~]# vim /usr/local/reids/dump.rdb
REDIS0009ú      redis-ver^E5.0.0ú                              #表示压缩
redis-bitsÀ@ú^EctimeÂ#èãú^Hused-mem°^R^L^@ú^Laof-preambleÀ^@þ^@û^B^@^@        friendone^Czxj^@  #存储具体的值     friendtwo^CwrlÿY^Z^Zz<9b>ÇjÞ
 
#RDB备份为全库备份,即每备份一次均备份数据库所有数据。


#开启AOF备份
[root@localhost ~]# vim /usr/local/reids/redis.conf
# Please check http://redis.io/topics/persistence for more information.
appendonly no             #appendonly默认为no,若要开启AOF备份,此处改为yes

#同步机制
# appendfsync always      #只要更改就同步到Redis当中
appendfsync everysec      #默认为每秒同步
# appendfsync no          按操作系统备份,一般为半小时

#AOF存储的是具体的操作命令
#开启AOF后启动进入Redis
[root@localhost ~]# redis-server redis.conf
[root@localhost ~]# ss -tnl
LISTEN    0    128  192.168.16.4:6379       *:*
[root@localhost ~]# ll
-rw-r--r--. 1 root root       0 May 22 14:50 appendonly.aof  #已生成aof文件
[root@localhost ~]# vim appendonly.aof   #由于数据库未进行操作,文件是空的
~
192.168.16.4:6379> set username zxj      #插入数据
OK

#退出后再查看备份文件
192.168.16.4:6379> exit
[root@localhost ~]# vim appendonly.aof
*2
$6
SELECT                                  #存储的是具体的命令
$1
0
*3
$3
set
$8
username
$3
zxj
~   
#存储文件的绝对路径=dir+appendfieldname

2.Redis主从架构的搭建

  Redis架构也是读写分离,与mariadb不同的是,mariadb的从数据库可以写但不能同步到主数据库(readonly),Redis从数据库只能读不能写。

Redis架构搭建:

  为节省空间,此处仅有一台主机进行搭建,在不同的窗口进行查看

#复制一份Redis配置文件slave.conf作为从数据库
[root@localhost ~]# cd /usr/local/redis/
[root@localhost redis]# cp redis.conf slave.conf
[root@localhost redis]# vim slave.conf
# JUST COMMENT THE FOLLOWING LINE.
#~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
bind 192.168.16.4
slaveof 192.168.16.4 6379                        #从属于192.168.16.4 63279
# If port 0 is specified Redis will not listen on a TCP socket.
port 6380                                        #便于区分,将从节点端口改为6380

#启动从节点文件
[root@localhost redis]# redis-server slave.conf
[root@localhost redis]# ss -tnl                           #两实例
LISTEN   0   128  192.168.16.4:6379              *:*      #主            
LISTEN   0   128  192.168.16.4:6380              *:*      #从            

#打开两个窗口,进入数据库。窗口1:6379;窗口2:6380
#窗口1:
[root@localhost ~]# redis-cli -h 192.168.16.4 -p 6379
192.168.16.4:6379>
#窗口2:
[root@localhost ~]# redis-cli -h 192.168.16.4 -p 6380
192.168.16.4:6380>
192.168.16.4:6380> keys *                        #已同步
1) "username1" 
2) "username"
192.168.16.4:6380> set username3 lvmy
(error) READONLY You can't write against a read only replica. #从节点无法写

3.Redis集群

  Redis集群一个主对应一个从,而且至少需要3个主(避免脑裂),因此需要6个集群。 

 

Redis集群搭建实例详解

  为精简实验,在一台主机上搭建6个实例

#方便起见,端口设置为7001~7006
#按照习惯,将配置文件写在 conf下
[root@localhost conf]# vim 7001.conf
port 7000                             #绑定端口
bind 192.168.16.4                     #绑定对外连接提供的ip
daemonize yes                         #开启守护进程
pidfile 7001.pid                      #进程文件名
cluster-enabled yes                   #是否是集群
cluster-config-file 7001_node.conf    #集群配置文件
cluster-node-timeout 15000            #集群连接超时时间
appendonly yes                        #数据持久化(备份)类型
~              
#在7001.conf的基础上修改7002~7006.conf,只修该端口、进程、和配置。

#关闭原先实验的redis
[root@localhost conf]# ps aux | grep redis
root       3883  0.1  1.3 163100 13468 ?        Ssl  14:50   0:05 redis-server 192.168.16.4:6379
root       3919  0.1  1.4 165660 14404 ?        Ssl  15:17   0:03 redis-server 192.168.16.4:6380
root       3948  0.0  0.7  24852  7624 pts/1    S+   15:21   0:00 redis-cli -h 192.168.16.4 -p 6379
root       3996  0.0  0.0 112708   968 pts/0    S+   15:52   0:00 grep --color=auto redis
[root@localhost conf]# kill -9 3883 3919 3948
[root@localhost conf ]# ps aux | grep redis
root       3998  0.0  0.0 112708   968 pts/0    S+   15:53   0:00 grep --color=auto redis

#启动集群
[root@localhost conf]# vim start_conf.sh
#!/bin/bash
for i in {1..6}
do
        redis-server 700$i.conf
done
~  
[root@localhost conf]# bash start_conf.sh
[root@localhost conf]# ps aux | grep redis
root       4003  0.1  0.7 153884  7772 ?        Ssl  15:55   0:00 redis-server 192.168.16.4:7001 [cluster]
root       4005  0.1  0.7 153884  7772 ?        Ssl  15:55   0:00 redis-server 192.168.16.4:7002 [cluster]
root       4010  0.1  0.7 153884  7776 ?        Ssl  15:55   0:00 redis-server 192.168.16.4:7003 [cluster]
root       4012  0.1  0.7 153884  7776 ?        Ssl  15:55   0:00 redis-server 192.168.16.4:7004 [cluster]
root       4017  0.1  0.7 153884  7772 ?        Rsl  15:55   0:00 redis-server 192.168.16.4:7005 [cluster]
root       4022  0.1  0.7 153884  7776 ?        Ssl  15:55   0:00 redis-server 192.168.16.4:7006 [cluster]
 
#建立主从联系
[root@localhost conf]# redis-cli --cluster create 192.168.16.4:7001 192.168.16.4:7002 192.168.16.4:7003 192.168.16.4:7004 192.168.16.4:7005 192.168.16.4:7006 --cluster-replicas 1                   #每个主节点有一个从节点   
………
M: 7140c88b7bd0778bbe71250dbe7597cc3df8ac95 192.168.16.4:7001
   slots:[0-5460] (5461 slots) master             #M 主;S 从
M: c60cd50870fc97320bb4cf19557f6c5daced6745 192.168.16.4:7002
   slots:[5461-10922] (5462 slots) master
M: 03783aeedfd9553e8b1bbe9a2054fba46f9f7e00 192.168.16.4:7003
   slots:[10923-16383] (5461 slots) master        #一共16384个槽,每个数据分配到槽里
S: 96396dbeeb6a94e632f9a9f585694a21d7235766 192.168.16.4:7004
   replicates 03783aeedfd9553e8b1bbe9a2054fba46f9f7e00
S: d035beb18deb48a027b6c3fce0e123bebc3584c2 192.168.16.4:7005
   replicates 7140c88b7bd0778bbe71250dbe7597cc3df8ac95
S: 67d8400b228762ad14841114775a3ee210fff5cd 192.168.16.4:7006
   replicates c60cd50870fc97320bb4cf19557f6c5daced6745
Can I set the above configuration? (type 'yes' to accept):yes  #是否要配置
…….
waiting …….
[OK] All nodes agree about slots configuration.
>>> Check for open slots...
>>> Check slots coverage...
[OK] All 16384 slots covered.

#以集群的方式连接登录(-c),若以单个节点登录写入数据会报错
[root@localhost conf]# redis-cli -h 192.168.16.4 -p 7001
192.168.16.4:7001> set username zcv
(error) MOVED 14315 192.168.16.4:7003
[root@localhost conf]# redis-cli -c -h 192.168.16.4 -p 7001
192.168.16.4:7001>

#集群数据库是不存在以数字形式表示的数据库
192.168.16.4:7001> select 8
(error) ERR SELECT is not allowed in cluster mode

192.168.16.4:7001> set username zxj                         #每次输入数据会跳转数据库
-> Redirected to slot [14315] located at 192.168.16.4:7003  #值插入到槽14315当中,归7003管
OK
192.168.16.4:7003> set username2 aSD
-> Redirected to slot [8937] located at 192.168.16.4:7002
OK
192.168.16.4:7002> set username3 gfhn
-> Redirected to slot [13000] located at 192.168.16.4:7003
OK
192.168.16.4:7003>                                         #主节点均正常,在主节点间跳转


#输入数据时的跳转并不影响取值
192.168.16.4:7002> get username -> Redirected to slot [14315] located at 192.168.16.4:7003 "zxj" 192.168.16.4:7003> get username2 -> Redirected to slot [8937] located at 192.168.16.4:7002 "aSD" 192.168.16.4:7002> get username4 192.168.16.4:7001> get username3 -> Redirected to slot [13000] located at 192.168.16.4:7003 "gfhn" 192.168.16.4:7003> #模拟7002宕掉,自动从从节点补位 [root@localhost conf]# ps aux | grep redis [root@localhost conf]# kill -9 4160 [root@localhost conf]# redis-cli -c -h 192.168.16.4 -p 7002 192.168.16.4:7002> set bu dfgj -> Redirected to slot [890] located at 192.168.16.4:7005 OK 192.168.16.4:7005> set bu1 sdflj; -> Redirected to slot [9429] located at 192.168.16.4:7002 OK 192.168.16.4:7002> set bu3 josd -> Redirected to slot [1175] located at 192.168.16.4:7005 OK 192.168.16.4:7005>