首页 » redis » 正文

Redis主从复制

前言:

单台redis会遇到单点故障的问题,为了解决redis的高可用,主从复制应该是学习的第二个步骤。本文想达到的目的是,一个master带一个slave,而slave又带了一个slave,这样的好处是,当master故障后,直接把slaver1改为master,其他的配置不需要修改,服务又可以正常使用。

环境:

redis_master:192.168.1.2

redis_slave1:192.168.1.3

redis_slave2:192.168.1.4

redis的版本是:3.0.2

安装:

安装请参考:http://www.lmyw.net.cn/?p=753

主从配置:

作为master的redis配置文件不需要修改,此处只修改slave1和slave12的配置文件,对于slave1只需在redis.conf中找到# slaveof <masterip> <masterport>并修改为 slaveof 192.168.1.2 6379 然后保存即可,而slave12则修改为slaveof 192.168.1.3 6379 ,这样就可以了,启动slave1和slave12试试吧。

在192.168.1.2上执行如下内容:

#redis-cli

127.0.0.1:6379> info

# Server

redis_version:3.0.2

redis_git_sha1:00000000

redis_git_dirty:0

redis_build_id:8b1689a41dd01f6

redis_mode:standalone

os:Linux 2.6.32-71.el6.x86_64 x86_64

arch_bits:64

multiplexing_api:epoll

gcc_version:4.4.4

process_id:61041

run_id:60d2e7dc1f829b02153f2f8ddbafa96b1e0dc2c7

tcp_port:6379

uptime_in_seconds:1865

uptime_in_days:0

hz:10

lru_clock:7942419

config_file:/etc/redis.conf

# Clients

connected_clients:1

client_longest_output_list:0

client_biggest_input_buf:0

blocked_clients:0

# Memory

used_memory:1884424

used_memory_human:1.80M

used_memory_rss:9822208

used_memory_peak:1884424

used_memory_peak_human:1.80M

used_memory_lua:36864

mem_fragmentation_ratio:5.21

mem_allocator:jemalloc-3.6.0

# Persistence

loading:0

rdb_changes_since_last_save:0

rdb_bgsave_in_progress:0

rdb_last_save_time:1434005160

rdb_last_bgsave_status:ok

rdb_last_bgsave_time_sec:0

rdb_current_bgsave_time_sec:-1

aof_enabled:0

aof_rewrite_in_progress:0

aof_rewrite_scheduled:0

aof_last_rewrite_time_sec:-1

aof_current_rewrite_time_sec:-1

aof_last_bgrewrite_status:ok

aof_last_write_status:ok

# Stats

total_connections_received:4

total_commands_processed:627

instantaneous_ops_per_sec:1

total_net_input_bytes:22378

total_net_output_bytes:2861

instantaneous_input_kbps:0.04

instantaneous_output_kbps:0.00

rejected_connections:0

sync_full:2

sync_partial_ok:0

sync_partial_err:0

expired_keys:0

evicted_keys:0

keyspace_hits:0

keyspace_misses:0

pubsub_channels:0

pubsub_patterns:0

latest_fork_usec:925

migrate_cached_sockets:0

# Replication

role:master

connected_slaves:1

slave0:ip=192.168.1.3,port=6379,state=online,offset=897,lag=0

master_repl_offset:897

repl_backlog_active:1

repl_backlog_size:1048576

repl_backlog_first_byte_offset:2

repl_backlog_histlen:896

# CPU

used_cpu_sys:0.05

used_cpu_user:0.00

used_cpu_sys_children:0.01

used_cpu_user_children:0.00

# Cluster

cluster_enabled:0

# Keyspace

在192.168.1.3上执行如下内容:

#redis-cli

127.0.0.1:6379> info

# Server

redis_version:3.0.2

redis_git_sha1:00000000

redis_git_dirty:0

redis_build_id:9eec45ade6d41949

redis_mode:standalone

os:Linux 3.10.0-229.el7.x86_64 x86_64

arch_bits:64

multiplexing_api:epoll

gcc_version:4.8.3

process_id:42723

run_id:25ce087c5eb935e5862968bbe80da9cf524f5a4e

tcp_port:6379

uptime_in_seconds:448

uptime_in_days:0

hz:10

lru_clock:7942248

config_file:/etc/redis.conf

# Clients

connected_clients:2

client_longest_output_list:0

client_biggest_input_buf:0

blocked_clients:0

# Memory

used_memory:1905536

used_memory_human:1.82M

