Redis主从配置详细过程

NoSQL

Redis主从架构主要是写服务由Redis主服务器来完成,Redis从服务器可以为客户端提供只读操作的服务,可以分载Redis主服务器的读操作压力,系统的伸缩性还是得到了很大的提高。Redis主服务器可以将数据保存操作交给Redis从服务器完成,从而避免了在Redis主服务器中要有独立的进程来完成此操作。下面PHP程序员雷雪松记录了Linux下Redis主从配置的详细过程。

创建Redis相关目录

1
2
[root@Redis ~]# mkdir -p /usr/local/redis/{etc,bin,var}
[root@Redis ~]# mkdir -p /redisDb/logs/ /redisDb/data

编译安装Redis

1
2
3
4
5
6
[root@Redis ~]# tar -xvzf redis-2.4.13.tar.gz
[root@Redis ~]# cd redis-2.4.13
[root@Redis ~]# make
[root@Redis ~]# cd src && cp redis-server redis-cli redis-benchmark /usr/local/redis/bin
[root@Redis ~]# echo "vm.overcommit_memory=1" >> /etc/sysctl.conf
[root@Redis ~]# /sbin/sysctl -p

Redis主服务器设置【192.168.1.112】

1
[root@Redis ~]# vim /usr/local/redis/etc/redis.conf
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
45
46
47
48
49
50
51
52
daemonize yes
pidfile /usr/local/redis/var/redis.pid
port 6379
bind 192.168.1.112
unixsocket /usr/local/redis/var/redis.sock
unixsocketperm 755
timeout 300
loglevel verbose
logfile /redisDb/logs/redis.log
# syslog-enabled no
# syslog-ident redis
# syslog-facility local0
databases 16
save 900 1
save 300 10
save 60 10000
rdbcompression yes
dbfilename dump.rdb
dir /data/dbcache
# slaveof <masterip> <masterport>
# masterauth <master-password>
# repl-ping-slave-period 10
# repl-timeout 60
requirepass redis123
# rename-command CONFIG ""
maxclients 0
# maxmemory <bytes>
# maxmemory-policy volatile-lru
# maxmemory-samples 3
appendonly no
appendfilename appendonly.aof
appendfsync always
no-appendfsync-on-rewrite no
auto-aof-rewrite-percentage 100
auto-aof-rewrite-min-size 64mb
slowlog-log-slower-than 10000
slowlog-max-len 128
vm-enabled no
vm-swap-file /redisDb/data/redis.swap
vm-max-memory 0
vm-page-size 32
vm-pages 134217728
vm-max-threads 4
glueoutputbuf yes
hash-max-zipmap-entries 512
hash-max-zipmap-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

Redis从服务器设置【192.168.1.118】

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
45
46
47
48
49
50
51
52
53
54
daemonize yes
pidfile /usr/local/redis/var/redis.pid
port 6379
bind 192.168.1.118
unixsocket /usr/local/redis/var/redis.sock
unixsocketperm 755
timeout 300
loglevel verbose
logfile /redisDb/log/redis.log
# syslog-enabled no
# syslog-ident redis
# syslog-facility local0
databases 16
save 900 1
save 300 10
save 60 10000
rdbcompression yes
dbfilename dump.rdb
dir /data/dbcache
# slaveof <masterip> <masterport>
# masterauth <master-password>
# repl-ping-slave-period 10
# repl-timeout 60
# rename-command CONFIG ""
maxclients 0
# maxmemory <bytes>
# maxmemory-policy volatile-lru
# maxmemory-samples 3
appendonly no
appendfilename appendonly.aof
appendfsync always
no-appendfsync-on-rewrite no
auto-aof-rewrite-percentage 100
auto-aof-rewrite-min-size 64mb
slowlog-log-slower-than 10000
slowlog-max-len 128
vm-enabled no
vm-swap-file redisDb/data/redis.swap
vm-max-memory 0
vm-page-size 32
vm-pages 134217728
vm-max-threads 4
glueoutputbuf yes
hash-max-zipmap-entries 512
hash-max-zipmap-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
slave-serve-stale-data yes
slaveof 192.168.1.112 6379
masterauth redis123

启动Redis服务:

1
[root@Redis ~]# /usr/local/redis/bin/redis-server /usr/local/redis/etc/redis.conf

Redis主服务器上执行:

1
[root@Redis ~]# /usr/local/redis/bin/redis-cli -h 192.168.1.112 -a redis123 set test 123456

Redis从服务器上执行:

1
[root@Redis ~]# /usr/local/redis/bin/redis-cli -h 192.168.1.118 get test

测试Redis是否已启动

1
[root@Redis ~]# /usr/local/redis/bin/redis-cli ping

性能Redis测试

1
[root@Redis ~]# /usr/local/redis/bin/redis-benchmark

关闭Redis服务

1
[root@Redis ~]# /usr/local/redis/bin/redis-cli -p 6379 shutdown

强制刷新数据到磁盘【Redis默认是异步写入磁盘的】

1
[root@Redis ~]# /usr/local/redis/bin/redis-cli -p 6379 save

来源:Redis主从配置详细过程

发表评论

电子邮件地址不会被公开。 必填项已用*标注