Redis使用问题整理

源码安装reids

  1. 在redis官方下载自己需要的redis源码
  2. 连接服务器通过wget下载到/usr/local/src/目录下
1
2
[root@local ~]# cd /usr/local/src/
[root@local src]# wget http://download.redis.io/releases/redis-5.0.7.tar.gz
  1. 解压
1
[root@lcoal src]# tar -zxvf redis-5.0.7.tar.gz
  1. 编译安装
1
2
3
4
5
6
7
8
9
10
11
12
[root@lcoal src]# cd redis-5.0.7
[root@lcoal redis-5.0.7]# make
// 如果出现‘make[3]: gcc:命令未找到'则还需要安装gcc,重新make'
// 安装到指定的目录
[root@lcoal redis-5.0.7]# make install PREFIX=/usr/local/redis
[root@lcoal redis-5.0.7]# mkdir -p /usr/local/redis/etc
// 把配置文件复制到安装目录
[root@lcoal redis-5.0.7]# cp redis.conf /usr/local/redis/etc
[root@lcoal redis-5.0.7]# cd /usr/local/redis
// 启动redis
[root@lcoal redis]# ./bin/redis-server
//redis 启动会占据控制台,编辑etc目录下的redis.cnf文件将daemonize no改为daemonize yes,即可把redis启动放在后台启动

Redis配置开机启动

Redis配置开机启动脚本

1.vim /etc/init.d/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
#!/bin/bash

# chkconfig: 2345 90 10
# description: Redis is a persistent key-value database

REDISPORT=6379
EXEC=/usr/local/redis/bin/redis-server
CLIEXEC=/usr/local/redis/bin/redis-cli

PIDFILE=/var/run/redis_${REDISPORT}.pid
CONF="/usr/local/redis/etc/redis.conf"

case "$1" in
start)
if [ -f $PIDFILE ];then
echo "$PIDFILE exists,process is already running or crashed"
else
echo "Starting Redis server..."
$EXEC $CONF
fi
;;
stop)
if [ ! -f $PIDFILE ];then
echo "$PIDFILE does not exist,process is not running"
else
PID=$(cat $PIDFILE)
echo "Stopping..."
$CLIEXEC -p $REDISPORT shutdown
while [ -x /proc/${PID} ]
do
echo "Waiting for Redis to shutdown..."
sleep 1
done
echo "Redis stopped"
fi
;;
restart)
"$0" stop
sleep 3
"$0" start
;;
*)
echo "Please use start or stop or restart as first argument"
;;
esac

chkconfig: 2345 90 10
description: Redis is a persistent key-value database
redis服务必须在运行级2,3,4,5下被启动或关闭,启动的优先级是90,关闭的优先级是10。

  1. 修改文件权限
1
chmod +x /etc/init.d/redis
  1. 把脚本添加到系统服务列表
1
2
#chkconfig --add redis
#chkconfig redis on

Linux 系统的运行级别

缺省的运行级,RHS用到的级别如下:
0:关机 机器关闭
1:单用户模式 Win9x下的安全模式类似
2:无网络支持的多用户模式 为多用户模式,但是没有NFS支持。
3:有网络支持的多用户模式 为完整的多用户模式,是标准的运行级
4:保留,未使用 一般不用,在一些特殊情况下可以用它来做一些事情.
5:有网络支持有X-Window支持的多用户模式 X11,进到X Window系统了
6:重新引导系统,即重启 运行init 6机器就会重启。

chkconfig用法

chkconfig命令可以用来检查、设置系统的各种服务
使用语法:
chkconfig [–add][–del][–list][系统服务] 或 chkconfig [–level <等级代号>][系统服务][on/off/reset]

1
2
3
4
chkconfig –list                    # 列出所有的系统服务
chkconfig –add redis # 增加redis服务
chkconfig –del redis # 删除redis 服务
chkconfig –level redis 2345 on # 把redis在运行级别为2、3、4、5的情况下都是on(开启)的状态。
坚持原创技术分享,您的支持将鼓励我继续创作!