五大数据类型—开发必会的技能

  • string 字符串类型
  • hash 哈希类型,如同python的dict类型
  • Set 无序集合
  • Zset 有序集合
  • List, 双向队列,向左插入数据,向右插入数据,向左右读取数据

reference redis学习 - py鱼 - 博客园 (cnblogs.com)

安全启动与配置文件

common sense

  1. 1. Linux如何安装redis呢?
  2. 1. rpm包安装
  3. 2. yum 自动化安装, 在阿里云yum仓库中,存有redis软件包
  4. 3. 源代码编译安装
  5. 2. outer knowledge
  6. 1. 内存型数据库,断电,进程重启--->数据丢失
  7. 2. 配置数据持久化防止数据丢失
  8. 3. 支持数据备份,也就是matser-slave主从模式
  9. 4. 读写分离,防止单点故障,数据丢失
  10. 3. example of appication
  11. 1. 新浪微博,微信红包,淘宝轮播图;

installation-1

  1. yum -y install redis

installation-2

  1. 1. 下载redis源码
  2. wget http://download.redis.io/releases/redis-4.0.10.tar.gz
  3. 2. 解压缩
  4. tar -zxf redis-4.0.10.tar.gz
  5. 3. 切换redis源码目录
  6. cd redis-4.0.10.tar.gz
  7. 4. 编译源文件
  8. make
  9. 5. 编译好后,src/目录下有编译好的redis指令
  10. 6. make install 安装到指定目录,默认在/usr/local/bin

configuration of function

remote connection

Linux部署Redis - 图1

modify default port

  1. # Accept connections on the specified port, default is 6379 (IANA #815344).
  2. # If port 0 is specified Redis will not listen on a TCP socket.
  3. port 6500

set password

  1. # Warning: since Redis is pretty fast an outside user can try up to
  2. # 150k passwords per second against a good box. This means that you should
  3. # use a very strong password otherwise it will be very easy to break.
  4. #
  5. # requirepass redis1114

start security mode

  1. # By default protected mode is enabled. You should disable it only if
  2. # you are sure you want clients from other hosts to connect to Redis
  3. # even if no authentication is configured, nor a specific set of interfaces
  4. # are explicitly listed using the "bind" directive.
  5. protected-mode yes

run by daemonization

  1. # By default Redis does not run as a daemon. Use 'yes' if you need it.
  2. # Note that Redis will write a pid file in /var/run/redis.pid when daemonized.
  3. daemonize yes

usage

  1. [root@vvkt7whznuckhiz2-0723575 bin]# pwd
  2. /opt/redis/bin
  3. [root@vvkt7whznuckhiz2-0723575 bin]# redis-server /etc/redis.conf
  4. [root@vvkt7whznuckhiz2-0723575 bin]# redis-cli -p 6500 -h 192.168.1.9
  5. 192.168.1.9:6500> auth redis1114
  6. OK
  7. 192.168.1.9:6500> ping
  8. PONG

usual command

  1. # 列出所有的的key
  2. keys *

数据持久化[RDB & AOF]

introduction

  1. 一主一从比较简单,但可用性不高,最好是一主多从

test1 —[Why cofigurate data persistence]

  1. 先向数据库写入信息,然后重启进程,看是否丢失数据?[显而易见-absolutely]
  2. 设计实验:
  3. 1. 关闭redis所有进程 pkill -9 redfis
  4. 2. 进入 / 目录,创建 mkdir s25redis
  5. 3. 创建配置文件 vim redis_no_pr.conf
  6. conetent : bind 127.0.0.1 daemonize yes

command

  1. [root@vvkt7whznuckhiz2-0723575 /]# mkdir s25redis
  2. [root@vvkt7whznuckhiz2-0723575 /]# cd s25redis
  3. [root@vvkt7whznuckhiz2-0723575 s25redis]# ls
  4. [root@vvkt7whznuckhiz2-0723575 s25redis]# vim redis_no_per.conf
  5. [root@vvkt7whznuckhiz2-0723575 s25redis]# /opt/redis/bin/redis-server /s25redis/redis_no_per.conf
  6. *** FATAL CONFIG FILE ERROR (Redis 6.2.6) ***
  7. Reading the configuration file, at line 2
  8. >>> 'damonize yes'
  9. Bad directive or wrong number of arguments
  10. [root@vvkt7whznuckhiz2-0723575 s25redis]# vim redis_no_per.conf
  11. [root@vvkt7whznuckhiz2-0723575 s25redis]# cat redis_no_per.conf
  12. bind 127.0.0.1
  13. daemonize yes
  14. [root@vvkt7whznuckhiz2-0723575 s25redis]# /opt/redis/bin/redis-server /s25redis/redis_no_per.conf
  15. [root@vvkt7whznuckhiz2-0723575 s25redis]# redis-cli
  16. 127.0.0.1:6379> set name alex
  17. OK
  18. 127.0.0.1:6379> set name1 cs
  19. OK
  20. 127.0.0.1:6379> keys *
  21. 1) "name1"
  22. 2) "name"
  23. 127.0.0.1:6379> quit
  24. [root@vvkt7whznuckhiz2-0723575 s25redis]# pkill -9 redis
  25. [root@vvkt7whznuckhiz2-0723575 s25redis]# /opt/redis/bin/redis-server /s25redis/redis_no_per.conf
  26. [root@vvkt7whznuckhiz2-0723575 s25redis]# redis-cli
  27. 127.0.0.1:6379> keys *
  28. (empty array)
  29. 127.0.0.1:6379>

reference

  1. 1. difference between rdb and aof
  2. https://blog.csdn.net/ljheee/article/details/76284082

