(高可用)五、Redis

redis

  • 文档:
    1. https://redis.io/docs/
    1. http://redis.cn/documentation.html
  • 内存数据库:与memcache区别
    • 支持更多的数据类型
    • 可以持久化
    • 一共16个数据库,从0开始索引

一、redis基础

源码编译

# 源码下载地址:http://10.60.14.19:81/2Q2W150913379F9670F5F89739D36E2810DD4AB2AFB1_unknown_806833F0FD9E2BA71F7D33D228D0412FCC8AD15D_8/download.redis.io/releases/redis-5.0.8.tar.gz
# 因为已经有Makefile了,直接可以make
[root@server1 redis-5.0.8]# make & make install

[root@server1 redis-5.0.8]# which redis-server
/usr/local/bin/redis-server

启动server

# 脚本启动redis server
[root@server1 redis-5.0.8]# ./utils/install_server.sh
Welcome to the redis service installer
This script will help you easily set up a running redis server

Please select the redis port for this instance: [6379]
Selecting default: 6379
Please select the redis config file name [/etc/redis/6379.conf]
Selected default - /etc/redis/6379.conf
Please select the redis log file name [/var/log/redis_6379.log]
Selected default - /var/log/redis_6379.log
Please select the data directory for this instance [/var/lib/redis/6379]
Selected default - /var/lib/redis/6379
Please select the redis executable path [/usr/local/bin/redis-server]
Selected config:
Port           : 6379
Config file    : /etc/redis/6379.conf
Log file       : /var/log/redis_6379.log
Data dir       : /var/lib/redis/6379
Executable     : /usr/local/bin/redis-server
Cli Executable : /usr/local/bin/redis-cli
Is this ok? Then press ENTER to go on or Ctrl-C to abort.
Copied /tmp/6379.conf => /etc/init.d/redis_6379
Installing service...
Successfully added to chkconfig!
Successfully added to runlevels 345!
Starting Redis server...
Installation successful!

修改配置

# By default, if no "bind" configuration directive is specified, Redis listens
# for connections from all the network interfaces available on the server.
# It is possible to listen to just one or multiple selected interfaces using
# the "bind" configuration directive, followed by one or more IP addresses.
#
# Examples:
#
# bind 192.168.1.100 10.0.0.1
# bind 127.0.0.1 ::1
#
# ~~~ WARNING ~~~ If the computer running Redis is directly exposed to the
# internet, binding to all the interfaces is dangerous and will expose the
# instance to everybody on the internet. So by default we uncomment the
# following bind directive, that will force Redis to listen only into
# the IPv4 loopback interface address (this means Redis will be able to
# accept connections only from clients running into the same computer it
# is running).
#
# IF YOU ARE SURE YOU WANT YOUR INSTANCE TO LISTEN TO ALL THE INTERFACES
# JUST COMMENT THE FOLLOWING LINE.
# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
bind 0.0.0.0  #########  这里修改为监听任意ip


[root@server1 redis-5.0.8]# /etc/init.d/redis_6379 restart  # 重启redis server
Stopping ...
Redis stopped
Starting Redis server...

命令redis-cli

# 交互式
[root@server1 redis-5.0.8]# redis-cli
127.0.0.1:6379>

# 非交互式
[root@server1 redis-5.0.8]# redis-cli config get bind
1) "bind"
2) "0.0.0.0"

二、redis的主从复制

  • redis使用异步复制,无法确定slave是否接收到写指令
  • slave只有只读权限
# 将server1上编译完成的redis及源码包拷贝到server2和server3上
[root@server1 ~]# scp /usr/local/bin/redis-* 192.168.147.138:/usr/local/bin/  # 拷贝二进制文件
[root@server1 ~]# scp -r /root/redis-5.0.8 192.168.147.138:/root/  # 拷贝源码


# server2上
[root@localhost bin]# rm -f redis-sentinel
[root@localhost bin]# ln -s redis-server redis-sentinel  # 创建软连接

