redis主从同步实战

  • redis主从同步实战已关闭评论
  • 105 views
  • A+
所属分类:redis

Redis 安装环境

CentOS 7
master:172.31.23.28:6666
slave:172.31.23.28:6666

Redis 下载

下载地址:http://download.redis.io/releases/redis-4.0.9.tar.gz

编译安装主从都需要:

$ wget http://download.redis.io/releases/redis-4.0.9.tar.gz 
$ tar xvzf redis-4.0.9.tar.gz -C /usr/local/ 
$ cd /usr/local/redis-4.0.9
 $ make & make test

编译过程报错如下:

(1)

zmalloc.h:50:31: fatal error: jemalloc/jemalloc.h: No such file or directory
 #include <jemalloc/jemalloc.h>
 ^
compilation terminated.
make[1]: *** [adlist.o] Error 1
make[1]: Leaving directory `/usr/local/redis-4.0.9/src'
make[1]: *** [adlist.o] Error 1
make[1]: Leaving directory `/usr/local/redis-4.0.9/src'
make: *** [all] Error 2
make: *** [test] Error 2
[1]+ Exit 2 make

解决方法:

make MALLOC=libc

(2)

[root@ip-172-31-18-226 redis-4.0.9]# make test
cd src && make test
make[1]: Entering directory `/usr/local/redis-4.0.9/src'
 CC Makefile.dep
make[1]: Leaving directory `/usr/local/redis-4.0.9/src'
make[1]: Entering directory `/usr/local/redis-4.0.9/src'
You need tcl 8.5 or newer in order to run the Redis test
make[1]: *** [test] Error 1
make[1]: Leaving directory `/usr/local/redis-4.0.9/src'
make: *** [test] Error 2

解决方法:

yum install -y tcl

主redis配置

daemonize yes
pidfile /data/redis6666/redis.pid
port 6666
unixsocket /tmp/redis6666.sock
unixsocketperm 755
timeout 30
loglevel notice
logfile /data/redis6666/redis.log
databases 16
save ""
stop-writes-on-bgsave-error no 
rdbcompression yes 
rdbchecksum yes 
dbfilename dump6666.rdb 
dir /data/redis6666
#redis40 new
maxmemory-policy noeviction
#redis40 new
maxclients 10000
maxmemory-samples 3
appendonly no
no-appendfsync-on-rewrite yes
slowlog-log-slower-than 1000
slowlog-max-len 1024
hash-max-ziplist-entries 512
hash-max-ziplist-value 64
list-max-ziplist-entries 512
list-max-ziplist-value 64
set-max-intset-entries 512
zset-max-ziplist-entries 128
zset-max-ziplist-value 64
activerehashing yes
client-output-buffer-limit normal 0 0 0
client-output-buffer-limit slave 0 0 0
client-output-buffer-limit pubsub 32mb 8mb 60

requirepass 769rt6f51e3t6cf3
masterauth 769rt6f51e3t6cf3
maxmemory 2G

启动观察

[root@ip-172-31-23-28 redis6666]# /usr/local/redis-4.0.9/src/redis-server /data/redis6666/redis6666.conf 
[root@ip-172-31-23-28 redis6666]# ps -axf|grep redis
22166 pts/0 S+ 0:00 \_ grep --color=auto redis
22161 ? Ssl 0:00 /usr/local/redis-4.0.9/src/redis-server *:6666

从redis配置

从配置比较简单,如下:

daemonize yes
pidfile /data/redis6666/redis.pid
port 6666
unixsocket /tmp/redis6666.sock
unixsocketperm 755
timeout 30
loglevel notice
logfile /data/redis6666/redis.log
databases 16
save ""
stop-writes-on-bgsave-error no 
rdbcompression yes 
rdbchecksum yes 
dbfilename dump6666.rdb 
dir /data/redis6666
#redis40 new
maxmemory-policy noeviction
#redis40 new
maxclients 10000
maxmemory-samples 3
appendonly no
no-appendfsync-on-rewrite yes
slowlog-log-slower-than 1000
slowlog-max-len 1024
hash-max-ziplist-entries 512
hash-max-ziplist-value 64
list-max-ziplist-entries 512
list-max-ziplist-value 64
set-max-intset-entries 512
zset-max-ziplist-entries 128
zset-max-ziplist-value 64
activerehashing yes
client-output-buffer-limit normal 0 0 0
client-output-buffer-limit slave 0 0 0
client-output-buffer-limit pubsub 32mb 8mb 60