RDB[data persistence]

  1. 1. 配置RDB机制数据持久化,数据文件为二进制文件,且配置触发机制
  2. cd /s25redis 写入[vim s25rdb.conf]如下配置:
  3. daemonize yes
  4. port 6379
  5. logfile /data/6379/redis.log #指定redis的运行日志存储位置
  6. dir /data/6379 # 指定redis的数据文件的存放路径
  7. dbfilename S25dbmp.rdb # 指定数据持久化的文件名字
  8. bind 10.0.0.10 127.0.0.1
  9. # redis触发save命令的频率
  10. save 900 1 # 900秒内一个修改动作,如set mset del
  11. save 300 10 # 300秒内10个
  12. save 60 10000 # 60秒内10000个操作
  13. 2. 创建redis数据文件夹
  14. mkdir -p /s25redis/data/6379
  15. 3. check and stop process
  16. ps -ef |grep redis
  17. pkill -9 redis
  18. 4. restart process
  19. /opt/redis/bin/redis-server s25redis/s25rdb.conf
  20. 5. 存储数据
  21. set name sq
  22. set bilibili os
  23. save
  24. 6. 新开窗口,查看/data/6379下的文件

effort

Linux部署Redis - 图2

error

  1. *** FATAL CONFIG FILE ERROR (Redis 6.2.6) ***
  2. Reading the configuration file, at line 3
  3. >>> 'logfile "" #指定redis的运行日志存储位置'
  4. Bad directive or wrong number of arguments

effort

  1. 1. 在/data/6379下创建redis.log文件
  2. 2. 进入 /opt/redis/bin/下使用./redis-server启动
  3. 3. 查看权限 ls -ll 该文件具有属主的读写权限
  4. 4. 使用 /sur/local/bin/redis-server启动,仍然错误
  5. 5. 修改配置文件为 logfile "/data/6379/redis.log"
  6. 6. 测试方案 ---> 物理机redis跑一次代码就用默认的redis 3.2.1

feedback

  1. 版本差异,该配置在redis3.2.12下并无问题

feedback2—maybe syntax error

Linux部署Redis - 图3

AOF

feature

  1. 1. AOFappend-only log file
  2. 记录服务器执行的所有变更操作命令(例如set del等),并在服务器启动时,通过重新执行这些命令来还原数据集
  3. AOF 文件中的命令全部以redis协议的格式保存,新命令追加到文件末尾。
  4. 优点:最大程序保证数据不丢
  5. 缺点:日志记录非常大
  6. 2. 比较
  7. rdb:基于快照的持久化,速度更快,一般用作备份,主从复制也是依赖于rdb持久化功能
  8. aof:以追加的方式记录redis操作日志的文件。可以最大程度的保证redis数据安全,类似于mysqlbinlog

operation

  1. 1. 创建一个文件,写入如下配置
  2. vim redisaof.conf
  3. daemonize yes
  4. bind 192.168.1.9
  5. port 6379
  6. logfile /data/6379/redis.log
  7. dir /data/6379aof/
  8. appendonly yes
  9. appendfsync everysec
  10. 2. 创建数据文件夹
  11. mkdir -p /data/6379aof/
  12. 3. 检查redis进程是否启动
  13. ps -ef | grep redis
  14. pkill -9 redis
  15. 4. 指定配置文件启动redis
  16. /opt/redis/bin/redis-server /s25redis/redisaof.conf
  17. redis-cli -p 6379 -h 192.168.1.9
  18. 5. 写入数据
  19. set name wangsic
  20. set tecent bye
  21. 6. 实时刷新和查看append文件
  22. cd /data/6379aof
  23. tail -f appendonly.aof
  24. 7. 关闭进程查看数据是否存在
  25. >exit
  26. pkill -9 redis
  27. /opt/redis/bin/redis-server /s25redis/redisaof.conf
  28. redis-cli -p 6379 -h 192.168.1.9
  29. > keys *
  30. 8. 删除appendonly.aof重启redis
  31. rm -rf /data/6379aof/appendonly.aof
  32. /opt/redis/bin/redis-server /s25redis/redisaof.conf
  33. redis-cli -p 6379 -h 192.168.1.9
  34. > keys *

数据主从同步以及故障

Introduction

  1. 在一台机器上运行两个及以上的Redis数据库,是redis支持的多实例功能,基于端口号的不同,就能运行多个相互独立的redis数据库
  2. 什么是多实例?
  3. 就是机器上运行了多个redis独立的进程,互不干涉的独立的redis数据库;叫做

operation

  1. 1. 切换工作目录 创建多个配置文件分别配置端口号,IP地址以及后台启动
  2. cd /s25redis
  3. vim redish1.conf
  4. vim redish2.conf
  5. bind 0.0.0.0
  6. port 6379
  7. daemonize yes
  8. bind 0.0.0.0
  9. port 6399
  10. daemonize yes
  11. 2. 分别进入两个数据库写入键值对
  12. redisaof.conf redish1.conf redish2.conf redis_no_per.conf s25rdb.conf
  13. [root@vvkt7whznuckhiz2-0723575 s25redis]# /opt/redis/bin/redis-server /s25redis/redish1.conf
  14. [root@vvkt7whznuckhiz2-0723575 s25redis]# /opt/redis/bin/redis-server /s25redis/redish2.conf
  15. [root@vvkt7whznuckhiz2-0723575 s25redis]# redis-cli -p 6379
  16. 127.0.0.1:6379> keys *
  17. (empty array)
  18. 127.0.0.1:6379> set name haiyoushei
  19. OK
  20. 127.0.0.1:6379> exit
  21. [root@vvkt7whznuckhiz2-0723575 s25redis]# redis-cli -p 6399
  22. 127.0.0.1:6399> keys *
  23. (empty array)
  24. 127.0.0.1:6399> set nickname lichousha
  25. OK
  26. 127.0.0.1:6399> exit
  27. [root@vvkt7whznuckhiz2-0723575 s25redis]# redis-cli -p 6399 get nickname
  28. "lichousha"
  29. [root@vvkt7whznuckhiz2-0723575 s25redis]# redis-cli -p 6379 get name
  30. "haiyoushei"
  31. [root@vvkt7whznuckhiz2-0723575 s25redis]#
  32. 3.

