1)方法一:
redis-server --slaveof ,配置当前服务器成为某redis服务的Slave
[root@db ~]# redis-cli -p 6379
127.0.0.1:6379> keys *
(empty list or set)
127.0.0.1:6379> set a 123 //设置主库的key值
OK
127.0.0.1:6379> get a
"123"
127.0.0.1:6379>
//6380端口配置文件内容:
[root@db ~]# vim /etc/redis/6380.conf
port 6380 //启动端口
# Generated by CONFIG REWRITE
dir "/etc/redis" //目录位置
//根据配置文件,再启动一个redis数据库示例
[root@db redis]# redis-server --port 6380 --slaveof 127.0.0.1 6379 //--port,指明从库的端口号;--slaveof,指明主库的IP地址和端口号
[2294] 05 Feb 17:13:54.588 * Increased maximum number of open files to 10032 (it was originally set to 1024).
//登录6380数据库并同步6379主库
[root@db ~]# redis-cli -p 6380
127.0.0.1:6380> keys *
1) "a"
127.0.0.1:6380> get a
"123" //有此数据同步完成
127.0.0.1:6380> exit
2)方法二:
SLAVEOF host port命令,将当前服务器状态从Master修改为指定服务器的Slave
[root@db ~]# redis-cli -p 6379
127.0.0.1:6379> set bbb 456 //添加主库数据
OK
127.0.0.1:6379> keys *
1) "bbb"
127.0.0.1:6379> get bbb
"456"
127.0.0.1:6379>
[root@db redis]# redis-server 6380.conf //启动从库
[2362] 05 Feb 17:29:05.779 * Increased maximum number of open files to 10032 (it was originally set to 1024).
[root@db ~]# redis-cli -p 6380
127.0.0.1:6380> keys *
(empty list or set) //此时从库还没有任何数据
127.0.0.1:6380>
127.0.0.1:6380> SLAVEOF 127.0.0.1 6379 //指明需要同步的主库的IP地址和端口号
OK
127.0.0.1:6380> keys *
1) "bbb"
127.0.0.1:6380> get bbb
"456" //数据已然同步完成
127.0.0.1:6380>
127.0.0.1:6380> SLAVEOF no one //退出数据同步状态
OK
127.0.0.1:6380> keys * //但是数据仍然会存在
1) "bbb"
127.0.0.1:6380>
观察redis信息变化:
[2362] 05 Feb 17:29:05.783 * DB loaded from disk: 0.000 seconds
[2362] 05 Feb 17:29:05.783 * The server is now ready to accept connections on port 6380
[2362] 05 Feb 17:32:16.975 * SLAVE OF 127.0.0.1:6379 enabled (user request)
[2362] 05 Feb 17:32:17.379 * Connecting to MASTER 127.0.0.1:6379
[2362] 05 Feb 17:32:17.380 * MASTER <-> SLAVE sync started
[2362] 05 Feb 17:32:17.380 * Non blocking connect for SYNC fired the event.
[2362] 05 Feb 17:32:17.380 * Master replied to PING, replication can continue...
[2362] 05 Feb 17:32:17.381 * Partial resynchronization not possible (no cached master)
[2362] 05 Feb 17:32:17.384 * Full resync from master: 9b67e86972cf1a7ba949268aa1553d5f73970bae:1570
[2362] 05 Feb 17:32:17.482 * MASTER <-> SLAVE sync: receiving 28 bytes from master
[2362] 05 Feb 17:32:17.483 * MASTER <-> SLAVE sync: Flushing old data
[2362] 05 Feb 17:32:17.483 * MASTER <-> SLAVE sync: Loading DB in memory
[2362] 05 Feb 17:32:17.483 * MASTER <-> SLAVE sync: Finished with success
[2362] 05 Feb 17:33:46.677 # Connection with master lost.
[2362] 05 Feb 17:33:46.677 * Caching the disconnected master state.
[2362] 05 Feb 17:33:46.677 * Discarding previously cached master state.
[2362] 05 Feb 17:33:46.677 * MASTER MODE enabled (user request)
3)方法三:
配置方式:启动时,服务器读取配置文件,并且自动成为指定的从服务器
[root@db ~]# redis-cli -p 6379
127.0.0.1:6379> set ccc 789 //添加主库数据
OK
127.0.0.1:6379> keys *
1) "ccc"
127.0.0.1:6379> get ccc
"789"
127.0.0.1:6379>
[root@db ~]# vim /etc/redis/6381.conf
slaveof 127.0.0.1 6379 //在配置文件里指明主库的IP地址和端口号信息
port 6381
# Generated by CONFIG REWRITE
dir "/etc/redis"
[root@db redis]# redis-server 6381.conf //启动从库
[2405] 05 Feb 17:42:40.279 * Increased maximum number of open files to 10032 (it was originally set to 1024).
[root@db ~]# redis-cli -p 6381
127.0.0.1:6381> keys *
1) "ccc"
127.0.0.1:6381> get ccc
"789" //数据存在,启动时便是从库
127.0.0.1:6381>
//从库状态信息:
[2405] 05 Feb 17:42:40.283 * DB loaded from disk: 0.000 seconds
[2405] 05 Feb 17:42:40.283 * The server is now ready to accept connections on port 6381
[2405] 05 Feb 17:42:41.281 * Connecting to MASTER 127.0.0.1:6379
[2405] 05 Feb 17:42:41.281 * MASTER <-> SLAVE sync started
[2405] 05 Feb 17:42:41.281 * Non blocking connect for SYNC fired the event.
[2405] 05 Feb 17:42:41.282 * Master replied to PING, replication can continue...
[2405] 05 Feb 17:42:41.282 * Partial resynchronization not possible (no cached master)
[2405] 05 Feb 17:42:41.285 * Full resync from master: 9b67e86972cf1a7ba949268aa1553d5f73970bae:2481
[2405] 05 Feb 17:42:41.385 * MASTER <-> SLAVE sync: receiving 28 bytes from master
[2405] 05 Feb 17:42:41.385 * MASTER <-> SLAVE sync: Flushing old data
[2405] 05 Feb 17:42:41.385 * MASTER <-> SLAVE sync: Loading DB in memory
[2405] 05 Feb 17:42:41.386 * MASTER <-> SLAVE sync: Finished with success