# 安装redis server
[root@localhost redis-5.0.8]# ./utils/install_server.sh
Welcome to the redis service installer
This script will help you easily set up a running redis server

Please select the redis port for this instance: [6379]
Selecting default: 6379
Please select the redis config file name [/etc/redis/6379.conf]
Selected default - /etc/redis/6379.conf
Please select the redis log file name [/var/log/redis_6379.log]
Selected default - /var/log/redis_6379.log
Please select the data directory for this instance [/var/lib/redis/6379]
Selected default - /var/lib/redis/6379
Please select the redis executable path [/usr/local/bin/redis-server]
Selected config:
Port           : 6379
Config file    : /etc/redis/6379.conf
Log file       : /var/log/redis_6379.log
Data dir       : /var/lib/redis/6379
Executable     : /usr/local/bin/redis-server
Cli Executable : /usr/local/bin/redis-cli
Is this ok? Then press ENTER to go on or Ctrl-C to abort.
Copied /tmp/6379.conf => /etc/init.d/redis_6379
Installing service...
Successfully added to chkconfig!
Successfully added to runlevels 345!
Starting Redis server...
Installation successful!


# 修改配置
[root@localhost redis-5.0.8]# vim /etc/redis/6379.conf

bind 0.0.0.0

################################# REPLICATION #################################

# Master-Replica replication. Use replicaof to make a Redis instance a copy of
# another Redis server. A few things to understand ASAP about Redis replication.
#
#   +------------------+      +---------------+
#   |      Master      | ---> |    Replica    |
#   | (receive writes) |      |  (exact copy) |
#   +------------------+      +---------------+
#
# 1) Redis replication is asynchronous, but you can configure a master to
#    stop accepting writes if it appears to be not connected with at least
#    a given number of replicas.
# 2) Redis replicas are able to perform a partial resynchronization with the
#    master if the replication link is lost for a relatively small amount of
#    time. You may want to configure the replication backlog size (see the next
#    sections of this file) with a sensible value depending on your needs.
# 3) Replication is automatic and does not need user intervention. After a
#    network partition replicas automatically try to reconnect to masters
#    and resynchronize with them.
#
# replicaof <masterip> <masterport>
replicaof 192.168.147.139 6379


# 重启redis server
[root@localhost redis-5.0.8]# /etc/init.d/redis_6379 restart
Stopping ...
Redis stopped
Starting Redis server...


# 可以看到主从复制状态
127.0.0.1:6379> info

# Replication
role:master
connected_slaves:2
slave0:ip=192.168.147.138,port=6379,state=online,offset=602,lag=0   # 可以看到有两个slave
slave1:ip=192.168.147.140,port=6379,state=online,offset=602,lag=1
master_replid:a703223a554f5f5345b3479aa99cabe041af1165
master_replid2:0000000000000000000000000000000000000000
master_repl_offset:602
second_repl_offset:-1
repl_backlog_active:1
repl_backlog_size:1048576
repl_backlog_first_byte_offset:1
repl_backlog_histlen:602

  • 修改min-slaves-to-write参数
    防止出现master无法向slave同步数据时,仍然在接收客户端写入的情况

三、redis的主从切换

[root@server1 redis-5.0.8]# cp sentinel.conf /etc/redis/
[root@server1 redis-5.0.8]# vim /etc/redis/sentinel.conf

sentinel monitor mymaster 192.168.147.139 6379 2  # 表示当有两个slave认为master挂掉之后,才会推举新的master


[root@server1 redis-5.0.8]# grep -v ^# /etc/redis/sentinel.conf


port 26379

daemonize no

pidfile /var/run/redis-sentinel.pid

logfile ""


dir /tmp

sentinel monitor mymaster 192.168.147.139 6379 2


sentinel down-after-milliseconds mymaster 30000

sentinel parallel-syncs mymaster 1

