redis主从实践
# 环境
角色 | 主机 | IP |
主库(master) | db01 | 10.0.0.51 |
从库(slave01) | db01 | 10.0.0.51 |
主库(slave02) | db01 | 10.0.0.51 |
# 配置多实例
#创建多实例目录
[root@db01 ~]# /etc/redis/{6379,6380,6381}
#编辑多实例配置文件
[root@db01 ~]# cat /etc/redis/6379/redis.conf /etc/redis/6380/redis.conf /etc/redis/6381/redis.conf
1
2
3
4
2
3
4
#redis 6379 配置文件
port 6379
daemonize yes
pidfile /etc/redis/6379/redis.pid
loglevel notice
logfile /etc/redis/6379/redis.log
dbfilename dump.rdb
dir /etc/redis/6379
bind 127.0.0.1 10.0.0.51
protected-mode no
1
2
3
4
5
6
7
8
9
10
2
3
4
5
6
7
8
9
10
#redis 6380 配置文件
port 6380
daemonize yes
pidfile /etc/redis/6380/redis.pid
loglevel notice
logfile /etc/redis/6380/redis.log
dbfilename dump.rdb
dir /etc/redis/6380
bind 127.0.0.1 10.0.0.51
protected-mode no
1
2
3
4
5
6
7
8
9
10
2
3
4
5
6
7
8
9
10
#redis 6381 配置文件
port 6381
daemonize yes
pidfile /etc/redis/6381/redis.pid
loglevel notice
logfile /etc/redis/6381/redis.log
dbfilename dump.rdb
dir /etc/redis/6381
bind 127.0.0.1 10.0.0.51
protected-mode no
1
2
3
4
5
6
7
8
9
10
2
3
4
5
6
7
8
9
10
#启动redis多实例
[root@db01 ~]# redis-server /etc/redis/6379/redis.conf
[root@db01 ~]# redis-server /etc/redis/6380/redis.conf
[root@db01 ~]# redis-server /etc/redis/6381/redis.conf
1
2
3
4
2
3
4
#查看进程
[root@db01 ~]# ps -ef|grep redis
root 3570 1 0 22:44 ? 00:00:00 redis-server 127.0.0.1:6379
root 3574 1 0 22:44 ? 00:00:00 redis-server 127.0.0.1:6380
root 3578 1 0 22:44 ? 00:00:00 redis-server 127.0.0.1:6381
1
2
3
4
5
2
3
4
5
# 开启主从
#连接从库slave01(6380)
[root@db01 ~]# redis-cli -p 6380
#开启主从
127.0.0.1:6380> SLAVEOF 127.0.0.1 6379
OK
#查从信息
127.0.0.1:6380> INFO replication
# Replication
role:slave //角色变成了从库
master_host:127.0.0.1 //主库的ip
master_port:6379 //主库的端口
master_link_status:up
master_last_io_seconds_ago:7
master_sync_in_progress:0
slave_repl_offset: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
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
#连接从库slave02(6381)
[root@db01 ~]# redis-cli -p 6381
#开启主从
127.0.0.1:6381> SLAVEOF 127.0.0.1 6379
OK
#查看主从信息
127.0.0.1:6381> INFO replication
# Replication
role:slave //角色变成了从库
master_host:127.0.0.1 //主库的ip
master_port:6379 //主库的端口
master_link_status:up
master_last_io_seconds_ago:9
master_sync_in_progress:0
slave_repl_offset:225
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
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
#连接master(6379)
[root@db01 ~]# redis-cli -p 6379
#在主库上查看主从复制信息
127.0.0.1:6379> INFO replication
# Replication
role:master //角色master
connected_slaves:2 //两台slave
slave0:ip=127.0.0.1,port=6380,state=online,offset=337,lag=1
slave1:ip=127.0.0.1,port=6381,state=online,offset=337,lag=1
master_repl_offset:337
repl_backlog_active:1
repl_backlog_size:1048576
repl_backlog_first_byte_offset:2
repl_backlog_histlen:336
1
2
3
4
5
6
7
8
9
10
11
12
13
14
2
3
4
5
6
7
8
9
10
11
12
13
14
# 主从切换
#连接master(6379)
[root@db01 ~]# redis-cli -p 6379
#关闭主库
127.0.0.1:6379> shutdown
#连接从库slave01(6380)
[root@db01 ~]# redis-cli -p 6380
#查看主从信息
127.0.0.1:6380> INFO replication
# Replication
role:slave
master_host:127.0.0.1
master_port:6379
master_link_status:down //连接主库的状态是:down
master_last_io_seconds_ago:-1
master_sync_in_progress:0
slave_repl_offset:1877
master_link_down_since_seconds:58
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
#取消6380的主从关系
127.0.0.1:6380> SLAVEOF no one
OK
127.0.0.1:6380> info replication
# Replication
role:master //此时6380的角色就变成了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
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
#将其他从库重新指向新主(6380)
#连接6381从库
[root@db01 ~]# redis-cli -p 6381
#将6381从库变成6380的从库
127.0.0.1:6381> SLAVEOF 127.0.0.1 6380
OK
#查看主从信息
127.0.0.1:6381> INFO replication
# Replication
role:slave //角色还是slave
master_host:127.0.0.1
master_port:6380 //主库的端口已经变成了6380
master_link_status:up
master_last_io_seconds_ago:4
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
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
![微信](http://pic.zzppjj.top/LightPicture/2023/02/cebf13bbcea9264d.jpg)
![支付宝](http://pic.zzppjj.top/LightPicture/2023/02/d98d15501306c7c4.jpg)
上次更新: 2024/07/18, 20:15:56