operation2 master-slave

  1. 1. 准备两个配置文件,分别写入如下内容
  2. pwd /s25redis
  3. vim master_redis.conf
  4. port 6379
  5. daemonize yes
  6. pidfile /s25/6379/redis.pid
  7. loglevel notice
  8. logfile "/data/6379/redis.log"
  9. dbfilename dump.rdb
  10. dir /s25/6379
  11. protected-mode no
  12. 2. 准备第二个配置文件
  13. pwd /s25redis
  14. vim slave_redis.conf
  15. port 6389
  16. daemonize yes
  17. pidfile /s25/6389/redis.pid
  18. loglevel notice
  19. logfile "/data/6389/redis.log"
  20. dbfilename dump.rdb
  21. dir /s25/6389
  22. protected-mode no
  23. 3. 分别创建两个文件夹存储数据
  24. mkdir -p /s25/{6379,6389}
  25. 4. 分别通过配置文件启动redis
  26. /opt/redis/bin/redis-server /s25redis/master_redis.conf
  27. /opt/redis/bin/redis-server /s25redis/slave_redis.conf
  28. 5. 检查主从关系
  29. [root@vvkt7whznuckhiz2-0723575 s25redis]# redis-cli -p 6379 info replication
  30. # Replication
  31. role:master
  32. connected_slaves:0
  33. master_failover_state:no-failover
  34. master_replid:25d1bb4b918f0b05b93e93815199b624c43e49e1
  35. master_replid2:0000000000000000000000000000000000000000
  36. master_repl_offset:0
  37. second_repl_offset:-1
  38. repl_backlog_active:0
  39. repl_backlog_size:1048576
  40. repl_backlog_first_byte_offset:0
  41. repl_backlog_histlen:0
  42. [root@vvkt7whznuckhiz2-0723575 s25redis]# redis-cli -p 6389 info replication
  43. # Replication
  44. role:master
  45. connected_slaves:0
  46. master_failover_state:no-failover
  47. master_replid:a28d09fae5aaee018b1266ada36f8fc80e074883
  48. master_replid2:0000000000000000000000000000000000000000
  49. master_repl_offset:0
  50. second_repl_offset:-1
  51. repl_backlog_active:0
  52. repl_backlog_size:1048576
  53. repl_backlog_first_byte_offset:0
  54. repl_backlog_histlen:0
  55. 6. 从数据库绑定主数据库,并且查看从数据库的信息
  56. [root@vvkt7whznuckhiz2-0723575 s25redis]# redis-cli -p 6389 slaveof 127.0.0.1 6379
  57. OK
  58. [root@vvkt7whznuckhiz2-0723575 s25redis]# redis-cli -p 6389 info replication
  59. # Replication
  60. role:slave
  61. master_host:127.0.0.1
  62. master_port:6379
  63. master_link_status:up
  64. master_last_io_seconds_ago:1
  65. master_sync_in_progress:0
  66. slave_read_repl_offset:28
  67. slave_repl_offset:28
  68. slave_priority:100
  69. slave_read_only:1
  70. replica_announced:1
  71. connected_slaves:0
  72. master_failover_state:no-failover
  73. master_replid:672933a58085330846bea78a8b284c6958942037
  74. master_replid2:0000000000000000000000000000000000000000
  75. master_repl_offset:28
  76. second_repl_offset:-1
  77. repl_backlog_active:1
  78. repl_backlog_size:1048576
  79. repl_backlog_first_byte_offset:1
  80. repl_backlog_histlen:28

7 检查两数据库内是否存有数据

  1. [root@vvkt7whznuckhiz2-0723575 s25redis]# redis-cli -p 6379
  2. 127.0.0.1:6379> keys *
  3. (empty array)
  4. 127.0.0.1:6379> exit
  5. [root@vvkt7whznuckhiz2-0723575 s25redis]# redis-cli -p 6389
  6. 127.0.0.1:6389> keys *
  7. (empty array)
  8. 127.0.0.1:6389> exit

8 在主数据库写入数据,在从数据库读取数据[从数据库不能写入]

  1. [root@vvkt7whznuckhiz2-0723575 s25redis]# redis-cli -p 6379
  2. 127.0.0.1:6379> set name alex
  3. OK
  4. 127.0.0.1:6379> keys *
  5. 1) "name"
  6. 127.0.0.1:6379> get name
  7. "alex"
  8. 127.0.0.1:6379> exit
  9. [root@vvkt7whznuckhiz2-0723575 s25redis]# redis-cli -p 6389
  10. 127.0.0.1:6389> keys *
  11. 1) "name"
  12. 127.0.0.1:6389> set name1 wupeiqi
  13. (error) READONLY You can't write against a read only replica.
  14. 127.0.0.1:6389> exit
  15. [root@vvkt7whznuckhiz2-0723575 s25redis]# redis-cli -p 6379
  16. 127.0.0.1:6379> set name2 wupeiqi
  17. OK
  18. 127.0.0.1:6379> exit
  19. [root@vvkt7whznuckhiz2-0723575 s25redis]# redis-cli -p 6389
  20. 127.0.0.1:6389> keys *
  21. 1) "name2"
  22. 2) "name"
  23. 127.0.0.1:6389> exit

一主多从以及主从故障切换

