- 五大数据类型—开发必会的技能
- 安全启动与配置文件
- 数据持久化[RDB & AOF]
- 高可用—哨兵
- redis-cluster配置 - py鱼 - 博客园 (cnblogs.com)]">集群[redis-cluster配置 - py鱼 - 博客园 (cnblogs.com)]
五大数据类型—开发必会的技能
string
字符串类型hash
哈希类型,如同python的dict
类型Set
无序集合Zset
有序集合List
, 双向队列,向左插入数据,向右插入数据,向左右读取数据
reference redis学习 - py鱼 - 博客园 (cnblogs.com)
安全启动与配置文件
common sense
1. Linux如何安装redis呢?
1. rpm包安装
2. yum 自动化安装, 在阿里云yum仓库中,存有redis软件包
3. 源代码编译安装
2. outer knowledge
1. 内存型数据库,断电,进程重启--->数据丢失
2. 配置数据持久化防止数据丢失
3. 支持数据备份,也就是matser-slave主从模式
4. 读写分离,防止单点故障,数据丢失
3. example of appication
1. 新浪微博,微信红包,淘宝轮播图;
installation-1
yum -y install redis
installation-2
1. 下载redis源码
wget http://download.redis.io/releases/redis-4.0.10.tar.gz
2. 解压缩
tar -zxf redis-4.0.10.tar.gz
3. 切换redis源码目录
cd redis-4.0.10.tar.gz
4. 编译源文件
make
5. 编译好后,src/目录下有编译好的redis指令
6. make install 安装到指定目录,默认在/usr/local/bin
configuration of function
remote connection
modify default port
# Accept connections on the specified port, default is 6379 (IANA #815344).
# If port 0 is specified Redis will not listen on a TCP socket.
port 6500
set password
# Warning: since Redis is pretty fast an outside user can try up to
# 150k passwords per second against a good box. This means that you should
# use a very strong password otherwise it will be very easy to break.
#
# requirepass redis1114
start security mode
# By default protected mode is enabled. You should disable it only if
# you are sure you want clients from other hosts to connect to Redis
# even if no authentication is configured, nor a specific set of interfaces
# are explicitly listed using the "bind" directive.
protected-mode yes
run by daemonization
# By default Redis does not run as a daemon. Use 'yes' if you need it.
# Note that Redis will write a pid file in /var/run/redis.pid when daemonized.
daemonize yes
usage
[root@vvkt7whznuckhiz2-0723575 bin]# pwd
/opt/redis/bin
[root@vvkt7whznuckhiz2-0723575 bin]# redis-server /etc/redis.conf
[root@vvkt7whznuckhiz2-0723575 bin]# redis-cli -p 6500 -h 192.168.1.9
192.168.1.9:6500> auth redis1114
OK
192.168.1.9:6500> ping
PONG
usual command
# 列出所有的的key
keys *
数据持久化[RDB & AOF]
introduction
一主一从比较简单,但可用性不高,最好是一主多从
test1 —[Why cofigurate data persistence]
先向数据库写入信息,然后重启进程,看是否丢失数据?[显而易见-absolutely]
设计实验:
1. 关闭redis所有进程 pkill -9 redfis
2. 进入 / 目录,创建 mkdir s25redis
3. 创建配置文件 vim redis_no_pr.conf
conetent : bind 127.0.0.1 daemonize yes
command
[root@vvkt7whznuckhiz2-0723575 /]# mkdir s25redis
[root@vvkt7whznuckhiz2-0723575 /]# cd s25redis
[root@vvkt7whznuckhiz2-0723575 s25redis]# ls
[root@vvkt7whznuckhiz2-0723575 s25redis]# vim redis_no_per.conf
[root@vvkt7whznuckhiz2-0723575 s25redis]# /opt/redis/bin/redis-server /s25redis/redis_no_per.conf
*** FATAL CONFIG FILE ERROR (Redis 6.2.6) ***
Reading the configuration file, at line 2
>>> 'damonize yes'
Bad directive or wrong number of arguments
[root@vvkt7whznuckhiz2-0723575 s25redis]# vim redis_no_per.conf
[root@vvkt7whznuckhiz2-0723575 s25redis]# cat redis_no_per.conf
bind 127.0.0.1
daemonize yes
[root@vvkt7whznuckhiz2-0723575 s25redis]# /opt/redis/bin/redis-server /s25redis/redis_no_per.conf
[root@vvkt7whznuckhiz2-0723575 s25redis]# redis-cli
127.0.0.1:6379> set name alex
OK
127.0.0.1:6379> set name1 cs
OK
127.0.0.1:6379> keys *
1) "name1"
2) "name"
127.0.0.1:6379> quit
[root@vvkt7whznuckhiz2-0723575 s25redis]# pkill -9 redis
[root@vvkt7whznuckhiz2-0723575 s25redis]# /opt/redis/bin/redis-server /s25redis/redis_no_per.conf
[root@vvkt7whznuckhiz2-0723575 s25redis]# redis-cli
127.0.0.1:6379> keys *
(empty array)
127.0.0.1:6379>
reference
1. difference between rdb and aof
https://blog.csdn.net/ljheee/article/details/76284082
RDB[data persistence]
1. 配置RDB机制数据持久化,数据文件为二进制文件,且配置触发机制
cd /s25redis 写入[vim s25rdb.conf]如下配置:
daemonize yes
port 6379
logfile /data/6379/redis.log #指定redis的运行日志存储位置
dir /data/6379 # 指定redis的数据文件的存放路径
dbfilename S25dbmp.rdb # 指定数据持久化的文件名字
bind 10.0.0.10 127.0.0.1
# redis触发save命令的频率
save 900 1 # 900秒内一个修改动作,如set mset del
save 300 10 # 300秒内10个
save 60 10000 # 60秒内10000个操作
2. 创建redis数据文件夹
mkdir -p /s25redis/data/6379
3. check and stop process
ps -ef |grep redis
pkill -9 redis
4. restart process
/opt/redis/bin/redis-server s25redis/s25rdb.conf
5. 存储数据
set name sq
set bilibili os
save
6. 新开窗口,查看/data/6379下的文件
effort
error
*** FATAL CONFIG FILE ERROR (Redis 6.2.6) ***
Reading the configuration file, at line 3
>>> 'logfile "" #指定redis的运行日志存储位置'
Bad directive or wrong number of arguments
effort
1. 在/data/6379下创建redis.log文件
2. 进入 /opt/redis/bin/下使用./redis-server启动
3. 查看权限 ls -ll 该文件具有属主的读写权限
4. 使用 /sur/local/bin/redis-server启动,仍然错误
5. 修改配置文件为 logfile "/data/6379/redis.log"
6. 测试方案 ---> 物理机redis跑一次代码就用默认的redis 3.2.1
feedback
版本差异,该配置在redis3.2.12下并无问题
feedback2—maybe syntax error
AOF
feature
1. AOF(append-only log file)
记录服务器执行的所有变更操作命令(例如set del等),并在服务器启动时,通过重新执行这些命令来还原数据集
AOF 文件中的命令全部以redis协议的格式保存,新命令追加到文件末尾。
优点:最大程序保证数据不丢
缺点:日志记录非常大
2. 比较
rdb:基于快照的持久化,速度更快,一般用作备份,主从复制也是依赖于rdb持久化功能
aof:以追加的方式记录redis操作日志的文件。可以最大程度的保证redis数据安全,类似于mysql的binlog
operation
1. 创建一个文件,写入如下配置
vim redisaof.conf
daemonize yes
bind 192.168.1.9
port 6379
logfile /data/6379/redis.log
dir /data/6379aof/
appendonly yes
appendfsync everysec
2. 创建数据文件夹
mkdir -p /data/6379aof/
3. 检查redis进程是否启动
ps -ef | grep redis
pkill -9 redis
4. 指定配置文件启动redis
/opt/redis/bin/redis-server /s25redis/redisaof.conf
redis-cli -p 6379 -h 192.168.1.9
5. 写入数据
set name wangsic
set tecent bye
6. 实时刷新和查看append文件
cd /data/6379aof
tail -f appendonly.aof
7. 关闭进程查看数据是否存在
>exit
pkill -9 redis
/opt/redis/bin/redis-server /s25redis/redisaof.conf
redis-cli -p 6379 -h 192.168.1.9
> keys *
8. 删除appendonly.aof重启redis
rm -rf /data/6379aof/appendonly.aof
/opt/redis/bin/redis-server /s25redis/redisaof.conf
redis-cli -p 6379 -h 192.168.1.9
> keys *
数据主从同步以及故障
Introduction
在一台机器上运行两个及以上的Redis数据库,是redis支持的多实例功能,基于端口号的不同,就能运行多个相互独立的redis数据库
什么是多实例?
就是机器上运行了多个redis独立的进程,互不干涉的独立的redis数据库;叫做
operation
1. 切换工作目录 创建多个配置文件分别配置端口号,IP地址以及后台启动
cd /s25redis
vim redish1.conf
vim redish2.conf
bind 0.0.0.0
port 6379
daemonize yes
bind 0.0.0.0
port 6399
daemonize yes
2. 分别进入两个数据库写入键值对
redisaof.conf redish1.conf redish2.conf redis_no_per.conf s25rdb.conf
[root@vvkt7whznuckhiz2-0723575 s25redis]# /opt/redis/bin/redis-server /s25redis/redish1.conf
[root@vvkt7whznuckhiz2-0723575 s25redis]# /opt/redis/bin/redis-server /s25redis/redish2.conf
[root@vvkt7whznuckhiz2-0723575 s25redis]# redis-cli -p 6379
127.0.0.1:6379> keys *
(empty array)
127.0.0.1:6379> set name haiyoushei
OK
127.0.0.1:6379> exit
[root@vvkt7whznuckhiz2-0723575 s25redis]# redis-cli -p 6399
127.0.0.1:6399> keys *
(empty array)
127.0.0.1:6399> set nickname lichousha
OK
127.0.0.1:6399> exit
[root@vvkt7whznuckhiz2-0723575 s25redis]# redis-cli -p 6399 get nickname
"lichousha"
[root@vvkt7whznuckhiz2-0723575 s25redis]# redis-cli -p 6379 get name
"haiyoushei"
[root@vvkt7whznuckhiz2-0723575 s25redis]#
3.
operation2 master-slave
1. 准备两个配置文件,分别写入如下内容
pwd /s25redis
vim master_redis.conf
port 6379
daemonize yes
pidfile /s25/6379/redis.pid
loglevel notice
logfile "/data/6379/redis.log"
dbfilename dump.rdb
dir /s25/6379
protected-mode no
2. 准备第二个配置文件
pwd /s25redis
vim slave_redis.conf
port 6389
daemonize yes
pidfile /s25/6389/redis.pid
loglevel notice
logfile "/data/6389/redis.log"
dbfilename dump.rdb
dir /s25/6389
protected-mode no
3. 分别创建两个文件夹存储数据
mkdir -p /s25/{6379,6389}
4. 分别通过配置文件启动redis
/opt/redis/bin/redis-server /s25redis/master_redis.conf
/opt/redis/bin/redis-server /s25redis/slave_redis.conf
5. 检查主从关系
[root@vvkt7whznuckhiz2-0723575 s25redis]# redis-cli -p 6379 info replication
# Replication
role:master
connected_slaves:0
master_failover_state:no-failover
master_replid:25d1bb4b918f0b05b93e93815199b624c43e49e1
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
[root@vvkt7whznuckhiz2-0723575 s25redis]# redis-cli -p 6389 info replication
# Replication
role:master
connected_slaves:0
master_failover_state:no-failover
master_replid:a28d09fae5aaee018b1266ada36f8fc80e074883
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
6. 从数据库绑定主数据库,并且查看从数据库的信息
[root@vvkt7whznuckhiz2-0723575 s25redis]# redis-cli -p 6389 slaveof 127.0.0.1 6379
OK
[root@vvkt7whznuckhiz2-0723575 s25redis]# redis-cli -p 6389 info replication
# Replication
role:slave
master_host:127.0.0.1
master_port:6379
master_link_status:up
master_last_io_seconds_ago:1
master_sync_in_progress:0
slave_read_repl_offset:28
slave_repl_offset:28
slave_priority:100
slave_read_only:1
replica_announced:1
connected_slaves:0
master_failover_state:no-failover
master_replid:672933a58085330846bea78a8b284c6958942037
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
7 检查两数据库内是否存有数据
[root@vvkt7whznuckhiz2-0723575 s25redis]# redis-cli -p 6379
127.0.0.1:6379> keys *
(empty array)
127.0.0.1:6379> exit
[root@vvkt7whznuckhiz2-0723575 s25redis]# redis-cli -p 6389
127.0.0.1:6389> keys *
(empty array)
127.0.0.1:6389> exit
8 在主数据库写入数据,在从数据库读取数据[从数据库不能写入]
[root@vvkt7whznuckhiz2-0723575 s25redis]# redis-cli -p 6379
127.0.0.1:6379> set name alex
OK
127.0.0.1:6379> keys *
1) "name"
127.0.0.1:6379> get name
"alex"
127.0.0.1:6379> exit
[root@vvkt7whznuckhiz2-0723575 s25redis]# redis-cli -p 6389
127.0.0.1:6389> keys *
1) "name"
127.0.0.1:6389> set name1 wupeiqi
(error) READONLY You can't write against a read only replica.
127.0.0.1:6389> exit
[root@vvkt7whznuckhiz2-0723575 s25redis]# redis-cli -p 6379
127.0.0.1:6379> set name2 wupeiqi
OK
127.0.0.1:6379> exit
[root@vvkt7whznuckhiz2-0723575 s25redis]# redis-cli -p 6389
127.0.0.1:6389> keys *
1) "name2"
2) "name"
127.0.0.1:6389> exit
一主多从以及主从故障切换
设置一主多从
1. 创建6399端口的数据库,设置从属关系
cd /s25redis
vim slave_redis2.conf
port 6399
daemonize yes
pidfile /s25/6399/redis.pid
loglevel notice
logfile "/data/6399/redis.log"
dbfilename dump.rdb
dir /s25/6389
protected-mode no
slaveof 127.0.0.1 6379
2. 创建数据文件夹
mkdir -p /s25/6399
3. 分别启动数据库,从6389设置从属于6379
[root@vvkt7whznuckhiz2-0723575 s25redis]# redis-cli -p 6389 slaveof 127.0.0.1 6379
OK
[root@vvkt7whznuckhiz2-0723575 s25redis]# redis-cli -p 6379
127.0.0.1:6379> set addr 188
OK
127.0.0.1:6379> exit
[root@vvkt7whznuckhiz2-0723575 s25redis]# redis-cli -p 6389
127.0.0.1:6389> keys *
1) "name"
2) "age"
3) "addr"
127.0.0.1:6389> exit
[root@vvkt7whznuckhiz2-0723575 s25redis]# redis-cli -p 6399
127.0.0.1:6399> keys *
1) "age"
2) "name"
3) "addr"
127.0.0.1:6399>
设置故障切换
情景模拟
1. 从库故障,新建一个从库,再加入从属关系
2. 主库故障,问题大了咋办?
1. 数据同步了三份,主,从1,从2. 主库还有数据;
2. 解决办法:某一个从库切换为主库,从库2重新分配从属关系 6389 主 9399 从
3. 主库恢复,重新加入主从关系,b从库
1start three databases and check relationship
[root@vvkt7whznuckhiz2-0723575 ~]# /opt/redis/bin/redis-server /s25redis/master_redis.conf
[root@vvkt7whznuckhiz2-0723575 ~]# /opt/redis/bin/redis-server /s25redis/slave_redis.conf
[root@vvkt7whznuckhiz2-0723575 ~]# /opt/redis/bin/redis-server /s25redis/slave_redis2.conf
[root@vvkt7whznuckhiz2-0723575 ~]# ps -ef | grep redis
root 15551 1 0 00:39 ? 00:00:00 /opt/redis/bin/redis-server *:6379
root 15565 1 0 00:39 ? 00:00:00 /opt/redis/bin/redis-server *:6389
root 15575 1 0 00:39 ? 00:00:00 /opt/redis/bin/redis-server *:6399
root 15590 15288 0 00:39 pts/0 00:00:00 grep --color=auto redis
[root@vvkt7whznuckhiz2-0723575 ~]# redis-cli -p 6379 info replication
# Replication
role:master
connected_slaves:1
slave0:ip=127.0.0.1,port=6399,state=online,offset=56,lag=1
master_failover_state:no-failover
master_replid:05e16bfb67f7d5b60b5cb2a2814412f63c603ff5
master_replid2:0000000000000000000000000000000000000000
master_repl_offset:56
second_repl_offset:-1
repl_backlog_active:1
repl_backlog_size:1048576
repl_backlog_first_byte_offset:1
repl_backlog_histlen:56
[root@vvkt7whznuckhiz2-0723575 ~]# redis-cli -p 6389 info replication
# Replication
role:master
connected_slaves:0
master_failover_state:no-failover
master_replid:166b3019798f2218fd3eca3ecfaf648d84b424c8
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
[root@vvkt7whznuckhiz2-0723575 ~]# redis-cli -p 6399 info replication
# Replication
role:slave
master_host:127.0.0.1
master_port:6379
master_link_status:up
master_last_io_seconds_ago:3
master_sync_in_progress:0
slave_read_repl_offset:84
slave_repl_offset:84
slave_priority:100
slave_read_only:1
replica_announced:1
connected_slaves:0
master_failover_state:no-failover
master_replid:05e16bfb67f7d5b60b5cb2a2814412f63c603ff5
master_replid2:0000000000000000000000000000000000000000
master_repl_offset:84
second_repl_offset:-1
repl_backlog_active:1
repl_backlog_size:1048576
repl_backlog_first_byte_offset:1
repl_backlog_histlen:84
[root@vvkt7whznuckhiz2-0723575 ~]# redis-cli -p 6389 slaveof 127.0.0.1 6379
OK
[root@vvkt7whznuckhiz2-0723575 ~]# redis-cli -p 6389 info replication
# Replication
role:slave
master_host:127.0.0.1
master_port:6379
master_link_status:up
master_last_io_seconds_ago:2
master_sync_in_progress:0
slave_read_repl_offset:308
slave_repl_offset:308
slave_priority:100
slave_read_only:1
replica_announced:1
connected_slaves:0
master_failover_state:no-failover
master_replid:05e16bfb67f7d5b60b5cb2a2814412f63c603ff5
master_replid2:0000000000000000000000000000000000000000
master_repl_offset:308
second_repl_offset:-1
repl_backlog_active:1
repl_backlog_size:1048576
repl_backlog_first_byte_offset:281
repl_backlog_histlen:28
2 kill PID of master redis db ; enter slave db ; storage data
[root@vvkt7whznuckhiz2-0723575 ~]# ps -ef | grep redis
root 15551 1 0 00:39 ? 00:00:00 /opt/redis/bin/redis-server *:6379
root 15565 1 0 00:39 ? 00:00:00 /opt/redis/bin/redis-server *:6389
root 15575 1 0 00:39 ? 00:00:00 /opt/redis/bin/redis-server *:6399
root 15950 15288 0 00:46 pts/0 00:00:00 grep --color=auto redis
[root@vvkt7whznuckhiz2-0723575 ~]# kill -9 15551
[root@vvkt7whznuckhiz2-0723575 ~]# ps -ef | grep redis
root 15565 1 0 00:39 ? 00:00:00 /opt/redis/bin/redis-server *:6389
root 15575 1 0 00:39 ? 00:00:00 /opt/redis/bin/redis-server *:6399
root 15967 15288 0 00:46 pts/0 00:00:00 grep --color=auto redis
[root@vvkt7whznuckhiz2-0723575 ~]# redis-cli -p 6389
127.0.0.1:6389> set dizhi changping
(error) READONLY You can't write against a read only replica.
127.0.0.1:6389> exit
[root@vvkt7whznuckhiz2-0723575 ~]# redis-cli -p 6399
127.0.0.1:6399> set kpp wqe
(error) READONLY You can't write against a read only replica.
127.0.0.1:6399> exit
3relieve slave relationship with 6379 db and check relationship info
[root@vvkt7whznuckhiz2-0723575 ~]# redis-cli -p 6399 slaveof no one
OK
[root@vvkt7whznuckhiz2-0723575 ~]# redis-cli -p 6399 info replication
# Replication
role:master
connected_slaves:0
master_failover_state:no-failover
master_replid:ca156586055e5ff7666613b1e18aa9f2adcf39cd
master_replid2:05e16bfb67f7d5b60b5cb2a2814412f63c603ff5
master_repl_offset:616
second_repl_offset:617
repl_backlog_active:1
repl_backlog_size:1048576
repl_backlog_first_byte_offset:1
repl_backlog_histlen:616
4 bind slave db 6389 with master db 6399 and check relationship info
[root@vvkt7whznuckhiz2-0723575 ~]# redis-cli -p 6389 slaveof 127.0.0.1 6399
OK
[root@vvkt7whznuckhiz2-0723575 ~]# redis-cli -p 6399 info replication
# Replication
role:master
connected_slaves:1
slave0:ip=127.0.0.1,port=6389,state=online,offset=714,lag=1
master_failover_state:no-failover
master_replid:ca156586055e5ff7666613b1e18aa9f2adcf39cd
master_replid2:05e16bfb67f7d5b60b5cb2a2814412f63c603ff5
master_repl_offset:714
second_repl_offset:617
repl_backlog_active:1
repl_backlog_size:1048576
repl_backlog_first_byte_offset:1
repl_backlog_histlen:714
[root@vvkt7whznuckhiz2-0723575 ~]#
5check data in two db
[root@vvkt7whznuckhiz2-0723575 ~]# redis-cli -p 6399
127.0.0.1:6399> set slogan "I am back in future"
OK
127.0.0.1:6399> exit
[root@vvkt7whznuckhiz2-0723575 ~]# redis-cli -p 6389
127.0.0.1:6389> keys *
1) "addr"
2) "age"
3) "name"
4) "slogan"
127.0.0.1:6389> get slogan
"I am back in future"
127.0.0.1:6389> exit
高可用—哨兵
introduction
配置好Redis的哨兵[保安]进程,一般为三个
哨兵的作用是盯着主库,不断询问它是否存活,如果超过30S(设置的时间阈值)都没有回应
三个哨兵就会判断主库宕机,谈话进行投票[三巨头会晤],自动选择从库为新的主库,每个哨兵的意见可能都不一样
因此引出投票规则,少数服从多数
当多个哨兵达成一致,选择某个从库,自动修改配置文件,切换新的主库
redis-sentinel
1. 准备3个redis节点,1master + 3slave 集群
redis支持多实例,基于多个配置文件,运行多个相互独立的redis进程
redis6379.conf
redis6380.conf
redis6381.conf
并且写入该内容:
============================================================
redis6379.conf
port 6379
daemonize yes
logfile "6379.log"
dbfilename "dump-6379.rdb"
dir "/var/redis/data/"
============================================================
redis6380.conf
port 6380
daemonize yes
logfile "6380.log"
dbfilename "dump-6380.rdb"
dir "/var/redis/data/"
slaveof 127.0.0.1 6379
============================================================
redis6381.conf
port 6381
daemonize yes
logfile "6381.log"
dbfilename "dump-6381.rdb"
dir "/var/redis/data/"
slaveof 127.0.0.1 6379
2分别启动三个数据库确认主从关系
[root@vvkt7whznuckhiz2-0723575 sentinel]# /opt/redis/bin/redis-server /s25/sentinel/redis6379.conf
[root@vvkt7whznuckhiz2-0723575 sentinel]# /opt/redis/bin/redis-server /s25/sentinel/redis6380.conf
[root@vvkt7whznuckhiz2-0723575 sentinel]# /opt/redis/bin/redis-server /s25/sentinel/redis6381.conf
[root@vvkt7whznuckhiz2-0723575 sentinel]# redis-cli -p 6379 info replication
# Replication
role:master
connected_slaves:2
slave0:ip=127.0.0.1,port=6380,state=online,offset=140,lag=0
slave1:ip=127.0.0.1,port=6381,state=online,offset=140,lag=0
master_failover_state:no-failover
master_replid:63ad06a34215b522282347cc7bf3e9581c44defa
master_replid2:0000000000000000000000000000000000000000
master_repl_offset:140
second_repl_offset:-1
repl_backlog_active:1
repl_backlog_size:1048576
repl_backlog_first_byte_offset:1
repl_backlog_histlen:140
[root@vvkt7whznuckhiz2-0723575 sentinel]# redis-cli -p 6379 info CPU
# CPU
used_cpu_sys:0.146287
used_cpu_user:0.202460
used_cpu_sys_children:0.002344
used_cpu_user_children:0.000799
used_cpu_sys_main_thread:0.139133
used_cpu_user_main_thread:0.196296
3准备三个哨兵的配置文件
=========================================================================================================
sentinel26380.conf
port 26380
dir /var/redis/data/
logfile "26380.log"
sentinel monitor mymaster 127.0.0.1 6379 2
sentinel down-after-milliseconds mymaster 30000
sentinel parallel-syncs mymaster 1
sentinel failover-timeout mymaster 180000
daemonize yes
sentinel26381.conf
port 26381
dir /var/redis/data/
logfile "26381.log"
sentinel monitor mymaster 127.0.0.1 6379 2
sentinel down-after-milliseconds mymaster 30000
sentinel parallel-syncs mymaster 1
sentinel failover-timeout mymaster 180000
daemonize yes
sentinel26382.conf
port 26382
dir /var/redis/data/
logfile "26382.log"
sentinel monitor mymaster 127.0.0.1 6379 2
sentinel down-after-milliseconds mymaster 30000
sentinel parallel-syncs mymaster 1
sentinel failover-timeout mymaster 180000daemonize yes
分别启动以上三个哨兵
[root@vvkt7whznuckhiz2-0723575 sentinel]# /opt/redis/bin/redis-sentinel /s25/sentinel/sentinel26380.conf
[root@vvkt7whznuckhiz2-0723575 sentinel]# /opt/redis/bin/redis-sentinel /s25/sentinel/sentinel26381.conf
[root@vvkt7whznuckhiz2-0723575 sentinel]# /opt/redis/bin/redis-sentinel /s25/sentinel/sentinel26382.conf
4 检查哨兵配置和主从关系
[root@vvkt7whznuckhiz2-0723575 sentinel]# redis-cli -p 26380 info sentinel
# Sentinel
sentinel_masters:1
sentinel_tilt:0
sentinel_running_scripts:0
sentinel_scripts_queue_length:0
sentinel_simulate_failure_flags:0
master0:name=mymaster,status=ok,address=127.0.0.1:6379,slaves=2,sentinels=3
[root@vvkt7whznuckhiz2-0723575 sentinel]# redis-cli -p 6379 info replication
# Replication
role:master
connected_slaves:2
slave0:ip=127.0.0.1,port=6380,state=online,offset=149039,lag=1
slave1:ip=127.0.0.1,port=6381,state=online,offset=149039,lag=1
master_failover_state:no-failover
master_replid:63ad06a34215b522282347cc7bf3e9581c44defa
master_replid2:0000000000000000000000000000000000000000
master_repl_offset:149172
second_repl_offset:-1
repl_backlog_active:1
repl_backlog_size:1048576
repl_backlog_first_byte_offset:1
repl_backlog_histlen:149172
5 干掉端口号为6379的数据库,查看其他数据库的主从关系
[root@vvkt7whznuckhiz2-0723575 sentinel]# redis-cli -p 6380 info replication
# Replication
role:slave
master_host:127.0.0.1
master_port:6379
master_link_status:down
master_last_io_seconds_ago:-1
master_sync_in_progress:0
slave_read_repl_offset:177179
slave_repl_offset:177179
master_link_down_since_seconds:17
slave_priority:100
slave_read_only:1
replica_announced:1
connected_slaves:0
master_failover_state:no-failover
master_replid:63ad06a34215b522282347cc7bf3e9581c44defa
master_replid2:0000000000000000000000000000000000000000
master_repl_offset:177179
second_repl_offset:-1
repl_backlog_active:1
repl_backlog_size:1048576
repl_backlog_first_byte_offset:1
repl_backlog_histlen:177179
[root@vvkt7whznuckhiz2-0723575 sentinel]# redis-cli -p 6381 info replication
# Replication
role:slave
master_host:127.0.0.1
master_port:6379
master_link_status:down
master_last_io_seconds_ago:-1
master_sync_in_progress:0
slave_read_repl_offset:177179
slave_repl_offset:177179
master_link_down_since_seconds:29
slave_priority:100
slave_read_only:1
replica_announced:1
connected_slaves:0
master_failover_state:no-failover
master_replid:63ad06a34215b522282347cc7bf3e9581c44defa
master_replid2:0000000000000000000000000000000000000000
master_repl_offset:177179
second_repl_offset:-1
repl_backlog_active:1
repl_backlog_size:1048576
repl_backlog_first_byte_offset:15
repl_backlog_histlen:177165
[root@vvkt7whznuckhiz2-0723575 sentinel]# redis-cli -p 6380 info replication
# Replication
role:master
connected_slaves:1
slave0:ip=127.0.0.1,port=6381,state=online,offset=180821,lag=1
master_failover_state:no-failover
master_replid:a7c231b0baeb9a7873036931b778be5be689ae84
master_replid2:63ad06a34215b522282347cc7bf3e9581c44defa
master_repl_offset:180821
second_repl_offset:177180
repl_backlog_active:1
repl_backlog_size:1048576
repl_backlog_first_byte_offset:1
repl_backlog_histlen:180821
6 重启端口号为6379的数据库—多次读取数据库,查看关系变更
vim pureredis6379.conf
port 6379
daemonize yes
logfile "pure6379.log"
dbfilename "dump-pure6379.rdb"
dir "/var/redis/data"
/opt/redis/bin/redis-server /s25/sentinel/pureredis6379.conf
redis-cli -p 6379 info replication
# Replication
role:slave
master_host:127.0.0.1
master_port:6380
master_link_status:up
master_last_io_seconds_ago:1
master_sync_in_progress:0
slave_read_repl_offset:405999
slave_repl_offset:405999
slave_priority:100
slave_read_only:1
replica_announced:1
connected_slaves:0
master_failover_state:no-failover
master_replid:a7c231b0baeb9a7873036931b778be5be689ae84
master_replid2:0000000000000000000000000000000000000000
master_repl_offset:405999
second_repl_offset:-1
repl_backlog_active:1
repl_backlog_size:1048576
repl_backlog_first_byte_offset:405312
repl_backlog_histlen:688
# 查看配置文件的更新
cat pureredis6379.conf
port 6379
daemonize yes
logfile "pure6379.log"
dbfilename "dump-pure6379.rdb"
dir "/var/redis/data"
# Generated by CONFIG REWRITE
pidfile "/var/run/redis.pid"
save 3600 1
save 300 100
save 60 10000
user default on nopass ~* &* +@all
replicaof 127.0.0.1 6380
集群[redis-cluster配置 - py鱼 - 博客园 (cnblogs.com)]
准备六匹马儿亦六个节点,也就是六个配置文件
vim rediscl7000.conf
port 7000
daemonize yes
dir "/var/redis/data"
logfile "7000.log"
dbfilename "dump-7000.rdb"
cluster-enabled yes
cluster-config-file nodes-7000.conf
cluster-require-full-coverage no
vim rediscl7001.conf
port 7001
daemonize yes
dir "/var/redis/data"
logfile "7001.log"
dbfilename "dump-7001.rdb"
cluster-enabled yes
cluster-config-file nodes-7001.conf
cluster-require-full-coverage no
vim rediscl7002.conf
port 7002
daemonize yes
dir "/var/redis/data"
logfile "7002.log"
dbfilename "dump-7002.rdb"
cluster-enabled yes
cluster-config-file nodes-7002.conf
cluster-require-full-coverage no
vim rediscl7003.conf
port 7003
daemonize yes
dir "/var/redis/data"
logfile "7003.log"
dbfilename "dump-7003.rdb"
cluster-enabled yes
cluster-config-file nodes-7003.conf
cluster-require-full-coverage no
vim rediscl7004.conf
port 7004
daemonize yes
dir "/var/redis/data"
logfile "7004.log"
dbfilename "dump-7004.rdb"
cluster-enabled yes
cluster-config-file nodes-7004.conf
cluster-require-full-coverage no
vim rediscl7005.conf
port 7005
daemonize yes
dir "/var/redis/data"
logfile "7005.log"
dbfilename "dump-7005.rdb"
cluster-enabled yes
cluster-config-file nodes-7005.conf
cluster-require-full-coverage no
复制并且修改编号,并且逐个启动,尝试向集群中的节点写入数据
vim /rediscluster/rediscl7000.conf
vim /rediscluster/rediscl7001.conf
vim /rediscluster/rediscl7002.conf
vim /rediscluster/rediscl7003.conf
vim /rediscluster/rediscl7004.conf
vim /rediscluster/rediscl7005.conf
# 启动多个配置文件
/opt/redis/bin/redis-server /rediscluster/rediscl7000.conf
/opt/redis/bin/redis-server /rediscluster/rediscl7001.conf
/opt/redis/bin/redis-server /rediscluster/rediscl7002.conf
/opt/redis/bin/redis-server /rediscluster/rediscl7003.conf
/opt/redis/bin/redis-server /rediscluster/rediscl7004.conf
/opt/redis/bin/redis-server /rediscluster/rediscl7005.conf
# 测试单个数据库的文件写入
========================================================================================
此时写入数据会报错
redis-cli -p 7000
127.0.0.1:7000> keys *
(empty array)
127.0.0.1:7000> set name cj
(error) CLUSTERDOWN Hash slot not served
127.0.0.1:7000>
安装额外配置
1 安装ruby
yum -y install ruby
2 查看rub 以及 gem 版本
ruby -v
ruby 2.0.0p648 (2015-12-16) [x86_64-linux]
gem -v
2.0.14.1
3 使用gem包管理下载redis插件
gem install redis -v 3.3.0
4 搜索用于创建redis集群的脚本 redis-trib.rb
which 用于搜索环境变量
find 才是搜索文件的
find -name "redis-trib.rb"
5. 批量创建集群
redis-cli --cluster create 127.0.0.1:7001 127.0.0.1:7002 127.0.0.1:7003 127.0.0.1:7004 127.0.0.1:7005
>>> Performing hash slots allocation on 5 nodes...
Master[0] -> Slots 0 - 3276
Master[1] -> Slots 3277 - 6553
Master[2] -> Slots 6554 - 9829
Master[3] -> Slots 9830 - 13106
Master[4] -> Slots 13107 - 16383
M: 0f1ed10559d2484e0ed79f56dd08bd00c0f927f5 127.0.0.1:7001
slots:[0-3276] (3277 slots) master
M: 0a6cbdde7741b2cdb155dc876906e67e02e1c365 127.0.0.1:7002
slots:[3277-6553] (3277 slots) master
M: 6b390a5a1b1821d2b3701a5398ec2065705933d0 127.0.0.1:7003
slots:[6554-9829] (3276 slots) master
M: b2d844c1bb2a4c3b4073236a5e629bd94b74969c 127.0.0.1:7004
slots:[9830-13106] (3277 slots) master
M: 9e01cee325efff465d41431d3d078f239f3a3aad 127.0.0.1:7005
slots:[13107-16383] (3277 slots) master
Can I set the above configuration? (type 'yes' to accept): yes
>>> Nodes configuration updated
>>> Assign a different config epoch to each node
>>> Sending CLUSTER MEET messages to join the cluster
Waiting for the cluster to join
.
>>> Performing Cluster Check (using node 127.0.0.1:7001)
M: 0f1ed10559d2484e0ed79f56dd08bd00c0f927f5 127.0.0.1:7001
slots:[0-3276] (3277 slots) master
M: 0a6cbdde7741b2cdb155dc876906e67e02e1c365 127.0.0.1:7002
slots:[3277-6553] (3277 slots) master
M: 9e01cee325efff465d41431d3d078f239f3a3aad 127.0.0.1:7005
slots:[13107-16383] (3277 slots) master
M: 6b390a5a1b1821d2b3701a5398ec2065705933d0 127.0.0.1:7003
slots:[6554-9829] (3276 slots) master
M: b2d844c1bb2a4c3b4073236a5e629bd94b74969c 127.0.0.1:7004
slots:[9830-13106] (3277 slots) master
[OK] All nodes agree about slots configuration.
>>> Check for open slots...
>>> Check slots coverage...
[OK] All 16384 slots covered.
查看主从关系
[root@vvkt7whznuckhiz2-0723575 rediscluster]# redis-cli -p 7001 -c
127.0.0.1:7001> cluster nodes
b2d844c1bb2a4c3b4073236a5e629bd94b74969c 127.0.0.1:7004@17004 master - 0 1637067122000 4 connected 9830-13106
0a6cbdde7741b2cdb155dc876906e67e02e1c365 127.0.0.1:7002@17002 master - 0 1637067123111 2 connected 3277-6553
9e01cee325efff465d41431d3d078f239f3a3aad 127.0.0.1:7005@17005 master - 0 1637067124114 5 connected 13107-16383
6b390a5a1b1821d2b3701a5398ec2065705933d0 127.0.0.1:7003@17003 master - 0 1637067122000 3 connected 6554-9829
0f1ed10559d2484e0ed79f56dd08bd00c0f927f5 127.0.0.1:7001@17001 myself,master - 0 1637067124000 1 connected 0-3276
127.0.0.1:7001>