redis資料庫主從複製

來源:互聯網
上載者:User

標籤:資料庫   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資料庫主從複製

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.