Nosql数据库学习(三)主从复制 、 持久化 、 数据类型

本文介绍Redis的主从复制机制,包括一主一从、一主多从及主从从结构的配置方法,同时讲解了Redis服务的高可用性实现——哨兵服务。此外,还介绍了Redis的两种持久化方式:RDB和AOF,以及如何通过这些备份文件恢复数据。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

Nosql_day03
一 Redis主从复制 (类似于MySQL的主从同步)
功能 实现数据的自动备份
角色:主数据库服务器 和 从数据库服务器

    配置主从复制结构存储数据:
      配置主:
	* 默认Redis服务运行后 就是 master 服务器

相关命令
	查看角色 info replication 
      配置从:        
            修改配置文件永久设置 
            vim  /etc/redis/6379.conf
		slaveof 主ip地址  端口号
            :wq

重启Redis服务 /etc/init.d/redis_6379  start


            slaveof no one #临时恢复为master服务器

            命令指定为临时slave服务器 slaveof  主ip地址  端口号

一.配置一主一从结构
51 主 (不做任何配置 )
连接51 存储数据 mset x 1 y 2 z 3

             52 从
	vim /etc/redis/6379.conf
	282  slaveof  192.168.4.51 6351
            :wq

	]#redis-cli  -h 192.168.4.52 -p 6352 shutdown
            ]#/etc/init.d/redis_6379 start
	]#redis-cli  -h 192.168.4.52 -p 6352
	> info  replication  
	> keys *   #能够看和51上一样的数据

]#redis-cli -h 192.168.4.51 -p 6351

mset a 1 b 3 c 4

]#redis-cli -h 192.168.4.51 -p 6351

keys * #可以看到 51 新存储的数据

	> slaveof no one
	> info  replication 
	> slaveof  192.168.4.51 6351
            > info  replication

二.配置一主多从 把服务器53 也配置为 51 的 slave服务器

	 在主机53做如下配置:

 *如果想永久做从服务器 就修改配置文件重启服务
vim /etc/redis/6379.conf
     	slaveof 192.168.4.51 6351
    :wq
]#redis-cli  -h 192.168.4.52 -p 6352 shutdown
    ]#/etc/init.d/redis_6379 start

 *如果临时设置为从服务器 命令行使用slaveof 配置即可
    > slaveof 192.168.4.51  6351

在51主机查看时 会看到从服务器 有2台    
]#redis-cli  -h 192.168.4.51 -p 6351
> info  replication
> set y  99     #存储的数据在2台从本机都可以查看到	

三.配置主从从结构 (给主从结构中的slave服务器配置从服务器)
给主从结构中的53主机配置1台从服务器 IP 192.168.4.54
在54主机做如下配置:

*如果想永久做从服务器 就修改配置文件重启服务
vim /etc/redis/6379.conf
slaveof 192.168.4.53 6353
:wq
]#redis-cli -h 192.168.4.54 -p 6354 shutdown
]#/etc/init.d/redis_6379 start

 *如果临时设置为从服务器 命令行使用slaveof 配置即可
    > slaveof 192.168.4.53  6353



]#redis-cli  -h 192.168.4.53 -p 6353
> info  replication 在53主机查看时 会看到连接的从服务器的台数

在51主机查看时 会看到从服务器 有2台    
]#redis-cli  -h 192.168.4.51 -p 6351
> info  replication
> set y  99     #存储的数据在54本机也可以查看到	     


带验证的主从复制(主服务器有连接密码时,配置主从复制)
主服务器51设置连接密码

[root@host51 ~]# redis-cli -h 192.168.4.51 -p 6351 shutdown
[root@host51 ~]# vim +501 /etc/redis/6379.conf
requirepass 123456
:wq
[root@host51 ~]# /etc/init.d/redis_6379 start

    从服务器指定主服务器连接密码  

]# redis-cli -h 192.168.4.52 -p 6352 shutdown
]# vim +289 /etc/redis/6379.conf
masterauth 123456
:wq
]# /etc/init.d/redis_6379 start
]# redis-cli -h 192.168.4.52 -p 6352

info replication

四.哨兵服务: 实现Redis服务的高可用

环境准备 一主一从结构的高可用

  1  53 和 54 的redis服务停止

  2  给52 主机设置连接密码 密码要和51 一样。

  3  启动57 主机,不需要运行redis服务 若是新克隆的虚拟机57 要拷贝 redis-4.0.8.tar.gz

57主机配置哨兵服务:
]# yum -y install gcc
]# tar -zxvf redis-4.0.8.tar.gz
]# cd redis-4.0.8
]# make
]# make install

]#vim /etc/sentinel.conf
bind 192.168.4.57
port 26379
sentinel monitor redisserver 192.168.4.51 6351 1
sentinel auth-pass redisserver 123456
:wq

]# redis-sentinel /etc/sentinel.conf 启动信息会占用终端输出

]# cat /etc/sentinel.conf 文件的内容 会自动更新

