redis 主从复制

redis 主从复制

名词

建立复制 取消复制 切主 slaveof <masterip> <masterport> slaveof no one info replication config rewrite dump.rdb

复制 bin 和 redis.conf

[root@localhost redis-3.2.8]# mkdir -p /usr/local/src/redis/redis-master-slave/redis-6379
[root@localhost redis-3.2.8]# cp -r bin/ /usr/local/src/redis/redis-master-slave/redis-6379/
[root@localhost redis-3.2.8]# cp redis.conf /usr/local/src/redis/redis-master-slave/redis-6379/

[root@localhost redis-master-slave]# cp -r redis-6379/ redis-6380
[root@localhost redis-master-slave]# cp -r redis-6379/ redis-6381
[root@localhost redis-master-slave]# ll
total 0
drwxr-xr-x. 3 root root 35 Jul 20 23:20 redis-6379
drwxr-xr-x. 3 root root 35 Jul 20 23:22 redis-6380
drwxr-xr-x. 3 root root 35 Jul 20 23:22 redis-6381

修改 redis.conf

# 修改每个实例对应的 redis.conf
[root@localhost redis-master-slave]# vi redis-6379/redis.conf 
[root@localhost redis-master-slave]# vi redis-6380/redis.conf 
[root@localhost redis-master-slave]# vi redis-6381/redis.conf

# 修改以下配置项
# bind 127.0.0.1
protected-mode no # 如果为 yes 则只允许 bind 参数指定的主机进行访问。
daemonize yes
port 6379
port 6380
port 6381

启动 redis 实例

# 启动 redis 实例
[root@localhost redis-master-slave]# ./redis-6379/bin/redis-server ./redis-6379/redis.conf 
[root@localhost redis-master-slave]# ./redis-6380/bin/redis-server ./redis-6380/redis.conf 
[root@localhost redis-master-slave]# ./redis-6381/bin/redis-server ./redis-6381/redis.conf

[root@localhost redis-master-slave]# ps -ef | grep redis
root       5075   4880  0 23:16 pts/3    00:00:02 ./bin/redis-server *:6379
root       5168      1  0 23:39 ?        00:00:00 ./redis-6380/bin/redis-server *:6380
root       5172      1  0 23:39 ?        00:00:00 ./redis-6381/bin/redis-server *:6381
root       5178   5082  0 23:39 pts/2    00:00:00 grep --color=auto redis

# 启动客户端
[root@localhost ~]# /usr/local/src/redis/redis-master-slave/redis-6379/bin/redis-cli -h 192.168.20.132 -p 6379
192.168.20.132:6379>
[root@localhost ~]# /usr/local/src/redis/redis-master-slave/redis-6380/bin/redis-cli -h 192.168.20.132 -p 6380
192.168.20.132:6380>
[root@localhost ~]# /usr/local/src/redis/redis-master-slave/redis-6381/bin/redis-cli -h 192.168.20.132 -p 6381
192.168.20.132:6381>

# 未建立主从
192.168.20.132:6381> info replication
# Replication
role:master
connected_slaves:0
master_repl_offset:0
repl_backlog_active:0
repl_backlog_size:1048576
repl_backlog_first_byte_offset:0
repl_backlog_histlen:0

动态配置主从

slaveof <masterip> <masterport> info replication

# 6379 主节点
127.0.0.1:6379> info replication
# Replication
role:master
connected_slaves:2
slave0:ip=192.168.20.132,port=6380,state=online,offset=169,lag=1
slave1:ip=192.168.20.132,port=6381,state=online,offset=169,lag=1
master_repl_offset:169
repl_backlog_active:1
repl_backlog_size:1048576
repl_backlog_first_byte_offset:2
repl_backlog_histlen:168

# 6380 从节点
192.168.20.132:6380> slaveof 192.168.20.132 6379
OK
192.168.20.132:6380> info replication
# Replication
role:slave
master_host:192.168.20.132
master_port:6379
master_link_status:up
master_last_io_seconds_ago:7
master_sync_in_progress:0
slave_repl_offset:1
slave_priority:100
slave_read_only:1
connected_slaves:0
master_repl_offset:0
repl_backlog_active:0
repl_backlog_size:1048576
repl_backlog_first_byte_offset:0
repl_backlog_histlen:0

# 6381 从节点
192.168.20.132:6381> slaveof 192.168.20.132 6379
OK
192.168.20.132:6381> info replication
# Replication
role:slave
master_host:192.168.20.132
master_port:6379
master_link_status:up
master_last_io_seconds_ago:5
master_sync_in_progress:0
slave_repl_offset:71
slave_priority:100
slave_read_only:1
connected_slaves:0
master_repl_offset:0
repl_backlog_active:0
repl_backlog_size:1048576
repl_backlog_first_byte_offset:0
repl_backlog_histlen:0

主节点宕机

如果是持久化配置,则主节点的角色不会发生改变。

