1:redis配置dump.db固化方式,不打开aof,不修改iphosts,参数如下:
# more IP addresses.
#
# Examples:
#
# bind 192.168.1.100 10.0.0.1
# bind 127.0.0.1
#bind 192.168.16.239
# output for logging but daemonize, logs will be sent to /dev/null
logfile "/opt/redis-3.0.2/redis.log"
# like in the following example:
#
# save ""
save 900 1
save 300 10
save 60 10000
# The filename where to dump the DB
dbfilename dump.rdb
# The working directory.
#
# The DB will be written inside this directory, with the filename specified
# above using the 'dbfilename' configuration directive.
#
# The Append Only File will also be created inside this directory.
#
# Note that you must specify a directory here, not a file name.
dir /opt/redis-3.0.2/src/
其他参数默认
2:根据上述参数启动redis,并插入数据。
/opt/redis-3.0.2/src
./redis-server ../redis.conf
vim redis-setkey.sh
#!/bin/bash
n=100000
for (( i=n ;i>=1 ; i-- ))
do
echo $i
redis-cli set liang$i aa
done
执行脚本,并监控后台日志输出:
5763:M 18 Dec 09:48:30.820 * 1 changes in 900 seconds. Saving...
5763:M 18 Dec 09:48:30.821 * Background saving started by pid 5843
5843:C 18 Dec 09:48:30.887 * DB saved on disk
5843:C 18 Dec 09:48:30.888 * RDB: 4 MB of memory used by copy-on-write
5763:M 18 Dec 09:48:30.921 * Background saving terminated with success
5763:M 18 Dec 09:49:31.011 * 10000 changes in 60 seconds. Saving...
5763:M 18 Dec 09:49:31.012 * Background saving started by pid 27088
27088:C 18 Dec 09:49:31.086 * DB saved on disk
27088:C 18 Dec 09:49:31.086 * RDB: 6 MB of memory used by copy-on-write
5763:M 18 Dec 09:49:31.112 * Background saving terminated with success
5763:M 18 Dec 09:50:32.064 * 10000 changes in 60 seconds. Saving...
5763:M 18 Dec 09:50:32.066 * Background saving started by pid 15041
15041:C 18 Dec 09:50:32.163 * DB saved on disk
15041:C 18 Dec 09:50:32.163 * RDB: 6 MB of memory used by copy-on-write
5763:M 18 Dec 09:50:32.167 * Background saving terminated with success
5763:M 18 Dec 09:51:33.048 * 10000 changes in 60 seconds. Saving...
5763:M 18 Dec 09:51:33.050 * Background saving started by pid 2308
2308:C 18 Dec 09:51:33.162 * DB saved on disk
2308:C 18 Dec 09:51:33.162 * RDB: 8 MB of memory used by copy-on-write
5763:M 18 Dec 09:51:33.250 * Background saving terminated with success
5763:M 18 Dec 09:52:34.062 * 10000 changes in 60 seconds. Saving...
5763:M 18 Dec 09:52:34.064 * Background saving started by pid 22023
22023:C 18 Dec 09:52:34.216 * DB saved on disk
22023:C 18 Dec 09:52:34.216 * RDB: 8 MB of memory used by copy-on-write
5763:M 18 Dec 09:52:34.264 * Background saving terminated with success
5763:M 18 Dec 09:53:35.089 * 10000 changes in 60 seconds. Saving...
5763:M 18 Dec 09:53:35.090 * Background saving started by pid 8713
8713:C 18 Dec 09:53:35.237 * DB saved on disk
8713:C 18 Dec 09:53:35.237 * RDB: 4 MB of memory used by copy-on-write
5763:M 18 Dec 09:53:35.291 * Background saving terminated with success
客户端查看数据:
[root@shell src]# redis-cli
127.0.0.1:6379> get liang1
"aa"
127.0.0.1:6379> get liang2
"aa"
127.0.0.1:6379> get liang10000
"aa"
127.0.0.1:6379> get liang100000
"aa"
1)关闭服务端,不修改配置,并重启服务,查看是否做了持久化,查询数据:
5763:M 18 Dec 09:58:52.911 # User requested shutdown...
5763:M 18 Dec 09:58:52.911 * Saving the final RDB snapshot before exiting.
5763:M 18 Dec 09:58:53.050 * DB saved on disk
5763:M 18 Dec 09:58:53.050 * Removing the pid file.
5763:M 18 Dec 09:58:53.050 # Redis is now ready to exit, bye bye...
8732:M 18 Dec 09:59:20.619 * Increased maximum number of open files to 10032 (it was originally set to 1024).
_._
_.-``__ ''-._
_.-`` `. `_. ''-._ Redis 3.0.2 (00000000/0) 64 bit
.-`` .-```. ```/ _.,_ ''-._
( ' , .-` | `, ) Running in standalone mode
|`-._`-...-` __...-.``-._|'` _.-'| Port: 6379
| `-._ `._ / _.-' | PID: 8732
`-._ `-._ `-./ _.-' _.-'
|`-._`-._ `-.__.-' _.-'_.-'|
| `-._`-._ _.-'_.-' | http://redis.io
`-._ `-._`-.__.-'_.-' _.-'
|`-._`-._ `-.__.-' _.-'_.-'|
| `-._`-._ _.-'_.-' |
`-._ `-._`-.__.-'_.-' _.-'
`-._ `-.__.-' _.-'
`-._ _.-'
`-.__.-'
8732:M 18 Dec 09:59:20.621 # Server started, Redis version 3.0.2
8732:M 18 Dec 09:59:20.730 * DB loaded from disk: 0.109 seconds
8732:M 18 Dec 09:59:20.730 * The server is now ready to accept connections on port 6379
[root@shell src]# redis-cli
127.0.0.1:6379> get liang1
"aa"
127.0.0.1:6379> get liang100000
"aa"
2)关闭服务,修改配置:
1:只修改dump.rdb参数路径,直接使用绝对路径:
# Note that you must specify a directory here, not a file name.
dir /opt/redis-3.0.2/src/
[root@shell src]# redis-cli
127.0.0.1:6379> get liang1
"aa"
127.0.0.1:6379> get liang100000
"aa"
发现数据存在。
2:修改dump.rdb参数路径,直接使用绝对路径,并修改HOSTSIP地址:
bind 192.168.16.239
dir /opt/redis-3.0.2/src/
[root@shell src]# ps aux|grep redis
root 5767 0.0 0.0 100928 616 pts/0 S+ 09:32 0:00 tail -fn 200 redis.log
root 8797 0.5 1.1 145620 17676 ? Ssl 10:08 0:00 ./redis-server 192.168.16.239:6379
8797:M 18 Dec 10:08:15.737 * Increased maximum number of open files to 10032 (it was originally set to 1024).
_._
_.-``__ ''-._
_.-`` `. `_. ''-._ Redis 3.0.2 (00000000/0) 64 bit
.-`` .-```. ```/ _.,_ ''-._
( ' , .-` | `, ) Running in standalone mode
|`-._`-...-` __...-.``-._|'` _.-'| Port: 6379
| `-._ `._ / _.-' | PID: 8797
`-._ `-._ `-./ _.-' _.-'
|`-._`-._ `-.__.-' _.-'_.-'|
| `-._`-._ _.-'_.-' | http://redis.io
`-._ `-._`-.__.-'_.-' _.-'
|`-._`-._ `-.__.-' _.-'_.-'|
| `-._`-._ _.-'_.-' |
`-._ `-._`-.__.-'_.-' _.-'
`-._ `-.__.-' _.-'
`-._ _.-'
`-.__.-'
8797:M 18 Dec 10:08:15.739 # Server started, Redis version 3.0.2
8797:M 18 Dec 10:08:15.848 * DB loaded from disk: 0.109 seconds
8797:M 18 Dec 10:08:15.848 * The server is now ready to accept connections on port 6379
[root@shell src]# redis-cli -h 192.168.16.239
192.168.16.239:6379> get liang1
"aa"
192.168.16.239:6379> get liang10000
"aa"
持久化正常,实例恢复正常,根据上述测试也可以证明,将dump.rdb文件拷贝到其他机器,将配置指向此文件路径,也可以恢复实例。
2:在默认配置情况下,测试在不同路径下产生的dump.rdb的路径是否不同:
dir ./
[root@shell src]# pwd
/opt/redis-3.0.2/src
[root@shell src]# ./redis-server ../redis.conf
dump.rdb生成路径为/opt/redis-3.0.2/src
/opt/redis-3.0.2/src/redis-server /opt/redis-3.0.2/redis.conf
当保存在硬盘的时候,路径为/root/dump.rdb
3:为了使dump.rdb生成路径永远不变,保证此类情况下数据不丢失,采用启动脚本的方式:
#!/bin/bash
# Source function library.
. /etc/rc.d/init.d/functions
# Source networking configuration.
. /etc/sysconfig/network
# Check that networking is up.
[ "$NETWORKING" = "no" ] && exit 0
BIN="/usr/bin"
CONFIGFILE="/etc/redis/redis.conf"
PIDFILE="/var/run/redis-server.pid"
LOCKFILE=${LOCKFILE-/var/lock/subsys/redis}
STOP_TIMEOUT=${STOP_TIMEOUT-10}
[ -r "$SYSCONFIG" ] && source "$SYSCONFIG"
RETVAL=0
prog="redis-server"
desc="Redis Server"
start(){
if [ -e $PIDFILE ];then
echo "$desc already running..."
exit 1
fi
echo -n $"Starting $prog: "
daemon $BIN/$prog $CONFIGFILE
RETVAL=$?
echo
[ $RETVAL -eq 0 ] && touch ${LOCKFILE}
return $RETVAL
}
stop(){
#echo -n "----------------------------------------------------------------"
echo -n $"Stopping $prog: "
#echo -n "********************************************************************"
killproc -d ${STOP_TIMEOUT} $prog
RETVAL=$?
echo
[ $RETVAL -eq 0 ] && rm -rf ${LOCKFILE}
return $RETVAL
}
case "$1" in
start)
start
;;
stop)
stop
;;
restart)
stop
start
;;
status)
status $prog
retval=$?
;;
*)
echo $"Usage: $0 {start|stop|status|restart}"
esac
useradd -o -M -s /sbin/nologin redis1 -d /var/lib/redis1 -u 0 -g root
#!/bin/sh
#
# redis init file for starting up the redis daemon
#
# chkconfig: - 20 80
# description: Starts and stops the redis daemon.
# Source function library.
. /etc/rc.d/init.d/functions
name="redis-server"
exec="/opt/redis-3.0.2/src/$name"
pidfile="/var/run/redis/redis.pid"
REDIS_CONFIG="/etc/redis/redis.conf"
[ -e /etc/sysconfig/redis ] && . /etc/sysconfig/redis
lockfile=/var/lock/subsys/redis
start() {
[ -f $REDIS_CONFIG ] || exit 6
[ -x $exec ] || exit 5
echo -n $"Starting $name: "
daemon --user ${REDIS_USER-redis} "$exec $REDIS_CONFIG"
retval=$?
echo
[ $retval -eq 0 ] && touch $lockfile
return $retval
}
stop() {
echo -n $"Stopping $name: "
killproc -p $pidfile $name
retval=$?
echo
[ $retval -eq 0 ] && rm -f $lockfile
return $retval
}
restart() {
stop
start
}
reload() {
false
}
rh_status() {
status -p $pidfile $name
}
rh_status_q() {
rh_status >/dev/null 2>&1
}
case "$1" in
start)
rh_status_q && exit 0
$1
;;
stop)
rh_status_q || exit 0
$1
;;
restart)
$1
;;
reload)
rh_status_q || exit 7
$1
;;
force-reload)
force_reload
;;
status)
rh_status
;;
condrestart|try-restart)
rh_status_q || exit 0
restart
;;
*)
echo $"Usage: $0 {start|stop|status|restart|condrestart|try-restart}"
exit 2
esac
exit $?