24123r2 发表于 2016-12-30 10:02:04

Redis 主从搭建

目录

一、redis主从库相同操作... 2

1.   创建分用户(用于启动redis)... 2

2.   切换到分用户环境... 2

3.   上传redis软件或者wget http://download.redis.io/releases/redis-3.2.6.tar.gz. 2

4.   解压缩redis文件并安装... 2

二、redis主库操作... 2

1.   修改redis的配置文件,将redis的配置文件修改成如下内容:... 2

2.   创建redis所需的日志文件以及存储数据所需的目录... 3

3.   启动redis. 3

三、redis从库操作... 4

1.   修改redis的配置文件,将redis的配置文件修改成如下内容:... 4

2.   创建redis所需的日志文件以及存储数据所需的目录... 6

3.   启动redis. 6

四、redis主从测试... 7

1.   主库写入数据... 7

2.   从库读取数据... 7

3.   主从库查看日志... 8

4.   主从库磁盘备份数据... 8














一、redis主从库相同操作

    1.       创建分用户(用于启动redis)

# useradd redis

    2.       切换到分用户环境

# sudo su – redis

    3.       上传redis软件或者wget http://download.redis.io/releases/redis-3.2.6.tar.gz

$ rz -y

rz waitingto receive.

Startingzmodem transfer.Press Ctrl+C to cancel.

Transferringredis-3.2.6.tar.gz...

100%   1508 KB    1508 KB/sec    00:00:01       0 Errors

$ ls

redis-3.2.6.tar.gz

    4.       解压缩redis文件并安装

$ mkdir run

$ tar zxf redis-3.2.6.tar.gz –C run/

$ cd run/redis-3.2.6/

[redis@Centos6redis-3.2.6]$ make
二、redis主库操作

1.      修改redis的配置文件,将redis的配置文件修改成如下内容:

$ egrep -v "^$|^#" run/redis-3.2.6/redis.conf   

bind 10.0.0.189

protected-modeyes

port 16001

tcp-backlog511

timeout 0

tcp-keepalive300

daemonize yes

supervisedno

pidfile/var/run/redis_6379.pid

loglevelnotice

logfile "/home/redis/log/16001/redis-master.log"

databases16

save 900 1

save 30010

save 6010000

stop-writes-on-bgsave-erroryes

rdbcompressionyes

rdbchecksumyes

dbfilenamedump.rdb

dir /home/redis/data/16001/

slave-serve-stale-datayes

slave-read-onlyyes

repl-diskless-syncno

repl-diskless-sync-delay5

repl-disable-tcp-nodelayno

slave-priority100

appendonlyno

appendfilename"appendonly.aof"

appendfsynceverysec

no-appendfsync-on-rewriteno

auto-aof-rewrite-percentage100

auto-aof-rewrite-min-size64mb

aof-load-truncatedyes

lua-time-limit5000

slowlog-log-slower-than10000

slowlog-max-len128

latency-monitor-threshold0

notify-keyspace-events""

hash-max-ziplist-entries512

hash-max-ziplist-value64

list-max-ziplist-size-2

list-compress-depth0

set-max-intset-entries512

zset-max-ziplist-entries128

zset-max-ziplist-value64

hll-sparse-max-bytes3000

activerehashingyes

client-output-buffer-limitnormal 0 0 0

client-output-buffer-limitslave 256mb 64mb 60

client-output-buffer-limitpubsub 32mb 8mb 60

hz 10

aof-rewrite-incremental-fsyncyes

2.      创建redis所需的日志文件以及存储数据所需的目录

$ mkdir -p data/16001 log/16001

$ ln -s ~/run/redis-3.2.6 ~/run/redis

3.      启动redis

$ ~/run/redis/src/redis-server ~/run/redis/redis.conf

$ netstat-lantup|grep redis

(Not all processes could be identified, non-owned process info

will not be shown, youwould have to be root to see it all.)

tcp   0   0 10.0.0.189:16001    0.0.0.0:*       LISTEN      8981/redis-server 1