requirepass 769rt6f51e3tdfgz
masterauth 769rt6f51e3tdfgz
maxmemory 2G
slaveof 172.31.23.28 6666

注,就增加了一行,然后我们将从库启动起来,看看状态

/usr/local/redis-4.0.9/src/redis-server /data/redis6666/redis6666.conf

测试

登录到主库,如下:

[root@redis6667]# /usr/local/redis-4.0.9/src/redis-cli -h 127.0.0.1 -p 6666
127.0.0.1:6666> auth 769rt6f51e3tdfgz
OK
127.0.0.1:6666> info
# Server
redis_version:4.0.9
redis_git_sha1:00000000
redis_git_dirty:0
redis_build_id:6d8e430d4491cb5b
redis_mode:standalone
os:Linux 3.10.0-327.el7.x86_64 x86_64
arch_bits:64
multiplexing_api:epoll
atomicvar_api:atomic-builtin
gcc_version:4.8.5
process_id:22161
run_id:f784809ffa22d645fd8f8d8608bad73425901d6e
tcp_port:6666
uptime_in_seconds:48087
uptime_in_days:0
hz:10
lru_clock:834292
executable:/usr/local/redis-4.0.9/src/redis-server
config_file:/data/redis6666/redis6666.conf

# Clients
connected_clients:1
client_longest_output_list:0
client_biggest_input_buf:0
blocked_clients:0

# Memory
used_memory:1968764
used_memory_human:1.88M
used_memory_rss:2199552
used_memory_rss_human:2.10M
used_memory_peak:1968764
used_memory_peak_human:1.88M
used_memory_peak_perc:100.05%
used_memory_overhead:1967792
used_memory_startup:852720
used_memory_dataset:972
used_memory_dataset_perc:0.09%
total_system_memory:15332077568
total_system_memory_human:14.28G
used_memory_lua:37888
used_memory_lua_human:37.00K
maxmemory:2000000000
maxmemory_human:1.86G
maxmemory_policy:noeviction
mem_fragmentation_ratio:1.12
mem_allocator:libc
active_defrag_running:0
lazyfree_pending_objects:0

# Persistence
loading:0
rdb_changes_since_last_save:0
rdb_bgsave_in_progress:0
rdb_last_save_time:1527560675
rdb_last_bgsave_status:ok
rdb_last_bgsave_time_sec:0
rdb_current_bgsave_time_sec:-1
rdb_last_cow_size:217088
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
aof_last_cow_size:0

# Stats
total_connections_received:2
total_commands_processed:277
instantaneous_ops_per_sec:1
total_net_input_bytes:9876
total_net_output_bytes:647
instantaneous_input_kbps:0.04
instantaneous_output_kbps:0.00
rejected_connections:0
sync_full:1
sync_partial_ok:0
sync_partial_err:0
expired_keys:0
expired_stale_perc:0.00
expired_time_cap_reached_count:0
evicted_keys:0
keyspace_hits:0
keyspace_misses:0
pubsub_channels:0
pubsub_patterns:0
latest_fork_usec:185
migrate_cached_sockets:0
slave_expires_tracked_keys:0
active_defrag_hits:0
active_defrag_misses:0
active_defrag_key_hits:0
active_defrag_key_misses:0

# Replication
role:master
connected_slaves:1
slave0:ip=172.31.23.28,port=6667,state=online,offset=378,lag=1
master_replid:85b9b60cfecd8e85a863909789bf064461b886a7
master_replid2:0000000000000000000000000000000000000000
master_repl_offset:378
second_repl_offset:-1
repl_backlog_active:1
repl_backlog_size:1048576
repl_backlog_first_byte_offset:1
repl_backlog_histlen:378

# CPU
used_cpu_sys:19.84
used_cpu_user:4.76
used_cpu_sys_children:0.00
used_cpu_user_children:0.00
# Cluster
cluster_enabled:0
# Keyspace