设置一主多从

  1. 1. 创建6399端口的数据库,设置从属关系
  2. cd /s25redis
  3. vim slave_redis2.conf
  4. port 6399
  5. daemonize yes
  6. pidfile /s25/6399/redis.pid
  7. loglevel notice
  8. logfile "/data/6399/redis.log"
  9. dbfilename dump.rdb
  10. dir /s25/6389
  11. protected-mode no
  12. slaveof 127.0.0.1 6379
  13. 2. 创建数据文件夹
  14. mkdir -p /s25/6399
  15. 3. 分别启动数据库,从6389设置从属于6379
  16. [root@vvkt7whznuckhiz2-0723575 s25redis]# redis-cli -p 6389 slaveof 127.0.0.1 6379
  17. OK
  18. [root@vvkt7whznuckhiz2-0723575 s25redis]# redis-cli -p 6379
  19. 127.0.0.1:6379> set addr 188
  20. OK
  21. 127.0.0.1:6379> exit
  22. [root@vvkt7whznuckhiz2-0723575 s25redis]# redis-cli -p 6389
  23. 127.0.0.1:6389> keys *
  24. 1) "name"
  25. 2) "age"
  26. 3) "addr"
  27. 127.0.0.1:6389> exit
  28. [root@vvkt7whznuckhiz2-0723575 s25redis]# redis-cli -p 6399
  29. 127.0.0.1:6399> keys *
  30. 1) "age"
  31. 2) "name"
  32. 3) "addr"
  33. 127.0.0.1:6399>

设置故障切换

情景模拟

  1. 1. 从库故障,新建一个从库,再加入从属关系
  2. 2. 主库故障,问题大了咋办?
  3. 1. 数据同步了三份,主,从1,从2. 主库还有数据;
  4. 2. 解决办法:某一个从库切换为主库,从库2重新分配从属关系 6389 9399
  5. 3. 主库恢复,重新加入主从关系,b从库

1start three databases and check relationship

  1. [root@vvkt7whznuckhiz2-0723575 ~]# /opt/redis/bin/redis-server /s25redis/master_redis.conf
  2. [root@vvkt7whznuckhiz2-0723575 ~]# /opt/redis/bin/redis-server /s25redis/slave_redis.conf
  3. [root@vvkt7whznuckhiz2-0723575 ~]# /opt/redis/bin/redis-server /s25redis/slave_redis2.conf
  4. [root@vvkt7whznuckhiz2-0723575 ~]# ps -ef | grep redis
  5. root 15551 1 0 00:39 ? 00:00:00 /opt/redis/bin/redis-server *:6379
  6. root 15565 1 0 00:39 ? 00:00:00 /opt/redis/bin/redis-server *:6389
  7. root 15575 1 0 00:39 ? 00:00:00 /opt/redis/bin/redis-server *:6399
  8. root 15590 15288 0 00:39 pts/0 00:00:00 grep --color=auto redis
  9. [root@vvkt7whznuckhiz2-0723575 ~]# redis-cli -p 6379 info replication
  10. # Replication
  11. role:master
  12. connected_slaves:1
  13. slave0:ip=127.0.0.1,port=6399,state=online,offset=56,lag=1
  14. master_failover_state:no-failover
  15. master_replid:05e16bfb67f7d5b60b5cb2a2814412f63c603ff5
  16. master_replid2:0000000000000000000000000000000000000000
  17. master_repl_offset:56
  18. second_repl_offset:-1
  19. repl_backlog_active:1
  20. repl_backlog_size:1048576
  21. repl_backlog_first_byte_offset:1
  22. repl_backlog_histlen:56
  23. [root@vvkt7whznuckhiz2-0723575 ~]# redis-cli -p 6389 info replication
  24. # Replication
  25. role:master
  26. connected_slaves:0
  27. master_failover_state:no-failover
  28. master_replid:166b3019798f2218fd3eca3ecfaf648d84b424c8
  29. master_replid2:0000000000000000000000000000000000000000
  30. master_repl_offset:0
  31. second_repl_offset:-1
  32. repl_backlog_active:0
  33. repl_backlog_size:1048576
  34. repl_backlog_first_byte_offset:0
  35. repl_backlog_histlen:0
  36. [root@vvkt7whznuckhiz2-0723575 ~]# redis-cli -p 6399 info replication
  37. # Replication
  38. role:slave
  39. master_host:127.0.0.1
  40. master_port:6379
  41. master_link_status:up
  42. master_last_io_seconds_ago:3
  43. master_sync_in_progress:0
  44. slave_read_repl_offset:84
  45. slave_repl_offset:84
  46. slave_priority:100
  47. slave_read_only:1
  48. replica_announced:1
  49. connected_slaves:0
  50. master_failover_state:no-failover
  51. master_replid:05e16bfb67f7d5b60b5cb2a2814412f63c603ff5
  52. master_replid2:0000000000000000000000000000000000000000
  53. master_repl_offset:84
  54. second_repl_offset:-1
  55. repl_backlog_active:1
  56. repl_backlog_size:1048576
  57. repl_backlog_first_byte_offset:1
  58. repl_backlog_histlen:84
  59. [root@vvkt7whznuckhiz2-0723575 ~]# redis-cli -p 6389 slaveof 127.0.0.1 6379
  60. OK
  61. [root@vvkt7whznuckhiz2-0723575 ~]# redis-cli -p 6389 info replication
  62. # Replication
  63. role:slave
  64. master_host:127.0.0.1
  65. master_port:6379
  66. master_link_status:up
  67. master_last_io_seconds_ago:2
  68. master_sync_in_progress:0
  69. slave_read_repl_offset:308
  70. slave_repl_offset:308
  71. slave_priority:100
  72. slave_read_only:1
  73. replica_announced:1
  74. connected_slaves:0
  75. master_failover_state:no-failover
  76. master_replid:05e16bfb67f7d5b60b5cb2a2814412f63c603ff5
  77. master_replid2:0000000000000000000000000000000000000000
  78. master_repl_offset:308
  79. second_repl_offset:-1
  80. repl_backlog_active:1
  81. repl_backlog_size:1048576
  82. repl_backlog_first_byte_offset:281
  83. repl_backlog_histlen:28

