redis 主从复制 RDB AOF

还原51-52成为独立的redis服务器

# /etc/init.d/redis_6379 stop
# rm -rf /var/lib/redis/6379/*
# vim /etc/redis/6379.conf 注释掉3行集群配置
#cluster-enabled yes
#cluster-config-file nodes-6351.conf
#cluster-node-timeout 5000

# /etc/init.d/redis_6379 start

# redis-cli -c -h 192.168.4.52 -p 6352
192.168.4.52:6352> CLUSTER INFO
ERR This instance has cluster support disabled 查看到这个提示就对了
########################################################################
50

# /etc/init.d/redis_6379 stop
# redis-cli -h 192.168.4.51 -p 6351

192.168.4.51:6351> keys *
(empty list or set)
192.168.4.51:6351> set x 1
OK
192.168.4.51:6351> set y 2
OK
192.168.4.51:6351> keys *
1) "x"
2) "y"
192.168.4.51:6351> exit


# redis-cli -h 192.168.4.52 -p 6352
192.168.4.52:6352> keys *
(empty list or set)
192.168.4.52:6352> exit
############################################################################
51-52 在没配置时,每个redis服务器都认为自己是主库

# redis-cli -c -h 192.168.4.51 -p 6351
192.168.4.51:6351> keys *
1) "x"
2) "y"

192.168.4.51:6351> INFO repliction 这个是看是否是主库,如果打错了单词repliction,它就会没有显示结果。
192.168.4.51:6351> info replication 这个是看是否是主库
# Replication
role:master 角色:主库
connected_slaves:0 从库个数:0
master_replid:eb8c8ba721e4b63699a4c49459cb59f39e20508d
master_replid2:0000000000000000000000000000000000000000
master_repl_offset:0
second_repl_offset:-1
repl_backlog_active:0
repl_backlog_size:1048576
repl_backlog_first_byte_offset:0
repl_backlog_histlen:0
##########################################################################
52


# redis-cli -c -h 192.168.4.52 -p 6352
192.168.4.52:6352> keys *
(empty list or set)

192.168.4.52:6352> info replication
# Replication
role:master 角色:主库
connected_slaves:0
master_replid:2ff0d784de32ac84cb8ecbdbde7bcc649eaf285f
master_replid2:0000000000000000000000000000000000000000
master_repl_offset:0
second_repl_offset:-1
repl_backlog_active:0
repl_backlog_size:1048576
repl_backlog_first_byte_offset:0
repl_backlog_histlen:0

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 主库:192.168.4.51
master_port:6351 主库端口:6351
master_link_status:up 状态:up
master_last_io_seconds_ago:6
master_sync_in_progress:0
slave_repl_offset:14
slave_priority:100
slave_read_only:1
connected_slaves:0
master_replid:b747da698cbb9b02eed088fee0a4da9ae7fd47ac
master_replid2:0000000000000000000000000000000000000000
master_repl_offset:14
second_repl_offset:-1
repl_backlog_active:1
repl_backlog_size:1048576
repl_backlog_first_byte_offset:1
repl_backlog_histlen:14
192.168.4.52:6352> keys *
1) "y"
2) "x"
############################################################################
51

192.168.4.51:6351> info replication
# Replication
role:master 角色:主库
connected_slaves:1 从库个数:1
slave0:ip=192.168.4.52,port=6352,state=online,offset=70,lag=1
master_replid:b747da698cbb9b02eed088fee0a4da9ae7fd47ac
master_replid2:0000000000000000000000000000000000000000
master_repl_offset:70
second_repl_offset:-1
repl_backlog_active:1
repl_backlog_size:1048576
repl_backlog_first_byte_offset:1
repl_backlog_histlen:70
############################################################################
50

# redis-cli -h 192.168.4.51 -p 6351
192.168.4.51:6351> keys *
1) "x"
2) "y"
192.168.4.51:6351> set z 3
OK
192.168.4.51:6351> set i 4
OK
192.168.4.51:6351> set j 5
OK
192.168.4.51:6351> keys *
1) "z"
2) "i"
3) "x"
4) "y"
5) "j"
##################################################################
52

192.168.4.52:6352> keys *
1) "z"
2) "x"
3) "j"
4) "y"
5) "i"
192.168.4.52:6352> get i
"4"
192.168.4.52:6352> exit
##################################################################
52

# /etc/init.d/redis_6379 stop

# vim /etc/redis/6379.conf 改配置,永久有效
282 slaveof 192.168.4.51 6351 去掉注释# slaveof <masterip> <masterport>尖角号的意思是一定要写这一项

