Skip to content

Commit cbb24d3

Browse files
committed
2017-02-26完善redis
1 parent a8200ff commit cbb24d3

File tree

2 files changed

+193
-0
lines changed

2 files changed

+193
-0
lines changed

Redis-Install-And-Settings.md

Lines changed: 107 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@
2525
- 把旧值:`daemonize no`
2626
- 改为新值:`daemonize yes`
2727
- 启动:`/usr/local/bin/redis-server /etc/redis.conf`
28+
- 关闭:`redis-cli -h 127.0.0.1 -p 6379 shutdown`
2829
- 查看是否启动:`ps -ef | grep redis`
2930
- 进入客户端:`redis-cli`
3031
- 关闭客户端:`redis-cli shutdown`
@@ -100,6 +101,12 @@ aof-rewrite-incremental-fsync yes
100101
- 去掉注释,把 `foobared` 改为你想要设置的密码,比如我打算设置为:123456,所以我改为:`requirepass 123456`
101102
- 修改之后重启下服务
102103
- 有了密码之后,进入客户端,就得这样访问:`redis-cli -h 127.0.0.1 -p 6379 -a 123456 `
104+
- 如果用 IP 进入客户端,但是报:`Could not connect to Redis at 192.168.1.121:6379: Connection refused`
105+
- 原因:Redis 默认只允许本机访问,可是有时候我们也需要 Redis 被远程访问。
106+
- 解决办法:
107+
- 修改 Redis 配置文件:`vim /etc/redis.conf`
108+
- 找到 bind 那行配置,默认是:`# bind 127.0.0.1`
109+
- 去掉 # 注释并改为:`bind 0.0.0.0`
103110

104111
## Redis 常用命令
105112

@@ -125,6 +132,106 @@ aof-rewrite-incremental-fsync yes
125132
- `FLUSHALL`,清空所有数据库的所有键值
126133

127134

135+
## 把 redis 添加到系统服务中
136+
137+
- 新建文件:`vim /etc/init.d/redis`
138+
- 添加如下内容:
139+
140+
``` nginx
141+
#!/bin/sh
142+
#
143+
# redis - this script starts and stops the redis-server daemon
144+
#
145+
# chkconfig: - 85 15
146+
# description: Redis is a persistent key-value database
147+
# processname: redis-server
148+
# config: /usr/local/redis-2.4.X/bin/redis-server
149+
# config: /usr/local/ /redis-2.4.X/etc/redis.conf
150+
# Source function library.
151+
. /etc/rc.d/init.d/functions
152+
# Source networking configuration.
153+
. /etc/sysconfig/network
154+
# Check that networking is up.
155+
[ "$NETWORKING" = "no" ] && exit 0
156+
redis="/usr/local/bin/redis-server"
157+
prog=$(basename $redis)
158+
REDIS_CONF_FILE="/etc/redis.conf"
159+
[ -f /etc/sysconfig/redis ] && . /etc/sysconfig/redis
160+
lockfile=/var/lock/subsys/redis
161+
start() {
162+
[ -x $redis ] || exit 5
163+
[ -f $REDIS_CONF_FILE ] || exit 6
164+
echo -n $"Starting $prog: "
165+
daemon $redis $REDIS_CONF_FILE
166+
retval=$?
167+
echo
168+
[ $retval -eq 0 ] && touch $lockfile
169+
return $retval
170+
}
171+
stop() {
172+
echo -n $"Stopping $prog: "
173+
killproc $prog -QUIT
174+
retval=$?
175+
echo
176+
[ $retval -eq 0 ] && rm -f $lockfile
177+
return $retval
178+
}
179+
restart() {
180+
stop
181+
start
182+
}
183+
reload() {
184+
echo -n $"Reloading $prog: "
185+
killproc $redis -HUP
186+
RETVAL=$?
187+
echo
188+
}
189+
force_reload() {
190+
restart
191+
}
192+
rh_status() {
193+
status $prog
194+
}
195+
rh_status_q() {
196+
rh_status >/dev/null 2>&1
197+
}
198+
case "$1" in
199+
start)
200+
rh_status_q && exit 0
201+
$1
202+
;;
203+
stop)
204+
rh_status_q || exit 0
205+
$1
206+
;;
207+
restart|configtest)
208+
$1
209+
;;
210+
reload)
211+
rh_status_q || exit 7
212+
$1
213+
;;
214+
force-reload)
215+
force_reload
216+
;;
217+
status)
218+
rh_status
219+
;;
220+
condrestart|try-restart)
221+
rh_status_q || exit 0
222+
;;
223+
*)
224+
echo $"Usage: $0 {start|stop|status|restart|condrestart|try-restart| reload|orce-reload}"
225+
exit 2
226+
esac
227+
```
228+
229+
- 修改权限:`chmod 755 /etc/init.d/redis`
230+
- 启动服务:`service redis start`
231+
- 停止服务:`service redis stop`
232+
- 重启服务:`service ngredisnx restart`
233+
234+
128235
## Redis 客户端
129236