used_memory_rss:9863168

used_memory_peak:1920584

used_memory_peak_human:1.83M

used_memory_lua:36864

mem_fragmentation_ratio:5.18

mem_allocator:jemalloc-3.6.0

# Persistence

loading:0

rdb_changes_since_last_save:0

rdb_bgsave_in_progress:0

rdb_last_save_time:1434005427

rdb_last_bgsave_status:ok

rdb_last_bgsave_time_sec:0

rdb_current_bgsave_time_sec:-1

aof_enabled:0

aof_rewrite_in_progress:0

aof_rewrite_scheduled:0

aof_last_rewrite_time_sec:-1

aof_current_rewrite_time_sec:-1

aof_last_bgrewrite_status:ok

aof_last_write_status:ok

# Stats

total_connections_received:3

total_commands_processed:227

instantaneous_ops_per_sec:1

total_net_input_bytes:7046

total_net_output_bytes:18171

instantaneous_input_kbps:0.04

instantaneous_output_kbps:0.05

rejected_connections:0

sync_full:1

sync_partial_ok:0

sync_partial_err:0

expired_keys:0

evicted_keys:0

keyspace_hits:0

keyspace_misses:0

pubsub_channels:0

pubsub_patterns:0

latest_fork_usec:14680

migrate_cached_sockets:0

# Replication

role:slave

master_host:192.168.1.2

master_port:6379

master_link_status:up

master_last_io_seconds_ago:3

master_sync_in_progress:0

slave_repl_offset:659

slave_priority:100

slave_read_only:1

connected_slaves:1

slave0:ip=192.168.1.4,port=6379,state=online,offset=239,lag=1

master_repl_offset:253

repl_backlog_active:1

repl_backlog_size:1048576

repl_backlog_first_byte_offset:2

repl_backlog_histlen:252

# CPU

used_cpu_sys:0.46

used_cpu_user:0.00

used_cpu_sys_children:0.04

used_cpu_user_children:0.00

# Cluster

cluster_enabled:0

# Keyspace

127.0.0.1:6379>

在192.168.1.4上执行如下内容:

# redis-cli

127.0.0.1:6379> info

# Server

redis_version:3.0.2

redis_git_sha1:00000000

redis_git_dirty:0

redis_build_id:3fa5096d744edc8f

redis_mode:standalone

os:Linux 3.10.0-229.el7.x86_64 x86_64

arch_bits:64

multiplexing_api:epoll

gcc_version:4.8.3

process_id:57833

run_id:c07f131400ef305800b9aa6c2a926c3c50c37640

tcp_port:6379

uptime_in_seconds:10

uptime_in_days:0

hz:10

lru_clock:7942076

config_file:/etc/redis.conf

# Clients

connected_clients:2

client_longest_output_list:0

client_biggest_input_buf:0

blocked_clients:0

# Memory

used_memory:836848

used_memory_human:817.23K

used_memory_rss:7696384

used_memory_peak:836848

used_memory_peak_human:817.23K

used_memory_lua:36864

mem_fragmentation_ratio:9.20

mem_allocator:jemalloc-3.6.0

# Persistence

loading:0

rdb_changes_since_last_save:0

rdb_bgsave_in_progress:0

rdb_last_save_time:1434005426

rdb_last_bgsave_status:ok

rdb_last_bgsave_time_sec:-1

rdb_current_bgsave_time_sec:-1

aof_enabled:0

aof_rewrite_in_progress:0

aof_rewrite_scheduled:0

aof_last_rewrite_time_sec:-1

aof_current_rewrite_time_sec:-1

aof_last_bgrewrite_status:ok

aof_last_write_status:ok

# Stats

total_connections_received:1

total_commands_processed:1

instantaneous_ops_per_sec:0

total_net_input_bytes:46

total_net_output_bytes:307

instantaneous_input_kbps:0.01

instantaneous_output_kbps:0.02

rejected_connections:0

sync_full:0

sync_partial_ok:0

sync_partial_err:0

expired_keys:0

evicted_keys:0

keyspace_hits:0

keyspace_misses:0

pubsub_channels:0

pubsub_patterns:0

latest_fork_usec:0

migrate_cached_sockets:0

# Replication

role:slave

master_host:192.168.1.3

master_port:6379

master_link_status:up

master_last_io_seconds_ago:1

master_sync_in_progress:0

slave_repl_offset:15

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

# CPU

used_cpu_sys:0.15

