赞
踩
进入到 redis 根目录, 新建文件夹 configTest , 在redis 根目录找到 redis.windows.conf 文件复制一份到 configTest 目录,重命名为 redis6379.conf 。
在 configTest 目录新建 6379 端口启动文件 startRedis6379.bat
@echo off
echo 使用前请先配置REDIS_HOME
echo redis_home : %REDIS_HOME%
%REDIS_HOME%\redis-server.exe %REDIS_HOME%\configTest\redis6379.conf
@pause
本文配置一主二从模式, 6379 为主库端口号, 6380、6381 为从库端口号。
进入 configTest 文件夹
1、拷贝配置文件
复制 redis6379.conf 文件 重命名为 redis6380.config ,修改如下配置
port 6380
# slaveof <masterip> <masterport>
slaveof 127.0.0.1 6379
2、编辑启动文件
新建 6380 端口启动文件 startRedis6380.bat
@echo off
echo 使用前请先配置REDIS_HOME
echo redis_home : %REDIS_HOME%
%REDIS_HOME%\redis-server.exe %REDIS_HOME%\configTest\redis6380.conf
@pause
ps:运行时如果发现中文乱码,这是编码格式引起的,以记事本打开bat文件,另存文件,编码选择ANSI。
3、重复1、2 步骤,配置 6381端口
依次运行 startRedis6379.bat 、 startRedis6380.bat 、 startRedis6381.bat,启动 redis 服务。
启动后可以看到6379端口输出如下内容,从库 6380 和 6381 向主库 6379 发出同步请求且被成功响应。
[12164] 15 Sep 10:34:22.303 * Slave 127.0.0.1:6380 asks for synchronization
[12164] 15 Sep 10:34:22.303 * Full resync requested by slave 127.0.0.1:6380
[12164] 15 Sep 10:34:22.303 * Starting BGSAVE for SYNC with target: disk
[12164] 15 Sep 10:34:22.306 * Background saving started by pid 17288
[12164] 15 Sep 10:34:22.374 # fork operation complete
[12164] 15 Sep 10:34:22.374 * Background saving terminated with success
[12164] 15 Sep 10:34:22.377 * Synchronization with slave 127.0.0.1:6380 succeeded
[12164] 15 Sep 10:34:24.183 * Slave 127.0.0.1:6381 asks for synchronization
[12164] 15 Sep 10:34:24.183 * Full resync requested by slave 127.0.0.1:6381
[12164] 15 Sep 10:34:24.183 * Starting BGSAVE for SYNC with target: disk
[12164] 15 Sep 10:34:24.186 * Background saving started by pid 11896
[12164] 15 Sep 10:34:24.307 # fork operation complete
[12164] 15 Sep 10:34:24.307 * Background saving terminated with success
[12164] 15 Sep 10:34:24.309 * Synchronization with slave 127.0.0.1:6381 succeeded
从库窗口输出如下内容,从库向主库发送同步请求并成功被响应。
[2644] 15 Sep 10:34:24.183 * Connecting to MASTER 127.0.0.1:6379
[2644] 15 Sep 10:34:24.183 * MASTER <-> SLAVE sync started
[2644] 15 Sep 10:34:24.183 * Non blocking connect for SYNC fired the event.
[2644] 15 Sep 10:34:24.183 * Master replied to PING, replication can continue...
[2644] 15 Sep 10:34:24.183 * Partial resynchronization not possible (no cached master)
[2644] 15 Sep 10:34:24.186 * Full resync from master: 01d4c4cc3aa143e31ee85b43817012686dd50930:1
[2644] 15 Sep 10:34:24.309 * MASTER <-> SLAVE sync: receiving 880 bytes from master
[2644] 15 Sep 10:34:24.310 * MASTER <-> SLAVE sync: Flushing old data
[2644] 15 Sep 10:34:24.310 * MASTER <-> SLAVE sync: Loading DB in memory
[2644] 15 Sep 10:34:24.310 * MASTER <-> SLAVE sync: Finished with success
当主库宕机时,从库输出如下内容,从库试图向主库发送同步请求,但是未得到响应。
[11916] 15 Sep 10:56:10.052 # Connection with master lost.
[11916] 15 Sep 10:56:10.052 * Caching the disconnected master state.
[11916] 15 Sep 10:56:11.005 * Connecting to MASTER 127.0.0.1:6379
[11916] 15 Sep 10:56:11.005 * MASTER <-> SLAVE sync started
[11916] 15 Sep 10:56:13.024 * Non blocking connect for SYNC fired the event.
[11916] 15 Sep 10:56:13.886 # Sending command to master in replication handshake: -Writing to master: Unknown error
主库重新启动,从库重新连接到主库进行同步数据
[11916] 15 Sep 10:58:08.410 * Connecting to MASTER 127.0.0.1:6379
[11916] 15 Sep 10:58:08.410 * MASTER <-> SLAVE sync started
[11916] 15 Sep 10:58:08.926 * Non blocking connect for SYNC fired the event.
[11916] 15 Sep 10:58:08.926 * Master replied to PING, replication can continue...
[11916] 15 Sep 10:58:08.927 * Trying a partial resynchronization (request e1682b3493d27f55e4a40c203fb652c1087e765f:30).
[11916] 15 Sep 10:58:08.932 * Full resync from master: db0bef8d7c16c150e0ae198215c9b8e222f1ea5f:1
[11916] 15 Sep 10:58:08.932 * Discarding previously cached master state.
[11916] 15 Sep 10:58:09.046 * MASTER <-> SLAVE sync: receiving 880 bytes from master
[11916] 15 Sep 10:58:09.048 * MASTER <-> SLAVE sync: Flushing old data
[11916] 15 Sep 10:58:09.049 * MASTER <-> SLAVE sync: Loading DB in memory
[11916] 15 Sep 10:58:09.049 * MASTER <-> SLAVE sync: Finished with success
主库窗口输出如下内容,主库丢失从库连接。
[10116] 15 Sep 11:05:07.263 # Connection with slave 127.0.0.1:6380 lost.
从库宕机恢复服务主库正常运行时,可以正常连接主库进行数据同步,无需其它操作。
远程 redis 客户端 脚本
@echo off
echo %REDIS_HOME%
set /p host=please enter you host:
set /p port=please enter you port:
echo %REDIS_HOME%\redis-cli.exe -h %host% -p %port%
%REDIS_HOME%\redis-cli.exe -h %host% -p %port%
@pause
使用info replication
查看服务器状态, 可以看到主库角色为master、有两个从库,从库角色为slave
连接的主库为127.0.0.1:6379
主库客户端输入 set test master
写入一个键test ,值为 master
从库客户端输入 set test slave
写入报错,从库不允许写入数据 ,执行get test
查到主表写入的键test,值为master
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。