配置环境:
Master服务器 : 192.168.189.129:44444
Slave服务器 : 192.168.189.131:44444
一:下载安装redis软件包,并解压
1
2
3
4
|
# cd /usr/local/src
# wget http://redis.googlecode.com/files/redis-2.6.4.tar.gz
# tar -zxvf redis-2.6.4.tar.gz
# cd redis-2.6.4
|
二:安装
redis软件不依赖其他的库,也不需要额外的编译参数,可直接make install进行安装
1
|
# make install
|
三:创建所需目录与文件,便于管理
1
2
3
4
5
6
|
# mkdir -p /usr/local/redis/bin
# mkdir -p /usr/local/redis/etc
# mkdir -p /usr/local/redis/var
# mkdir -p /usr/local/redis/data
# cp redis.conf /usr/local/redis/etc/
# cp /usr/local/bin/redis-* /usr/local/redis/bin
|
四:配置redis.conf文件如下
Master服务器端:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
|
# grep -v '^#' redis.conf |grep -v '^$'
daemonize
yes
pidfile
/usr/local/redis/var/redis
.pid
#定义端口
port 44444
#绑定IP
bind 192.168.189.129
timeout 300
loglevel notice
#指定日志目录
logfile
/usr/local/redis/var/redis
.log
databases 16
#配置持久化(rdb模式)
save 900 1
save 300 10
save 60 10000
stop-writes-on-bgsave-error
yes
rdbcompression
yes
rdbchecksum
yes
#定义数据文件和目录
dbfilename master.rdb
dir
/usr/local/redis/data
slave-serve-stale-data
yes
slave-
read
-only
yes
slave-priority 100
appendonly no
appendfsync everysec
no-appendfsync-on-rewrite no
auto-aof-rewrite-percentage 100
auto-aof-rewrite-min-size 64mb
lua-
time
-limit 5000
slowlog-log-slower-than 10000
slowlog-max-len 128
hash
-max-ziplist-entries 512
hash
-max-ziplist-value 64
list-max-ziplist-entries 512
list-max-ziplist-value 64
set
-max-intset-entries 512
zset-max-ziplist-entries 128
zset-max-ziplist-value 64
activerehashing
yes
client-output-buffer-limit normal 0 0 0
client-output-buffer-limit slave 256mb 64mb60
client-output-buffer-limit pubsub 32mb 8mb 60
|
Slave服务器端:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
|
# grep -v '^#' redis.conf |grep -v '^$'
daemonize
yes
pidfile
/usr/local/redis/var/redis
.pid
port 44444
bind 192.168.189.131
timeout 300
loglevel notice
logfile
/usr/local/redis/var/redis
.log
databases 16
save 900 1
save 300 10
save 60 10000
stop-writes-on-bgsave-error
yes
rdbcompression
yes
rdbchecksum
yes
dbfilename slave.rdb
dir
/usr/local/redis/data
slaveof 192.168.189.129 44444 ------指定master的地址
slave-serve-stale-data
yes
slave-
read
-only
yes
slave-priority 100
appendonly no
appendfsync everysec
no-appendfsync-on-rewrite no
auto-aof-rewrite-percentage 100
auto-aof-rewrite-min-size 64mb
lua-
time
-limit 5000
slowlog-log-slower-than 10000
slowlog-max-len 128
hash
-max-ziplist-entries 512
hash
-max-ziplist-value 64
list-max-ziplist-entries 512
list-max-ziplist-value 64
set
-max-intset-entries 512
zset-max-ziplist-entries 128
zset-max-ziplist-value 64
activerehashing
yes
client-output-buffer-limit normal 0 0 0
client-output-buffer-limit slave 256mb 64mb60
client-output-buffer-limit pubsub 32mb 8mb 60
|
五:依次开启主从redis,并测试
1
2
3
4
|
Master:
# /usr/local/redis/bin/redis-server /usr/local/redis/etc/redis.conf
Slave:
# /usr/local/redis/bin/redis-server /usr/local/redis/etc/redis.conf
|
使用客户端连接主redis,并进行一些简单操作
# /usr/local/redis/bin/redis-cli -h 192.168.189.129 -p 44444
连接从redis,查看数据是否同步
# /usr/local/redis/bin/redis-cli -h 192.168.189.131 -p 44444
从上可看到数据已同步,证明redis主从配置生效
注:由于在配置文件中配置了slave-read-only yes,因此从节点处于只读状态,无法对数据库进行操作和更改。
本文转自 icenycmh 51CTO博客,原文链接:http://blog.51cto.com/icenycmh/1792017,如需转载请自行联系原作者