2 kill PID of master redis db ; enter slave db ; storage data

  1. [root@vvkt7whznuckhiz2-0723575 ~]# ps -ef | grep redis
  2. root 15551 1 0 00:39 ? 00:00:00 /opt/redis/bin/redis-server *:6379
  3. root 15565 1 0 00:39 ? 00:00:00 /opt/redis/bin/redis-server *:6389
  4. root 15575 1 0 00:39 ? 00:00:00 /opt/redis/bin/redis-server *:6399
  5. root 15950 15288 0 00:46 pts/0 00:00:00 grep --color=auto redis
  6. [root@vvkt7whznuckhiz2-0723575 ~]# kill -9 15551
  7. [root@vvkt7whznuckhiz2-0723575 ~]# ps -ef | grep redis
  8. root 15565 1 0 00:39 ? 00:00:00 /opt/redis/bin/redis-server *:6389
  9. root 15575 1 0 00:39 ? 00:00:00 /opt/redis/bin/redis-server *:6399
  10. root 15967 15288 0 00:46 pts/0 00:00:00 grep --color=auto redis
  11. [root@vvkt7whznuckhiz2-0723575 ~]# redis-cli -p 6389
  12. 127.0.0.1:6389> set dizhi changping
  13. (error) READONLY You can't write against a read only replica.
  14. 127.0.0.1:6389> exit
  15. [root@vvkt7whznuckhiz2-0723575 ~]# redis-cli -p 6399
  16. 127.0.0.1:6399> set kpp wqe
  17. (error) READONLY You can't write against a read only replica.
  18. 127.0.0.1:6399> exit

3relieve slave relationship with 6379 db and check relationship info

  1. [root@vvkt7whznuckhiz2-0723575 ~]# redis-cli -p 6399 slaveof no one
  2. OK
  3. [root@vvkt7whznuckhiz2-0723575 ~]# redis-cli -p 6399 info replication
  4. # Replication
  5. role:master
  6. connected_slaves:0
  7. master_failover_state:no-failover
  8. master_replid:ca156586055e5ff7666613b1e18aa9f2adcf39cd
  9. master_replid2:05e16bfb67f7d5b60b5cb2a2814412f63c603ff5
  10. master_repl_offset:616
  11. second_repl_offset:617
  12. repl_backlog_active:1
  13. repl_backlog_size:1048576
  14. repl_backlog_first_byte_offset:1
  15. repl_backlog_histlen:616

4 bind slave db 6389 with master db 6399 and check relationship info

  1. [root@vvkt7whznuckhiz2-0723575 ~]# redis-cli -p 6389 slaveof 127.0.0.1 6399
  2. OK
  3. [root@vvkt7whznuckhiz2-0723575 ~]# redis-cli -p 6399 info replication
  4. # Replication
  5. role:master
  6. connected_slaves:1
  7. slave0:ip=127.0.0.1,port=6389,state=online,offset=714,lag=1
  8. master_failover_state:no-failover
  9. master_replid:ca156586055e5ff7666613b1e18aa9f2adcf39cd
  10. master_replid2:05e16bfb67f7d5b60b5cb2a2814412f63c603ff5
  11. master_repl_offset:714
  12. second_repl_offset:617
  13. repl_backlog_active:1
  14. repl_backlog_size:1048576
  15. repl_backlog_first_byte_offset:1
  16. repl_backlog_histlen:714
  17. [root@vvkt7whznuckhiz2-0723575 ~]#

5check data in two db

  1. [root@vvkt7whznuckhiz2-0723575 ~]# redis-cli -p 6399
  2. 127.0.0.1:6399> set slogan "I am back in future"
  3. OK
  4. 127.0.0.1:6399> exit
  5. [root@vvkt7whznuckhiz2-0723575 ~]# redis-cli -p 6389
  6. 127.0.0.1:6389> keys *
  7. 1) "addr"
  8. 2) "age"
  9. 3) "name"
  10. 4) "slogan"
  11. 127.0.0.1:6389> get slogan
  12. "I am back in future"
  13. 127.0.0.1:6389> exit

高可用—哨兵

introduction

  1. 配置好Redis的哨兵[保安]进程,一般为三个
  2. 哨兵的作用是盯着主库,不断询问它是否存活,如果超过30S(设置的时间阈值)都没有回应
  3. 三个哨兵就会判断主库宕机,谈话进行投票[三巨头会晤],自动选择从库为新的主库,每个哨兵的意见可能都不一样
  4. 因此引出投票规则,少数服从多数
  5. 当多个哨兵达成一致,选择某个从库,自动修改配置文件,切换新的主库

redis-sentinel

1. 准备3个redis节点,1master + 3slave 集群

  1. redis支持多实例,基于多个配置文件,运行多个相互独立的redis进程
  2. redis6379.conf
  3. redis6380.conf
  4. redis6381.conf
  5. 并且写入该内容:
  6. ============================================================
  7. redis6379.conf
  8. port 6379
  9. daemonize yes
  10. logfile "6379.log"
  11. dbfilename "dump-6379.rdb"
  12. dir "/var/redis/data/"
  13. ============================================================
  14. redis6380.conf
  15. port 6380
  16. daemonize yes
  17. logfile "6380.log"
  18. dbfilename "dump-6380.rdb"
  19. dir "/var/redis/data/"
  20. slaveof 127.0.0.1 6379
  21. ============================================================
  22. redis6381.conf
  23. port 6381
  24. daemonize yes
  25. logfile "6381.log"
  26. dbfilename "dump-6381.rdb"
  27. dir "/var/redis/data/"
  28. slaveof 127.0.0.1 6379