# /etc/init.d/redis_6379 start
# redis-cli -c -h 192.168.4.52 -p 6352
192.168.4.52:6352> info replication
# Replication
role:slave
master_host:192.168.4.51
master_port:6351
master_link_status:up
master_last_io_seconds_ago:8
master_sync_in_progress:0
slave_repl_offset:1504
slave_priority:100
slave_read_only:1
connected_slaves:0
master_replid:b747da698cbb9b02eed088fee0a4da9ae7fd47ac
master_replid2:0000000000000000000000000000000000000000
master_repl_offset:1504
second_repl_offset:-1
repl_backlog_active:1
repl_backlog_size:1048576
repl_backlog_first_byte_offset:1491
repl_backlog_histlen:14

192.168.4.52:6352> keys *
1) "y"
2) "x"
3) "i"
4) "z"
5) "j"

192.168.4.52:6352> keys *
1) "k"
2) "y"
3) "x"
4) "i"
5) "z"
6) "j"
192.168.4.52:6352> get k
"6"
192.168.4.52:6352> slaveof no one 本次临时生效,重启失效。就是52成为主库
OK
192.168.4.52:6352> info replication
# Replication
role:master
connected_slaves:0
master_replid:710a904d8daa8278736c21dcc0b6a17d2a14a245
master_replid2:b747da698cbb9b02eed088fee0a4da9ae7fd47ac
master_repl_offset:1806
second_repl_offset:1807
repl_backlog_active:1
repl_backlog_size:1048576
repl_backlog_first_byte_offset:1491
repl_backlog_histlen:316
192.168.4.52:6352> set h 7
OK
192.168.4.52:6352> keys *
1) "k"
2) "y"
3) "x"
4) "i"
5) "z"
6) "h"
7) "j"
192.168.4.52:6352> exit

# /etc/init.d/redis_6379 restart
Stopping ...
Redis stopped
Starting Redis server...

# redis-cli -c -h 192.168.4.52 -p 6352
192.168.4.52:6352> info replication
# Replication
role:slave
master_host:192.168.4.51
master_port:6351
master_link_status:up
master_last_io_seconds_ago:5
master_sync_in_progress:0
slave_repl_offset:1834
slave_priority:100
slave_read_only:1
connected_slaves:0
master_replid:b747da698cbb9b02eed088fee0a4da9ae7fd47ac
master_replid2:0000000000000000000000000000000000000000
master_repl_offset:1834
second_repl_offset:-1
repl_backlog_active:1
repl_backlog_size:1048576
repl_backlog_first_byte_offset:1807
repl_backlog_histlen:28
192.168.4.52:6352> set o 9
(error) READONLY You can't write against a read only slave.
192.168.4.52:6352> exit

# vim /etc/redis/6379.conf

# /etc/init.d/redis_6379 restart

# redis-cli -c -h 192.168.4.52 -p 6352
192.168.4.52:6352> info replication
# Replication
role:master
connected_slaves:0
master_replid:183cd3a65a93b07ecdb6ca8335b4e2035abc50e2
master_replid2:0000000000000000000000000000000000000000
master_repl_offset:0
second_repl_offset:-1
repl_backlog_active:0
repl_backlog_size:1048576
repl_backlog_first_byte_offset:0
repl_backlog_histlen:0
################################################################
51 给主库加上密码

# /etc/init.d/redis_6379 stop 关闭服务
# vim /etc/redis/6379.conf 改配置
501 requirepass 123456 修改501行,打开注释

# redis-cli -h 192.168.4.51 -p 6351 -a 123456 连接时,加-a 密码123456
192.168.4.51:6351> keys *
1) "j"
2) "x"
3) "z"
4) "y"
5) "i"
6) "k"
192.168.4.51:6351> exit


# sed -n '501p' /etc/redis/6379.conf 指定看文件里的501行的内容
requirepass 123456

# /etc/init.d/redis_6379 stop 关闭服务
# vim /etc/init.d/redis_6379 修改脚本,默认连接时加上密码
43 $CLIEXEC -h 192.168.4.51 -p 6351 -a 123456 shutdown


# sed -n '43p' /etc/init.d/redis_6379 指定看文件里的43行的内容
$CLIEXEC -h 192.168.4.51 -p 6351 -a 123456 shutdown

# /etc/init.d/redis_6379 start 开启服务
###########################################################################
52

# /etc/init.d/redis_6379 stop
# vim /etc/redis/6379.conf
289 # masterauth <master-password>

289 masterauth "123456"

# /etc/init.d/redis_6379 start

# redis-cli -h 192.168.4.52 -p 6352
192.168.4.52:6352> info replication
# Replication
role:slave
master_host:192.168.4.51
master_port:6351
master_link_status:up
master_last_io_seconds_ago:8
master_sync_in_progress:0
slave_repl_offset:28
slave_priority:100
slave_read_only:1
connected_slaves:0
master_replid:600ebe0b6f076a7a260b3dc97433dc3a2ef80aac
master_replid2:0000000000000000000000000000000000000000
master_repl_offset:28
second_repl_offset:-1
repl_backlog_active:1
repl_backlog_size:1048576
repl_backlog_first_byte_offset:1
repl_backlog_histlen:28
192.168.4.52:6352>
###########################################################
53 成为从库

