redis配置及优化
一、理论
redis是一个非关系型数据库。
关系型数据库:结构化数据库,创建在关系模型基础上,一般面向于记录。借助于集合代数等数学概念和方法来处理数据库中的数据。关系模型就是指二维表格模型,因而一个关系型数据库就是由二维表及其之间的联系组成的一个数据组织。
主流的关系型数据库:oracle、mysql、sql server等
非关系型数据库
nosql(nosql=not only sql)意为“不仅仅是sql”,是非关系型数据库的总称。
主流的nosql数据库:redis、mongdb、hbase、couhdb等等。
redis(remote dictionary server,远程字典型)是一个开源的、使用c语言编写的nosql数据库。redis基于内存运行并支持持久化,采用key-value(键值对)的存储形式,是目前分布式架构不可或缺的一环。
redis是单进程模型,在一台服务器上可以同时启动多个redis进程,而redis的实际处理速度则是完全依靠于主进程的执行效率。
redis的持久化
RDB
AOF
二、实践
1、环境
redis 192.168.10.101
2、过程
[root@localhost ~]# dnf -y install gcc
[root@localhost ~]# tar zxf redis-4.0.9.tar.gz
[root@localhost ~]# cd redis-4.0.9
[root@localhost redis-4.0.9]# make
[root@localhost redis-4.0.9]# make PREFIX=/usr/local/redis install
[root@localhost redis-4.0.9]# ln -s /usr/local/redis/bin/* /usr/local/bin/
[root@localhost redis-4.0.9]# ls
00-RELEASENOTES COPYING Makefile redis.conf runtest-sentinel tests
BUGS deps MANIFESTO runtest sentinel.conf utils
CONTRIBUTING INSTALL README.md runtest-cluster src
[root@localhost redis-4.0.9]# cd utils/
[root@localhost utils]# ls
build-static-symbols.tcl hashtable redis_init_script.tpl
cluster_fail_time.tcl hyperloglog redis-sha1.rb
corrupt_rdb.c install_server.sh releasetools
create-cluster lru speed-regression.tcl
generate-command-help.rb redis-copy.rb whatisdoing.sh
graphs redis_init_script
[root@localhost utils]# ./install_server.sh # 初始化redis。
Welcome to the redis service installer
This script will help you easily set up a running redis serveregrep: warning: egrep is obsolescent; using grep -E
Please select the redis port for this instance: [6379]
egrep: warning: egrep is obsolescent; using grep -E
Selecting default: 6379
Please select the redis config file name [/etc/redis/6379.conf]
Selected default - /etc/redis/6379.conf
Please select the redis log file name [/var/log/redis_6379.log]
Selected default - /var/log/redis_6379.log
Please select the data directory for this instance [/var/lib/redis/6379]
Selected default - /var/lib/redis/6379
Please select the redis executable path [/usr/local/bin/redis-server] /usr/local/redis/bin/redis-server # 选择redis可执行文件路径,这里手动输入。
Selected config:
Port : 6379
Config file : /etc/redis/6379.conf # 配置文件路径
Log file : /var/log/redis_6379.log # 日志文件路径
Data dir : /var/lib/redis/6379 # 数据文件路径
Executable : /usr/local/redis/bin/redis-server # 可执行文件路径
Cli Executable : /usr/local/bin/redis-cli # 客户端命令行工具
Is this ok? Then press ENTER to go on or Ctrl-C to abort.
Copied /tmp/6379.conf => /etc/init.d/redis_6379
Installing service...
Successfully added to chkconfig!
Successfully added to runlevels 345!
/var/run/redis_6379.pid exists, process is already running or crashed
Installation successful![root@localhost utils]# netstat -lnupt | grep redis
tcp 0 0 127.0.0.1:6379 0.0.0.0:* LISTEN 6649/redis-server 1 [root@localhost utils]# /etc/init.d/redis_6379 stop
Stopping ...
Redis stopped
[root@localhost utils]# /etc/init.d/redis_6379 start
Starting Redis server...
[root@localhost utils]# /etc/init.d/redis_6379 restart
Stopping ...
Redis stopped
Starting Redis server...
[root@localhost utils]# /etc/init.d/redis_6379 status
Redis is running (6757)[root@localhost utils]# vim /etc/redis/6379.conf
bind 127.0.0.1 192.168.10.101 # 70行
port 6379 # 93行
daemonize yes # 137行
pidfile /var/run/redis_6379.pid # 159行[root@localhost utils]# /etc/init.d/redis_6379 restart
Stopping ...
Waiting for Redis to shutdown ...
Redis stopped
Starting Redis server...[root@localhost utils]# /usr/local/redis/bin/redis-cli
127.0.0.1:6379> ping # 检测redis服务是否启动,并不是imcp的ping(连通性测试),仅仅是用来测试进程是否存活。
PONG # 返回pong说明服务已启动。[root@localhost utils]# redis-cli -h 192.168.10.101 -p 6379 # -h 指定主机名,-p 指定端口。
192.168.10.101:6379>
192.168.10.101:6379> info # 显示统计信息
# Server
redis_version:4.0.9
redis_git_sha1:00000000
redis_git_dirty:0
redis_build_id:631fbb53b5ff087d
redis_mode:standalone
……略[root@localhost ~]# redis-cli
127.0.0.1:6379> help @list # 查看所有与list数据类型的相关命令BLPOP key [key ...] timeoutsummary: Remove and get the first element in a list, or block until one is availablesince: 2.0.0……略[root@localhost ~]# redis-benchmark -h 192.168.10.101 -p 6379 -c 100 -n 100000 # 向192.168.10.101:6379的redis服务器发送100个并发连接与100000个请求测试性能。
====== PING_INLINE ======100000 requests completed in 3.92 seconds100 parallel clients3 bytes payloadkeep alive: 18.68% <= 1 milliseconds # 8.68%的数据在1毫秒内处理完毕。
46.08% <= 2 milliseconds
74.33% <= 3 milliseconds
92.06% <= 4 milliseconds
95.83% <= 5 milliseconds
96.62% <= 6 milliseconds
97.28% <= 7 milliseconds
97.67% <= 8 milliseconds
98.06% <= 9 milliseconds
98.52% <= 10 milliseconds
98.94% <= 11 milliseconds
99.02% <= 12 milliseconds
99.15% <= 13 milliseconds
99.39% <= 14 milliseconds
99.57% <= 15 milliseconds
99.66% <= 16 milliseconds
99.73% <= 17 milliseconds
99.75% <= 18 milliseconds
99.76% <= 19 milliseconds
99.77% <= 29 milliseconds
99.77% <= 30 milliseconds
99.82% <= 31 milliseconds
99.87% <= 32 milliseconds
99.89% <= 34 milliseconds
99.90% <= 35 milliseconds
99.92% <= 36 milliseconds
99.95% <= 64 milliseconds
99.99% <= 65 milliseconds
100.00% <= 65 milliseconds # 所有的数据在65毫秒内处理完毕。
25523.23 requests per second # 每秒25523.23个请求。[root@localhost ~]# redis-benchmark -h 192.168.10.101 -p 6379 -q -d 100 # 测试存取大小为100字节的数据包的性能。
PING_INLINE: 29446.41 requests per second # 每秒钟ping的请求数。
PING_BULK: 27397.26 requests per second
SET: 29455.08 requests per second # 每秒钟设置的键数。(写入)
GET: 27307.48 requests per second # 每秒钟读取的值数。(读取)
INCR: 34806.82 requests per second # 每秒执行多少个原子性(类似事务)。
LPUSH: 38865.14 requests per second
RPUSH: 32113.04 requests per second
LPOP: 39588.28 requests per second
RPOP: 34328.87 requests per second
SADD: 28868.36 requests per second
HSET: 27027.03 requests per second
SPOP: 27262.81 requests per second
LPUSH (needed to benchmark LRANGE): 27397.26 requests per second
LRANGE_100 (first 100 elements): 12933.26 requests per second
LRANGE_300 (first 300 elements): 4373.69 requests per second
LRANGE_500 (first 450 elements): 2811.20 requests per second
LRANGE_600 (first 600 elements): 2098.42 requests per second
MSET (10 keys): 24119.63 requests per second[root@localhost ~]# redis-benchmark -t set,lpush -n 100000 -q # 测试redis服务在进行set与lpush操作时的性能。
SET: 30950.17 requests per second
LPUSH: 27487.63 requests per second127.0.0.1:6379> set techer ooos # 键为techer 值为ooos
OK
127.0.0.1:6379> get techer # 获取techer键的值。
"ooos"127.0.0.1:6379> set k1 1
OK
127.0.0.1:6379> set k 2
OK
127.0.0.1:6379> set k3 3
OK
127.0.0.1:6379> set k4 4
OK
127.0.0.1:6379> set v1 4
OK
127.0.0.1:6379> set v5 5
OK
127.0.0.1:6379> keys * # 查看当前数据库中所有键1) "k4"2) "k1"3) "k"4) "k3"5) "v5"6) "mylist"7) "myset:__rand_int__"8) "key:__rand_int__"9) "v1"
10) "counter:__rand_int__"
11) "techer"127.0.0.1:6379> keys v* # 查看当前数据库中以v开头的数据。
1) "v22"
2) "v5"
3) "v1"
127.0.0.1:6379> keys v? # 查看当前数据库中以v开头后面包含任意一位的数据。
1) "v5"
2) "v1"
127.0.0.1:6379> keys v?? # 查看当前数据库中以v开头 v后面包含任意两位的数据。
1) "v22"127.0.0.1:6379> exists techer # 判断techer是否存在。
(integer) 1 # 为1表示存在。
127.0.0.1:6379> exists sooo # 判断sooo是否存在。
(integer) 0 # 为0表示不存在。127.0.0.1:6379> keys *1) "k4"2) "v22"3) "k1"4) "k"5) "k3"6) "v5"7) "mylist"8) "myset:__rand_int__"9) "key:__rand_int__"
10) "v1"
11) "counter:__rand_int__"
12) "techer"
127.0.0.1:6379> del v5 # 删除指定键
(integer) 1
127.0.0.1:6379> get v5
(nil)127.0.0.1:6379> type k1 # 获取key对应的value值类型
string127.0.0.1:6379> keys v*
1) "v22"
2) "v1"
127.0.0.1:6379> rename v22 v2 # 重命名,如果命名重复,重命名后的会覆盖已存在的。
OK
127.0.0.1:6379> keys v*
1) "v2"
2) "v1"
127.0.0.1:6379> get v1
"4"
127.0.0.1:6379> get v2
"5"
127.0.0.1:6379> rename v1 v2
OK
127.0.0.1:6379> get v2
"4"
127.0.0.1:6379> get v1
(nil)127.0.0.1:6379> keys *1) "k4"2) "k1"3) "k"4) "k3"5) "v2"6) "mylist"7) "myset:__rand_int__"8) "key:__rand_int__"9) "counter:__rand_int__"
10) "techer"
127.0.0.1:6379> get techer
"ooos"
127.0.0.1:6379> get v2
"4"
127.0.0.1:6379> renamenx v2 techer # 当重命名的名称存在,不会进行重命名与覆盖。
(integer) 0
127.0.0.1:6379> keys *1) "k4"2) "k1"3) "k"4) "k3"5) "v2"6) "mylist"7) "myset:__rand_int__"8) "key:__rand_int__"9) "counter:__rand_int__"
10) "techer"
127.0.0.1:6379> get techer
"ooos"
127.0.0.1:6379> get v2
"4"127.0.0.1:6379> dbsize # 查看当前数据库中key的数目。
(integer) 10127.0.0.1:6379> select 10 # 切换到序号为10的数据库
OK
127.0.0.1:6379[10]> select 15
OK
127.0.0.1:6379[15]> select 0
OK
127.0.0.1:6379> # redis数据库中,各库之间独立,无法跨库查询数据。
127.0.0.1:6379> set k1 100
OK
127.0.0.1:6379> get k1
"100"
127.0.0.1:6379> select k1
(error) ERR invalid DB index
127.0.0.1:6379> get k1
"100"
# 移动键到另一个库,该库中键会查询不到,因为被移走了。
127.0.0.1:6379> select 0
OK
127.0.0.1:6379> get k1
"100"
127.0.0.1:6379> move k1 1
(integer) 1
127.0.0.1:6379> select 1
OK
127.0.0.1:6379[1]> get k1
"100"
127.0.0.1:6379[1]> select 0
OK
127.0.0.1:6379> get k1
(nil)127.0.0.1:6379> info memory # 查看redis使用内存值
# Memory
used_memory:12791864
used_memory_human:12.20M
used_memory_rss:28033024 # rss是resident set size的缩写,表示该进程所占物理内存的大小,即为操作系统分配给redis实例的内存大小。
used_memory_rss_human:26.73M
used_memory_peak:25671456
used_memory_peak_human:24.48M
used_memory_peak_perc:49.83%
used_memory_overhead:836790
used_memory_startup:786600
used_memory_dataset:11955074
used_memory_dataset_perc:99.58%
total_system_memory:3536605184
total_system_memory_human:3.29G
used_memory_lua:37888
used_memory_lua_human:37.00K
maxmemory:0
maxmemory_human:0B
maxmemory_policy:noeviction
mem_fragmentation_ratio:2.19
mem_allocator:jemalloc-4.0.3
active_defrag_running:0
lazyfree_pending_objects:0expire 时间 为数据设置过期时间。config set requirepass 密码 设置redis的临时密码。