
1.redis介绍
Redis(Remote Dictionary Server,远程字典服务器)是一个开源的、使用C语言编写的NoSQL数据库。Redis基于内存运行并支持持久化,采用key-value(键值对)的存储形式,是目前分布式架构中不可或缺的一环。Redis服务器程序是单进程模型,也就是在一台服务器上可以同时启动多个Redis进程,而Redis的实际处理速度则是完全依靠于主进程的执行效率。若在服务器上只运行一个Redis进程,当多个客户端同时访问时,服务器的处理能力是会有一定程度的下降;若在同一台服务器上开启多个Redis进程,Redis会提高并发处理能力的同时会给服务器的CPU造成很大压力。也就是说在实际生产环境中,需要根据实际的需求来决定开启多少个Redis进程。若对高并发要求更高一些,可能会考虑在同一台服务器上开启多个进程;若CPU资源比较紧张,采用单进程即可。
Redis具有以下几个优点:
- 具有极高的数据读写速度,数据读取的速度最高可达到110000次/s,数据写入速度最高可达到81000次/s。
- 支持丰富的数据类型,不仅仅支持简单的key-value类型的数据,还支持Strings,Lists, Hashes,Sets及Ordered Sets等数据类型操作。
- 支持数据的持久化,可以将内存中的数据保存在磁盘中,重启的时候可以再次加载进行使用。
- 原子性,Redis所有操作都是原子性的。
- 支持数据备份,即 master-salve模式的数据备份。
- Sharding技术: 很容易将数据分布到多个Redis实例中,数据库的扩展是个永恒的话题,在关系型数据库中,主要是以添加硬件、以分区为主要技术形式的纵向扩展解决了很多的应用场景,但随着web2.0、移动互联网、云计算等应用的兴起,这种扩展模式已经不太适合了,所以近年来,像采用主从配置、数据库复制形式的,Sharding这种技术把负载分布到多个特理节点上去的横向扩展方式用处越来越多。
Redis缺点:
- 是数据库容量受到物理内存的限制,不能用作海量数据的高性能读写,因此Redis适合的场景主要局限在较小数据量的高性能操作和运算上。
- Redis较难支持在线扩容,在集群容量达到上限时在线扩容会变得很复杂。为避免这一问题,运维人员在系统上线时必须确保有足够的空间,这对资源造成了很大的浪费。
Redis作为基于内存运行的数据库,缓存是其最常应用的场景之一,除此之外,Redis 常见应用场景还包括:获取最新N个数据的操作、排行榜类应用、计数器应用、存储关系、实时分析系统、日志记录。
2.安装
- 在官网下载.gz压缩包然后解压
[root@yichen-redis ~]# wget -c http://download.redis.io/releases/redis-5.0.7.tar.gz
[root@yichen-redis ~]# tar zxvf redis-5.0.7.tar.gz -C /usr/src/
[root@yichen-redis ~]# cd /usr/src/redis-5.0.7/
[root@yichen-redis redis-5.0.7]# make
[root@yichen-redis redis-5.0.7]# make PREFIX=/usr/local/redis install
# 创建软链接
[root@yichen-redis redis-5.0.7]# ln -s /usr/local/redis/bin/* /usr/local/bin/
[root@yichen-redis redis-5.0.7]# groupadd -g 994 redis
[root@yichen-redis redis-5.0.7]# useradd -g 994 -s /sbin/nologin redis
[root@yichen-redis redis-5.0.7]# mkdir /etc/redis
[root@yichen-redis redis-5.0.7]# chown redis:redis /etc/redis/
[root@yichen-redis redis-5.0.7]# cp redis.conf /etc/redis/
[root@yichen-redis redis-5.0.7]# vim /etc/redis/redis.conf
bind 127.0.0.1 192.168.150.50
daemonize yes
dir /etc/redis
[root@yichen-redis ~]# cat /etc/redis/redis.conf | grep -v '^#'
#绑定地址
#后台守护进程启动
#储存目录
bind 127.0.0.1 192.168.150.50
daemonize yes
dir /etc/redis
#生成systemd启动脚本
[root@yichen-redis ~]# vim /usr/lib/systemd/system/redis.service
[Unit]
Description=Redis persistent key-value database
After=network.target
After=network-online.target
Wants=network-online.target
[Service]
ExecStart=/usr/local/redis/bin/redis-server /etc/redis/redis.conf --supervised systemd
ExecStop=/usr/bin/pkill redis-server
Type=notify
User=redis
Group=redis
RuntimeDirectory=redis
RuntimeDirectoryMode=0775
[Install]
WantedBy=multi-user.target
[root@yichen-redis ~]# systemctl daemon-reload
[root@yichen-redis ~]# systemctl restart redis.service
[root@yichen-redis ~]# systemctl status redis.service
● redis.service - Redis persistent key-value database
Loaded: loaded (/usr/lib/systemd/system/redis.service; disabled; vendor preset: disabled)
Active: active (running) since 三 2020-04-08 10:27:21 CST; 2s ago
Main PID: 12294 (redis-server)
CGroup: /system.slice/redis.service
└─12294 /usr/local/redis/bin/redis-server 127.0.0.1:6379
4月 08 10:27:21 yichen-redis redis-server[12294]: `-._ `-.__.-' _.-'
4月 08 10:27:21 yichen-redis redis-server[12294]: `-._ _.-'
4月 08 10:27:21 yichen-redis redis-server[12294]: `-.__.-'
4月 08 10:27:21 yichen-redis redis-server[12294]: 12294:M 08 Apr 2020 10:27:21.773 # WARNING: The TCP backlog setting of 511 cannot be enforced because /proc/sys/net/core/somaxconn is set...value of 128.
4月 08 10:27:21 yichen-redis systemd[1]: Started Redis persistent key-value database.
4月 08 10:27:21 yichen-redis redis-server[12294]: 12294:M 08 Apr 2020 10:27:21.773 # Server initialized
4月 08 10:27:21 yichen-redis redis-server[12294]: 12294:M 08 Apr 2020 10:27:21.773 # WARNING overcommit_memory is set to 0! Background save may fail under low memory condition. To fix thi... take effect.
4月 08 10:27:21 yichen-redis redis-server[12294]: 12294:M 08 Apr 2020 10:27:21.773 # WARNING you have Transparent Huge Pages (THP) support enabled in your kernel. This will create latency and memory u...
4月 08 10:27:21 yichen-redis redis-server[12294]: 12294:M 08 Apr 2020 10:27:21.773 * DB loaded from disk: 0.000 seconds
4月 08 10:27:21 yichen-redis redis-server[12294]: 12294:M 08 Apr 2020 10:27:21.773 * Ready to accept connections
Hint: Some lines were ellipsized, use -l to show in full.
[root@yichen-redis ~]# echo never >/sys/kernel/mm/transparent_hugepage/enabled
[root@yichen-redis ~]# vim /etc/sysctl.conf
[root@yichen-redis ~]# sysctl -p
vm.overcommit_memory = 1
net.core.somaxconn = 1024
[root@yichen-redis ~]# systemctl restart redis.service
[root@yichen-redis ~]# systemctl status redis.service
● redis.service - Redis persistent key-value database
Loaded: loaded (/usr/lib/systemd/system/redis.service; disabled; vendor preset: disabled)
Active: active (running) since 三 2020-04-08 10:31:46 CST; 2s ago
Process: 12403 ExecStop=/usr/bin/pkill redis-server (code=exited, status=0/SUCCESS)
Main PID: 12406 (redis-server)
CGroup: /system.slice/redis.service
└─12406 /usr/local/redis/bin/redis-server 127.0.0.1:6379
4月 08 10:31:46 yichen-redis redis-server[12406]: `-._ `-._`-.__.-'_.-' _.-'
4月 08 10:31:46 yichen-redis redis-server[12406]: |`-._`-._ `-.__.-' _.-'_.-'|
4月 08 10:31:46 yichen-redis redis-server[12406]: | `-._`-._ _.-'_.-' |
4月 08 10:31:46 yichen-redis redis-server[12406]: `-._ `-._`-.__.-'_.-' _.-'
4月 08 10:31:46 yichen-redis redis-server[12406]: `-._ `-.__.-' _.-'
4月 08 10:31:46 yichen-redis redis-server[12406]: `-._ _.-'
4月 08 10:31:46 yichen-redis redis-server[12406]: `-.__.-'
4月 08 10:31:46 yichen-redis redis-server[12406]: 12406:M 08 Apr 2020 10:31:46.831 # Server initialized
4月 08 10:31:46 yichen-redis redis-server[12406]: 12406:M 08 Apr 2020 10:31:46.834 * DB loaded from disk: 0.000 seconds
4月 08 10:31:46 yichen-redis redis-server[12406]: 12406:M 08 Apr 2020 10:31:46.834 * Ready to accept connections
3.参数优化
bind 127.0.0.1 192.168.150.50 #监听的主机地址
port 6379 #端口
daemonize yes #启用守护进程
pidfile /var/run/redis_6379.pid #指定PID文件
loglevel notice #日志级别
logfile "/var/log/redis_6379.log" #指定日志文件
dir /etc/redis #指定本地数据库存放目录
dbfilename dump.rdb #指定本地数据库文件名吗,默认值为dump.rdb
activerehashing yes #指定是否激活重置哈希,默认是开启
include /path/to/local.conf #指定包含其他的配置文件,可以在统一主机上实现说个redis实例之间使用同一份配置文件,儿童诗各个实力有拥有自己的特定的配置文件
rdbcompression yes #指定储存至本地数据库时是否启用压缩,默认开启
#RDB 持久化
save 900 1 #在900秒(15分钟)之后,如果至少有1个key发生变化,则dump内存快照。
save 300 10 #在300秒(5分钟)之后,如果至少有10个key发生变化,则dump内存快照。
save 60 10000 #在60秒(1分钟)之后,如果至少有10000个key发生变化,则dump内存快照。
#AOF持久化配置
appendonly yes #开启AOF持久化,默认为no
appendfilename "appendonly.aof" #指定更新日志文件名
appendfsync always #每次有数据修改发生时都会写入AOF文件
#appendfsync everysec #每秒钟同步一次,该策略为AOF的缺省策略
#appendfsync no #从不同步。高效但是数据不会被持久化。
[root@yichen-redis ~]# ps aux | grep redis
redis 7323 0.7 0.9 167204 18820 ? Ssl 19:37 0:04 /usr/local/redis/bin/redis-server 127.0.0.1:6379
root 7352 0.0 0.0 112724 988 pts/0 S+ 19:47 0:00 grep --color=auto redis
[root@yichen-redis ~]# mkdir /path/to/ -p
[root@yichen-redis ~]# cd /path/to/
[root@yichen-redis to]# touch local.conf
[root@yichen-redis ~]# setfacl -m u:redis:rwx /path/to/
[root@yichen-redis ~]# getfacl /path/to/
getfacl: Removing leading '/' from absolute path names
# file: path/to/
# owner: root
# group: root
user::rwx
user:redis:rwx
group::r-x
mask::rwx
other::r-x
[root@yichen-redis ~]# setfacl -m u:redis:rwx /var/log/
[root@yichen-redis ~]# getfacl /var/log/
getfacl: Removing leading '/' from absolute path names
# file: var/log/
# owner: root
# group: root
user::rwx
user:redis:rwx
group::r-x
mask::rwx
other::r-x
[root@yichen-redis ~]# systemctl restart redis.service
[root@yichen-redis ~]# systemctl status redis.service
● redis.service - Redis persistent key-value database
Loaded: loaded (/usr/lib/systemd/system/redis.service; disabled; vendor preset: disabled)
Active: active (running) since 三 2020-04-08 19:37:09 CST; 17s ago
Process: 7281 ExecStop=/usr/bin/pkill redis-server (code=exited, status=1/FAILURE)
Main PID: 7323 (redis-server)
CGroup: /system.slice/redis.service
└─7323 /usr/local/redis/bin/redis-server 127.0.0.1:6379
4月 08 19:37:09 yichen-redis systemd[1]: Starting Redis persistent key-value .....
4月 08 19:37:09 yichen-redis systemd[1]: Started Redis persistent key-value d...e.
Hint: Some lines were ellipsized, use -l to show in full.
4.工具使用
命令行工具
[root@yichen-redis ~]# redis-cli
127.0.0.1:6379> ping
PONG
127.0.0.1:6379>
[root@yichen-redis ~]# redis-cli -h 192.168.150.50 -p 6379
192.168.150.50:6379> ping
PONG
192.168.150.50:6379> info
# Server
redis_version:5.0.7
redis_git_sha1:00000000
redis_git_dirty:0
redis_build_id:188f4d89d2b2565a
redis_mode:standalone
os:Linux 3.10.0-957.el7.x86_64 x86_64
migrate_cached_sockets:0
slave_expires_tracked_keys:0
active_defrag_hits:0
active_defrag_misses:0
active_defrag_key_hits:0
active_defrag_key_misses:0
# Replication
role:master
connected_slaves:0
master_replid:050c1c0e48045209c98a778104f71cec1a6220c2
master_replid2:0000000000000000000000000000000000000000
master_repl_offset:0
second_repl_offset:-1
repl_backlog_active:0
repl_backlog_size:1048576
repl_backlog_first_byte_offset:0
repl_backlog_histlen:0
# CPU
used_cpu_sys:0.255360
used_cpu_user:0.108994
used_cpu_sys_children:0.000000
used_cpu_user_children:0.000000
# Cluster
cluster_enabled:0
# Keyspace
192.168.150.50:6379>
[root@yichen-redis ~]# redis-benchmark -h 192.168.150.50 -p 6379 -c 100 -n 10000
====== PING_INLINE ======
10000 requests completed in 0.21 seconds
100 parallel clients
3 bytes payload
keep alive: 1
12285.01 requests per second
====== MSET (10 keys) ======
10000 requests completed in 0.18 seconds
100 parallel clients
3 bytes payload
keep alive: 1
77.07% <= 1 milliseconds
86.82% <= 2 milliseconds
90.56% <= 3 milliseconds
94.69% <= 4 milliseconds
96.03% <= 5 milliseconds
99.57% <= 6 milliseconds
99.93% <= 7 milliseconds
100.00% <= 7 milliseconds
55555.55 requests per second