# redis-cli -c -h 192.168.4.53 -p 6353
192.168.4.53:6353> INFO replication 当前还是独立的库
# Replication
role:master 角色:主库
connected_slaves:0
master_replid:84b249ad58ab2cda4ca6a91330865ea0baec336d
master_replid2:0000000000000000000000000000000000000000
master_repl_offset:5936
second_repl_offset:-1
repl_backlog_active:1
repl_backlog_size:1048576
repl_backlog_first_byte_offset:1
repl_backlog_histlen:5936

192.168.4.53:6353> keys * 当前库是空的
(empty list or set)
192.168.4.53:6353> exit 退出

# /etc/init.d/redis_6379 stop 停止服务

# vim /etc/redis/6379.conf 改配置
282 slaveof 192.168.4.51 6351 成为51的从
289 masterauth "123456" 指定51的密码是123456

# sed -n '282p' /etc/redis/6379.conf 查看文件里的指定行数
slaveof 192.168.4.51 6351

# sed -n '289p' /etc/redis/6379.conf
masterauth "123456"

# redis-cli -c -h 192.168.4.53 -p 6353
192.168.4.53:6353> info replication 查看到已经是51的从了
# Replication
role:slave 角色:从库
master_host:192.168.4.51 主库:192.168.4.51
master_port:6351 主库端口:6351
master_link_status:up 连接主库的状态:开启
master_last_io_seconds_ago:9
master_sync_in_progress:0
slave_repl_offset:448
slave_priority:100
slave_read_only:1
connected_slaves:0
master_replid:600ebe0b6f076a7a260b3dc97433dc3a2ef80aac
master_replid2:0000000000000000000000000000000000000000
master_repl_offset:448
second_repl_offset:-1
repl_backlog_active:1
repl_backlog_size:1048576
repl_backlog_first_byte_offset:435
repl_backlog_histlen:14

###########################################################
51 能看到有2个从库了:52,53


# redis-cli -h 192.168.4.51 -p 6351 -a 123456
192.168.4.51:6351> info replication
# Replication
role:master 角色:主库
connected_slaves:2 从库个数:2个
slave0:ip=192.168.4.52,port=6352,state=online,offset=504,lag=0
slave1:ip=192.168.4.53,port=6353,state=online,offset=504,lag=1
master_replid:600ebe0b6f076a7a260b3dc97433dc3a2ef80aac
master_replid2:0000000000000000000000000000000000000000
master_repl_offset:504
second_repl_offset:-1
repl_backlog_active:1
repl_backlog_size:1048576
repl_backlog_first_byte_offset:1
repl_backlog_histlen:504
##################################################################
53

192.168.4.53:6353> set masterauth "123456" 这个只是设置了变量,根本和修改配置文件里的密码没有任何关系!
OK
192.168.4.53:6353> set conf masterauth="123456" 这个只是设置了变量,根本和修改配置文件里的密码没有任何关系!
OK
#####################################################################
53 先取消成为51的从,然后再成为52的从

先取消成为51的从,就是注释配置的那2行
# /etc/init.d/redis_6379 stop 停止服务
# vim /etc/redis/6379.conf 改配置
282 # slaveof 192.168.4.51 6351 注释282行,主库的ip和端口
289 # masterauth "123456" 注释289行,主库的密码

# /etc/init.d/redis_6379 start 启动服务,成为独立的库

# redis-cli -c -h 192.168.4.53 -p 6353
192.168.4.53:6353> info replication 能看到是独立的库了


然后再成为52的从
# /etc/init.d/redis_6379 stop 停止服务
# vim /etc/redis/6379.conf 改配置
282 slaveof 192.168.4.52 6352 修改282行,成为52的从。不用指定密码,因为52没有密码。

# /etc/init.d/redis_6379 start 启动服务

# redis-cli -c -h 192.168.4.53 -p 6353
192.168.4.53:6353> info replication 能看到成为52的从了
# Replication
role:slave 角色:从库
master_host:192.168.4.52 主库:192.168.4.52
master_port:6352 主库端口:6352
master_link_status:up 连接主库的状态:开启
master_last_io_seconds_ago:7
master_sync_in_progress:0
slave_repl_offset:2254
slave_priority:100
slave_read_only:1
connected_slaves:0
master_replid:600ebe0b6f076a7a260b3dc97433dc3a2ef80aac
master_replid2:0000000000000000000000000000000000000000
master_repl_offset:2254
second_repl_offset:-1
repl_backlog_active:1
repl_backlog_size:1048576
repl_backlog_first_byte_offset:2241
repl_backlog_histlen:14
####################################################################
50
# redis-cli -h 192.168.4.51 -p 6351
192.168.4.51:6351> keys *
(error) NOAUTH Authentication required.
192.168.4.51:6351> auth 123456
OK
192.168.4.51:6351> keys *
1) "j"
2) "y"
3) "k"
4) "i"
5) "z"
6) "x"
192.168.4.51:6351> set v1 1 建立一个新的变量v1,在51-53都可以看到这个变量
OK
192.168.4.51:6351> get v1
"1"
######################################################################
51-53 都能看到客户端刚建立的变量v1

