一. 安装,不用./configure,不用make install就编译一下就可邓
# useradd redis
# passwd redis
# su - redis
$ tar zxvf redis-2.0.4.tar.gz
$ cd redis-2.0.4
$ make
二.配置
$ vim redis.conf
主要配置的参数
pidfile /var/run/redis.pid 进程号
port 6379 端口号
loglevel verbose 打印日志级别,分别有debug(所有日志信息),verbose(有用的日志信息),notice(高于
verbose级别),warning(只输出重要紧急信息)
# logfile stdout 表示直接输出
logfile /home/redis/redis-2.0.4/logs/redis.log 表示把日志输入到此文件中
####################################################
把数据写入磁盘的时间配置(以下是默认的情况)
save 900 1 至少一个值变化的情况下,间隔900秒从内存写入磁盘
save 300 10 至少10个值变化的情况下,间隔300秒从内存写入磁盘
save 60 10000
# after 900 sec (15 min) if at least 1 key changed
# after 300 sec (5 min) if at least 10 keys changed
# after 60 sec if at least 10000 keys changed
rdbcompression yes 是否采用压缩存储,如果不想耗CPU,刚取no
dbfilename dump.rdb 备份数据的文件名与位置
####################################################
进行限制的配置
# maxclients 128 最大客户端连接数,默认128个
# maxmemory 最大占用内存大小,单位为bytes 1kb=1024bytes 1mb => 1024*1024 bytes
maxmemory 633572
###################################################
自带VM(虚拟内存)配置
redis支持采用VM技术,以达到当数据超过设置的可使用的物理内存的时候能够正常运行。当数据超过物理内
存的时候,把一部分数据写入磁盘中的一块空间来代替物理内存
vm-enabled no 默认不采用
# vm-enabled yes
vm-swap-file /tmp/redis.swap 如果采用VM技术,设置磁盘上的物理空间
vm-max-memory 0 虚拟内存的大小,一般设为物理内存的60~80%
vm-pages 134217728 采用内存页的方式存储,在磁盘上的每8页要消耗物理内存的1byte
vm-page-size 32 设置采用的page的数量,根据自己数据库的大小决定page的数量
三. 启动
$ nohub ./redis-server redis.conf &
$ ps aux | grep redis
root 3239 0.0 0.3 4908 1236 pts/0 S 10:07 0:00 su - redis
redis 3240 0.0 0.3 4532 1460 pts/0 S 10:07 0:02 -bash
redis 3510 0.5 0.2 2692 1148 pts/0 S 10:54 0:00 ./redis-server redis.conf
redis 3511 0.0 0.2 4252 940 pts/0 R+ 10:54 0:00 ps aux
redis 3512 0.0 0.1 3912 676 pts/0 R+ 10:54 0:00 grep redis
$ tail -f /home/redis/redis-2.0.4/logs/redis.log
[3507] 20 Feb 10:54:22 * Server started, Redis version 2.0.4
[3507] 20 Feb 10:54:22 # WARNING overcommit_memory is set to 0! Background save may fail under
low memory condition. To fix this issue add 'vm.overcommit_memory = 1' to /etc/sysctl.conf and
then reboot or run the command 'sysctl vm.overcommit_memory=1' for this to take effect.
[3507] 20 Feb 10:54:22 * DB loaded from disk: 0 seconds
[3507] 20 Feb 10:54:22 * The server is now ready to accept connections on port 6379
[3507] 20 Feb 10:54:22 - DB 0: 4 keys (0 volatile) in 4 slots HT.
[3507] 20 Feb 10:54:22 - 0 clients connected (0 slaves), 533980 bytes in use
[3507] 20 Feb 10:54:27 - DB 0: 4 keys (0 volatile) in 4 slots HT.
[3507] 20 Feb 10:54:27 - 0 clients connected (0 slaves), 533980 bytes in use
[3510] 20 Feb 10:54:32 * Server started, Redis version 2.0.4
此处报了错,是因为内存太小,使用比较紧张,需要修改内核参数。首先切换到root用户下
echo 1 > /proc/sys/vm/overcommit_memory
$ pkill redis
$ nohub ./redis-server redis.conf &
$ tail -f /home/redis/redis-2.0.4/logs/redis.log
[3565] 20 Feb 10:58:50 * Server started, Redis version 2.0.4
[3565] 20 Feb 10:58:50 * DB loaded from disk: 0 seconds
[3565] 20 Feb 10:58:50 * The server is now ready to accept connections on port 6379
[3565] 20 Feb 10:58:51 - DB 0: 4 keys (0 volatile) in 4 slots HT.
[3565] 20 Feb 10:58:51 - 0 clients connected (0 slaves), 533980 bytes in use
问题解决!
四. 测试
redis自带了测试工具redis-benchmark
[redis@localhost redis-2.0.4]$ ./redis-benchmark --help 有自己的参数
Wrong option '--help' or option argument missing
Usage: redis-benchmark [-h ] [-p ] [-c ] [-n [-k ]
-h Server hostname (default 127.0.0.1)
-p Server port (default 6379)
-c Number of parallel connections (default 50)
-n Total number of requests (default 10000)
-d Data size of SET/GET value in bytes (default 2)
-k 1=keep alive 0=reconnect (default 1)
-r Use random keys for SET/GET/INCR, random values for SADD
Using this option the benchmark will get/set keys
in the form mykey_rand000000012456 instead of constant
keys, the argument determines the max
number of values for the random number. For instance
if set to 10 only rand000000000000 - rand000000000009
range will be allowed.
-q Quiet. Just show query/sec values
-l Loop. Run the tests forever
-I Idle mode. Just open N idle connections and wait.
-D Debug mode. more verbose.
[redis@localhost redis-2.0.4]$ ./redis-benchmark
====== PING ======
10000 requests completed in 1.69 seconds
50 parallel clients
3 bytes payload
keep alive: 1
0.04% <= 0 milliseconds
0.05% <= 1 milliseconds
0.09% <= 2 milliseconds
0.14% <= 3 milliseconds
0.20% <= 4 milliseconds
0.25% <= 5 milliseconds
0.31% <= 6 milliseconds
2.04% <= 7 milliseconds
69.22% <= 8 milliseconds
97.44% <= 9 milliseconds
99.82% <= 10 milliseconds
100.00% <= 11 milliseconds
5931.20 requests per second
通过redis-cli命令实际操作一下,比如:
redis-cli set foo bar
OK
redis-cli get foo
bar
在设置键对应的值的时候,按照协议的规定是要提供数据大小这个参数的,上面的redis-cli命令之所以没有
提供这个参数是因为redis-cli本身进行了封装。
可以通过telnet来验证一点:
$ telnet 127.0.0.1 6379
Trying 127.0.0.1...
Connected to localhost.localdomain (127.0.0.1).
Escape character is '^]'.
set foo 5 设长度为5
abcdef 此处输入6个字符
+OK
get foo
$5
abcde 得到的长度为5个字符
^]
telnet> q
Connection closed.