Redis主从配置

本文详细介绍了Redis主从配置的步骤,包括解压安装、配置主从节点,以及启动和验证主从复制功能。主Redis配置在6379端口,两个从节点分别在6380和6381端口。通过修改redis.conf文件,配置主从关系,并启动Redis实例。通过示例展示在主Redis上设置数据,然后在从Redis上查看数据的同步情况。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

1.解压redis

tar -xvf redis-3.2.11.tar.gz

cd redis-3.2.11

make

“如果make有报错,则执行,没有报错就不需要 make MALLOC=libc  ”

配置redis主从

主:0.0.0.0:6379

从:0.0.0.0:6380

从:0.0.0.0:6381

主Redis配置

cp -r redis-3.2.11 /usr/local/redis-1

修改redis.conf文件

Cd /usr/local/redis-1

Vim redis.conf

 

 

redis.conf

#下面是redis.conf的主 要配置参数的意义:

#daemonize:是否以后台daemon方式运行

#pidfile:pid文件位置

#port:监听的端口号

#timeout:请求超时时间

#loglevel:log信息级别

#logfile:log文件位置

#databases:开启数据库的数量

#save * *:保存快照的频率,第一个*表示多长时间,第三个*表示执行多少次写操作。在一定时间内执行一定数量的写操作时,自动保存快照。可设置多个条件。

#rdbcompression:是否使用压缩

#dbfilename:数据快照文件名(只是文件名,不包括目录)

#dir:数据快照的保存目录(这个是目录)

#appendonly:是否开启appendonlylog,开启的话每次写操作会记一条log,这会提高数据抗风险能力,但影响效率。

#appendfsync:appendonlylog如何同步到磁盘(三个选项,分别是每次写都强制调用fsync、每秒启用一次fsync、不调用fsyn

c等待系统自己同步)

 

daemonize yes

bind 0.0.0.0

pidfile "/var/run/redis_1.pid"

port 6379

 

maxclients 10000

tcp-backlog 511

timeout 300

tcp-keepalive 0

loglevel warning

maxmemory 80gb

logfile "/var/log/redis-01.log"

databases 16

#save 900 1

#save 300 10

#save 60 10000

#stop-writes-on-bgsave-error yes

rdbcompression yes

rdbchecksum yes

#dbfilename dump.rdb

dir"/usr/local/redis1/dir"

slave-serve-stale-data yes

slave-read-only yes

repl-disable-tcp-nodelay no

slave-priority 100

appendonly no

#appendfilename "appendonly.aof"

appendfsync no

no-appendfsync-on-rewrite no

auto-aof-rewrite-percentage 100

auto-aof-rewrite-min-size 64mb

lua-time-limit 5000

slowlog-log-slower-than 5000

slowlog-max-len 128

notify-keyspace-events ""

hash-max-ziplist-entries 512

hash-max-ziplist-value 64

list-max-ziplist-entries 512

list-max-ziplist-value 64

set-max-intset-entries 512

zset-max-ziplist-entries 128

zset-max-ziplist-value 64

hll-sparse-max-bytes 3000

activerehashing yes

client-output-buffer-limit normal 0 0 0

client-output-buffer-limit slave 256mb 64mb60

client-output-buffer-limit pubsub 32mb 8mb 60

hz 10

aof-rewrite-incremental-fsync yes

# Generated by CONFIG REWRITE

 

从Redis配置

cp -r redis2 /usr/local/redis-2

修改redis.conf文件

Cd /usr/local/redis-2

Vim redis.conf

daemonize yes

bind 0.0.0.0

pidfile "/var/run/redis2.pid"

port 6380

 

maxclients 10000

tcp-backlog 511

timeout 300

tcp-keepalive 0

loglevel warning

maxmemory 80gb

logfile "/var/log/redis-02.log"

databases 16

#save 900 1

#save 300 10

#save 60 10000

#stop-writes-on-bgsave-error yes

rdbcompression yes

rdbchecksum yes

#dbfilename dump.rdb

dir"/usr/local/redis-2/dir"

slave-serve-stale-data yes

slave-read-only yes

repl-disable-tcp-nodelay no

slave-priority 100

appendonly no

#appendfilename "appendonly.aof"

appendfsync no

no-appendfsync-on-rewrite no

auto-aof-rewrite-percentage 100

auto-aof-rewrite-min-size 64mb

lua-time-limit 5000

slowlog-log-slower-than 5000

slowlog-max-len 128

notify-keyspace-events ""

hash-max-ziplist-entries 512

hash-max-ziplist-value 64

list-max-ziplist-entries 512

list-max-ziplist-value 64

set-max-intset-entries 512

zset-max-ziplist-entries 128

zset-max-ziplist-value 64

hll-sparse-max-bytes 3000

activerehashing yes

client-output-buffer-limit normal 0 0 0

client-output-buffer-limit slave 256mb 64mb60

client-output-buffer-limit pubsub 32mb 8mb 60

hz 10

aof-rewrite-incremental-fsync yes

# Generated by CONFIG REWRITE

slaveof 127.0.0.1 6379

 

从Redis配置

cp -r redis-3.2.11 /usr/local/redis-3

修改redis.conf文件

Cd /usr/local/redis-3

Vim redis.conf

daemonize yes

bind 0.0.0.0

pidfile "/var/run/redis3.pid"

port 6381

 

maxclients 10000

tcp-backlog 511

timeout 300

tcp-keepalive 0

loglevel warning

maxmemory 80gb

logfile "/var/log/redis-03.log"

databases 16

#save 900 1

#save 300 10

#save 60 10000

#stop-writes-on-bgsave-error yes

rdbcompression yes

rdbchecksum yes

#dbfilename dump.rdb

dir"/usr/local/redis-3/dir"

slave-serve-stale-data yes

slave-read-only yes

repl-disable-tcp-nodelay no

slave-priority 100

appendonly no

#appendfilename "appendonly.aof"

appendfsync no

no-appendfsync-on-rewrite no

auto-aof-rewrite-percentage 100

auto-aof-rewrite-min-size 64mb

lua-time-limit 5000

slowlog-log-slower-than 5000

slowlog-max-len 128

notify-keyspace-events ""

hash-max-ziplist-entries 512

hash-max-ziplist-value 64

list-max-ziplist-entries 512

list-max-ziplist-value 64

set-max-intset-entries 512

zset-max-ziplist-entries 128

zset-max-ziplist-value 64

hll-sparse-max-bytes 3000

activerehashing yes

client-output-buffer-limit normal 0 0 0

client-output-buffer-limit slave 256mb 64mb60

client-output-buffer-limit pubsub 32mb 8mb 60

hz 10

aof-rewrite-incremental-fsync yes

# Generated by CONFIG REWRITE

slaveof 127.0.0.1 6379

 

 

 

启动redis

/usr/local/redis-1/src/redis-server/usr/local/redis-1/redis.conf

/usr/local/redis-2/src/redis-server/usr/local/redis-2/redis.conf

/usr/local/redis-3/src/redis-server/usr/local/redis-3/redis.conf

登入redis

/usr/local/redis-1/src/redis-cli-h 127.0.0.1 –p 6379

/usr/local/redis-2/src/redis-cli-h 127.0.0.1 –p 6380

/usr/local/redis-3/src/redis-cli-h 127.0.0.1 –p 6381

 

测试数据

在主redis执行

set name person

get name

在从redis执行

get name

在从redis执行

get name

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值