[root@redis ~]# sysctl -p
#############kerbel####################
[root@redis ~]# echo never > /sys/kernel/mm/transparent_hugepage/enabled
?
重新启动Redis服务器
#######将缓存保存到硬盘上#####
[root@redis ~]# redis-cli -h 10.168.85.25 -p 6379 BGSAVE
Background saving started
#######关闭Redis#############
[root@redis ~]# redis-cli -h 10.168.85.25 -p 6379 SHUTDOWN
########启动Redis############
[root@redis ~]# redis-server /etc/redis.conf
编辑Redis启动脚本
[root@redis ~]# vi /etc/init.d/redis
#!/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="/usr/local/bin/$name"? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? # 指定redis-server命令的位置(whereis redis-server)
pidfile="/var/run/redis/redis.pid"? ? ? ? ? ? ? ? ? ? ? ? ? ? # 指定redis的pid文件路径(和配置文件里保持一致)
REDIS_CONFIG="/etc/redis.conf"? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? # 指定redis的配置文件路径
[ -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 $?
[root@redis ~]# chmod 700 /etc/init.d/redis
[root@redis ~]# servcie redis restart
附加信息
Redis无法编译安装报错处理?
###########make时错误信息#########
[root@redis redis-2.8.19]# make
cd src && make all
make[1]: Entering directory `/root/redis-2.8.19/src'
? ? CC adlist.o
In file included from adlist.c:34:
zmalloc.h:50:31: error: jemalloc/jemalloc.h: No such file or directory
zmalloc.h:55:2: error: #error "Newer version of jemalloc required"
make[1]: *** [adlist.o] Error 1
make[1]: Leaving directory `/root/redis-2.8.19/src'
make: *** [all] Error 2
############解决方法#############
make MALLOC=libc
vm.overcommit_memory参数解析
如果内存情况比较紧张的话,需要设定内核参数overcommit_memory,指定内核针对内存分配的策略,其值可以是0、1、2。
? ? ? 0,表示内核将检查是否有足够的可用内存供应用进程使用;如果有足够的可用内存,内存申请允许;否则,内存申请失败,并把错误返回给应用进程。
? ? ? 1,表示内核允许分配所有的物理内存,而不管当前的内存状态如何。
? ? ? 2,表示内核允许分配超过所有物理内存和交换空间总和的内存
Redis在dump数据的时候,会fork出一个子进程,理论上child进程所占用的内存和parent是一样的,比如parent占用的内存为 8G,这个时候也要同样分配8G的内存给child, 如果内存无法负担,往往会造成redis服务器的down机或者IO负载过高,效率下降。所以这里比较优化的内存分配策略应该设置为 1(表示内核允许分配所有的物理内存,而不管当前的内存状态如何)。
设置方式有两种,需确定当前用户的权限活使用root用户修改:
? ? 1:重设文件 echo 1 > /proc/sys/vm/overcommit_memory(默认为0)
? ? 2: echo "vm.overcommit_memory=1" >> /etc/sysctl.conf? && sysctl -p