首页 > 代码库 > redis数据库主从复制

redis数据库主从复制

redis数据库



一、安装和配置redis

    

  1、安装redis


[root@localhost ~]# wget http://download.redis.io/releases/redis-3.2.8.tar.gz

    [root@localhost ~]# tar -zxvf redis-3.2.8.tar.gz -C /usr/local/src/

    [root@localhost ~]# ln -sv /usr/local/src/redis-3.2.8/ /usr/local/redis

    [root@localhost ~]# cd /usr/local/redis/

[root@localhost 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

    

    [root@localhost redis]# make

    [root@localhost redis]# make install


    [root@localhost redis]# cp redis.conf /etc


    [root@localhost redis]# vim /etc/redis.conf

    bind 0.0.0.0             #设置redis监听的地址

    daemonize yes            #设置redis以守护进程的方式启动

    dir /redisdb             #设置redis缓存数据的目录

    

    logfile "/redisdb/redis.log"


    appendonly yes           #开启AOF持久化日志

    [root@localhost redis]# mkdir /redisdb

    

  2、启动redis


    [root@localhost redis]# redis-server /etc/redis.conf


    [root@localhost 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


[root@localhost redis]# netstat -anptul|grep redis

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

    

  2、停止redis

    

    [root@localhost redis]# redis-cli shutdown


二、主从复制:


         主:192.168.1.78   ----------------->从:192.168.1.53


         主的配置就没啥可讲的了,配置如上。下面讲下从的配置,从的配置也很简单,只需要在其配置文件中开启slaveof就行了,例如:

         

         slaveof 192.168.1.78 6379

masterauth flux100plat


         [root@localhost ~]# 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



         然后重启:


           [root@localhost redis]# redis-cli shutdown


           [root@localhost redis]# redis-server /etc/redis.conf


        查看配置:

           [root@localhost ~]# 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"

          

          ---------------------可以看到数据都同步过来了


          在主上查看:


          [root@localhost ~]# 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数据库主从复制