Redis的主从设置

Posted jfcat

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Redis的主从设置相关的知识,希望对你有一定的参考价值。

准备环境

从官方网站下载最新的稳定版本

$ wget https://download.redis.io/releases/redis-6.2.4.tar.gz
$ tar xzf redis-6.2.4.tar.gz
$ cd redis-6.2.4
$ make

make test使用tcl的版本低,就更新下

apt-get install tcl

执行make test,成功后就可以运行redis了

$ echo "PATH=/home/user/redis/redis-6.2.4/src:$PATH" >> ~/.zshrc
$ source ~/.zshrc
$ redis-server
$ redis-server redis6379.conf
30438:C 17 Jun 2021 12:31:35.231 # oO0OoO0OoO0Oo Redis is starting oO0OoO0OoO0Oo
30438:C 17 Jun 2021 12:31:35.231 # Redis version=6.2.4, bits=64, commit=00000000, modified=0, pid=30438, just started
30438:C 17 Jun 2021 12:31:35.231 # Configuration loaded
30438:M 17 Jun 2021 12:31:35.232 * Increased maximum number of open files to 10032 (it was originally set to 1024).
30438:M 17 Jun 2021 12:31:35.232 * monotonic clock: POSIX clock_gettime
                _._
           _.-``__ ''-._
      _.-``    `.  `_.  ''-._           Redis 6.2.4 (00000000/0) 64 bit
  .-`` .-```.  ```\\/    _.,_ ''-._
 (    '      ,       .-`  | `,    )     Running in standalone mode
 |`-._`-...-` __...-.``-._|'` _.-'|     Port: 6379
 |    `-._   `._    /     _.-'    |     PID: 30438
  `-._    `-._  `-./  _.-'    _.-'
 |`-._`-._    `-.__.-'    _.-'_.-'|
 |    `-._`-._        _.-'_.-'    |           https://redis.io
  `-._    `-._`-.__.-'_.-'    _.-'
 |`-._`-._    `-.__.-'    _.-'_.-'|
 |    `-._`-._        _.-'_.-'    |
  `-._    `-._`-.__.-'_.-'    _.-'
      `-._    `-.__.-'    _.-'
          `-._        _.-'
              `-.__.-'

30438:M 17 Jun 2021 12:31:35.232 # Server initialized
30438:M 17 Jun 2021 12:31:35.232 # WARNING overcommit_memory is set to 0! Background save may fail under low memory condition. To fix this issue add 'vm.overcommit_memory = 1' to /etc/sysctl.conf and then reboot or run the command 'sysctl vm.overcommit_memory=1' for this to take effect.
30438:M 17 Jun 2021 12:31:35.232 * Loading RDB produced by version 6.2.4

 

Redis主从

Redis的主从设置只要执行slaveof ip port 就可以了,当然也可以配置文件设置

使用下载源文件的配置redis.conf,拷贝两个文件做主从

修改配置文件的以下内容

port 6379

pidfile /home/user/redis/pid/6379.pid

dir /home/user/redis/lib/6379/

保持两个文件中的端口,配置文件,数据文件,日志等不同就可以,默认日志配置是标准输出可以不管。这里设置6379和6380两个配置文件。

$ redis-server redis6379.conf
30438:C 17 Jun 2021 12:31:35.231 # oO0OoO0OoO0Oo Redis is starting oO0OoO0OoO0Oo
30438:C 17 Jun 2021 12:31:35.231 # Redis version=6.2.4, bits=64, commit=00000000, modified=0, pid=30438, just started
30438:C 17 Jun 2021 12:31:35.231 # Configuration loaded
30438:M 17 Jun 2021 12:31:35.232 * Increased maximum number of open files to 10032 (it was originally set to 1024).
30438:M 17 Jun 2021 12:31:35.232 * monotonic clock: POSIX clock_gettime
                _._
           _.-``__ ''-._
      _.-``    `.  `_.  ''-._           Redis 6.2.4 (00000000/0) 64 bit
  .-`` .-```.  ```\\/    _.,_ ''-._
 (    '      ,       .-`  | `,    )     Running in standalone mode
 |`-._`-...-` __...-.``-._|'` _.-'|     Port: 6379
 |    `-._   `._    /     _.-'    |     PID: 30438
  `-._    `-._  `-./  _.-'    _.-'
 |`-._`-._    `-.__.-'    _.-'_.-'|
 |    `-._`-._        _.-'_.-'    |           https://redis.io
  `-._    `-._`-.__.-'_.-'    _.-'
 |`-._`-._    `-.__.-'    _.-'_.-'|
 |    `-._`-._        _.-'_.-'    |
  `-._    `-._`-.__.-'_.-'    _.-'
      `-._    `-.__.-'    _.-'
          `-._        _.-'
              `-.__.-'

30438:M 17 Jun 2021 12:31:35.232 # Server initialized
30438:M 17 Jun 2021 12:31:35.232 # WARNING overcommit_memory is set to 0! Background save may fail under low memory condition. To fix this issue add 'vm.overcommit_memory = 1' to /etc/sysctl.conf and then reboot or run the command 'sysctl vm.overcommit_memory=1' for this to take effect.
30438:M 17 Jun 2021 12:31:35.232 * Loading RDB produced by version 6.2.4

两个服务都启动后在另外预设为从服务的客户端中执行命令

127.0.0.1:6380> SLAVEOF 127.0.0.1 6379
OK
127.0.0.1:6380> info
# Server
...  //这里省略一堆输出
...
# Replication
role:slave
master_host:127.0.0.1
master_port:6379

看到上面的role已经改为slave

然后在mater下看下添加一些key在从服务器上是否有影响

127.0.0.1:6379> set port 6379
OK

在从服务器下执行get命令

127.0.0.1:6380> get port
"6379"

这样主从设置就ok了。

 

 

 

引用:

https://redis.io/download

 

以上是关于Redis的主从设置的主要内容,如果未能解决你的问题,请参考以下文章

redis安全设置及主从配置

Redis的主从设置

Redis 主从配置

windows下redis主从复制设置

Redis + keepalived 主从设置与搭建

redis主从切换