sentinel failover-timeout mymaster 180000





sentinel deny-scripts-reconfig yes


# 拷贝到server2和server3
[root@server1 redis-5.0.8]# scp /etc/redis/sentinel.conf 192.168.147.138:/etc/redis/
root@192.168.147.138's password:
sentinel.conf                                                                                                                                             100% 9716     5.9MB/s   00:00
[root@server1 redis-5.0.8]# scp /etc/redis/sentinel.conf 192.168.147.140:/etc/redis/
root@192.168.147.140's password:
sentinel.conf    

# 注意要先同步sentinel.conf,再开启redis-sentinel进程,因为启动了redis-sentinel进程会修改sentinel.conf,导致配置不一致
[root@server1 redis-5.0.8]# redis-sentinel /etc/redis/sentinel.conf
8268:X 15 Jan 2023 00:19:08.872 # oO0OoO0OoO0Oo Redis is starting oO0OoO0OoO0Oo
8268:X 15 Jan 2023 00:19:08.872 # Redis version=5.0.8, bits=64, commit=00000000, modified=0, pid=8268, just started
8268:X 15 Jan 2023 00:19:08.872 # Configuration loaded
8268:X 15 Jan 2023 00:19:08.873 * Increased maximum number of open files to 10032 (it was originally set to 1024).
                _._
           _.-``__ ''-._
      _.-``    `.  `_.  ''-._           Redis 5.0.8 (00000000/0) 64 bit
  .-`` .-```.  ```\/    _.,_ ''-._
 (    '      ,       .-`  | `,    )     Running in sentinel mode
 |`-._`-...-` __...-.``-._|'` _.-'|     Port: 26379
 |    `-._   `._    /     _.-'    |     PID: 8268
  `-._    `-._  `-./  _.-'    _.-'
 |`-._`-._    `-.__.-'    _.-'_.-'|
 |    `-._`-._        _.-'_.-'    |           http://redis.io
  `-._    `-._`-.__.-'_.-'    _.-'
 |`-._`-._    `-.__.-'    _.-'_.-'|
 |    `-._`-._        _.-'_.-'    |
  `-._    `-._`-.__.-'_.-'    _.-'
      `-._    `-.__.-'    _.-'
          `-._        _.-'
              `-.__.-'

8268:X 15 Jan 2023 00:19:08.874 # WARNING: The TCP backlog setting of 511 cannot be enforced because /proc/sys/net/core/somaxconn is set to the lower value of 128.
8268:X 15 Jan 2023 00:19:08.875 # Sentinel ID is de1b8ebc78fe9b16f48addb7e0f1196cc9a4b8cd
8268:X 15 Jan 2023 00:19:08.875 # +monitor master mymaster 192.168.147.139 6379 quorum 2
8268:X 15 Jan 2023 00:19:08.876 * +slave slave 192.168.147.138:6379 192.168.147.138 6379 @ mymaster 192.168.147.139 6379
8268:X 15 Jan 2023 00:19:08.878 * +slave slave 192.168.147.140:6379 192.168.147.140 6379 @ mymaster 192.168.147.139 6379
8268:X 15 Jan 2023 00:19:38.964 # +sdown slave 192.168.147.138:6379 192.168.147.138 6379 @ mymaster 192.168.147.139 6379
8268:X 15 Jan 2023 00:19:38.964 # +sdown slave 192.168.147.140:6379 192.168.147.140 6379 @ mymaster 192.168.147.139 6379
8268:X 15 Jan 2023 00:19:55.764 * +sentinel sentinel ae304f8925cb72bb40d5bae07e52ef348b22e353 192.168.147.138 26379 @ mymaster 192.168.147.139 6379
8268:X 15 Jan 2023 00:20:17.292 * +sentinel sentinel 882f4ecbc5f4cbdcd019756a736247b9a9f31e9e 192.168.147.140 26379 @ mymaster 192.168.147.139 6379
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值