在Debian系统上配置Redis主从复制涉及几个步骤。以下是详细的配置过程:
首先,确保在主服务器和从服务器上都安装了Redis。
sudo apt update
sudo apt install redis-server
编辑主服务器上的Redis配置文件 /etc/redis/redis.conf
。
sudo nano /etc/redis/redis.conf
找到并修改以下配置项:
bind
:绑定主服务器的IP地址,例如 bind 192.168.1.100
。port
:确保端口号一致,例如 port 6379
。requirepass
:设置主服务器的密码,例如 requirepass your_master_password
。保存并退出编辑器。
编辑从服务器上的Redis配置文件 /etc/redis/redis.conf
。
sudo nano /etc/redis/redis.conf
找到并修改以下配置项:
bind
:绑定从服务器的IP地址,例如 bind 192.168.1.101
。port
:确保端口号一致,例如 port 6379
。slaveof
:指定主服务器的IP地址和端口,例如 slaveof 192.168.1.100 6379
。masterauth
:设置主服务器的密码,例如 masterauth your_master_password
。保存并退出编辑器。
在主服务器和从服务器上分别重启Redis服务以应用配置更改。
sudo systemctl restart redis-server
在主服务器和从服务器上分别使用 redis-cli
连接到Redis实例,并检查复制状态。
redis-cli -h 192.168.1.100 -p 6379 -a your_master_password
info replication
你应该看到类似以下的输出,表示从服务器已成功连接:
# Replication
role:master
connected_slaves:1
slave0:ip=192.168.1.101,port=6379,state=online,offset=0,lag=0
redis-cli -h 192.168.1.101 -p 6379 -a your_master_password
info replication
你应该看到类似以下的输出,表示从服务器已成功连接到主服务器:
# Replication
role:slave
master_host:192.168.1.100
master_port:6379
master_link_status:up
在主服务器上写入数据,并在从服务器上读取数据,以验证复制功能是否正常工作。
redis-cli -h 192.168.1.100 -p 6379 -a your_master_password
set test_key "Hello, Redis!"
redis-cli -h 192.168.1.101 -p 6379 -a your_master_password
get test_key
如果从服务器返回 Hello, Redis!
,则表示复制功能正常工作。
通过以上步骤,你就可以在Debian系统上成功配置Redis的主从复制。