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的路径是否不同:
原文:http://www.cnblogs.com/liangsky/p/5056583.html