应用示例:
这里我们假设Master-Slave已经建立。
#启动master服务器。
[[email protected] redis]# redis-cli -p 6379
redis 127.0.0.1:6379>
#情况Master当前数据库中的所有Keys。
redis 127.0.0.1:6379> flushdb
OK
#在Master中创建新的Keys作为测试数据。
redis 127.0.0.1:6379> set mykey hello
OK
redis 127.0.0.1:6379> set mykey2 world
OK
#查看Master中存在哪些Keys。
redis 127.0.0.1:6379> keys *
1) "mykey"
2) "mykey2"
#启动slave服务器。
[[email protected] redis]# redis-cli -p 6380
#查看Slave中的Keys是否和Master中一致,从结果看,他们是相等的。
redis 127.0.0.1:6380> keys *
1) "mykey"
2) "mykey2"
#在Master中删除其中一个测试Key,并查看删除后的结果。
redis 127.0.0.1:6379> del mykey2
(integer) 1
redis 127.0.0.1:6379> keys *
1) "mykey"
#在Slave中查看是否mykey2也已经在Slave中被删除。
redis 127.0.0.1:6380> keys *
1) "mykey"
redis主从复制配置:
环境:Linux一台(192.168.6.205)、redis端口分别为6379、6370
安装好redis之后我的测试目录是这样的:
1
2
3
4
|
[[email protected] redis] # pwd
/usr/local/redis
[[email protected] redis] # ls
master master-redis-2.8.1 slave slave-redis-2.8.1
|
master、slave是我新建的一个目录,下面分别是bin、ect目录用来存放各自的redis命令和配置文件
1
2
3
4
|
[[email protected] bin] # pwd
/usr/local/redis/master/bin
[[email protected] bin] # ls
dump.rdb mkreleasehdr.sh redis-benchmark redis-check-aof redis-check-dump redis-cli redis-sentinel redis-server
|
1
2
3
4
|
[[email protected] etc] # pwd
/usr/local/redis/master/etc
[[email protected] etc] # ls
master-redis.conf
|
1
2
3
4
|
[[email protected] bin] # pwd
/usr/local/redis/slave/bin
[[email protected] bin] # ls
dump.rdb mkreleasehdr.sh redis-benchmark redis-check-aof redis-check-dump redis-cli redis-sentinel redis-server
|
1
2
3
4
|
[[email protected] etc] # pwd
/usr/local/redis/slave/etc
[[email protected] etc] # ls
slave-redis.conf
|
/usr/local/bin:
1
2
3
4
|
[[email protected] redis] # pwd
/usr/local/bin/redis
[[email protected] redis] # ls
master slave
|
配置:主服务没动,只在slave的配置文件中修改# slaveof <masterip> <masterport>为master所在的ip 和端口,如:
1
2
3
4
|
[[email protected] master] # cd /usr/local/redis/slave/etc/
[[email protected] etc] # vi slave-redis.conf
# slaveof <masterip> <masterport>
slaveof 192.168.6.205 6379
|
启动master:
1
2
3
4
|
[[email protected] etc] # ps -ef |grep redis
root 6830 5985 0 13:54 pts /2 00:00:00 grep redis
[[email protected] bin] # cd /usr/local/redis/master/bin/
[[email protected] bin] # ./redis-server /usr/local/redis/master/etc/master-redis.conf
|
连接master:
1
2
3
4
5
6
|
[[email protected] bin] # ./redis-cli
127.0.0.1:6379> keys *
1) "aaaaaaaaaaa"
2) "security"
3) "mm"
4) "a"
|
查看master连接信息:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
|
127.0.0.1:6379> info
...
config_file: /usr/local/redis/master/etc/master-redis .conf
# Clients
connected_clients:1
client_longest_output_list:0
client_biggest_input_buf:0
blocked_clients:0
# Replication
role:master
connected_slaves:0
master_repl_offset:0
repl_backlog_active:0
repl_backlog_size:1048576
repl_backlog_first_byte_offset:0
repl_backlog_histlen:0
|
此时可以看到该redis role为master
启动slave:
1
2
|
[[email protected] bin] # cd /usr/local/redis/slave/bin/
[[email protected] bin] # ./redis-server /usr/local/redis/slave/etc/slave-redis.conf
|
连接slave:
1
2
3
4
5
6
|
[[email protected] bin] # ./redis-cli -p 6370
127.0.0.1:6370> keys *
1) "mm"
2) "a"
3) "security"
4) "aaaaaaaaaaa"
|
可以看到该连接信息为:127.0.0.1:6370>,master的数据已经被同步过来了。
./redis-cli 该客户端默认连接的端口号为6379,可以通过-p指定端口,-h指定主机。
查看进程可以看到已经有两个redis服务在运行
1
2
3
4
|
[[email protected] bin] # ps -ef |grep redis
root 6835 1 0 13:56 ? 00:00:00 . /redis-server *:6379
root 6866 1 0 14:01 ? 00:00:00 . /redis-server *:6370
root 6895 4071 0 14:05 pts /1 00:00:00 grep redis
|
查看slave连接信息:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
|
127.0.0.1:6370> info
...
config_file: /usr/local/redis/slave/etc/slave-redis .conf
...
# Replication
role:slave
master_host:192.168.6.205
master_port:6379
master_link_status:up
master_last_io_seconds_ago:3
master_sync_in_progress:0
slave_repl_offset:463
slave_priority:100
slave_read_only:1
connected_slaves:0
master_repl_offset:0
repl_backlog_active:0
repl_backlog_size:1048576
repl_backlog_first_byte_offset:0
repl_backlog_histlen:0
|
可以看到redis role为slave, master_port为6379,master_host:192.168.6.205,master_link_status:up
时间: 2024-10-20 03:04:34