晚上在家尝试做了一下redis的主从复制。下面的Linux命令有一部分需要sudo才能完成。
首先复制一份redis.conf:
1
$ cp /etc/redis/redis.conf /etc/redis/slave.conf
有几处地方需要修改:
1
2
3
4
5
6
$ vi /etc/redis/slave.conf
port 6380
logfile /var/log/redis/redis-server-slave.log
dbfilename slave.rdb
slaveof 127.0.0.1 6379
设置开机启动:
1
$ cp /etc/init.d/redis-server /etc/init.d/redis-server-slave
修改启动项:
(redis-server-slave) download
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
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
#! /bin/sh
### BEGIN INIT INFO
# Provides: redis-server
# Required-Start: $syslog $remote_fs
# Required-Stop: $syslog $remote_fs
# Should-Start: $local_fs
# Should-Stop: $local_fs
# Default-Start: 2 3 4 5
# Default-Stop: 0 1 6
# Short-Description: redis-server - Persistent key-value db
# Description: redis-server - Persistent key-value db
### END INIT INFO
PATH = /usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin
DAEMON = /usr/bin/redis-server
DAEMON_ARGS = /etc/redis/slave.conf # modified to slave
NAME = redis-server-slave # modified to slave
DESC = redis-server-slave # modified to slave
RUNDIR = /var/run/redis
PIDFILE = $RUNDIR /redis-server-slave.pid # modified to slave
test -x $DAEMON || exit 0
if [ -r /etc/default/$NAME ]
then
. /etc/default/$NAME
fi
set -e
case "$1" in
start)
echo -n "Starting $DESC: "
mkdir -p $RUNDIR
touch $PIDFILE
chown redis:redis $RUNDIR $PIDFILE
chmod 755 $RUNDIR
if [ -n "$ULIMIT" ]
then
ulimit -n $ULIMIT
fi
if start-stop-daemon --start --quiet --umask 007 --pidfile $PIDFILE --chuid redis:redis --exec $DAEMON -- $DAEMON_ARGS
then
echo "$NAME."
else
echo "failed"
fi
;;
stop)
echo -n "Stopping $DESC: "
if start-stop-daemon --stop --retry forever/TERM/1 --quiet --oknodo --pidfile $PIDFILE --exec $DAEMON
then
echo "$NAME."
else
echo "failed"
fi
rm -f $PIDFILE
sleep 1
;;
restart|force-reload)
${ 0 } stop
${ 0 } start
;;
status)
echo -n "$DESC is "
if start-stop-daemon --stop --quiet --signal 0 --name ${ NAME } --pidfile ${ PIDFILE }
then
echo "running"
else
echo "not running"
exit 1
fi
;;
*)
echo "Usage: /etc/init.d/$NAME {start|stop|restart|force-reload}" >&2
exit 1
;;
esac
exit 0
最后执行:
1
$ service redis-server-slave start
最后确认同步是否成功:
1
2
$ cd /var/lib/redis
$ md5sum *.rdb
如果checksum值是相同的,则表示同步成功。
把redis-server-slave设置为开机启动:
1
$ update-rc.d redis-server-slave defaults
如果要取消开机启动:
1
$ update-rc.d -f redis-server-slave remove
配置文件redis.conf里有一部分和save相关的参数,缺省如下:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
# Save the DB on disk:
#
# save <seconds> <changes>
#
# Will save the DB if both the given number of seconds and the given
# number of write operations against the DB occurred.
#
# In the example below the behaviour will be to save:
# after 900 sec (15 min) if at least 1 key changed
# after 300 sec (5 min) if at least 10 keys changed
# after 60 sec if at least 10000 keys changed
save 900 1
save 300 10
save 60 10000
在主服务器上,我们可以去掉上面的设置,改成类似下面的设置(只要参数值够大即可):
1
save 10000000000 10000000000
如此一来主服务器变成一个完全的内存服务器,所有的操作都在内存里完成,“永远”不会再往磁盘上持久化保存数据,异步的也没有。持久化则通过从服务器来完成,这样在操作主服务器的时候效率会更高。不过要注意的一点是此方法不适合保存关键数据,否则一旦主服务器挂掉,如果你头脑一热简单的重启服务,那么从服务器的数据也会跟着消失,此时,必须拷贝一份备份数据到主服务器,然后再重启服务才可以,数据的恢复稍显麻烦。
从服务器也可以通过设置这个参数来调整从内存同步到磁盘的频率。
Have a nice day!