验证配置
停止当前主服务器上的redis服务

哨兵服务会把对应的从升级为主,并自动监视新的主服务

]# cat /etc/sentinel.conf

    从服务器查看状态 角色会变成master

二 持久化
持久化 (Redis服务实现数据永久存储的2种方式)
RDB(默认) 指的是 /var/lib/redis/6379/dump.rdb

    rdb介绍
    存盘频率
    手动存盘

    通过备份rdb文件实现数据恢复

192.168.4.50:6350> mset a 1 b 3 c 5 d 6 f 7
192.168.4.50:6350> save

exit
]# cp /var/lib/redis/6379/dump.rdb /opt/
]# scp /opt/dump.rdb root@192.168.4.51:/root/

 恢复

]# /etc/init.d/redis_6379 stop
]# ls /var/lib/redis/6379/dump.rdb
]# rm -rf /var/lib/redis/6379/dump.rdb
]# cp /root/dump.rdb /var/lib/redis/6379/
]# /etc/init.d/redis_6379 start

192.168.4.51:6351> keys *

存盘频率验证
[root@localhost ~]# redis-cli -h 192.168.4.50 -p 6350 -a 123456 shutdown
[root@localhost ~]# ls /var/lib/redis/6379/
dump.rdb
[root@localhost ~]# rm -rf /var/lib/redis/6379/dump.rdb
[root@localhost ~]# vim /etc/redis/6379.conf
219 save 900 1
220 #save 300 10
221 save 120 10
222 save 60 10000
:wq
/etc/init.d/redis_6379 start
525 ls /var/lib/redis/6379/
527 redis-cli -h 192.168.4.50 -p 6350 -a 123456

争取在2分钟以为存储 大于等于 10个变量
exit
]#ls -l /var/lib/redis/6379/dump.rdb

RDB优点与缺点?

AOF(需要手动启用)        

[root@localhost ~]# redis-cli -h 192.168.4.50 -p 6350 -a 123456
192.168.4.50:6350> config get appendonly
192.168.4.50:6350> config set appendonly yes
192.168.4.50:6350> config get appendonly

192.168.4.50:6350> config rewrite

[root@localhost ~]# ls /var/lib/redis/6379/
appendonly.aof dump.rdb

[root@host51 ~]# vim +673 /etc/redis/6379.conf
673:appendonly no
677:appendfilename “appendonly.aof”
[root@host51 ~]# redis-cli -h 192.168.4.51 -p 6351 -a 123456 shutdown
[root@host51 ~]# /etc/init.d/redis_6379 start
Starting Redis server…
[root@host51 ~]# ls /var/lib/redis/6379/
appendonly.aof dump.rdb
[root@host51 ~]#

通过备份aof 文件恢复数据

[root@localhost ~]# ls /var/lib/redis/6379/appendonly.aof dump.rdb
[root@localhost ~]# cp /var/lib/redis/6379/appendonly.aof /opt/
[root@localhost ~]# ls /opt/appendonly.aof
/opt/appendonly.aof
[root@localhost ~]#
[root@localhost ~]# scp /opt/appendonly.aof root@192.168.4.51:/root/

在51 主机使用AOF文件恢复数据
[root@host51 ~]# redis-cli -h 192.168.4.51 -p 6351 -a 123456 shutdown
[root@host51 ~]# rm -rf /var/lib/redis/6379/*
[root@host51 ~]# mv /root/appendonly.aof /var/lib/redis/6379/
[root@host51 ~]# ls /var/lib/redis/6379/
[root@host51 ~]# /etc/init.d/redis_6379 start

keys *

aof优化配置

修复aof文件
566 redis-cli -h 192.168.4.50 -p 6350 -a 123456 shutdown
568 vim appendonly.aof
572 /etc/init.d/redis_6379 start
573 redis-cli -h 192.168.4.50 -p 6350 -a 123456
576 vim /var/log/redis_6379.log
581 redis-check-aof --fix /var/lib/redis/6379/appendonly.aof
583 rm -rf /var/run/redis_6379.pid
584 /etc/init.d/redis_6379 start
585 redis-cli -h 192.168.4.50 -p 6350 -a 123456
三 数据类型
(1)字符类型管理命令
1.set x 9 ex 10 :创建生命周期10秒的x变量,值为9
2.set y 29 px 10 :创建生命周期10毫秒的y变量,值为29
3.set y 39 nx :如果变量y不存在就赋值,存在就不赋值
4.set y 49 xx :变量存在也会重新赋值

