NoSQL数据库

Redis主从复制配置(一主一从/多从)

2023-03-16  本文已影响0人  技术老男孩

一、环境准备:

主机名 IP地址 端口 角色
Host51 192.168.88.51 6379 主服务器
Host52 192.168.88.52 6379 从服务器

二、配置思路流程:

  1. 准备主和从的数据库服务器
  2. 配置Host52服务器并指定主服务器信息
  3. 验证测试

三、实操:

第一步:准备主和从的数据库服务器

[root@host51 ~]# yum -y install gcc 
[root@host51 ~]# tar -xf redis-4.0.8.tar.gz 
[root@host51 ~]# cd redis-4.0.8/
[root@host51 ~]# make && make install
[root@host51 ~]# ./utils/install_server.sh  遇到提示就回车

[root@host51 ~]# /etc/init.d/redis_6379  stop
[root@host51 ~]# vim /etc/redis/6379.conf 
bind 192.168.88.51 
[root@host51 ~]# /etc/init.d/redis_6379  start
[root@host52 ~]# yum -y install gcc 
[root@host52 ~]# tar -xf redis-4.0.8.tar.gz 
[root@host52 ~]# cd redis-4.0.8/
[root@host52 ~]# make && make install
[root@host52 ~]# ./utils/install_server.sh  遇到提示就回车

[root@host52 ~]# /etc/init.d/redis_6379  stop
[root@host52 ~]# vim /etc/redis/6379.conf
bind 192.168.88.52 
           
[root@host52 ~]# /etc/init.d/redis_6379  start
# host51主机 连接服务  
[root@host51 ~]# redis-cli  -h 192.168.88.51 -p 6379  
# 查看复制信息
192.168.88.51:6379> info replication   
# Replication
role:master  # 角色是 master 
connected_slaves:0  # 从服务器的个数 是 零个 

第二步:配置Host52服务器并指定主服务器信息

# 连接服务 指定主服务器的ip 和端口
[root@host52 ~]# redis-cli  -h 192.168.88.52 -p 6379  
192.168.88.52:6379> slaveof  192.168.88.51  6379
# 永久保存
192.168.88.52:6379> config rewrite   
OK
192.168.88.52:6352> info replication
# Replication
role:slave  角色变为 slave 
master_host:192.168.88.51   主服务器ip
master_port:6351  主服务器端口
master_link_status:up   能与主服务器连接   如是down 表示连接不上master 服务
.....       

第三步:验证测试

[root@host51 ~]# redis-cli  -h 192.168.88.51 -p 6379 
192.168.88.51:6379> mset   x 1  y  2  z 3  
OK
192.168.88.51:6379> keys *
1) "x"
2) "z"
3) "y"
192.168.88.51:6351>         
[root@host52 ~]# redis-cli  -h 192.168.88.52 -p 6379 
192.168.88.52:6379> keys *
1) "z"
2) "y"
3) "x"

四、补充(配置带密码的主从复制)

第一步:配置master角色服务器51的连接密码

[root@host51 ~]# redis-cli  -h 192.168.88.51 -p 6379
# 设置密码
192.168.88.51:6379> config set requirepass 123456 
OK
# 输入密码
192.168.88.51:6379> auth 123456 
OK
# 把密码保存到 文件的最后1行
192.168.88.51:6379> config rewrite 
OK
# 查看密码
192.168.88.51:6379> config get requirepass 
1"requirepass"
2) "123456"
192.168.88.51:6379> exit
# 查看保存的密码
[root@host51 ~]# tail -1 /etc/redis/6379.conf  
requirepass "123456"

第二步:配置从服务器host52设置master连接密码

[root@host52 ~]# redis-cli  -h 192.168.88.52  -p 6379
# 设置连接master服务器密码
192.168.88.52:6379> config set masterauth 123456 
OK
# 保存配置到主配置文件的最后1行
192.168.88.52:6379> config rewrite  
OK
#  查看连接主服务器密码
192.168.88.52:6379> config get masterauth 
1) "masterauth"
2) "123456"
# 查看复制信息
192.168.88.52:6379> info replication  
role:slave
master_host:192.168.88.51
master_port:6379
master_link_status:up
....
....
192.168.4.52:6379> exit
# 查看保存的配置
[root@host52 redis-4.0.8]# tail -1 /etc/redis/6379.conf  
masterauth "123456"
上一篇 下一篇

猜你喜欢

热点阅读