安装redis
shell> tar -zxvf redis-2.8.13.tar.gz -C /db/redis/
shell> cd /db/redis/redis-2.8.13/
shell> make
shell> cp redis.conf /etc/ #redis启动的配置文件
shell> cd src
shell> cp redis-benchmark redis-cli redis-server /usr/bin/ #redis常用命令
启动redis服务
shell> redis-server /etc/redis.conf
[4757] 21 Oct 01:10:28.680 * Increased maximum number of open files to 10032 (it was originally set to 1024).
_._
_.-``__ ''-._
_.-`` `. `_. ''-._ Redis 2.8.13 (00000000/0) 32 bit
.-`` .-```. ```\/ _.,_ ''-._
( ' , .-` | `, ) Running in stand alone mode
|`-._`-...-` __...-.``-._|'` _.-'| Port: 6379
| `-._ `._ / _.-' | PID: 4757
`-._ `-._ `-./ _.-' _.-'
|`-._`-._ `-.__.-' _.-'_.-'|
| `-._`-._ _.-'_.-' | http://redis.io
`-._ `-._`-.__.-'_.-' _.-'
|`-._`-._ `-.__.-' _.-'_.-'|
| `-._`-._ _.-'_.-' |
`-._ `-._`-.__.-'_.-' _.-'
`-._ `-.__.-' _.-'
`-._ _.-'
`-.__.-'
[4757] 21 Oct 01:10:28.699 # Server started, Redis version 2.8.13
[4757] 21 Oct 01:10:28.701 * The server is now ready to accept connections on port 6379
查看进程,确认redis已经启动
shell> ps -ef | grep redis |grep -v grep
root 4757 1804 0 01:10 pts/0 00:00:00 redis-server *:6379
将配置文件中的daemonize修改为yes,将redis设置为后台守护进程
同时最好指定logfile,否则不会有日志信息打印
设置内存分配策略
shell> vi /etc/sysctl.conf 增加vm.overcommit_memory = 1 并重启系统
或执行
shell> sysctl vm.overcommit_memory=1
vm.overcommit_memory = 1
0: 表示内核将检查是否有足够的可用内存供应用进程使用;如果有足够的可用内存,内存申请允许;否则内存申请失败,并把错误返回给应用进程。
1: 表示内核允许分配所有的物理内存,而不管当前的内存状态如何。
2: 表示内核允许分配超过所有物理内存和交换空间总和的内存
redis在dump数据的时候,会fork出一个子进程,理论上child进程所占用的内存和parent是一样的,比如parent占用的内存为8G,这时也要同样分配8G的内存给child,如果内存无法负担,往往会造成redis服务器的down机或者IO负载过高效率下降。所以这里比较优化的内存分配策略应该设置为 1(表示内核允许分配所有的物理内存,而不管当前的内存状态如何)
测试redis
shell> redis-cli
127.0.0.1:6379> get name
"lisi"
127.0.0.1:6379> set name "zhangsan"
OK
127.0.0.1:6379> get name
"zhangsan"
关闭redis服务
shell> redis-cli shutdown #redis服务关闭后,缓存数据会自动dump到硬盘上,文件由redis.conf中的配置项dbfilename dump.rdb所设定
shell> redis-cli save 或者 redis-cli -p 6380 save(指定端口) #强制备份数据到磁盘
参考:
http://www.oschina.net/question/12_18065
整理自网络
Svoid
2014-10-20