192.168.4.51:6351> keys *
1) "j"
2) "y"
3) "k"
4) "i"
5) "z"
6) "x"
7) "v1" 能看到客户端刚建立的变量v1
192.168.4.51:6351> get v1 能看到v1的值
"1"
#####################################################################
52 它状态依然是从,因为它的主库是51,但能查到52自己有一个从库53

192.168.4.52:6352> info replication
# Replication
role:slave 角色:从库
master_host:192.168.4.51 主库:192.168.4.51
master_port:6351 主库端口:6351
master_link_status:up 连接主库的状态:开启
master_last_io_seconds_ago:5
master_sync_in_progress:0
slave_repl_offset:2781
slave_priority:100
slave_read_only:1 从库只读:开启(1)
connected_slaves:1 连接从库的状态:开启
slave0:ip=192.168.4.53,port=6353,state=online,offset=2781,lag=1 从库的信息:能看到从库53的ip和端口,状态
master_replid:600ebe0b6f076a7a260b3dc97433dc3a2ef80aac
master_replid2:0000000000000000000000000000000000000000
master_repl_offset:2781
second_repl_offset:-1
repl_backlog_active:1
repl_backlog_size:1048576
repl_backlog_first_byte_offset:1
repl_backlog_histlen:2781
#########################################################################
54

# /etc/init.d/redis_6379 stop
# rm -rf /var/lib/redis/6379/*

# vim /etc/redis/6379.conf 注释掉3行集群配置
#cluster-enabled yes
#cluster-config-file nodes-6351.conf
#cluster-node-timeout 5000

# /etc/init.d/redis_6379 start

# redis-cli -h 192.168.4.54 -p 6354
192.168.4.54:6354> keys *
(empty list or set)
192.168.4.54:6354> set a1 1
OK
192.168.4.54:6354> set a2 2
OK
192.168.4.54:6354> keys *
1) "a1"
2) "a2"

192.168.4.54:6354> info replication
# Replication
role:master
connected_slaves:0
master_replid:e96da56242aa23666305780d5efb56b45b4a06d2
master_replid2:0000000000000000000000000000000000000000
master_repl_offset:0
second_repl_offset:-1
repl_backlog_active:0
repl_backlog_size:1048576
repl_backlog_first_byte_offset:0
repl_backlog_histlen:0

192.168.4.54:6354> config set masterauth "123456" 命令行设置主库51的密码,本次生效,重启失效。没有写入配置文件
OK
192.168.4.54:6354> slaveof 192.168.4.51 6351 命令行配置成为从库,临时生效,重启失效。
OK

192.168.4.54:6354> info replication
# Replication
role:slave
master_host:192.168.4.51
master_port:6351
master_link_status:up
master_last_io_seconds_ago:1
master_sync_in_progress:0
slave_repl_offset:5973
slave_priority:100
slave_read_only:1
connected_slaves:0
master_replid:600ebe0b6f076a7a260b3dc97433dc3a2ef80aac
master_replid2:0000000000000000000000000000000000000000
master_repl_offset:5973
second_repl_offset:-1
repl_backlog_active:1
repl_backlog_size:1048576
repl_backlog_first_byte_offset:5974
repl_backlog_histlen:0

192.168.4.54:6354> keys *
1) "x"
2) "z"
3) "j"
4) "i"
5) "k"
6) "y"
7) "v1"
##############################################################################
redis,在命令行改配置项!它不会写入配置文件!但当前是能生效的!仅本次生效!重启服务失效!

如指定主库的密码,config set masterauth "123456"
然后指定主库是谁slaveof 192.168.4.51 6351

192.168.4.54:6354>config set masterauth "123456" 一定要先指定主库的密码
192.168.4.54:6354>slaveof 192.168.4.51 6351 然后再指定主库是谁
192.168.4.54:6354> info replication 查看当前状态
# Replication
role:slave 成为了52的从库
master_host:192.168.4.51
master_port:6351
master_link_status:up
master_last_io_seconds_ago:4
master_sync_in_progress:0
slave_repl_offset:8110
slave_priority:100
slave_read_only:1
connected_slaves:0
master_replid:600ebe0b6f076a7a260b3dc97433dc3a2ef80aac
master_replid2:0000000000000000000000000000000000000000
master_repl_offset:8110
second_repl_offset:-1
repl_backlog_active:1
repl_backlog_size:1048576
repl_backlog_first_byte_offset:8111
repl_backlog_histlen:0