# 主节点宕机后从节点的信息
192.168.20.132:6381> info replication
# Replication
role:slave
master_host:192.168.20.132
master_port:6379
master_link_status:down
master_last_io_seconds_ago:-1
master_sync_in_progress:0
slave_repl_offset:1033
master_link_down_since_seconds:15
slave_priority:100
slave_read_only:1
connected_slaves:0
master_repl_offset:0
repl_backlog_active:0
repl_backlog_size:1048576
repl_backlog_first_byte_offset:0
repl_backlog_histlen:0

取消复制

slaveof no one 不会丢弃原有数据

127.0.0.1:6380> slaveof no one
OK
127.0.0.1:6380> info replication
# Replication
role:master
connected_slaves:0
master_repl_offset:155
repl_backlog_active:0
repl_backlog_size:1048576
repl_backlog_first_byte_offset:0
repl_backlog_histlen:0

127.0.0.1:6380> keys *
1) "mo2"
2) "mo1"

切主

原有数据将被丢弃

192.168.20.132:6381> info replication
# Replication
role:slave
master_host:192.168.20.132
master_port:6379
master_link_status:up
master_last_io_seconds_ago:10
master_sync_in_progress:0
slave_repl_offset:323
slave_priority:100
slave_read_only:1
connected_slaves:0
master_repl_offset:0
repl_backlog_active:0
repl_backlog_size:1048576
repl_backlog_first_byte_offset:0
repl_backlog_histlen:0
192.168.20.132:6381> slaveof 192.168.20.132 6380
OK
192.168.20.132:6381> info replication
# Replication
role:slave
master_host:192.168.20.132
master_port:6380
master_link_status:up
master_last_io_seconds_ago:1
master_sync_in_progress:0
slave_repl_offset:156
slave_priority:100
slave_read_only:1
connected_slaves:0
master_repl_offset:0
repl_backlog_active:0
repl_backlog_size:1048576
repl_backlog_first_byte_offset:0
repl_backlog_histlen:0

运行时配置持久化

config rewrite 原子操作

192.168.20.132:6381> config rewrite
OK
# Generated by CONFIG REWRITE
slaveof 192.168.20.132 6380

CONFIG REWRITE命令重写服务器启动时指定的redis.conf文件,将服务器当前使用的配置持久化到配置文件中。

重写以非常保守的方式执行:

  • 注释和原始 redis.conf 文件的整体结构会尽可能的保留下来。
  • 如果一个选项在旧的 redis.conf 文件中已经存在,那么它会在相同的位置(行号)被重写。
  • 如果某个选项在配置文件中尚不存在,但被设置为了该选项的默认值,那么他将不会被重写进程写入配置文件。
  • 如果某个选项在配置文件中尚不存在,但被设置了一个非默认值,那么它会被追加到文件的末尾。
  • 未使用的行将会留空。例如,如果你之前在配置文件中有多个save配置项,但由于你禁用了 RDB 持久化,当前的save配置变少了或者变为空,那么所有的那些行将会是空行。

杀死进程

[root@localhost redis-master-slave]# ps -ef | grep redis
root       5330      1  0 00:08 ?        00:00:02 redis-server *:6380
root       5335   5225  0 00:08 pts/4    00:00:00 redis-cli -p 6380
root       5359      1  0 00:11 ?        00:00:01 redis-server *:6379
root       5363   5202  0 00:11 pts/1    00:00:00 redis-cli
root       5375      1  0 00:22 ?        00:00:00 redis-server *:6381
root       5488   5465  0 00:37 pts/7    00:00:00 grep --color=auto redis

[root@localhost redis-master-slave]# man kill
[root@localhost redis-master-slave]# kill -s 9 5330 5335 5359 5363 5375 

redis 填坑 dump.rdb 文件在哪里

# The filename where to dump the DB
dbfilename dump.rdb

# The working directory.
#
# The DB will be written inside this directory, with the filename specified
# above using the 'dbfilename' configuration directive.
#
# The Append Only File will also be created inside this directory.
#
# Note that you must specify a directory here, not a file name.
dir ./

从 redis.conf 配置中可知道,dir 参数指定 rdb 文件生成位置,默认值为 ./ ,表示执行 redis.server 命令时的当前目录。所以要么你每次从需要生成 rdb 文件的位置来执行 redis.server 。要么将 dir 参数的值设置为固定值。

# dir ./ 默认配置下,执行 redis.server 时,pwd 命令显示当前目录为 /usr/local/src/redis。
# dump.rdb 文件将保存在 /usr/local/src/redis 下。
[root@localhost redis]# pwd
/usr/local/src/redis
[root@localhost redis]# redis-master-slave/redis-6379/bin/redis-server redis-master-slave/redis-6379/redis.conf
[root@localhost redis]# ll
total 4
-rw-r--r--. 1 root root 95 Jul 21 00:56 dump.rdb
drwxr-xr-x. 5 root root 60 Jul 21 00:48 redis-master-slave

查找 dump.rdb 文件

[root@localhost redis]# find ./ -name dump.rdb
./redis-master-slave/redis-6380/bin/dump.rdb
./dump.rdb
posted @ 2019-08-06 09:08  没有理由不会呀  阅读(203)  评论(0编辑  收藏  举报