5.改写值
192.168.4.51:6351> set first “hello world”
OK
192.168.4.51:6351> setrange first 6 “Redis” //改写为hello Redis
(integer) 11
192.168.4.51:6351> get first
“hello Redis”
6.strlen first :统计first字段里的值的长度
7.append myname jacob :存在myname变量就把jacob追加到变量的值后,不存在就创建,然后返回key的长度
8.setbit key offset value 对key所存储字串,设置或清除特定偏移量上的位(bit),value值可以为1或0,offset为0~2^32之间,key不存在,则创建新key
setbit peter 0 1
setbit peter 1 0
setbit peter 2 1
setbit peter 3 1
9.bitcount peter :查询所有所有值里1的个数
(integer) 3
10decr key :将key中的值减1,key不存在则先初始化为0,再减1。key的值是数字才可以
11.decrby cc 20:将cc变量里的值减去20
12.getrange key start end 返回字串值中的子字串,截取范围为start和end,负数偏移量表示从末尾开始计数,-1表示最后一个字符,-2表示倒数第二个字符
192.168.4.51:6379> set x 123456789
OK
192.168.4.51:6379> getrange x -5 -1
“56789”
192.168.4.51:6379> getrange x 0 4
“12345”
13.incr key 将key的值加1,如果key不存在,则初始为0后再加1,主要应用为计数器
192.168.4.51:6379> set page 20
OK
192.168.4.51:6379> incr page
(integer) 21
14.incrby key increment 将key的值增加increment
192.168.4.51:6379> set x 10
OK
192.168.4.51:6379> incr x
(integer) 11
192.168.4.51:6379> incr x
(integer) 12
15.incrbyfloat key increment 为key中所储存的值加上浮点数增量 increment
192.168.4.51:6379> set num 16.1
OK
192.168.4.51:6379> incrbyfloat num 1.1
“17.2”

(2)列表类型管理命令
1.lpush key value [value…] 将一个或多个值value插入到列表key的表头,Key不存在,则创建key
192.168.4.51:6379> lpush list a b c //list值依次为c b a
(integer) 3
2. lrange key start stop 从开始位置读取key的值到stop结束
192.168.4.51:6379> lrange list 0 2 //从0位开始,读到2位为止
1) “c”
2) “b”
3) “a”
192.168.4.51:6379> lrange list 0 -1 //从开始读到结束为止
1) “c”
2) “b”
3) “a”
192.168.4.51:6379> lrange list 0 -2 //从开始读到倒数第2位值
1) “c”
2) “b”
3. lpop key 移除并返回列表头元素数据,key不存在则返回nil
192.168.4.51:6379> lpop list //删除表头元素,可以多次执行
“c”
192.168.4.51:6379> LPOP list
“b”
4.llen key 返回列表key的长度

192.168.4.51:6379> llen list
(integer) 1
5.lindex key index 返回列表中第index个值
192.168.4.51:6379> lindex list 1
“c”
6.lset key index value 将key中index位置的值修改为value
192.168.4.51:6379> lpush list a b c d
(integer) 5
192.168.4.51:6379> lset list 3 test //将list中第3个值修改为test
OK
7.rpush key value [value…] 将value插入到key的末尾
192.168.4.51:6379> rpush list3 a b c //list3值为a b c
(integer) 3
192.168.4.51:6379> rpush list3 d //末尾插入d
(integer) 4
8.rpop key 删除并返回key末尾的值
192.168.4.51:6379> RPOP list3
“d”
(3)hash表类型管理命令
1.hset key field value 将hash表中field值设置为value
192.168.4.51:6379> hset site google ‘www.g.cn’
(integer) 1
192.168.4.51:6379> hset site baidu ‘www.baidu.com’
(integer) 1
2.get key filed 获取hash表中field的值
192.168.4.51:6379> hget site google
“www.g.cn”
3.hmset key field value [field value…] 同时给hash表中的多个field赋值
192.168.4.51:6379> hmset site google www.g.cn baidu www.baidu.com
OK
4.hmget key field [field…] 返回hash表中多个field的值
192.168.4.51:6379> hmget site google baidu
1) “www.g.cn”
2) “www.baidu.com”
5.hkeys key 返回hash表中所有field名称
192.168.4.51:6379> hmset site google www.g.cn baidu www.baidu.com
OK
192.168.4.51:6379> hkeys site
1) “google”
2) “baidu”
6.hgetall key 返回hash表中所有key名和对应的值列表
192.168.4.51:6379> hgetall site
1) “google”
2) “www.g.cn”
3) “baidu”
4) “www.baidu.com”
7.hvals key 返回hash表中所有key的值
192.168.4.51:6379> hvals site
1) “www.g.cn”
2) “www.baidu.com”
8.hdel key field [field…] 删除hash表中多个field的值,不存在则忽略
192.168.4.51:6379> hdel site google baidu
(integer) 2

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值