192.168.4.54:6354> keys *
1) "z"
2) "i"
3) "k"
4) "y"
5) "x"
6) "v2" 如果客户端连接主库51,新写入一个变量v2。这里也是能看到的。说明本次命令行的配置生效了。
7) "j"
8) "v1"

# /etc/init.d/redis_6379 restart 重启服务

# redis-cli -h 192.168.4.54 -p 6354
192.168.4.54:6354> info replication 当前又成为独立的库了。说明命令行配置成为从库,重启失效。
# Replication
role:master
###########################################################################
54 成为独立的库,并关掉redis服务 (此步骤省略)

55,56 先恢复成独立的库,然后在命令行设置,56成为55的从库,不用重启服务就马上生效!

# rm -rf /var/lib/redis/6379/*
# vim /etc/redis/6379.conf 注释那3行集群的配置
#cluster-enabled yes
#cluster-config-file nodes-6351.conf
#cluster-node-timeout 5000

# /etc/init.d/redis_6379 start
# ss -tunlp | grep redis-server 没有那个1万的集群端口,就证明是独立的库
#########################################################################
55

# redis-cli -h 192.168.4.55 -p 6355
192.168.4.55:6355> info replication
# Replication
role:master
connected_slaves:0
... ...

192.168.4.55:6355> config set requirepass "123456" 命令行设密码,本次生效,重启失效
OK
#########################################################################
50

# redis-cli -h 192.168.4.55 -p 6355
192.168.4.55:6355> keys * 因为55设密码了,所以没有权限查看
(error) NOAUTH Authentication required.

192.168.4.55:6355> auth 123456 命令行输入密码
OK
192.168.4.55:6355> keys *
(empty list or set)

###########################################################################
54

# cp sentinel.conf /etc/sentinel.conf
# vim /etc/sentinel.conf
# cat /etc/sentinel.conf
bind 0.0.0.0 本机的所有地址
sentinel monitor redis55 192.168.4.55 6355 1 sentinal(哨兵的) monitor(监控)
sentinel auth-pass redis55 123456


