q6542125 发表于 2015-11-12 14:30:36

Redis主从搭建


原创文章,转载请注明出处:http://www.huyanping.cn/?p=259

作者:Jenner



1






redis安装方法见:初识Redis——邂逅

安装环境:
# redis-server --version
Redis server v=2.8.7 sha=00000000:0 malloc=jemalloc-3.2.0 bits=32 build=df8b796b6fcf0127
# cat /proc/version
Linux version 2.6.18-92.el5 (mockbuild@builder16.centos.org) (gcc version 4.1.2 20071124 (Red Hat 4.1.2-42)) #1 SMP Tue Jun 10 18:49:47 EDT 2008
1、复制配置文件
#master配置文件
cp redis.conf redis-master.conf
#slave配置文件
cp redis.conf redis-slave.conf
#备份配置文件
cp redis.conf redis-bak.conf
2、修改配置文件
#master配置文件
#保证PID文件区分,PID文件主要用于保证守护进程单例运行
pidfile /var/run/redis-master.pid
#区分LOG文件
logfile "/data/redis/6379/log/redis.log"
#区分持久化文件
dir /data/redis/6379/data
#区分端口
port 6379
#slave配置文件
pidfile /var/run/redis-slave.pid
logfile "/data/redis/6380/log/redis.log"
dir /data/redis/6380/data
port 6380
slaveof 127.0.0.1 6379
#如果为yes,slave实例只读,如果为no,slave实例可读可写。默认
slave-read-only yes
3、启动
redis-server /etc/redis/redis-master.conf
redis-server /etc/redis/redis-slave.conf


4、验证
master启动日志
# cat /data/redis/6379/log/redis.log
13 Sep 10:39:45.143 * Max number of open files set to 10032
13 Sep 10:39:45.145 # Warning: 32 bit instance detected but no memory limit set. Setting 3 GB maxmemory limit with 'noeviction' policy now.
_._
_.-``__ ''-._
_.-``    `.`_.''-._         Redis 2.8.7 (00000000/0) 32 bit
.-`` .-```.```\/    _.,_ ''-._
(    '      ,       .-`| `,    )   Running in stand alone mode
|`-._`-...-` __...-.``-._|'` _.-'|   Port: 6379
|    `-._   `._    /   _.-'    |   PID: 24787
`-._    `-._`-./_.-'    _.-'
|`-._`-._    `-.__.-'    _.-'_.-'|
|    `-._`-._      _.-'_.-'    |         http://redis.io
`-._    `-._`-.__.-'_.-'    _.-'
|`-._`-._    `-.__.-'    _.-'_.-'|
|    `-._`-._      _.-'_.-'    |
`-._    `-._`-.__.-'_.-'    _.-'
`-._    `-.__.-'    _.-'
`-._      _.-'
`-.__.-'
13 Sep 10:39:45.146 # Server started, Redis version 2.8.7
13 Sep 10:39:45.146 # WARNING overcommit_memory is set to 0! Background save may fail under low memory condition. To fix this issue add 'vm.overcommit_memory = 1' to /etc/sysctl.conf and then reboot or run the command 'sysctl vm.overcommit_memory=1' for this to take effect.
13 Sep 10:39:45.146 * The server is now ready to accept connections on port 6379
13 Sep 10:40:54.342 * DB saved on disk
13 Sep 10:42:50.581 * Slave asks for synchronization
13 Sep 10:42:50.581 * Full resync requested by slave.
13 Sep 10:42:50.581 * Starting BGSAVE for SYNC
13 Sep 10:42:50.582 * Background saving started by pid 24816
13 Sep 10:42:50.586 * DB saved on disk
13 Sep 10:42:50.587 * RDB: 0 MB of memory used by copy-on-write
13 Sep 10:42:50.673 * Background saving terminated with success
13 Sep 10:42:50.673 * Synchronization with slave succeeded
13 Sep 10:47:04.093 * DB saved on disk




slave 启动日志
# cat /data/redis/6380/log/redis.log
13 Sep 10:42:50.578 * Max number of open files set to 10032
13 Sep 10:42:50.579 # Warning: 32 bit instance detected but no memory limit set. Setting 3 GB maxmemory limit with 'noeviction' policy now.
_._
_.-``__ ''-._
_.-``    `.`_.''-._         Redis 2.8.7 (00000000/0) 32 bit
.-`` .-```.```\/    _.,_ ''-._
(    '      ,       .-`| `,    )   Running in stand alone mode
|`-._`-...-` __...-.``-._|'` _.-'|   Port: 6380
|    `-._   `._    /   _.-'    |   PID: 24813
`-._    `-._`-./_.-'    _.-'
|`-._`-._    `-.__.-'    _.-'_.-'|
|    `-._`-._      _.-'_.-'    |         http://redis.io
`-._    `-._`-.__.-'_.-'    _.-'
|`-._`-._    `-.__.-'    _.-'_.-'|
|    `-._`-._      _.-'_.-'    |
`-._    `-._`-.__.-'_.-'    _.-'
`-._    `-.__.-'    _.-'
`-._      _.-'
`-.__.-'
13 Sep 10:42:50.580 # Server started, Redis version 2.8.7
13 Sep 10:42:50.580 # WARNING overcommit_memory is set to 0! Background save may fail under low memory condition. To fix this issue add 'vm.overcommit_memory = 1' to /etc/sysctl.conf and then reboot or run the command 'sysctl vm.overcommit_memory=1' for this to take effect.
13 Sep 10:42:50.580 * The server is now ready to accept connections on port 6380
13 Sep 10:42:50.580 * Connecting to MASTER 127.0.0.1:6379
13 Sep 10:42:50.580 * MASTER <-> SLAVE sync started
13 Sep 10:42:50.580 * Non blocking connect for SYNC fired the event.
13 Sep 10:42:50.581 * Master replied to PING, replication can continue...
13 Sep 10:42:50.581 * Partial resynchronization not possible (no cached master)
13 Sep 10:42:50.583 * Full resync from master: 9ef3d846e366f7643db9e9250b508d64a34c1079:1
13 Sep 10:42:50.673 * MASTER <-> SLAVE sync: receiving 31 bytes from master
13 Sep 10:42:50.673 * MASTER <-> SLAVE sync: Flushing old data
13 Sep 10:42:50.673 * MASTER <-> SLAVE sync: Loading DB in memory
13 Sep 10:42:50.674 * MASTER <-> SLAVE sync: Finished with success
13 Sep 10:47:04.096 * DB saved on disk


数据同步验证:
# redis-cli
127.0.0.1:6379> set name test
OK
127.0.0.1:6379> save
OK
# redis-cli -p 6380
127.0.0.1:6380> keys *
1) &quot;name&quot;
         
版权声明:本文为博主原创文章,未经博主允许不得转载。
页: [1]
查看完整版本: Redis主从搭建