used_cpu_user:0.00

used_cpu_sys_children:0.00

used_cpu_user_children:0.00

# Cluster

cluster_enabled:0

# Keyspace

127.0.0.1:6379>

此时我们想实现的目标已经实现,但是发现在两个slave上无法插入数据,当master故障后,需要在slave1上首先执行slaveof no one命令,再次执行info Replication时,它已经转为master了,此时slave12不受影响。

模拟环境:

  1. 在192.168.1.2上停止redis。
  2. 在192.168.1.3上做如下操作。

redis-cli

127.0.0.1:6379> slaveof no one

OK

127.0.0.1:6379> info

# Server

redis_version:3.0.2

redis_git_sha1:00000000

redis_git_dirty:0

redis_build_id:9eec45ade6d41949

redis_mode:standalone

os:Linux 3.10.0-229.el7.x86_64 x86_64

arch_bits:64

multiplexing_api:epoll

gcc_version:4.8.3

process_id:42723

run_id:25ce087c5eb935e5862968bbe80da9cf524f5a4e

tcp_port:6379

uptime_in_seconds:978

uptime_in_days:0

hz:10

lru_clock:7942778

config_file:/etc/redis.conf

# Clients

connected_clients:1

client_longest_output_list:0

client_biggest_input_buf:0

blocked_clients:0

# Memory

used_memory:1884840

used_memory_human:1.80M

used_memory_rss:9867264

used_memory_peak:1941456

used_memory_peak_human:1.85M

used_memory_lua:36864

mem_fragmentation_ratio:5.24

mem_allocator:jemalloc-3.6.0

# Persistence

loading:0

rdb_changes_since_last_save:0

rdb_bgsave_in_progress:0

rdb_last_save_time:1434006102

rdb_last_bgsave_status:ok

rdb_last_bgsave_time_sec:0

rdb_current_bgsave_time_sec:-1

aof_enabled:0

aof_rewrite_in_progress:0

aof_rewrite_scheduled:0

aof_last_rewrite_time_sec:-1

aof_current_rewrite_time_sec:-1

aof_last_bgrewrite_status:ok

aof_last_write_status:ok

# Stats

total_connections_received:154

total_commands_processed:1227

instantaneous_ops_per_sec:0

total_net_input_bytes:28958

total_net_output_bytes:52166

instantaneous_input_kbps:0.02

instantaneous_output_kbps:0.00

rejected_connections:0

sync_full:2

sync_partial_ok:0

sync_partial_err:0

expired_keys:0

evicted_keys:0

keyspace_hits:0

keyspace_misses:0

pubsub_channels:0

pubsub_patterns:0

latest_fork_usec:34974

migrate_cached_sockets:0

# Replication

role:master

connected_slaves:1

slave0:ip=192.168.1.4,port=6379,state=online,offset=967,lag=1

master_repl_offset:967

repl_backlog_active:1

repl_backlog_size:1048576

repl_backlog_first_byte_offset:2

repl_backlog_histlen:966

# CPU

used_cpu_sys:1.12

used_cpu_user:0.00

used_cpu_sys_children:0.11

used_cpu_user_children:0.00

# Cluster

cluster_enabled:0

# Keyspace

127.0.0.1:6379>

想要恢复192.168.1.2为主,那么只需要把192.168.1.3重启就可以了。

测试主从同步:

在192.168.1.2上执行的记录:

#redis-cli

127.0.0.1:6379> set pwd ‘111111’   //插入数据

OK

127.0.0.1:6379> get pwd   //查询数据

“111111”

127.0.0.1:6379>

在192.168.1.3上执行的记录:

#redis-cli

127.0.0.1:6379> set pwd ‘123456’  //写入数据,报错,以为是从,所以无法写入数据

(error) READONLY You can’t write against a read only slave.

127.0.0.1:6379> get pwd   //读取数据,能读取出来,并且和从是一致的,说明主从同步成功。

“111111”

127.0.0.1:6379>

在192.168.1.4上执行的记录:

#redis-cli

127.0.0.1:6379> set pwd ‘123456’  //写入数据,报错,以为是从,所以无法写入数据

(error) READONLY You can’t write against a read only slave.

127.0.0.1:6379> get pwd   //读取数据,能读取出来,并且和从是一致的,说明主从同步成功。

“111111”

127.0.0.1:6379>

至此,已经完成了主从复制。

参考资料:

http://blog.csdn.net/ssergsw/article/category/1511231

 

 

发表评论