潇湘夜雨移动版

主页 > 数据库 >

redis3.0配置和测试

1、下载安装redis
wget http://download.redis.io/releases/redis-3.2.8.tar.gz
tar -zxvf redis-3.2.8.tar.gz 
cd redis-3.2.8
make
cp -r -c src/ /usr/local/redis #复制编译后的应用程序到安装路径
更改配置文件:
bind 0.0.0.0
protected-mode no
port 6379
tcp-backlog 511
timeout 0
tcp-keepalive 300
daemonize yes #后台运行
supervised no
pidfile /var/run/redis_6379.pid
loglevel notice
logfile  /var/log/redis/redis.log #日志文件路径
databases 16 #默认16个库
save 900 1
save 300 10
save 60 10000
stop-writes-on-bgsave-error yes
rdbcompression yes
rdbchecksum yes
dbfilename dump.rdb
dir /data/redis/ #数据库快照路径
slave-serve-stale-data yes
slave-read-only yes
repl-diskless-sync no
repl-diskless-sync-delay 5
repl-disable-tcp-nodelay no
slave-priority 100
maxmemory 2gb #最大内存限制
maxmemory-policy volatile-lru #keys都有过期时间,可以通过该策略清楚过期的key
2、启动redis服务
[yanfa@web1 ~]$  /usr/local/redis/redis-server  /usr/local/redis/redis.conf 
[yanfa@web1 ~]$ netstat -tnlp |grep redis
tcp        0      0 0.0.0.0:6379                0.0.0.0:*                   LISTEN      13278/redis-server  
 
[yanfa@web1 redis]$ ./redis-cli -h web1 shutdown  #关闭redis
 
3、登录测试:
[yanfa@web1 redis]$ ./redis-cli
127.0.0.1:6379> keys *
(empty list or set)
127.0.0.1:6379> set mykey abc
OK
127.0.0.1:6379> get mykey
"abc"
 
test:6379> client list #查询客户端连接
id=39 addr=10.0.14.170:59492 fd=9 name= age=37 idle=7 flags=N db=0 sub=0 psub=0 multi=-1 qbuf=0 qbuf-free=0 obl=0 oll=0 omem=0 events=r cmd=ping
 
test:6379> info memory #查询内存配置信息
# Memory
used_memory:1014632
used_memory_human:990.85K
used_memory_rss:2486272
used_memory_rss_human:2.37M
used_memory_peak:1301488
used_memory_peak_human:1.24M
total_system_memory:4018601984
total_system_memory_human:3.74G
used_memory_lua:37888
used_memory_lua_human:37.00K
maxmemory:2097152
maxmemory_human:2.00M
maxmemory_policy:volatile-lru
mem_fragmentation_ratio:2.45
mem_allocator:jemalloc-4.0.3
 
[yanfa@web1 redis]$ ./redis-cli -r 100 -i 1 info | grep used_memory_human #实时查看redis的内存使用量
used_memory_human:2.74M
used_memory_human:2.74M
 
(integer) 1494398314
test:6379> monitor #实时打印出 Redis 服务器接收到的命令,调试用
OK
1494398968.836528 [0 10.0.14.170:56162] "PING"
1494398968.836676 [0 10.0.14.170:56162] "SELECT" "0"
1494398968.836771 [0 10.0.14.170:56162] "KEYS" "GPS_20_*"
 
test:6379> DBSIZE #统计keys的个数
(integer) 11334
 
test:6379> EXPIRE mykeys 600 #设置过期时间
(integer) 1
test:6379> TTL mykeys #查询生存时间
(integer) 592
test:6379> FLUSHALL #清楚所有数据
OK
test:6379> keys *
test:6379[1]> SELECT 2 #切换到数据库2
OK
test:6379[2]> KEYS *
1) "my"
 
4、常见错误或提醒
 
24631:M 09 May 16:12:06.889 # WARNING: The TCP backlog setting of 511 cannot be enforced because /proc/sys/net/core/somaxconn is set to the lower value of 128.
说明:设置/proc/sys/net/core/somaxconn参数大于511,系统默认128,并发量大的情况下可以调整,一般不用管。
 
24631:M 09 May 16:12:06.889 # Server started, Redis version 3.2.8
24631:M 09 May 16:12:06.889 # 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.
 
说明:过量使用内存的提醒,如果不想报警可设置 echo 1 > /proc/sys/vm/overcommit_memory
但是为了系统稳定,不建议修改。
5、redis压力测试
 
[yanfa@test script]$ cat redis.sh #批量插入redis key
#!/bin/bash
for ((i=1;i<20000;i++))
do
echo -en "helloworld" | /usr/local/redis/redis-cli -h test  -x set name$i >>redis.log
done
 
(责任编辑:liangzh)