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

时间:2022-02-15 07:56:16

安装Redis

 (1) 下载Redis

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

 (2) 编译并安装Redis

?
1
make && make install

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

?
1
2
cp redis.conf /etc/redis.conf
vi /etc/redis.conf

注意修改以下几项:

?
1
2
3
4
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)

?
1
2
3
4
5
6
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是否正常启动

?
1
2
```redis-cli set foo Bar
redis-cli get foo

Redis自动启动脚本

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
```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

启动脚本:

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
#! /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