$ catlog/16001/redis-master.log

8981:M 24Dec 15:26:44.340 # You requested maxclients of 10000 requiring at least 10032max file descriptors.

8981:M 24Dec 15:26:44.340 # Server can't set maximum open files to 10032 because of OSerror: Operation not permitted.

8981:M 24Dec 15:26:44.340 # Current maximum open files is 4096. maxclients has beenreduced to 4064 to compensate for low ulimit. If you need higher maxclientsincrease 'ulimit -n'.

                _._                                                

         _.-``__ ''-._                                          

      _.-``   `.`_.''-._         Redis 3.2.6 (00000000/0) 64 bit

.-`` .-```. ```\/    _.,_ ''-._                                 

(   '      ,       .-` | `,    )   Running in standalone mode

|`-._`-...-` __...-.``-._|'` _.-'|   Port: 16001

|   `-._   `._    /    _.-'    |   PID: 8981

`-._   `-._`-./_.-'   _.-'                                 

|`-._`-._   `-.__.-'    _.-'_.-'|                                 

|   `-._`-._      _.-'_.-'    |          http://redis.io      

`-._   `-._`-.__.-'_.-'    _.-'                                 

|`-._`-._   `-.__.-'    _.-'_.-'|                                 

|   `-._`-._      _.-'_.-'    |                                 

`-._   `-._`-.__.-'_.-'    _.-'                                 

      `-._   `-.__.-'    _.-'                                    

          `-._      _.-'                                          

            `-.__.-'                                             



8981:M 24Dec 15:26:44.376 # WARNING: The TCP backlog setting of 511 cannot be enforcedbecause /proc/sys/net/core/somaxconn is set to the lower value of 128.

8981:M 24Dec 15:26:44.376 # Server started, Redis version 3.2.6

8981:M 24Dec 15:26:44.377 # WARNING overcommit_memory is set to 0! Background save mayfail under low memory condition. To fix this issue add 'vm.overcommit_memory =1' to /etc/sysctl.conf and then reboot or run the command 'sysctlvm.overcommit_memory=1' for this to take effect.

8981:M 24Dec 15:26:44.377 # WARNING you have Transparent Huge Pages (THP) supportenabled in your kernel. This will create latency and memory usage issues withRedis. To fix this issue run the command 'echo never >/sys/kernel/mm/transparent_hugepage/enabled' as root, and add it to your/etc/rc.local in order to retain the setting after a reboot. Redis must berestarted after THP is disabled.

8981:M 24Dec 15:26:44.377 * Theserver is now ready to accept connections on port 16001
三、redis从库操作

1.      修改redis的配置文件,将redis的配置文件修改成如下内容:

$cp -rf ~/run/redis-3.2.6 ~/run/redis-slave

$ egrep -v "^$|^#" run/redis-slave/redis.conf

bind10.0.0.189

protected-modeyes

port 16002

tcp-backlog511

timeout 0

tcp-keepalive300

daemonizeyes

supervisedno

pidfile/var/run/redis_6379.pid

loglevelnotice

logfile"/home/redis/log/16002/redis-slave.log"

databases16

save 900 1

save 30010

save 6010000

stop-writes-on-bgsave-erroryes

rdbcompressionyes

rdbchecksumyes

dbfilenamedump.rdb

dir/home/redis/data/16002/

slaveof 10.0.0.189 16001

slave-serve-stale-datayes

slave-read-onlyyes

repl-diskless-syncno

repl-diskless-sync-delay5

repl-disable-tcp-nodelayno

slave-priority100

appendonlyno

appendfilename"appendonly.aof"

appendfsynceverysec

no-appendfsync-on-rewriteno

auto-aof-rewrite-percentage100

auto-aof-rewrite-min-size64mb

aof-load-truncatedyes

lua-time-limit5000

slowlog-log-slower-than10000

slowlog-max-len128

latency-monitor-threshold0

notify-keyspace-events""

hash-max-ziplist-entries512

hash-max-ziplist-value64

list-max-ziplist-size-2

list-compress-depth0

set-max-intset-entries512

zset-max-ziplist-entries128

zset-max-ziplist-value64

hll-sparse-max-bytes3000

activerehashingyes

client-output-buffer-limitnormal 0 0 0

client-output-buffer-limitslave 256mb 64mb 60

client-output-buffer-limitpubsub 32mb 8mb 60

hz 10

aof-rewrite-incremental-fsyncyes

2.      创建redis所需的日志文件以及存储数据所需的目录

$ mkdir -p data/16002 log/16002

3.      启动redis

$ ~/run/redis-slave/src/redis-server ~/run/redis-slave/redis.conf

$ netstat -lantup|grep redis

(Not allprocesses could be identified, non-owned process info

will not be shown, you would have to be rootto see it all.)

tcp    0    0 10.0.0.189:16001      0.0.0.0:*      LISTEN   8981/redis-server 1

tcp0    0 10.0.0.189:16002   0.0.0.0:*          LISTEN   8996/redis-server 1

tcp    0   0 10.0.0.189:57884   10.0.0.189:16001   ESTABLISHED 8996/redis-server 1

tcp00 10.0.0.189:16001   10.0.0.189:57884      ESTABLISHED 8981/redis-server 1

$ cat log/16002/redis-slave.log

8990:S 24Dec 15:36:09.108 # You requested maxclients of 10000 requiring at least 10032max file descriptors.

8990:S 24Dec 15:36:09.108 # Server can't set maximum open files to 10032 because of OSerror: Operation not permitted.

8990:S 24Dec 15:36:09.108 # Current maximum open files is 4096. maxclients has beenreduced to 4064 to compensate for low ulimit. If you need higher maxclientsincrease 'ulimit -n'.

                _._                                                

         _.-``__ ''-._                                          

      _.-``   `.`_. ''-._         Redis 3.2.6(00000000/0) 64 bit

.-`` .-```. ```\/    _.,_ ''-._                                 

(   '      ,       .-` | `,    )   Running in standalone mode

|`-._`-...-` __...-.``-._|'` _.-'|   Port: 16002

|    `-._   `._   /   _.-'    |    PID: 8990

`-._   `-._`-./_.-'   _.-'                                 

|`-._`-._   `-.__.-'    _.-'_.-'|                                 

|   `-._`-._      _.-'_.-'    |          http://redis.io      

`-._   `-._`-.__.-'_.-'    _.-'                                 

|`-._`-._   `-.__.-'    _.-'_.-'|                                 

|   `-._`-._      _.-'_.-'    |                                 

`-._   `-._`-.__.-'_.-'    _.-'                                 

      `-._   `-.__.-'    _.-'                                    

          `-._      _.-'                                          

            `-.__.-'                                             



8990:S 24Dec 15:36:09.231 # WARNING: The TCP backlog setting of 511 cannot be enforcedbecause /proc/sys/net/core/somaxconn is set to the lower value of 128.

8990:S 24Dec 15:36:09.231 # Server started, Redis version 3.2.6

8990:S 24Dec 15:36:09.231 # WARNING overcommit_memory is set to 0! Background save mayfail under low memory condition. To fix this issue add 'vm.overcommit_memory =1' to /etc/sysctl.conf and then reboot or run the command 'sysctlvm.overcommit_memory=1' for this to take effect.

8990:S 24Dec 15:36:09.231 # WARNING you have Transparent Huge Pages (THP) supportenabled in your kernel. This will create latency and memory usage issues withRedis. To fix this issue run the command 'echo never > /sys/kernel/mm/transparent_hugepage/enabled'as root, and add it to your /etc/rc.local in order to retain the setting aftera reboot. Redis must be restarted after THP is disabled.

8990:S 24Dec 15:36:09.231 * Theserver is now ready to accept connections on port 16002

8990:S 24Dec 15:36:09.232 * Connectingto MASTER 10.0.0.189:16001

8990:S 24Dec 15:36:09.269 * MASTER<-> SLAVE sync started

8990:S 24 Dec 15:36:09.269 * Non blocking connect for SYNC firedthe event.

8990:S 24 Dec 15:36:09.270 * Master replied to PING, replicationcan continue...

8990:S 24 Dec 15:36:09.270 * Partial resynchronization notpossible (no cached master)

8990:S 24 Dec 15:36:09.588 * Full resync from master:f97b8e62d896862a0941aeb66c199a0d0d31b943:1

8990:S 24 Dec 15:36:09.603 * MASTER <-> SLAVE sync: receiving76 bytes from master

8990:S 24 Dec 15:36:09.603 * MASTER <-> SLAVE sync: Flushingold data

8990:S 24 Dec 15:36:09.603 * MASTER <-> SLAVE sync: LoadingDB in memory

8990:S 24 Dec 15:36:09.603 * MASTER <-> SLAVE sync: Finishedwith success
四、redis主从测试

1.      主库写入数据

$ ~/run/redis/src/redis-cli -h 10.0.0.189 -p 16001

10.0.0.189:16001>set name abc

OK

10.0.0.189:16001>get name

"abc"

10.0.0.189:16001>

2.      从库读取数据

$ ~/run/redis/src/redis-cli -h 10.0.0.189 -p 16002

10.0.0.189:16002>get name

"abc"

10.0.0.189:16002> set age 30

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

10.0.0.189:16002>

3.      主从库查看日志

$ tail log/16001/redis-master.log

8981:M 24Dec 15:36:44.131 * Slave 10.0.0.189:16002 asks for synchronization

8981:M 24Dec 15:36:44.131 * Full resync requested by slave 10.0.0.189:16002

8981:M 24Dec 15:36:44.131 * Starting BGSAVE for SYNC with target: disk

8981:M 24Dec 15:36:44.559 * Background saving started by pid 8999

8999:C 24Dec 15:36:44.616 * DB saved on disk

8999:C 24Dec 15:36:44.616 * RDB: 8 MB of memory used by copy-on-write

8981:M 24Dec 15:36:44.669 * Background saving terminated with success

8981:M 24Dec 15:36:44.669 * Synchronization with slave 10.0.0.189:16002 succeeded

$ tail log/16002/redis-slave.log

8996:S 24Dec 15:36:44.126 * Connecting to MASTER 10.0.0.189:16001

8996:S 24Dec 15:36:44.131 * MASTER <-> SLAVE sync started

8996:S 24Dec 15:36:44.131 * Non blocking connect for SYNC fired the event.

8996:S 24Dec 15:36:44.131 * Master replied to PING, replication can continue...

8996:S 24Dec 15:36:44.131 * Partial resynchronization not possible (no cached master)

8996:S 24Dec 15:36:44.605 * Full resync from master:f97b8e62d896862a0941aeb66c199a0d0d31b943:43

8996:S 24Dec 15:36:44.669 * MASTER <-> SLAVE sync: receiving 76 bytes from master

8996:S 24Dec 15:36:44.669 * MASTER <-> SLAVE sync: Flushing old data

8996:S 24Dec 15:36:44.669 * MASTER <-> SLAVE sync: Loading DB in memory

8996:S 24Dec 15:36:44.670 * MASTER <-> SLAVE sync: Finished with success

4.      主从库磁盘备份数据

$ ll data/16001/

total 4

-rw-rw-r--.1 redis redis 76 Dec 24 15:36 dump.rdb

$ ll data/16002/

total 4

-rw-r--r--.1 redis redis 76 Dec 24 15:36 dump.rdb

$

Redis绑定的IP地址

Redis服务的端口

开启Daemon启动模式

Redis的日志文件

Redis存放DB文件的目录

查看redis的端口是否已经处于监听状态

查看redis的启动日志

指定主库的地址以及端口

从库端口已经处于监听状态,并且已经与主库建立连接

同步日志

无法写入数据,由于是从库



页: [1]
查看完整版本: Redis 主从搭建