一 redis 主从复制
1.1 主从复制介绍
原理:slave向master 发送sync命令,
master会创建一个新的进程
缺点:
网络繁忙 :传送数据 (加大带宽)
系统繁忙:cpu损耗 ( 提高硬件配置)
1.2 redis 主从复制,结构分类
一主一从
一主多从
主从从
1.3 配置redis主从复制 并验证
显示主从复制信息:
192.168.4.51:6351> info replication
# Replication
role:master # redis 服务运行后,默认都是master服务
connected_slaves:0 #
永旧配置
[root@host52 ~]# vim /etc/redis/6379.conf
slaveof 192.168.4.51 6351 #280行
手动将主机192.168.4.52配置为192.168.4.51的从库: #这样配置后,临时有效,当服务器重起以后,配置失效
[root@host52 ~]# redis-cli -h 192.168.4.52 -p 6352
192.168.4.52:6352> SLAVEOF 192.168.4.51 6351
OK
192.168.4.52:6352> info replication
# Replication
role:slave
master_host:192.168.4.51
master_port:6351
master_link_status:up # 只有up 了 主机52才能同步主机51的数据
master_last_io_seconds_ago:1
master_sync_in_progress:0
192.168.4.51:6351> info replication #在51查看信息
# Replication
role:master
connected_slaves:1
slave0:ip=192.168.4.52,port=6352,state=online,offset=140,lag=0
master_replid:f3f84f8b8de96eef68a65a97eb024a43ef455933
master_replid2:0000000000000000000000000000000000000000
一主多从
在以上实验的基础上面将主机53配置成主机51的从
在主机53上操作
[root@host53 ~]# vim /etc/redis/6379.conf
slaveof 192.168.4.51 6351 #280行
192.168.4.53:6353> info replication
# Replication
role:slave
master_host:192.168.4.51
master_port:6351
master_link_status:up
master_last_io_seconds_ago:6
重起服务
[root@host53 ~]# /etc/init.d/redis_6379 stop
Stopping ...
Redis stopped
[root@host53 ~]# /etc/init.d/redis_6379 start
主从从
把主机54配置为主机53的从
在主机54上操作
[root@host53 ~]# vim /etc/redis/6379.conf
slaveof 192.168.4.53 6353 #280行
验证
[root@host50 ~]# redis-cli -h 192.168.4.51 -p 6351
192.168.4.51:6351> set x 101
OK
192.168.4.51:6351> set y 102
OK
[root@host50 ~]# redis-cli -h 192.168.4.52/53/54 -p 6352/6353/6354
keys *
把从服务器恢复为独立的服务器
[root@host52 ~]# redis-cli -h 192.168.4.52 -p 6352 #l临时有效
192.168.4.52:6352> SLAVEOF no one
永久恢复:
[root@host52 ~]# vim /etc/redis/6379.conf
#slaveof 192.168.4.51 6351 注释掉
1.4 配置有密码的redis 主从复制结构
55主服务器
1设置服务器的连接密码
[root@host55 ~]# /etc/init.d/redis_6379 stop
[root@host55 ~]# vim /etc/redis/6379.conf
requirepass 123456 # 把密码设置为123456
[root@host55 ~]# redis-cli -h 192.168.4.55 -p 6355 -a 123456
192.168.4.55:6355> keys *
(empty list or set)
192.168.4.55:6355> set x 99
OK
192.168.4.55:6355> get x
"99"
192.168.4.55:6355>
[root@host55 ~]# vim +43 /etc/init.d/redis_6379
$CLIEXEC -h 192.168.4.55 -p 6355 -a 123456 shutdown # 是它能用脚本关闭服务
56 从服务机器
[root@host56 ~]# vim +281 /etc/redis/6379.conf
slaveof 192.168.4.55 6355 # 指定服务器的ip 和端口
masterauth 123456 # 指定主服务器的redi 的登陆密码
[root@host56 ~]# redis-cli -h 192.168.4.56 -p 6356
192.168.4.56:6356> info replication # 查看信息
# Replication
role:slave
master_host:192.168.4.55
master_port:6355
master_link_status:up
master_last_io_seconds_ago:3
master_sync_in_progress:0
slave_repl_offset:14
1.5哨兵服务 :监视主服务机器,主服务机器宕机后 ,把对应的从服务其 升级为 主服务机器
在主机58上运行哨兵服务
1 安装redis软件包
2创建哨兵服务的配置文件
[root@host58 redis-4.0.8]# vim sentinel.conf
bind 0.0.0.0 哨兵服务的ip 地址 0.0.0.0 表示所有
port 26379
sentinel monitor redisser 192.168.4.55 6355 1
sentinel auth-pass redisser 123456
# sentinel monitor <master-name> <ip> <redis-port> <quorum>
给监视主服务机器起名字 主服务器的ip 地址 主服务器的端口 哨兵服务器的台数(这里就只有58是哨兵服务,只要有一台哨兵服务机器监控到主服务挂掉旧切换)
# sentinel auth-pass <master-name> <password> # 如果主库设置了密码,
3启动哨兵服务
[root@host58 redis-4.0.8]# redis-sentinel /etc/sentinel.conf
4测试哨兵服务
[root@host55 ~]# /etc/init.d/redis_6379 stop #把主机55 服务停止
[root@host56 ~]# redis-cli -h 192.168.4.56 -p 6356 # 登陆主机56
192.168.4.56:6356> info replication # 查看信息
# Replication
role:master # 主机56变为主机
connected_slaves:0
master_replid:a15e8774538fb27c3299639141539e2789a4b848
master_replid2:46ce2bb65b0ef6f03d6bd114a74ebb2670f6ec46
[root@host55 ~]# /etc/init.d/redis_6379 start #重新在启动55
[root@host55 ~]# redis-cli -h 192.168.4.55 -p 6355 -a 123456
192.168.4.55:6355> info replication #查看55的信息
# Replication
role:slave #角色变为从
master_host:192.168.4.56
master_port:6356
master_link_status:up
master_last_io_seconds_ago:0
192.168.4.55:6355> keys * #主机 56后面写的数据 在主机55启动redis服务后同样能同步到主机55
1) "z"
2) "x"
3) "y"
192.168.4.56:6356> info replication # 主机56查看信息
# Replication
role:master # 主机56保持为主
connected_slaves:1
slave0:ip=192.168.4.55,port=6355,state=online,offset=124956,lag=1
master_replid:a15e8774538fb27c3299639141539e2789a4b848
master_replid2:46ce2bb65b0ef6f03d6bd114a74ebb2670f6ec46
master_repl_offset:124956
second_repl_offset:91256
二数据持久话
2.1RDB方式实现数据化持久化
2.1.1 RDB介绍
当redis 停止服务时,在数据库目录下面会产生dump.rdb文件存到硬盘中
当redis 重起服务时,dump。rbd 会调用到内存里
redis 数据文件 : dump.rbd
2.1.2 使用RDB文件恢复数据
[root@host51 ~]# redis-cli -h 192.168.4.51 -p 6351
192.168.4.51:6351> set x 1
OK
192.168.4.51:6351> set y 2
OK
192.168.4.51:6351> set z 4
192.168.4.51:6351> save # 快速写进硬盘里 # bgsave 表示不阻塞写进硬盘
OK
[root@host51 ~]# ls /var/lib/redis/6379/
dump.rdb
[root@host51 ~]# cp /var/lib/redis/6379/dump.rdb /root # 一定要从先备份 到 /root下
[root@host51 ~]# scp /root/dump.rdb [email protected]:/root # 一定要先传送到/root 下,不要直接/var/lib/redis/6379/该目录下,否则恢复不成功
主机52上面的操作
[root@host52 ~]# /etc/init.d/redis_6379 stop
[root@host52 ~]# rm -rf /var/lib/redis/6379/*
[root@host52 ~]# cp /root/dump.rdb /var/lib/redis/6379/
[root@host52 ~]# /etc/init.d/redis_6379 start
[root@host52 ~]# redis-cli -h 192.168.4.52 -p 6352
192.168.4.52:6352> keys * # 数据和主机51 一样
1) "y"
2) "z"
3) "x"
2.1.3 与RDB文件相关的配置
[root@host51 ~]# vim /etc/redis/6379.conf
dbfilename dump.rdb 文件名
数据从内存保存到硬盘的频率
save 900 1 900秒内且有一次修改
save 300 10 300秒内且有一次修改
save 60 10000 60秒内且有一次修改
2.1.4使用RDB实现数据持久化的优点和缺点
优点
高性能的持久化实现---
比较适合大规模的数据恢复
缺点:
意外宕机时,最后一次持久化的数据会丢失
2.2AOF 方式实现数据持久化 (如果aof 和RBD同时启用,,则默认用AOF )
2.1.1AOF 介绍
类似于mysql 的binlog日志文件
只做追加操作的文件,记录redis服务所有写操作,不断的将新的写操作,追加到
2.1.2 使用AOF文件恢复数据
[root@host51 ~]# vim /etc/redis/6379.conf
appendonly yes
appendfilename "appendonly.aof"
appendfsync always
[root@host51 ~]# /etc/init.d/redis_6379 stop
[root@host51 ~]# /etc/init.d/redis_6379 start #开启了aof后,在重起redis 服务后,之前的数据会丢失
红色为额外加的,,,针对 开启了aof后,在重起redis 服务后,之前的数据会丢失 的解决方法
{ *** 在命令行上面启用AOF,不会覆盖已有的数据
[root@host53 ~]# redis-cli -h 192.168.4.53 -p 6353
192.168.4.53:6353> keys *
1) "a1"
2) "y"
3) "v2"
192.168.4.53:6353> config set appendonly yes # 启用aof 临时设置
OK
192.168.4.53:6353> config rewrite # 写进配置文件里,永久设置
192.168.4.53:6353> keys * 有效数据
1) "a1"
2) "y"
3) "v2"
192.168.4.53:6353> save # 写进硬盘里
[root@host53 ~]# ls /var/lib/redis/6379/ # 含有
appendonly.aof dump.rdb
[root@host53 ~]# /etc/init.d/redis_6379 stop
[root@host53 6379]# /etc/init.d/redis_6379 start
[root@host53 6379]# redis-cli -h 192.168.4.53 -p 6353
192.168.4.53:6353> keys * # 数据没有丢失
1) "a1"
2) "y"
3) "v2"
}
[root@host51 ~]# ls /var/lib/redis/6379/
appendonly.aof dump.rdb
[root@host51 ~]# redis-cli -h 192.168.4.51 -p 6351
192.168.4.51:6351> keys *
192.168.4.51:6351> set i 88
OK
192.168.4.51:6351> set j 99
[root@host51 ~]# cat /var/lib/redis/6379/appendonly.aof # 拥有记录数据写入
[root@host51 ~]# cp /var/lib/redis/6379/appendonly.aof /root/
[root@host51 ~]# scp /root/appendonly.aof [email protected]:/root
在主机52上操作
[root@host52 ~]# vim /etc/redis/6379.conf
appendonly yes
appendfilename "appendonly.aof"
appendfsync always
[root@host52 ~]# /etc/init.d/redis_6379 stop
[root@host52 ~]# rm -rf /var/lib/redis/6379/*
[root@host52 ~]# cp /root/appendonly.aof /var/lib/redis/6379/
[root@host52 ~]# /etc/init.d/redis_6379 start
[root@host52 ~]# redis-cli -h 192.168.4.52 -p 6352
192.168.4.52:6352> keys * # 拥有跟主机 51数据一样
1) "j"
2) "i"
修复AOF 文件
[root@host53 6379]# vim /var/lib/redis/6379/appendonly.aof #故意把文件修改错
aaaa
[root@host53 6379]# /etc/init.d/redis_6379 stop
[root@host53 6379]# /etc/init.d/redis_6379 start
[root@host53 6379]# redis-check-aof --fix /var/lib/redis/6379/appendonly.aof
[root@host53 6379]# rm -rf /var/run/redis_6379.pid
[root@host53 6379]# /etc/init.d/redis_6379 start
[root@host53 6379]# redis-cli -h 192.168.4.53 -p 6353
192.168.4.53:6353> keys *
1) "a1"
2) "x"
3) "y"
4) "v2"
5) "v1"
2.1.3 与AOF文件相关的配置
appendfsync always //有新写的操作立即记录 即是写入appendonly.aof文件中,也写入磁盘里
appendfsync everysec //每秒记录一次 即是写入appendonly.aof文件中,也写入磁盘里
appendfsync no //从不记录 ,,指的是只时写入appendonly.aof文件中,不写入磁盘里
[root@host53 6379]# vim +280 /etc/redis/6379.conf
auto-aof-rewrite-percentage 100 当瘦身到50M 的文件增加到 100 的时候 继续对文件进行瘦身 ,, ,,50+50=100
auto-aof-rewrite-min-size 64mb 当文件达到 64MB 时,,进行文件瘦身 假设瘦身到 50M
2.1.4使用AOF实现数据持久化的优点和缺点
优点:
可以灵活化设置持久化方式
出现意外宕机时,仅仅可能丢失1秒的数据
缺点 :
执行fsync策略时的速度可能比rdb方式慢
持久化文件体积大与rdb
三数据类型
3.1字符类型 string 字符串
192.168.4.51:6351> setrange tel 3 *** 从第4个字符修改为***
(integer) 11
192.168.4.51:6351> get tel
"199***28888"
192.168.4.51:6351> set x 888
OK
192.168.4.51:6351> strlen tel 显示变量的 个数
(integer) 11
192.168.4.51:6351> strlen x
192.168.4.51:6351> append x 88 给变量追加88
(integer) 5
192.168.4.51:6351> append x2 88
(integer) 2
192.168.4.51:6351> get x2
"88"
192.168.4.51:6351> get y
"997"
192.168.4.51:6351> decr y y值减掉1
(integer) 996
192.168.4.51:6351> DECRBY y 50 y值减掉 50
(integer) 946
192.168.4.51:6351> decr v1 v1没值时,,,会赋值为0
(integer) -1
192.168.4.51:6351> get v1
"-1"
一次输出多个变量值
192.168.4.51:6351> mget x y
1) "88888"
2) "946"
192.168.4.51:6351> getrange key start end
192.168.4.51:6351> set hostname www.tedu.com
OK
192.168.4.51:6351> get hostname
"www.tedu.com"
192.168.4.51:6351> getrange key 0 2
""
192.168.4.51:6351> getrange hostname 0 2
"www"
192.168.4.51:6351> getrange hostname 1 2
"ww"
192.168.4.51:6351> getrange hostname -2 -1
"om"
192.168.4.51:6351> get x
"88888"
192.168.4.51:6351> incr x #自加1
(integer) 88889
192.168.4.51:6351> incrby x 3 加3
(integer) 88892
setbit 节省空间 单位是位,,1字节(byte)=8位(bit)
192.168.4.51:6351> setbit tom 1 0
(integer) 0
192.168.4.51:6351> setbit tom 2 1
(integer) 0
192.168.4.51:6351> setbit tom 3 0
192.168.4.51:6351> bitcount tom # 显示多少个1
(integer) 1
192.168.4.51:6351> setbit tom 4 1
(integer) 0
192.168.4.51:6351> bitcount tom
(integer) 2
192.168.4.51:6351> set pay 7
OK
192.168.4.51:6351> get pay
"7"
192.168.4.51:6351> INCRBYFLOAT pay 1.5 # 加小数
"8.5"
192.168.4.51:6351> mset v3 99 v4 88
OK
192.168.4.51:6351> mget v3 v4
1) "99"
2) "88"
管理字符类型的数据命令: set mset get mget setrange strlen append getrange decr decrby incr incrby incrbyfloat setbit bitcount
数据来源 :程序员所写的代码php 连接redis 服务机器
3.2列表类型 list
列表介绍: 让一个变量存储多个值, 输出时先进后出
192.168.4.51:6351> lpush stuname a b c
(integer) 3
192.168.4.51:6351> lpush stuname d e f
(integer) 6
192.168.4.51:6351> LRANGE stuname 0 -1
1) "f"
2) "e"
3) "d"
4) "c"
5) "b"
6) "a"
192.168.4.51:6351> LRANGE stuname 0 1
1) "f"
2) "e"
192.168.4.51:6351> LRANGE stuname -3 -1
1) "c"
2) "b"
3) "a"
192.168.4.51:6351> type stuname
list
192.168.4.51:6351> lpop stuname # 移除列表头元素数据
"f"
192.168.4.51:6351> LRANGE stuname 0 -1
1) "e"
2) "d"
3) "c"
4) "b"
5) "a"
192.168.4.51:6351> llen stuname
(integer) 5
192.168.4.51:6351>
192.168.4.51:6351> lindex stuname 0
"e"
192.168.4.51:6351> lindex stuname 2
"c"
192.168.4.51:6351> lindex stuname -1
"a"
192.168.4.51:6351> -5
(error) ERR unknown command '-5'
192.168.4.51:6351> lindex stuname -4
"d"
192.168.4.51:6351> LRANGE stuname 0 -1
1) "e"
2) "d"
3) "c"
4) "b"
5) "a"
192.168.4.51:6351> lset stuname 0 E
OK
192.168.4.51:6351> lset stuname 1 D
OK
192.168.4.51:6351> LRANGE stuname 0 -1
1) "E"
2) "D"
3) "c"
4) "b"
5) "a"
192.168.4.51:6351> RPUSH stuname tom lucy 在末尾加上 tom lucy
(integer) 7
192.168.4.51:6351> LRANGE stuname 0 -1
1) "E"
2) "D"
3) "c"
4) "b"
5) "a"
6) "tom"
7) "lucy"
192.168.4.51:6351> RPOP stuname #删除末尾的值
"lucy"
192.168.4.51:6351> lrange stuname 0 -1
1) "E"
2) "D"
3) "c"
4) "b"
5) "a"
6) "tom"
管理列表类型数据的命令 :lpush lrange lpop llen lindex lset rpush rpop
3.3 hash 类型 hash
一个变量可以对应多个filed,一个filed 对应一个value
比使用string 类型存储更节能内存
管理hash 类型数据的命令:hset hget hmset hmget hkeys hvals hgetall hdel
192.168.4.51:6351> HSET wz baidu 'www.baidu.com'
(integer) 1
192.168.4.51:6351> hset wz goole 'www.g.cn'
(integer) 1
192.168.4.51:6351> type wz
hash
192.168.4.51:6351> HGET wz baidu
"www.baidu.com"
192.168.4.51:6351> HGET wz goole
"www.g.cn"
192.168.4.51:6351> hmset wz jd www.jd.com sina www.sina.com
OK
192.168.4.51:6351> hmget wz jd
1) "www.jd.com"
192.168.4.51:6351> hmget wz jd sina
1) "www.jd.com"
2) "www.sina.com"
192.168.4.51:6351> hkeys site
1) "baidu"
2) "sina"
192.168.4.51:6351> HVALS site
1) "www.baidu.com"
2) "www.sina.com"
192.168.4.51:6351> hgetall site
1) "baidu"
2) "www.baidu.com"
3) "sina"
4) "www.sina.com"
192.168.4.51:6351> hkeys wz
1) "baidu"
2) "goole"
3) "jd"
4) "sina"
192.168.4.51:6351> HGETALL wz
1) "baidu"
2) "www.baidu.com"
3) "goole"
4) "www.g.cn"
5) "jd"
6) "www.jd.com"
7) "sina"
8) "www.sina.com"
192.168.4.51:6351> HDEL wz jd baidu
(integer) 2
192.168.4.51:6351> HGETALL wz
1) "goole"
2) "www.g.cn"
3) "sina"
4) "www.sina.com"
192.168.4.51:6351>