2分别启动三个数据库确认主从关系

  1. [root@vvkt7whznuckhiz2-0723575 sentinel]# /opt/redis/bin/redis-server /s25/sentinel/redis6379.conf
  2. [root@vvkt7whznuckhiz2-0723575 sentinel]# /opt/redis/bin/redis-server /s25/sentinel/redis6380.conf
  3. [root@vvkt7whznuckhiz2-0723575 sentinel]# /opt/redis/bin/redis-server /s25/sentinel/redis6381.conf
  4. [root@vvkt7whznuckhiz2-0723575 sentinel]# redis-cli -p 6379 info replication
  5. # Replication
  6. role:master
  7. connected_slaves:2
  8. slave0:ip=127.0.0.1,port=6380,state=online,offset=140,lag=0
  9. slave1:ip=127.0.0.1,port=6381,state=online,offset=140,lag=0
  10. master_failover_state:no-failover
  11. master_replid:63ad06a34215b522282347cc7bf3e9581c44defa
  12. master_replid2:0000000000000000000000000000000000000000
  13. master_repl_offset:140
  14. second_repl_offset:-1
  15. repl_backlog_active:1
  16. repl_backlog_size:1048576
  17. repl_backlog_first_byte_offset:1
  18. repl_backlog_histlen:140
  19. [root@vvkt7whznuckhiz2-0723575 sentinel]# redis-cli -p 6379 info CPU
  20. # CPU
  21. used_cpu_sys:0.146287
  22. used_cpu_user:0.202460
  23. used_cpu_sys_children:0.002344
  24. used_cpu_user_children:0.000799
  25. used_cpu_sys_main_thread:0.139133
  26. used_cpu_user_main_thread:0.196296

3准备三个哨兵的配置文件

  1. =========================================================================================================
  2. sentinel26380.conf
  3. port 26380
  4. dir /var/redis/data/
  5. logfile "26380.log"
  6. sentinel monitor mymaster 127.0.0.1 6379 2
  7. sentinel down-after-milliseconds mymaster 30000
  8. sentinel parallel-syncs mymaster 1
  9. sentinel failover-timeout mymaster 180000
  10. daemonize yes
  11. sentinel26381.conf
  12. port 26381
  13. dir /var/redis/data/
  14. logfile "26381.log"
  15. sentinel monitor mymaster 127.0.0.1 6379 2
  16. sentinel down-after-milliseconds mymaster 30000
  17. sentinel parallel-syncs mymaster 1
  18. sentinel failover-timeout mymaster 180000
  19. daemonize yes
  20. sentinel26382.conf
  21. port 26382
  22. dir /var/redis/data/
  23. logfile "26382.log"
  24. sentinel monitor mymaster 127.0.0.1 6379 2
  25. sentinel down-after-milliseconds mymaster 30000
  26. sentinel parallel-syncs mymaster 1
  27. sentinel failover-timeout mymaster 180000daemonize yes
  28. 分别启动以上三个哨兵
  29. [root@vvkt7whznuckhiz2-0723575 sentinel]# /opt/redis/bin/redis-sentinel /s25/sentinel/sentinel26380.conf
  30. [root@vvkt7whznuckhiz2-0723575 sentinel]# /opt/redis/bin/redis-sentinel /s25/sentinel/sentinel26381.conf
  31. [root@vvkt7whznuckhiz2-0723575 sentinel]# /opt/redis/bin/redis-sentinel /s25/sentinel/sentinel26382.conf

4 检查哨兵配置和主从关系

  1. [root@vvkt7whznuckhiz2-0723575 sentinel]# redis-cli -p 26380 info sentinel
  2. # Sentinel
  3. sentinel_masters:1
  4. sentinel_tilt:0
  5. sentinel_running_scripts:0
  6. sentinel_scripts_queue_length:0
  7. sentinel_simulate_failure_flags:0
  8. master0:name=mymaster,status=ok,address=127.0.0.1:6379,slaves=2,sentinels=3
  9. [root@vvkt7whznuckhiz2-0723575 sentinel]# redis-cli -p 6379 info replication
  10. # Replication
  11. role:master
  12. connected_slaves:2
  13. slave0:ip=127.0.0.1,port=6380,state=online,offset=149039,lag=1
  14. slave1:ip=127.0.0.1,port=6381,state=online,offset=149039,lag=1
  15. master_failover_state:no-failover
  16. master_replid:63ad06a34215b522282347cc7bf3e9581c44defa
  17. master_replid2:0000000000000000000000000000000000000000
  18. master_repl_offset:149172
  19. second_repl_offset:-1
  20. repl_backlog_active:1
  21. repl_backlog_size:1048576
  22. repl_backlog_first_byte_offset:1
  23. repl_backlog_histlen:149172

