欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

Linux下Redis数据库的安装方法与自动启动脚本分享

程序员文章站 2022-06-28 23:32:05
安装redis  (1) 下载redis wget http://redis.googlecode.com/files/redis-2.2.11.ta...

安装redis

 (1) 下载redis

wget http://redis.googlecode.com/files/redis-2.2.11.tar.gz
tar xzvf redis-2.2.11.tar.gz

 (2) 编译并安装redis

make && make install

 (3) 复制并修改配置文件

cp redis.conf /etc/redis.conf
vi /etc/redis.conf

注意修改以下几项:

daemonize yes
loglevel warning
logfile /var/log/redis.log
maxmemory 2gb

(自行斟酌是不要开通virtual memory选项)

 (4) 设置redis-server自动启动服务

上网下载启动脚本(gist.github.com,搜索redis init.d),并简单修改

(至少需要修改redis、redis_conf_file)

vi /etc/init.d/redisctl
chmod 755 redisctl
cp /etc/init.d/redisctl /etc/rc.d/init.d/redisctl \
&& ln -s /etc/init.d/redisctl /etc/rc.d/rc2.d/s90redisctl && ln -s /etc/init.d/redisctl /etc/rc.d/rc3.d/s90redisctl \
&& ln -s /etc/init.d/redisctl /etc/rc.d/rc4.d/s90redisctl && ln -s /etc/init.d/mongod /etc/rc.d/rc5.d/s90redisctl
chkconfig --add redisctl

(5) 重新启动后可以测试redis-server是否正常启动

```redis-cli set foo bar
redis-cli get foo

redis自动启动脚本

```ruby
#!/bin/sh
#
# redis - this script starts and stops the redis-server daemon
#
# chkconfig: 2345 90 10 
# description: redis is a persistent key-value database
# processname: redis-server
# config: /etc/redis.conf
# config: /etc/sysconfig/redis
# pidfile: /var/run/redis.pid

# source function library.
. /etc/rc.d/init.d/functions

# source networking configuration.
. /etc/sysconfig/network

# check that networking is up.
[ "$networking" = "no" ] && exit 0

redis="/usr/local/bin/redis-server"
prog=$(basename $redis)

redis_conf_file="/etc/redis.conf"

[ -f /etc/sysconfig/redis ] && . /etc/sysconfig/redis

lockfile=/var/lock/subsys/redis

start() {
 [ -x $redis ] || exit 5
 [ -f $redis_conf_file ] || exit 6
 echo -n $"starting $prog: "
 daemon $redis $redis_conf_file
 retval=$?
 echo
 [ $retval -eq 0 ] && touch $lockfile
 return $retval
}

stop() {
 echo -n $"stopping $prog: "
 killproc $prog -quit
 retval=$?
 echo
 [ $retval -eq 0 ] && rm -f $lockfile
 return $retval
}

restart() {
 stop
 start
}

reload() {
 echo -n $"reloading $prog: "
 killproc $redis -hup
 retval=$?
 echo
}

force_reload() {
 restart
}

rh_status() {
 status $prog
}

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|configtest)
 $1
 retval=$?
 echo
 [ $retval -eq 0 ] && rm -f $lockfile
 return $retval
}

restart() {
 stop
 start
}

reload() {
 echo -n $"reloading $prog: "
 killproc $redis -hup
 retval=$?
 echo
}

force_reload() {
 restart
}

rh_status() {
 status $prog
}

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|configtest)
 $1
 retval=$?
 echo
 [ $retval -eq 0 ] && rm -f $lockfile
 return $retval
}

restart() {
 stop
 start
}

reload() {
 echo -n $"reloading $prog: "
 killproc $redis -hup
 retval=$?
 echo
}

force_reload() {
 restart
}

rh_status() {
 status $prog
}

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|configtest)
 $1
 ;;
 reload)
 rh_status_q || exit 7
 $1
 ;;
 force-reload)
 force_reload
 ;;
 status)
 rh_status
 ;;
 condrestart|try-restart)
 rh_status_q || exit 0
  ;;
 *)
 echo $"usage: $0 {start|stop|status|restart|condrestart|try-restart|reload|force-reload}"
 exit 2
esac

redis自动启动脚本

记得修改:

(1) —chuid group:user

(2) ln /etc/init.d/redisctl /etc/rc2.d/redisctl,重复rc3,rc4,rc5

启动脚本:

#! /bin/sh
### begin init info
# provides:  redis-server
# required-start: $syslog
# required-stop: $syslog
# should-start:  $local_fs
# should-stop:  $local_fs
# default-start: 2 3 4 5
# default-stop:  0 1 6
# short-description: redis-server - persistent key-value db
# description:  redis-server - persistent key-value db
### end init info

daemon=/usr/local/bin/redis-server
daemon_args=/etc/redis.conf
name=redis-server
desc=redis-server
pidfile=/var/run/redis.pid
test -x $daemon || exit 0
test -x $daemonbootstrap || exit 0
set -e
case "$1" in
 start)
 echo -n "starting $desc: "
 touch $pidfile
 # chown redis:redis $pidfile
 if start-stop-daemon —start —quiet —umask 007 —pidfile $pidfile —chuid redis:redis —exec $daemon — $daemon_args
 then
  echo "$name."
 else
  echo "failed"
 fi
 ;;
 stop)
 echo -n "stopping $desc: "
 if start-stop-daemon —stop —retry 10 —quiet —oknodo —pidfile $pidfile —exec $daemon
 then
  echo "$name."
 else
  echo "failed"
 fi
 rm -f $pidfile
 ;;
 restart|force-reload)
 ${0} stop
 ${0} start
 ;;
 *)
 echo "usage: /etc/init.d/$name {start|stop|restart|force-reload}" >&2
 exit 1
 ;;
esac
exit 0

相关标签: Redis 安装