130237
- Java:<http://redis.io/clients#java>

favorite-file/redis/redis

Lines changed: 86 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,86 @@
1+
#!/bin/sh
2+
#
3+
# redis - this script starts and stops the redis-server daemon
4+
#
5+
# chkconfig: - 85 15
6+
# description: Redis is a persistent key-value database
7+
# processname: redis-server
8+
# config: /usr/local/redis-2.4.X/bin/redis-server
9+
# config: /usr/local/ /redis-2.4.X/etc/redis.conf
10+
# Source function library.
11+
. /etc/rc.d/init.d/functions
12+
# Source networking configuration.
13+
. /etc/sysconfig/network
14+
# Check that networking is up.
15+
[ "$NETWORKING" = "no" ] && exit 0
16+
redis="/usr/local/bin/redis-server"
17+
prog=$(basename $redis)
18+
REDIS_CONF_FILE="/etc/redis.conf"
19+
[ -f /etc/sysconfig/redis ] && . /etc/sysconfig/redis
20+
lockfile=/var/lock/subsys/redis
21+
start() {
22+
[ -x $redis ] || exit 5
23+
[ -f $REDIS_CONF_FILE ] || exit 6
24+
echo -n $"Starting $prog: "
25+
daemon $redis $REDIS_CONF_FILE
26+
retval=$?
27+
echo
28+
[ $retval -eq 0 ] && touch $lockfile
29+
return $retval
30+
}
31+
stop() {
32+
echo -n $"Stopping $prog: "
33+
killproc $prog -QUIT
34+
retval=$?
35+
echo
36+
[ $retval -eq 0 ] && rm -f $lockfile
37+
return $retval
38+
}
39+
restart() {
40+
stop
41+
start
42+
}
43+
reload() {
44+
echo -n $"Reloading $prog: "
45+
killproc $redis -HUP
46+
RETVAL=$?
47+
echo
48+
}
49+
force_reload() {
50+
restart
51+
}
52+
rh_status() {
53+
status $prog
54+
}
55+
rh_status_q() {
56+
rh_status >/dev/null 2>&1
57+
}
58+
case "$1" in
59+
start)
60+
rh_status_q && exit 0
61+
$1
62+
;;
63+
stop)
64+
rh_status_q || exit 0
65+
$1
66+
;;
67+
restart|configtest)
68+
$1
69+
;;
70+
reload)
71+
rh_status_q || exit 7
72+
$1
73+
;;
74+
force-reload)
75+
force_reload
76+
;;
77+
status)
78+
rh_status
79+
;;
80+
condrestart|try-restart)
81+
rh_status_q || exit 0
82+
;;
83+
*)
84+
echo $"Usage: $0 {start|stop|status|restart|condrestart|try-restart| reload|orce-reload}"
85+
exit 2
86+
esac

0 commit comments

Comments
 (0)