5 干掉端口号为6379的数据库,查看其他数据库的主从关系

  1. [root@vvkt7whznuckhiz2-0723575 sentinel]# redis-cli -p 6380 info replication
  2. # Replication
  3. role:slave
  4. master_host:127.0.0.1
  5. master_port:6379
  6. master_link_status:down
  7. master_last_io_seconds_ago:-1
  8. master_sync_in_progress:0
  9. slave_read_repl_offset:177179
  10. slave_repl_offset:177179
  11. master_link_down_since_seconds:17
  12. slave_priority:100
  13. slave_read_only:1
  14. replica_announced:1
  15. connected_slaves:0
  16. master_failover_state:no-failover
  17. master_replid:63ad06a34215b522282347cc7bf3e9581c44defa
  18. master_replid2:0000000000000000000000000000000000000000
  19. master_repl_offset:177179
  20. second_repl_offset:-1
  21. repl_backlog_active:1
  22. repl_backlog_size:1048576
  23. repl_backlog_first_byte_offset:1
  24. repl_backlog_histlen:177179
  25. [root@vvkt7whznuckhiz2-0723575 sentinel]# redis-cli -p 6381 info replication
  26. # Replication
  27. role:slave
  28. master_host:127.0.0.1
  29. master_port:6379
  30. master_link_status:down
  31. master_last_io_seconds_ago:-1
  32. master_sync_in_progress:0
  33. slave_read_repl_offset:177179
  34. slave_repl_offset:177179
  35. master_link_down_since_seconds:29
  36. slave_priority:100
  37. slave_read_only:1
  38. replica_announced:1
  39. connected_slaves:0
  40. master_failover_state:no-failover
  41. master_replid:63ad06a34215b522282347cc7bf3e9581c44defa
  42. master_replid2:0000000000000000000000000000000000000000
  43. master_repl_offset:177179
  44. second_repl_offset:-1
  45. repl_backlog_active:1
  46. repl_backlog_size:1048576
  47. repl_backlog_first_byte_offset:15
  48. repl_backlog_histlen:177165
  49. [root@vvkt7whznuckhiz2-0723575 sentinel]# redis-cli -p 6380 info replication
  50. # Replication
  51. role:master
  52. connected_slaves:1
  53. slave0:ip=127.0.0.1,port=6381,state=online,offset=180821,lag=1
  54. master_failover_state:no-failover
  55. master_replid:a7c231b0baeb9a7873036931b778be5be689ae84
  56. master_replid2:63ad06a34215b522282347cc7bf3e9581c44defa
  57. master_repl_offset:180821
  58. second_repl_offset:177180
  59. repl_backlog_active:1
  60. repl_backlog_size:1048576
  61. repl_backlog_first_byte_offset:1
  62. repl_backlog_histlen:180821

6 重启端口号为6379的数据库—多次读取数据库,查看关系变更

  1. vim pureredis6379.conf
  2. port 6379
  3. daemonize yes
  4. logfile "pure6379.log"
  5. dbfilename "dump-pure6379.rdb"
  6. dir "/var/redis/data"
  7. /opt/redis/bin/redis-server /s25/sentinel/pureredis6379.conf
  8. redis-cli -p 6379 info replication
  9. # Replication
  10. role:slave
  11. master_host:127.0.0.1
  12. master_port:6380
  13. master_link_status:up
  14. master_last_io_seconds_ago:1
  15. master_sync_in_progress:0
  16. slave_read_repl_offset:405999
  17. slave_repl_offset:405999
  18. slave_priority:100
  19. slave_read_only:1
  20. replica_announced:1
  21. connected_slaves:0
  22. master_failover_state:no-failover
  23. master_replid:a7c231b0baeb9a7873036931b778be5be689ae84
  24. master_replid2:0000000000000000000000000000000000000000
  25. master_repl_offset:405999
  26. second_repl_offset:-1
  27. repl_backlog_active:1
  28. repl_backlog_size:1048576
  29. repl_backlog_first_byte_offset:405312
  30. repl_backlog_histlen:688
  31. # 查看配置文件的更新
  32. cat pureredis6379.conf
  33. port 6379
  34. daemonize yes
  35. logfile "pure6379.log"
  36. dbfilename "dump-pure6379.rdb"
  37. dir "/var/redis/data"
  38. # Generated by CONFIG REWRITE
  39. pidfile "/var/run/redis.pid"
  40. save 3600 1
  41. save 300 100
  42. save 60 10000
  43. user default on nopass ~* &* +@all
  44. replicaof 127.0.0.1 6380

集群[redis-cluster配置 - py鱼 - 博客园 (cnblogs.com)]

准备六匹马儿亦六个节点,也就是六个配置文件

  1. vim rediscl7000.conf
  2. port 7000
  3. daemonize yes
  4. dir "/var/redis/data"
  5. logfile "7000.log"
  6. dbfilename "dump-7000.rdb"
  7. cluster-enabled yes
  8. cluster-config-file nodes-7000.conf
  9. cluster-require-full-coverage no
  10. vim rediscl7001.conf
  11. port 7001
  12. daemonize yes
  13. dir "/var/redis/data"
  14. logfile "7001.log"
  15. dbfilename "dump-7001.rdb"
  16. cluster-enabled yes
  17. cluster-config-file nodes-7001.conf
  18. cluster-require-full-coverage no
  19. vim rediscl7002.conf
  20. port 7002
  21. daemonize yes
  22. dir "/var/redis/data"
  23. logfile "7002.log"
  24. dbfilename "dump-7002.rdb"
  25. cluster-enabled yes
  26. cluster-config-file nodes-7002.conf
  27. cluster-require-full-coverage no
  28. vim rediscl7003.conf
  29. port 7003
  30. daemonize yes
  31. dir "/var/redis/data"
  32. logfile "7003.log"
  33. dbfilename "dump-7003.rdb"
  34. cluster-enabled yes
  35. cluster-config-file nodes-7003.conf
  36. cluster-require-full-coverage no
  37. vim rediscl7004.conf
  38. port 7004
  39. daemonize yes
  40. dir "/var/redis/data"
  41. logfile "7004.log"
  42. dbfilename "dump-7004.rdb"
  43. cluster-enabled yes
  44. cluster-config-file nodes-7004.conf
  45. cluster-require-full-coverage no
  46. vim rediscl7005.conf
  47. port 7005
  48. daemonize yes
  49. dir "/var/redis/data"
  50. logfile "7005.log"
  51. dbfilename "dump-7005.rdb"
  52. cluster-enabled yes
  53. cluster-config-file nodes-7005.conf
  54. cluster-require-full-coverage no