# redis-sentinel /etc/sentinel.conf 指定这个哨兵读这个文件,他才知道要监视哪一个
5982:X 03 Dec 19:09:54.525 # oO0OoO0OoO0Oo Redis is starting oO0OoO0OoO0Oo
5982:X 03 Dec 19:09:54.525 # Redis version=4.0.8, bits=64, commit=00000000, modified=0, pid=5982, just started
5982:X 03 Dec 19:09:54.525 # Configuration loaded
5982:X 03 Dec 19:09:54.527 * Increased maximum number of open files to 10032 (it was originally set to 1024).
_._
_.-``__ ''-._
_.-`` `. `_. ''-._ Redis 4.0.8 (00000000/0) 64 bit
.-`` .-```. ```\/ _.,_ ''-._
( ' , .-` | `, ) Running in sentinel mode
|`-._`-...-` __...-.``-._|'` _.-'| Port: 26379
| `-._ `._ / _.-' | PID: 5982
`-._ `-._ `-./ _.-' _.-'
|`-._`-._ `-.__.-' _.-'_.-'|
| `-._`-._ _.-'_.-' | http://redis.io
`-._ `-._`-.__.-'_.-' _.-'
|`-._`-._ `-.__.-' _.-'_.-'|
| `-._`-._ _.-'_.-' |
`-._ `-._`-.__.-'_.-' _.-'
`-._ `-.__.-' _.-'
`-._ _.-'
`-.__.-'

5982:X 03 Dec 19:09:54.529 # WARNING: The TCP backlog setting of 511 cannot be enforced because /proc/sys/net/core/somaxconn is set to the lower value of 128.
5982:X 03 Dec 19:09:54.570 # Sentinel ID is 3e215696fbdd55868251043dc4ec7c38e6b1c12e
5982:X 03 Dec 19:09:54.570 # +monitor master redis55 192.168.4.55 6355 quorum 1
5982:X 03 Dec 19:09:54.573 * +slave slave 192.168.4.56:6356 192.168.4.56 6356 @ redis55 192.168.4.55 6355

#########################################################
如果55关闭服务,大概1分钟这样,等哨兵监视到了这个情况,会自动跳出新的提示信息
哨兵会自动将56提升为主库。如果55修好后,会自动成为56的从库,而不会抢夺56的主库地位。

... ...
5982:X 03 Dec 19:11:58.201 # +sdown master redis55 192.168.4.55 6355
5982:X 03 Dec 19:11:58.202 # +odown master redis55 192.168.4.55 6355 #quorum 1/1
5982:X 03 Dec 19:11:58.202 # +new-epoch 1
5982:X 03 Dec 19:11:58.202 # +try-failover master redis55 192.168.4.55 6355
5982:X 03 Dec 19:11:58.240 # +vote-for-leader 3e215696fbdd55868251043dc4ec7c38e6b1c12e 1
5982:X 03 Dec 19:11:58.240 # +elected-leader master redis55 192.168.4.55 6355
5982:X 03 Dec 19:11:58.240 # +failover-state-select-slave master redis55 192.168.4.55 6355
5982:X 03 Dec 19:11:58.292 # +selected-slave slave 192.168.4.56:6356 192.168.4.56 6356 @ redis55 192.168.4.55 6355
5982:X 03 Dec 19:11:58.292 * +failover-state-send-slaveof-noone slave 192.168.4.56:6356 192.168.4.56 6356 @ redis55 192.168.4.55 6355
5982:X 03 Dec 19:11:58.392 * +failover-state-wait-promotion slave 192.168.4.56:6356 192.168.4.56 6356 @ redis55 192.168.4.55 6355
5982:X 03 Dec 19:11:59.041 # +promoted-slave slave 192.168.4.56:6356 192.168.4.56 6356 @ redis55 192.168.4.55 6355
5982:X 03 Dec 19:11:59.041 # +failover-state-reconf-slaves master redis55 192.168.4.55 6355
5982:X 03 Dec 19:11:59.049 # +failover-end master redis55 192.168.4.55 6355
5982:X 03 Dec 19:11:59.049 # +switch-master redis55 192.168.4.55 6355 192.168.4.56 6356
5982:X 03 Dec 19:11:59.049 * +slave slave 192.168.4.55:6355 192.168.4.55 6355 @ redis55 192.168.4.56 6356
5982:X 03 Dec 19:12:29.104 # +sdown slave 192.168.4.55:6355 192.168.4.55 6355 @ redis55 192.168.4.56 6356


可以ctrl+c 停止,这个哨兵服务也会停止
# ss -tunlp | grep :26379 已经找不到哨兵的端口号26379了


54的哨兵日志文件会自动变成如下内容:
# cat /etc/sentinel.conf
bind 0.0.0.0
sentinel myid 3e215696fbdd55868251043dc4ec7c38e6b1c12e
sentinel monitor redis55 192.168.4.56 6356 1
# Generated by CONFIG REWRITE
port 26379
dir "/root/redis/redis-4.0.8"
sentinel auth-pass redis55 123456
sentinel config-epoch redis55 1
sentinel leader-epoch redis55 1
sentinel known-slave redis55 192.168.4.55 6355
sentinel current-epoch 1
##############################################################################
52 既做从库,又做哨兵(节省服务器)

# vim /etc/sentinel.conf
bind 0.0.0.0
sentinel monitor redis51 192.168.4.51 6351 1
sentinel auth-pass redis51 123456

# redis-sentinel /etc/sentinel.conf 指定这个哨兵读这个文件,他才知道要监视哪一个
##############################################################################
存盘操作和处理客户端的进程不是同一个进程!是2个不同的进程!不占用响应客户端连接的资源!
替换!
先停止服务,再拷贝备份的文件回来,再启动服务!

192.168.4.50:6350> del v10 删除变量用 del 变量名


673 appendonly yes

# sed -n '673p' /etc/redis/6379.conf
appendonly yes
[root@client50 6379]# sed -n '677p' /etc/redis/6379.conf
appendfilename "appendonly.aof"
[root@client50 6379]# ls
appendonly.aof dump.rdb
[root@client50 6379]# redis-cli -h 192.168.4.50 -p 6350 -a 123456
192.168.4.50:6350> keys *
(empty list or set)
192.168.4.50:6350>


AOF优先,一定要和RDB一起启动!
AOF连flushall删除的操作都会记录!所以回复一定要先删掉flushall

 

appendfsync no 记录命令到AOF文件,但是不做存盘操作,也就是不录入dump

默认appendfsync everysec 每秒都记录并存盘


当前60MB,整理后50MB
如果百分比是80%,那么当到达第二次的大小50*80%时,再整理一次
################################################################
EX秒,PX毫秒,NX该变量的值不存在时才创建。加XX,和不加XX都是不一样,如果变量存在,都会覆盖。


append yy 99 若不存在,则创建并赋值
(integer) 2


192.168.4.50:6350> keys *
1) "i"
2) "y"
3) "x"
192.168.4.50:6350> set tel 18212340000
OK
192.168.4.50:6350> setrange tel 4 ****
(integer) 11
192.168.4.50:6350> get tel
"1821****000"
192.168.4.50:6350> get y
"1"
192.168.4.50:6350> get x
"1"
192.168.4.50:6350> strlen tel
(integer) 11
192.168.4.50:6350> strlen y
(integer) 1
192.168.4.50:6350> strlen x
(integer) 1
192.168.4.50:6350>

 

192.168.4.50:6350> strlen y
(integer) 1
192.168.4.50:6350> append y 222
(integer) 4
192.168.4.50:6350> strlen y
(integer) 4
192.168.4.50:6350> keys *
1) "tel"
2) "y"
3) "i"
4) "x"
192.168.4.50:6350> append yy 99
(integer) 2

 

setbit存储单位为位,节省存储空间。1字节=8位。如记录淘宝登陆次数,1年365天,也就365位,也就45个字节。

 

192.168.4.50:6350> decr x
(error) ERR value is not an integer or out of range
192.168.4.50:6350> get x
"1aa"
192.168.4.50:6350> set x 99
OK
192.168.4.50:6350> get x
"99"
192.168.4.50:6350> decr x
(integer) 98
192.168.4.50:6350> decr x
(integer) 97
192.168.4.50:6350> decrby x 20
(integer) 77
192.168.4.50:6350> decrby x 70
(integer) 7
变量从0位开始计算。从第几个字符开始,到第几个结束。从左往右,0是第一个!
负数是倒过来获取,从右往左

incrby只能加整数,默认加1
incrbyfloat能加小数

mset一起定义多个变量
mget一起获取多个变量


192.168.4.50:6350> lpush stuname tom sam haha lili zhangsan
(integer) 5
192.168.4.50:6350> lrange stuname 0 -1
1) "zhangsan"
2) "lili"
3) "haha"
4) "sam"
5) "tom"
192.168.4.50:6350> rpush stuname lisi jerry harry
(integer) 8
192.168.4.50:6350> lrange stuname 0 -1
1) "zhangsan"
2) "lili"
3) "haha"
4) "sam"
5) "tom"
6) "lisi"
7) "jerry"
8) "harry"
192.168.4.50:6350> lpop stuname
"zhangsan"
192.168.4.50:6350> rpop stuname
"harry"
192.168.4.50:6350> lrange stuname 0 -1
1) "lili"
2) "haha"
3) "sam"
4) "tom"
5) "lisi"
6) "jerry"
192.168.4.50:6350>
192.168.4.50:6350> lrange stuname 0 -1
1) "lili"
2) "haha"
3) "sam"
4) "tom"
5) "lisi"
6) "jerry"
192.168.4.50:6350> lrange stuname 0 2
1) "lili"
2) "haha"
3) "sam"
192.168.4.50:6350> lrange stuname -3 -1
1) "tom"
2) "lisi"
3) "jerry"
192.168.4.50:6350>

 

192.168.4.50:6350> lpush list a b c d e f g h i j k
(integer) 11

192.168.4.50:6350> get list
(error) WRONGTYPE Operation against a key holding the wrong kind of value

192.168.4.50:6350> lrange list 0 -1
1) "k"
2) "j"
3) "i"
4) "h"
5) "g"
6) "f"
7) "e"
8) "d"
9) "c"
10) "b"
11) "a"

192.168.4.50:6350> lindex list -1
"a"
192.168.4.50:6350> lindex list 0
"k"
192.168.4.50:6350> lindex list -2
"b"

 

192.168.4.50:6350> lrange list 0 1
1) "k"
2) "j"

192.168.4.50:6350> lrange list -3 -1
1) "c"
2) "b"
3) "a"


192.168.4.50:6350> lrange list 2 4
1) "i"
2) "h"
3) "g"


192.168.4.50:6350> type 0
string
192.168.4.50:6350> type list
list

 

不要轻易在redis上执行slushall!!!清空的意思!!!AOF记录这条命令,也会记入dump文件!


192.168.4.50:6350> hset site goole www.goole.cn
(integer) 1
192.168.4.50:6350> hset site baidu www.baidu.com
(integer) 1
192.168.4.50:6350> hset site taobao www.taobao.com
(integer) 1
192.168.4.50:6350> hset site tmall www.tmall.com
(integer) 1
192.168.4.50:6350> hset site jd www.jd.com
(integer) 1
192.168.4.50:6350> hgetall site
1) "goole"
2) "www.goole.cn"
3) "baidu"
4) "www.baidu.com"
5) "taobao"
6) "www.taobao.com"
7) "tmall"
8) "www.tmall.com"
9) "jd"
10) "www.jd.com"

192.168.4.50:6350> hdel site goole
(integer) 1

192.168.4.50:6350> hgetall site
1) "baidu"
2) "www.baidu.com"
3) "taobao"
4) "www.taobao.com"
5) "tmall"
6) "www.tmall.com"
7) "jd"
8) "www.jd.com"

192.168.4.50:6350> hkeys site
1) "baidu"
2) "taobao"
3) "tmall"
4) "jd"

192.168.4.50:6350> hvals site
1) "www.baidu.com"
2) "www.taobao.com"
3) "www.tmall.com"
4) "www.jd.com"


192.168.4.50:6350> hset site pdd www.pingduoduo.com wph www.weipinhui.com tmooc www.tmooc.cn
(integer) 3

192.168.4.50:6350> hkeys site
1) "baidu"
2) "taobao"
3) "tmall"
4) "jd"
5) "pdd"
6) "wph"
7) "tmooc"

192.168.4.50:6350> hdel site tmall pdd wph tmooc
(integer) 4

192.168.4.50:6350> hkeys site
1) "baidu"
2) "taobao"
3) "jd"


192.168.4.50:6350> hmget site baidu jd
1) "www.baidu.com"
2) "www.jd.com"

192.168.4.50:6350> hmget taobao jd
1) (nil)

不管是什么类型的变量,它本质上都是一个变量,就可以用del来删除!

192.168.4.50:6350> keys * 查看当前已经设置的所有变量
1) "yy"
2) "a"
3) "site"

192.168.4.50:6350> type yy
list 变量yy的类型是list(列表)
192.168.4.50:6350> type a
string 变量a的类型是string(字符串)
192.168.4.50:6350> type site
hash 变量site的类型是hash

192.168.4.50:6350> del yy a site 用del同时删除多个不同类型的变量
(integer) 3 integer(整数),后面的数字3,表示已经成功删除了3个变量!
192.168.4.50:6350> keys * 查看当前已经设置的所有变量
(empty list or set) 当前是空的,没有任何一个变量。

192.168.4.50:6350> del yy a site 当前这3个变量是不存在的!它们已被删除!
(integer) 0 所以 (integer)后面的数字是0,代表删除了0个。也就是没有删除任何一个变量!
192.168.4.50:6350> type site 当前site这个变量是不存在的,早就被删除了!
none 所以查看类型时,它显示:无。
192.168.4.50:6350> get site 查看变量的值时
(nil) 这个变量是不存在的,所以显示:空

 


管理命令总结:

删除一个或多个key:
del key [key...]

测试一个key是否存在
exists key

192.168.4.50:6350> keys *
1) "a"
192.168.4.50:6350> exists a
(integer) 1 如果存在,返回的是1
192.168.4.50:6350> exists yy
(integer) 0 如果不存在,返回的是0


设置key的生存周期,单位秒
expire key seconds

192.168.4.50:6350> get a
"1"
192.168.4.50:6350> expire a 10 设置a的生命周期是10秒
(integer) 1

查看key的生命周期
ttl key

192.168.4.50:6350> ttl a
(integer) 8
192.168.4.50:6350> ttl a
(integer) 1
192.168.4.50:6350> ttl a
(integer) -2

 

 

Redis 键(key)
Redis 键命令用于管理 redis 的键。

语法
Redis 键命令的基本语法如下:
redis 127.0.0.1:6379> COMMAND KEY_NAME
实例
redis 127.0.0.1:6379> SET w3ckey redis
OK
redis 127.0.0.1:6379> DEL w3ckey
(integer) 1
在以上实例中 DEL 是一个命令, w3ckey 是一个键。 如果键被删除成功,命令执行后输出 (integer) 1,否则将输出 (integer) 0

Redis keys 命令
下表给出了与 Redis 键相关的基本命令:
序号 命令及描述
1 DEL key 该命令用于在 key 存在是删除 key。
2 DUMP key 序列化给定 key ,并返回被序列化的值。
3 EXISTS key 检查给定 key 是否存在。
4 EXPIRE key seconds 为给定 key 设置过期时间。
5 EXPIREAT key timestamp EXPIREAT 的作用和 EXPIRE 类似,都用于为 key 设置过期时间。 不同在于 EXPIREAT 命令接受的时间参数是 UNIX 时间戳(unix timestamp)。
6 PEXPIRE key milliseconds 设置 key 的过期时间亿以毫秒计。
7 PEXPIREAT key milliseconds-timestamp 设置 key 过期时间的时间戳(unix timestamp) 以毫秒计
8 KEYS pattern 查找所有符合给定模式( pattern)的 key 。
9 MOVE key db 将当前数据库的 key 移动到给定的数据库 db 当中。
10 PERSIST key 移除 key 的过期时间,key 将持久保持。
11 PTTL key 以毫秒为单位返回 key 的剩余的过期时间。
12 TTL key 以秒为单位,返回给定 key 的剩余生存时间(TTL, time to live)。
13 RANDOMKEY 从当前数据库中随机返回一个 key 。
14 RENAME key newkey 修改 key 的名称
15 RENAMENX key newkey 仅当 newkey 不存在时,将 key 改名为 newkey 。
16 TYPE key 返回 key 所储存的值的类型。

 

posted @ 2019-04-30 22:37  安于夏  阅读(621)  评论(0编辑  收藏  举报