本文参考红薯的blog
1.准备
1.1 下载最新版的tcl
目前最新版本为
1.2 下载最新版本的redis
目前最新版本为
2.安装
2.1 编译安装tcl
tar zxvf tcl8.6.0-src.tar.gzcd tcl8.6.0/unix./configure #默认安装make make testmake install
2.2 编译安装redis
tar zxvf redis-2.6.13.tar.gzcd redis-2.6.13makemake test #如果不安装tcl,此步骤会报错cp redis.conf /etc/cd srccp redis-benchmark redis-cli redis-server /usr/bin/
3.配置优化
3.1 内存配置
设置内存分配策略(可选,根据服务器的实际情况进行设置)
/proc/sys/vm/overcommit_memory可选值:0、1、2。0, 表示内核将检查是否有足够的可用内存供应用进程使用;如果有足够的可用内存,内存申请允许;否则,内存申请失败,并把错误返回给应用进程。1, 表示内核允许分配所有的物理内存,而不管当前的内存状态如何。2, 表示内核允许分配超过所有物理内存和交换空间总和的内存一般操作系统默认是0
值得注意的一点是,redis在dump数据的时候,会fork出一个子进程,理论上child进程所占用的内存和parent是一样的,比如parent占用的内存为8G,这个时候也要同样分配8G的内存给child,如果内存无法负担,往往会造成redis服务器的down机或者IO负载过高,效率下降。所以这里比较优化的内存分配策略应该设置为 1(表示内核允许分配所有的物理内存,而不管当前的内存状态如何)
由于是在内网,红薯所说的对iptable的配置,我们就忽略吧,如果需要,请参考红薯的blog
3.2 启动redis
#redis-server /etc/redis.conf
会出现如下输出
_.-``__ ''-._
_.-`` `. `_. ''-._ Redis 2.6.13 (00000000/0) 64 bit .-`` .-```. ```\/ _.,_ ''-._ ( ' , .-` | `, ) Running in stand alone mode |`-._`-...-` __...-.``-._|'` _.-'| Port: 6379 | `-._ `._ / _.-' | PID: 1634 `-._ `-._ `-./ _.-' _.-' |`-._`-._ `-.__.-' _.-'_.-'| | `-._`-._ _.-'_.-' | http://redis.io `-._ `-._`-.__.-'_.-' _.-' |`-._`-._ `-.__.-' _.-'_.-'| | `-._`-._ _.-'_.-' | `-._ `-._`-.__.-'_.-' _.-' `-._ `-.__.-' _.-' `-._ _.-' `-.__.-'[2215] 09 May 15:24:51.164 # Server started, Redis version 2.6.13
[2215] 09 May 15:24:51.164 # WARNING overcommit_memory is set to 0! Background save may fail under low memory condition. To fix this issue add 'vm.overcommit_memory = 1' to /etc/sysctl.conf and then reboot or run the command 'sysctl vm.overcommit_memory=1' for this to take effect. [2215] 09 May 15:24:51.165 * The server is now ready to accept connections on port 6379可以看到,由于没有对内核的内存分配测略做优化,redis会提示warning
我们根据提示操作一下#echo "vm.overcommit_memory = 1" >>/etc/sysctl.conf
#sysctl vm.overcommit_memory=1
再次启动redis,就不会报warning了
[19550] 09 May 15:27:50.684 # Server started, Redis version 2.6.13
[19550] 09 May 15:27:50.684 * The server is now ready to accept connections on port 6379
补充:
redis默认是运行在console状态需要更改配置文件/etc/redis.conf把daemonize no 改成yes,重新启动
3.3 开机自启动
把下面的脚本复制到/etc/init.d/redis-server
#update-rc.d redis-server defaults
脚本
#!/bin/bash### BEGIN INIT INFO # Provides: apache2 # Required-Start: $local_fs $remote_fs $network $syslog # Required-Stop: $local_fs $remote_fs $network $syslog # Default-Start: 2 3 4 5 # Default-Stop: 0 1 6 # X-Interactive: true # Short-Description: Start/stop apache2 web server ### END INIT INFO # # apache2 This init.d script is used to start apache2. # It basically just calls apache2ctl. PATH=/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/binDAEMON=/usr/bin/redis-serverNAME=redis-serverDESC="Redis key-value db" test -x $DAEMON || exit 0set -estartdaemon(){ echo -n "Starting $DESC:" start-stop-daemon --start --verbose --exec $DAEMON /etc/redis.conf echo "$NAME start succ"}stopdaemon(){ echo -n "Stoppping $DESC:" start-stop-daemon --stop --verbose --oknodo --name $NAME echo "$NAME stop succ"}daemonstatus(){ echo -n "Checking $DESC status:" start-stop-daemon --status --verbose --name $NAME case $? in 0) echo "Programming is runnning" exit 0 ;; 1|3) echo "Programming is not running" exit 1 ;; *) echo "Unknow status" exit 2 esac}case "$1" in start) startdaemon ;; stop) stopdaemon ;; restart|force-reload) stopdaemon startdaemon ;; status) daemonstatus ;; *) name=/etc/init.d/$NAME echo "Usage: $name (start|stop|restart|force-reload)" exit 1 ;;esacexit 0
4.简单测试
4.1 redis命令行工具
#redis-cli
redis 127.0.0.1:6379> set name wilelm
OK redis 127.0.0.1:6379> get name "wilelm"
测试ok
4.2 redis关闭
redis 127.0.0.1:6379> shutdown
或者
# redis-cli shutdown
4.3 redis保存数据
redis服务关闭后,缓存数据会自动dump到硬盘上,硬盘地址为redis.conf中的配置项dbfilename dump.rdb所设定
强制备份数据到磁盘,使用如下命令