复制并且修改编号,并且逐个启动,尝试向集群中的节点写入数据

  1. vim /rediscluster/rediscl7000.conf
  2. vim /rediscluster/rediscl7001.conf
  3. vim /rediscluster/rediscl7002.conf
  4. vim /rediscluster/rediscl7003.conf
  5. vim /rediscluster/rediscl7004.conf
  6. vim /rediscluster/rediscl7005.conf
  7. # 启动多个配置文件
  8. /opt/redis/bin/redis-server /rediscluster/rediscl7000.conf
  9. /opt/redis/bin/redis-server /rediscluster/rediscl7001.conf
  10. /opt/redis/bin/redis-server /rediscluster/rediscl7002.conf
  11. /opt/redis/bin/redis-server /rediscluster/rediscl7003.conf
  12. /opt/redis/bin/redis-server /rediscluster/rediscl7004.conf
  13. /opt/redis/bin/redis-server /rediscluster/rediscl7005.conf
  14. # 测试单个数据库的文件写入
  15. ========================================================================================
  16. 此时写入数据会报错
  17. redis-cli -p 7000
  18. 127.0.0.1:7000> keys *
  19. (empty array)
  20. 127.0.0.1:7000> set name cj
  21. (error) CLUSTERDOWN Hash slot not served
  22. 127.0.0.1:7000>

安装额外配置

  1. 1 安装ruby
  2. yum -y install ruby
  3. 2 查看rub 以及 gem 版本
  4. ruby -v
  5. ruby 2.0.0p648 (2015-12-16) [x86_64-linux]
  6. gem -v
  7. 2.0.14.1
  8. 3 使用gem包管理下载redis插件
  9. gem install redis -v 3.3.0
  10. 4 搜索用于创建redis集群的脚本 redis-trib.rb
  11. which 用于搜索环境变量
  12. find 才是搜索文件的
  13. find -name "redis-trib.rb"
  14. 5. 批量创建集群
  15. 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
  16. >>> Performing hash slots allocation on 5 nodes...
  17. Master[0] -> Slots 0 - 3276
  18. Master[1] -> Slots 3277 - 6553
  19. Master[2] -> Slots 6554 - 9829
  20. Master[3] -> Slots 9830 - 13106
  21. Master[4] -> Slots 13107 - 16383
  22. M: 0f1ed10559d2484e0ed79f56dd08bd00c0f927f5 127.0.0.1:7001
  23. slots:[0-3276] (3277 slots) master
  24. M: 0a6cbdde7741b2cdb155dc876906e67e02e1c365 127.0.0.1:7002
  25. slots:[3277-6553] (3277 slots) master
  26. M: 6b390a5a1b1821d2b3701a5398ec2065705933d0 127.0.0.1:7003
  27. slots:[6554-9829] (3276 slots) master
  28. M: b2d844c1bb2a4c3b4073236a5e629bd94b74969c 127.0.0.1:7004
  29. slots:[9830-13106] (3277 slots) master
  30. M: 9e01cee325efff465d41431d3d078f239f3a3aad 127.0.0.1:7005
  31. slots:[13107-16383] (3277 slots) master
  32. Can I set the above configuration? (type 'yes' to accept): yes
  33. >>> Nodes configuration updated
  34. >>> Assign a different config epoch to each node
  35. >>> Sending CLUSTER MEET messages to join the cluster
  36. Waiting for the cluster to join
  37. .
  38. >>> Performing Cluster Check (using node 127.0.0.1:7001)
  39. M: 0f1ed10559d2484e0ed79f56dd08bd00c0f927f5 127.0.0.1:7001
  40. slots:[0-3276] (3277 slots) master
  41. M: 0a6cbdde7741b2cdb155dc876906e67e02e1c365 127.0.0.1:7002
  42. slots:[3277-6553] (3277 slots) master
  43. M: 9e01cee325efff465d41431d3d078f239f3a3aad 127.0.0.1:7005
  44. slots:[13107-16383] (3277 slots) master
  45. M: 6b390a5a1b1821d2b3701a5398ec2065705933d0 127.0.0.1:7003
  46. slots:[6554-9829] (3276 slots) master
  47. M: b2d844c1bb2a4c3b4073236a5e629bd94b74969c 127.0.0.1:7004
  48. slots:[9830-13106] (3277 slots) master
  49. [OK] All nodes agree about slots configuration.
  50. >>> Check for open slots...
  51. >>> Check slots coverage...
  52. [OK] All 16384 slots covered.

查看主从关系

  1. [root@vvkt7whznuckhiz2-0723575 rediscluster]# redis-cli -p 7001 -c
  2. 127.0.0.1:7001> cluster nodes
  3. b2d844c1bb2a4c3b4073236a5e629bd94b74969c 127.0.0.1:7004@17004 master - 0 1637067122000 4 connected 9830-13106
  4. 0a6cbdde7741b2cdb155dc876906e67e02e1c365 127.0.0.1:7002@17002 master - 0 1637067123111 2 connected 3277-6553
  5. 9e01cee325efff465d41431d3d078f239f3a3aad 127.0.0.1:7005@17005 master - 0 1637067124114 5 connected 13107-16383
  6. 6b390a5a1b1821d2b3701a5398ec2065705933d0 127.0.0.1:7003@17003 master - 0 1637067122000 3 connected 6554-9829
  7. 0f1ed10559d2484e0ed79f56dd08bd00c0f927f5 127.0.0.1:7001@17001 myself,master - 0 1637067124000 1 connected 0-3276
  8. 127.0.0.1:7001>