1. 程式人生 > >redis數據庫主從復制

redis數據庫主從復制

數據庫 nosql redis

redis數據庫



一、安裝和配置redis

1、安裝redis


[[email protected] ~]# wget http://download.redis.io/releases/redis-3.2.8.tar.gz

[[email protected] ~]# tar -zxvf redis-3.2.8.tar.gz -C /usr/local/src/

[[email protected] ~]# ln -sv /usr/local/src/redis-3.2.8/ /usr/local/redis

[[email protected]

/* */ ~]# cd /usr/local/redis/

[[email protected] redis]# ls

00-RELEASENOTES CONTRIBUTING deps Makefile README.md redis.conf runtest-cluster sentinel.conf tests

BUGS COPYING INSTALL MANIFESTO redis runtest runtest-sentinel src utils

[[email protected]

/* */ redis]# make

[[email protected] redis]# make install


[[email protected] redis]# cp redis.conf /etc


[[email protected] redis]# vim /etc/redis.conf

bind 0.0.0.0 #設置redis監聽的地址

daemonize yes #設置redis以守護進程的方式啟動

dir /redisdb #設置redis緩存數據的目錄

logfile "/redisdb/redis.log"


appendonly yes #開啟AOF持久化日誌

[[email protected] redis]# mkdir /redisdb

2、啟動redis


[[email protected] redis]# redis-server /etc/redis.conf


[[email protected] redis]# ps aux|grep redis

root 29198 0.0 0.3 129500 3596 ? Ssl 20:11 0:00 redis-server 0.0.0.0:6379

root 29202 0.0 0.2 103312 2108 pts/0 S+ 20:12 0:00 grep redis


[[email protected] redis]# netstat -anptul|grep redis

tcp 0 0 0.0.0.0:6379 0.0.0.0:* LISTEN 29198/redis-server

2、停止redis

[[email protected] redis]# redis-cli shutdown


二、主從復制:


主:192.168.1.78 ----------------->從:192.168.1.53


主的配置就沒啥可講的了,配置如上。下面講下從的配置,從的配置也很簡單,只需要在其配置文件中開啟slaveof就行了,例如:

slaveof 192.168.1.78 6379

masterauth flux100plat


[[email protected] ~]# grep -v "^#" /etc/redis.conf |grep -v "^$"

bind 0.0.0.0

protected-mode yes

port 6379

tcp-backlog 511

timeout 0

tcp-keepalive 300

daemonize yes

supervised no

pidfile /var/run/redis_6379.pid

loglevel notice

logfile ""

databases 16

save 900 1

save 300 10

save 60 10000

stop-writes-on-bgsave-error yes

rdbcompression yes

rdbchecksum yes

dbfilename dump.rdb

dir /redisdb

slaveof 192.168.1.78 6379

masterauth flux100plat

slave-serve-stale-data yes

slave-read-only yes

repl-diskless-sync no

repl-diskless-sync-delay 5

repl-disable-tcp-nodelay no

slave-priority 100

appendonly yes

appendfilename "appendonly.aof"

appendfsync everysec

no-appendfsync-on-rewrite no

auto-aof-rewrite-percentage 100

auto-aof-rewrite-min-size 64mb

aof-load-truncated yes

lua-time-limit 5000

slowlog-log-slower-than 10000

slowlog-max-len 128

latency-monitor-threshold 0

notify-keyspace-events ""

hash-max-ziplist-entries 512

hash-max-ziplist-value 64

list-max-ziplist-size -2

list-compress-depth 0

set-max-intset-entries 512

zset-max-ziplist-entries 128

zset-max-ziplist-value 64

hll-sparse-max-bytes 3000

activerehashing yes

client-output-buffer-limit normal 0 0 0

client-output-buffer-limit slave 256mb 64mb 60

client-output-buffer-limit pubsub 32mb 8mb 60

hz 10

aof-rewrite-incremental-fsync yes



然後重啟:


[[email protected] redis]# redis-cli shutdown


[[email protected] redis]# redis-server /etc/redis.conf


查看配置:

[[email protected] ~]# redis-cli

127.0.0.1:6379> INFO replication

# Replication

role:slave

master_host:192.168.1.78

master_port:6379

master_link_status:up

master_last_io_seconds_ago:6

master_sync_in_progress:0

slave_repl_offset:57

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

127.0.0.1:6379> scan 0

1) "128"

2) 1) "1386229"

2) "1529562"

3) "1377317"

4) "1886250"

5) "1381493"

6) "1889670"

7) "1365620"

8) "1377610"

9) "1377173"

10) "1515178"

---------------------可以看到數據都同步過來了


在主上查看:


[[email protected] ~]# redis-cli

127.0.0.1:6379> auth flux100plat

OK

127.0.0.1:6379> info replication

# Replication

role:master

connected_slaves:1

slave0:ip=192.168.1.53,port=6379,state=online,offset=1289,lag=1

master_repl_offset:1289

repl_backlog_active:1

repl_backlog_size:1048576

repl_backlog_first_byte_offset:2

repl_backlog_histlen:1288


在從庫上執行下面的命令:


slaveof 192.168.1.78 3789 masterauth flux100plat

也可以進行同步操作


slaveof no one 中斷復制


本文出自 “linunx運維專題” 博客,請務必保留此出處http://lijianmin2008.blog.51cto.com/621678/1925098

redis數據庫主從復制