登录到从库,如下:

[root@redis6666]# /usr/local/redis-4.0.9/src/redis-cli -h 127.0.0.1 -p 6666
127.0.0.1:6666> auth 769rt6f51e3tdfgz
OK
127.0.0.1:6666> info
# Server
redis_version:4.0.9
redis_git_sha1:00000000
redis_git_dirty:0
redis_build_id:6d8e430d4491cb5b
redis_mode:standalone
os:Linux 3.10.0-327.el7.x86_64 x86_64
arch_bits:64
multiplexing_api:epoll
atomicvar_api:atomic-builtin
gcc_version:4.8.5
process_id:940
run_id:01ef7b6c6abac31ee21f8d0c231fbaf0598c6e12
tcp_port:6667
uptime_in_seconds:107
uptime_in_days:0
hz:10
lru_clock:834126
executable:/usr/local/redis-4.0.9/src/redis-server
config_file:/data/redis6666/redis6666.conf

# Clients
connected_clients:2
client_longest_output_list:0
client_biggest_input_buf:0
blocked_clients:0

# Memory
used_memory:1969790
used_memory_human:1.88M
used_memory_rss:2191360
used_memory_rss_human:2.09M
used_memory_peak:1969790
used_memory_peak_human:1.88M
used_memory_peak_perc:100.05%
used_memory_overhead:1967814
used_memory_startup:852742
used_memory_dataset:1976
used_memory_dataset_perc:0.18%
total_system_memory:15332077568
total_system_memory_human:14.28G
used_memory_lua:37888
used_memory_lua_human:37.00K
maxmemory:2000000000
maxmemory_human:1.86G
maxmemory_policy:noeviction
mem_fragmentation_ratio:1.11
mem_allocator:libc
active_defrag_running:0
lazyfree_pending_objects:0

# Persistence
loading:0
rdb_changes_since_last_save:0
rdb_bgsave_in_progress:0
rdb_last_save_time:1527560675
rdb_last_bgsave_status:ok
rdb_last_bgsave_time_sec:-1
rdb_current_bgsave_time_sec:-1
rdb_last_cow_size:0
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
aof_last_cow_size:0

# Stats
total_connections_received:2
total_commands_processed:11
instantaneous_ops_per_sec:0
total_net_input_bytes:397
total_net_output_bytes:3803
instantaneous_input_kbps:0.00
instantaneous_output_kbps:0.02
rejected_connections:0
sync_full:0
sync_partial_ok:0
sync_partial_err:0
expired_keys:0
expired_stale_perc:0.00
expired_time_cap_reached_count:0
evicted_keys:0
keyspace_hits:0
keyspace_misses:0
pubsub_channels:0
pubsub_patterns:0
latest_fork_usec:0
migrate_cached_sockets:0
slave_expires_tracked_keys:0
active_defrag_hits:0
active_defrag_misses:0
active_defrag_key_hits:0
active_defrag_key_misses:0

# Replication
role:slave
master_host:172.31.23.28
master_port:6666
master_link_status:up
master_last_io_seconds_ago:9
master_sync_in_progress:0
slave_repl_offset:140
slave_priority:100
slave_read_only:1
connected_slaves:0
master_replid:85b9b60cfecd8e85a863909789bf064461b886a7
master_replid2:0000000000000000000000000000000000000000
master_repl_offset:140
second_repl_offset:-1
repl_backlog_active:1
repl_backlog_size:1048576
repl_backlog_first_byte_offset:1
repl_backlog_histlen:140

# CPU
used_cpu_sys:0.06
used_cpu_user:0.01
used_cpu_sys_children:0.00
used_cpu_user_children:0.00

# Cluster
cluster_enabled:0

# Keyspace

好了,看状态就成了,下来我们再测试一下,

主库:

127.0.0.1:6666> set name zhangsan
OK

从库:

127.0.0.1:6666> get name
"zhangsan"

好了,到此为止

  • 安卓客户端下载
  • 微信扫一扫
  • weinxin
  • 微信公众号
  • 微信公众号扫一扫
  • weinxin
avatar