|
一、Redis介绍
Redis 和 Memcached 一样都属于键值数据库,但是支持数据持久化,能够让数据从内存中保存在磁盘里,即使重启服务器数据也依然存在;同时也支持更多 value 类型,除了 string 外,还支持 hash、lists(链表)、sets(集合)和 sorted sets(有序集合)几种数据类型。 redis 使用了两种文件格式: 1)全量数据(RDB):是把内存中的数据写入磁盘,便于下次读取文件进行加载。 2)增量请求(AOF):是把内存中的数据序列化为操作请求,用于读取文件进行replay得到数据。 redis 存储分为内存储存、磁盘存储和log文件三部分。 Redis安装配置
1
2
3
4
5
6
| [iyunv@gz1 ~]# cd /usr/local/src/
[iyunv@gz1 redis-2.8.21]# yum install -y gcc epel-release ;yum install -y jemalloc-devel
[iyunv@gz1 src]# wget 'https://codeload.github.com/antirez/redis/tar.gz/2.8.21' -O redis-2.8.21.tar.gz
[iyunv@gz1 src]# tar -zxvf redis-2.8.21.tar.gz
[iyunv@gz1 src]# cd redis-2.8.21
[iyunv@gz1 redis-2.8.21]# make
|
注意:若make报错,执行以下命令
1
| [iyunv@gz1 redis-2.8.21]# cd deps;make hiredis lua jemalloc linenoise;cd ..;make
|
若看到Hint: It's a good idea to run 'make test' ;)说明已经成功了
1
2
3
| [iyunv@gz1 redis-2.8.21]# make PREFIX=/usr/local/redis install
[iyunv@gz1 redis-2.8.21]# ls /usr/local/redis/
bin
|
创建配置文件
1
2
| [iyunv@gz1 redis-2.8.21]# mkdir /usr/local/redis/etc
[iyunv@gz1 redis-2.8.21]# cd /usr/local/redis/etc/
|
编辑配置文件
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
| [iyunv@gz1 etc]# vim redis.conf
daemonize yes
pidfile /usr/local/redis/var/redis.pid
port 6379
timeout 300
loglevel debug
logfile /usr/local/redis/var/redis.log
databases 16
save 900 1
save 300 10
save 60 10000
rdbcompression yes
dbfilename dump.rdb
dir /usr/local/redis/var/
appendonly no
appendfsync always
|
配置参数说明: #daemonize:是否以后台daemon方式运行 #pidfile:pid文件位置 #port:监听的端口号 #timeout:请求超时时间 #loglevel:log信息级别 #logfile:log文件位置 #databases:开启数据库的数量 #save * *:保存快照的频率,第一个*表示多长时间,第二个*表示执行多少次写操作。在一定时间内执行一定数量的写操作时,自动保存快照。可设置多个条件 #rdbcompression:是否使用压缩 #dbfilename:数据快照文件名(只是文件名,不包括目录) #dir:数据快照的保存目录(这个是目录) #appendonly:是否开启appendonlylog,开启的话每次写操作会记一条log,这会提高数据抗风险能力,但影响效率 #appendfsync:appendonlylog如何同步到磁盘(三个选项,分别是每次写都强制调用fsync、每秒启用一次fsync、不调用fsync等待系统自己同步) #requirepass 123456:设置密码,我这里注释掉了。 编辑启动脚本
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
| [iyunv@gz1 etc]# vim /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"
basedir="/usr/local/redis"
exec="$basedir/bin/$name"
pidfile="$basedir/var/redis.pid"
REDIS_CONFIG="$basedir/etc/redis.conf"
[ -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 $?
|
1
2
3
4
| [iyunv@gz1 etc]# useradd -s /sbin/nologin redis 创建一个普通用户
[iyunv@gz1 etc]# mkdir /usr/local/redis/var
[iyunv@gz1 etc]# chmod 777 /usr/local/redis/var
[iyunv@gz1 etc]# chmod 755 /etc/init.d/redis
|
启动redis服务
1
2
3
4
5
6
7
8
| [iyunv@gz1 etc]# chkconfig --add redis 加入启动列表
[iyunv@gz1 etc]# chkconfig redis on 开机启动
[iyunv@gz1 etc]# service redis start
/etc/init.d/redis: line 1: a#!/bin/sh: No such file or directory
启动 : [确定]
[iyunv@gz1 etc]# ps aux |grep redis
redis 7380 0.1 0.7 137372 7476 ? Ssl 13:21 0:00 /usr/local/redis/bin/redis-server *:6379
root 7385 0.0 0.0 103316 892 pts/1 S+ 13:21 0:00 grep redis
|
|