0.环境安装
http://blog.csdn.net/xubo245/article/details/48318735
1.启动:
一个终端运行:
redis-server
另一个终端运行:
redis-cli
在redis-cli执行相关操作。
2.数据库选择:默认数据库个数是16,编号从0-15,选择语句为:
select 1
执行:
127.0.0.1:6379> select 1
OK
127.0.0.1:6379[1]> select 15
OK
127.0.0.1:6379[15]> select 16
(error) ERR invalid DB index
可以在redis.conf中修改,
将16改为64
# Set the number of databases. The default database is DB 0, you can select # a different one on a per-connection basis using SELECT <dbid> where # dbid is a number between 0 and 'databases'-1 databases 64
restart:
redis-server ./redis.conf
redis-cli
not connected> select 1
OK
127.0.0.1:6379[1]> select 11
OK
127.0.0.1:6379[11]> select 21
OK
127.0.0.1:6379[21]> select 63
OK
127.0.0.1:6379[63]> 64
(error) ERR unknown command '64'
127.0.0.1:6379[63]>
redis不支持自定义数据库的名字,每个数据库都是以编号命名
3.数据库清除:
Redis不支持为每个数据量库设置不同的访问密码,所以一个客户端要么可以访问全部数据库,要么连一个数据库也没有权限访问。最重要的一点是多个数据库之间并不是完全隔离的,比如说FLUSHALL命令可以清空一个Redis实例中所有数据库中的数据。
所以这些数据库更像是一种命令空间,而不适宜存储不同应用程序的数据。
不同的应用应该使用不同的Redis实例存储数据。
由于Redis非常轻量级,一个空Redis实例占用的内存只有1MB左右,所以不用担心多个Redis实例会额外占用很多内存。
flushdb //清除当前数据库
flushall //清除当前redis实力下的所有数据库
4.redis-benchmark 循环测试脚本:
新建redisBenchmark.sh文件:vi redisBenchmark.sh
写入:
#!/usr/bin/env bash
mkdir -p test
for ((i=1;i<100000000;i=i*10))
do
echo $i
redis-benchmark -n $i >> ./test/7000_$i.txt
done
保存退出:wq,修改权限:
sudo chmod 775 redisBenchmark.sh
执行脚本:
./redisBenchmark.sh
执行结果:
hadoop@Master:~/redisTest$ ./redisBenchmark.sh
1
10
100
1000
10000
100000
1000000
10000000
100000000
查看文件内容:
hadoop@Master:~/redisTest$ vi test/7000_10000.txt
PING_INLINE: 2.00^M====== PING_INLINE ======
10000 requests completed in 0.08 seconds
50 parallel clients
3 bytes payload
keep alive: 1
100.00% <= 0 milliseconds
121951.22 requests per second
====== PING_BULK ======
10000 requests completed in 0.08 seconds
50 parallel clients
3 bytes payload
keep alive: 1
99.67% <= 1 milliseconds
100.00% <= 1 milliseconds
121951.22 requests per second
====== SET ======
...//很多,省略
但测试的数据数量比较多时,会出现实时的转态,然后在txt里面有很多记录,-q也不行,还没找到具体的解决方法?如果谁有解决方法,求教。
执行脚本语句:
redis-benchmark -q -n $i > ./test/7000_$i.txt
显示如下:
PING_INLINE: -nan^MPING_INLINE: 79216.00^MPING_INLINE: 69050.00^MPING_INLINE: 70081.34^MPING_INLINE: 66780.00^MPING_INLINE: 70709.60^MPING_INLINE: 75744.66^MPING_INLINE: 75740.57^MPING_INLINE: 75120.50^MPING_INLINE: 74164.00^MPING_INLINE: 73846.80^MPING_INLINE: 74661.09^MPING_INLINE: 74759.66^MPING_INLINE: 75984.30^MPING_INLINE: 75772.29^MPING_INLINE: 74746.93^MPING_INLINE: 73542.00^MPING_INLINE: 72976.47^MPING_INLINE: 72417.55^MPING_INLINE: 72625.27^MPING_INLINE: 73251.80^MPING_INLINE: 73273.71^MPING_INLINE: 74197.27^MPING_INLINE: 73807.65^MPING_INLINE: 73549.00^MPING_INLINE: 73847.84^MPING_INLINE: 73365.84^MPING_INLINE: 72895.26^MPING_INLINE: 72973.43^MPING_INLINE: 72435.73^MPING_INLINE: 72089.34^MPING_INLINE: 71897.94^MPING_INLINE: 72111.00^MPING_INLINE: 72362.18^MPING_INLINE: 71955.77^MPING_INLINE: 71490.17^MPING_INLINE: 71425.22^MPING_INLINE: 71665.30^MPING_INLINE: 72450.10^MPING_INLINE: 72389.12^MPING_INLINE: 72351.50^MPING_INLINE: 72569.56^MPING_INLINE: 72282.86^MPING_INLINE: 72627.72^MPING_INLINE: 72360.45^MPING_INLINE: 72420.80^MPING_INLINE: 72463.56^MPING_INLINE: 72334.80^MPING_INLINE: 72479.66^MPING_INLINE: 72635.92^MPING_INLINE: 72734.72^MPING_INLINE: 72488.39^MPING_INLINE: 72301.16^MPING_INLINE: 72327.09^MPING_INLINE: 72679.41^MPING_INLINE: 72939.46 requests per second
5.关闭redis-cli的方式:
如果直接关掉,redis在内存中的数据不会保存到硬盘,需要执行sava,bgsave等,或者执行:
(1)在redis-cli下输入shutdown或者在命令行输入redis-cli shutdown
(2)可以直接kill掉redis进程,redis会对kill指令做出响应。
hadoop@Master:~/redisTest$ kill -9 29126
hadoop@Master:~/redisTest$ ps -aux |grep redis
hadoop 21466 62.1 45.3 3192140 2768376 pts/35 Rl+ 11:36 140:44 redis-server *:6379
hadoop 21470 0.0 0.0 18156 904 pts/36 S+ 11:36 0:00 redis-cli
hadoop 21579 0.0 0.0 16632 628 pts/38 S+ 11:37 0:00 bash ./redisBenchmark2.sh
hadoop 22646 36.6 0.8 96928 54096 pts/38 S+ 12:09 70:48 redis-benchmark -c 1 -n 10000000
hadoop 28996 9.6 0.2 31028 17012 pts/39 R 15:16 0:34 redis-benchmark -n 1000000
hadoop 28998 9.5 0.2 31040 17668 pts/39 R 15:16 0:33 redis-benchmark -n 1000000
hadoop 29108 10.6 0.1 23200 10400 pts/39 R 15:20 0:15 redis-benchmark -n 1000000
hadoop 29207 0.0 0.0 15952 2412 pts/39 S+ 15:22 0:00 grep --color=auto redis
[9]+ Killed redis-benchmark -n 1000000 > 1.log 2>&1
hadoop@Master:~/redisTest$ kill -9 28996
hadoop@Master:~/redisTest$ kill -9 28998
[4] Killed redis-benchmark -n 1000000 > 1.log 2>&1
hadoop@Master:~/redisTest$ kill -9 28108
-bash: kill: (28108) - No such process
[5]- Killed redis-benchmark -n 1000000 > 1.log 2>&1
hadoop@Master:~/redisTest$ ps -aux |grep redis
hadoop 21466 62.1 44.9 3192140 2744592 pts/35 Rl+ 11:36 141:11 redis-server *:6379
hadoop 21470 0.0 0.0 18156 904 pts/36 S+ 11:36 0:00 redis-cli
hadoop 21579 0.0 0.0 16632 628 pts/38 S+ 11:37 0:00 bash ./redisBenchmark2.sh
hadoop 22646 36.6 0.9 96928 54992 pts/38 D+ 12:09 71:00 redis-benchmark -c 1 -n 10000000
hadoop 29108 14.4 0.2 31044 18112 pts/39 S 15:20 0:26 redis-benchmark -n 1000000
hadoop 29232 96.0 45.2 3192144 2761652 pts/35 R+ 15:23 0:02 redis-rdb-bgsave *:6379
hadoop 29234 0.0 0.0 15952 2460 pts/39 S+ 15:23 0:00 grep --color=auto redis
hadoop@Master:~/redisTest$ ps -aux |grep redis
hadoop 21466 62.1 44.9 3192140 2744592 pts/35 Sl+ 11:36 141:16 redis-server *:6379
hadoop 21470 0.0 0.0 18156 904 pts/36 S+ 11:36 0:00 redis-cli
hadoop 21579 0.0 0.0 16632 628 pts/38 S+ 11:37 0:00 bash ./redisBenchmark2.sh
hadoop 22646 36.6 0.9 96928 54992 pts/38 R+ 12:09 71:02 redis-benchmark -c 1 -n 10000000
hadoop 29108 16.1 0.2 31044 18112 pts/39 R 15:20 0:31 redis-benchmark -n 1000000
hadoop 29237 0.0 0.0 15952 2464 pts/39 S+ 15:23 0:00 grep --color=auto redis
hadoop 29232 96.0 45.2 3192144 2761652 pts/35 R+ 15:23 0:02 redis-rdb-bgsave *:6379即在执行bgsave命令。执行完后kill才算完成。
6.自增or自减:
incr key
incrbt key increment
incrbyfloat key increment
decr key
decrby key decrement
7.向尾部加值:
127.0.0.1:6379> set xubo hello
OK
127.0.0.1:6379> get xubo
"hello"
127.0.0.1:6379> append xubo " world"
(integer) 11
127.0.0.1:6379> get xubo
"hello world"
127.0.0.1:6379>
8.获取字符长度:
127.0.0.1:6379> get xubo
"hello world"
127.0.0.1:6379> strlen xubo
(integer) 11
127.0.0.1:6379> set chinese 中国
OK
127.0.0.1:6379> strlen chinese
(integer) 4
127.0.0.1:6379>
9.同时设置多个值:
127.0.0.1:6379> mset xubo1 hello1 xubo2 hello2 xubo3 hello3
OK
127.0.0.1:6379> keys *
1) "xubo2"
2) "chinese"
3) "xubo1"
4) "num"
5) "xubo3"
6) "xubo"
127.0.0.1:6379> get xubo3
"hello3"
127.0.0.1:6379> mget xubo xubo1 xubo3
1) "hello world"
2) "hello1"
3) "hello3"
127.0.0.1:6379>
10.其他后面有时间再写
推荐博客:http://www.uml.org.cn/sjjm/201212205.asp