Redis的简介和安装
Redis简介
什么是Redis?
Redis(Remote Dictionary Server)在2009年发布,开发者Salvatore Sanfilippo是意大利开发者。redis是一个开源的、遵循BSD协议的、基于内存的而且目前比较流行的键值数据库(key-value database),是一个非关系型数据库,redis提供将内存通过网络远程共享的一种服务,提供类似功能的还有memcache,但相比memcache,redis还提供了易扩展、高性能、具备数据持久性等功能。
- Redis (Remote Dictionary Server)远程字典服务器,是用C语言开发的一个开源的高性能键值对( key-value )内存数据库。
- Redis提供了多数据类型来存储值:如字符串类型、散列类型、列表类型、集合类型、有序集合类型等。
- Redis是一种 NoSQL 数据存储。
存储系统
Redis发展历史
-
2008年,意大利的一家创业公司 Merzia 推出了一款基于 MySQL 的网站实时统计系统 LLOOGG ,然而没过多久该公司的创始人 Salvatore Sanfilippo ( antirez)便 对MySQL的性能感到失望,于是他决定亲自为 LLOOGG 量身定做一个数据库,并于2009年开发完成,这个数据库就是Redis。
-
Redis2.6
Redis2.6在2012年正式发布,主要特性如下:
服务端支持Lua脚本、去掉虚拟内存相关功能、键的过期时间支持毫秒、从节点提供只读功能、两个新的位图命令:bitcount和bitop、重构了大量的核心代码、优化了大量的命令。 -
Redis2.8
Redis2.8在2013年11月22日正式发布,主要特性如下:
添加部分主从复制(增量复制)的功能、可以用bind命令绑定多个IP地址、Redis设置了明显的进程名、发布订阅添加了pubsub命令、Redis Sentinel生产可用。 -
Redis3.0
Redis3.0在2015年4月1日正式发布,相比于Redis2.8主要特性如下:
Redis Cluster:Redis的官方分布式实现(Ruby)、全新的对象编码结果、lru算法大幅提升、部分命令的性能提升 -
Redis3.2
Redis3.2在2016年5月6日正式发布,相比于Redis3.0主要特征如下:
添加GEO相关功能、SDS在速度和节省空间上都做了优化、新的List编码类型:quicklist、从节点读取过期数据保证一致性、Lua脚本功能增强等。 -
Redis4.0
Redis4.0在2017年7月发布,主要特性如下:
提供了模块系统,方便第三方开发者拓展Redis的功能、PSYNC2.0:优化了之前版本中,主从节点切换必然引起全量复制的问题、提供了新的缓存剔除算法:LFU(Last Frequently Used),并对已有算法进行了优化、提供了RDB-AOF混合持久化格式等。
Redis应用场景
- 缓存使用,减轻DB压力
- DB使用,用于临时存储数据(字典表,购买记录)
- 解决分布式场景下Session分离问题(登录信息)
- 任务队列(秒杀、抢红包等等) 乐观锁
- 应用排行榜 zset
- 签到 bitmap
- 分布式锁
- 冷热数据交换
Redis安装
官网地址:https://redis.io/
中文官网地址:http://www.redis.cn/
//下载依赖包和编译工具
[root@localhost ~]# yum -y install systemd-devel gcc gcc-c++ tcl-devel make wget
//下载Redis安装包
[root@localhost ~]# cd /usr/src/
[root@localhost src]# wget https://download.redis.io/releases/redis-6.2.6.tar.gz
--2021-12-13 11:41:34-- https://download.redis.io/releases/redis-6.2.6.tar.gz
正在解析主机 download.redis.io (download.redis.io)... 45.60.125.1
正在连接 download.redis.io (download.redis.io)|45.60.125.1|:443... 已连接。
已发出 HTTP 请求,正在等待回应... 200 OK
长度:2476542 (2.4M) [application/octet-stream]
正在保存至: “redis-6.2.6.tar.gz”
redis-6.2.6.ta 100% 2.36M 13.4KB/s 用时 78s
2021-12-13 11:42:53 (31.2 KB/s) - 已保存 “redis-6.2.6.tar.gz” [2476542/2476542])
// 解压
[root@localhost src]# ls
debug kernels redis-6.2.6.tar.gz
[root@localhost src]# tar xf redis-6.2.6.tar.gz
[root@localhost src]# ls
debug kernels redis-6.2.6 redis-6.2.6.tar.gz
// 编译安装
[root@localhost src]# cd redis-6.2.6/
[root@localhost redis-6.2.6]# make MALLOC=libc USE_SYSTEMD=yes && \
> make install PREFIX=/usr/local/redis
[root@localhost ~]# cd /usr/local/
[root@localhost local]# ls
bin games lib libexec sbin src
etc include lib64 redis share
[root@localhost local]# cd redis/
[root@localhost redis]# ls
bin
[root@localhost redis]# cd bin/
[root@localhost bin]# ./redis-server
13889:C 13 Dec 2021 11:49:37.896 # oO0OoO0OoO0Oo Redis is starting oO0OoO0OoO0Oo
13889:C 13 Dec 2021 11:49:37.896 # Redis version=6.2.6, bits=64, commit=00000000, modified=0, pid=13889, just started
13889:C 13 Dec 2021 11:49:37.896 # Warning: no config file specified, using the default config. In order to specify a config file use ./redis-server /path/to/redis.conf
13889:M 13 Dec 2021 11:49:37.897 * Increased maximum number of open files to 10032 (it was originally set to 1024).
13889:M 13 Dec 2021 11:49:37.897 * monotonic clock: POSIX clock_gettime
_._
_.-``__ ''-._
_.-`` `. `_. ''-._ Redis 6.2.6 (00000000/0) 64 bit
.-`` .-```. ```\/ _.,_ ''-._
( ' , .-` | `, ) Running in standalone mode
|`-._`-...-` __...-.``-._|'` _.-'| Port: 6379
| `-._ `._ / _.-' | PID: 13889
`-._ `-._ `-./ _.-' _.-'
|`-._`-._ `-.__.-' _.-'_.-'|
| `-._`-._ _.-'_.-' | https://redis.io
`-._ `-._`-.__.-'_.-' _.-'
|`-._`-._ `-.__.-' _.-'_.-'|
| `-._`-._ _.-'_.-' |
`-._ `-._`-.__.-'_.-' _.-'
`-._ `-.__.-' _.-'
`-._ _.-'
`-.__.-'
13889:M 13 Dec 2021 11:49:37.898 # WARNING: The TCP backlog setting of 511 cannot be enforced because /proc/sys/net/core/somaxconn is set to the lower value of 128.
13889:M 13 Dec 2021 11:49:37.898 # Server initialized
13889:M 13 Dec 2021 11:49:37.898 # 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.
13889:M 13 Dec 2021 11:49:37.898 * Ready to accept connections
//因为是前台运行,所以要再打开一个终端查看6379端口
[root@localhost ~]# ss -anlt
State Recv-Q Send-Q Local Address:Port Peer Address:Port
LISTEN 0 128 0.0.0.0:22 0.0.0.0:*
LISTEN 0 128 0.0.0.0:6379 0.0.0.0:*
LISTEN 0 128 [::]:22 [::]:*
LISTEN 0 128 [::]:6379 [::]:*
//启动Redis时显示警告信息,设置取消
[root@localhost ~]# echo 511 > /proc/sys/net/core/somaxconn
[root@localhost ~]# echo vm.overcommit_memory = 1 >> /etc/sysctl.conf
[root@localhost ~]# sysctl -p
vm.overcommit_memory = 1
//ctrl+c 终止Redis,重新启动,警告信息已经消失了
[root@localhost bin]# ./redis-server
13933:C 13 Dec 2021 11:54:09.112 # oO0OoO0OoO0Oo Redis is starting oO0OoO0OoO0Oo
13933:C 13 Dec 2021 11:54:09.112 # Redis version=6.2.6, bits=64, commit=00000000, modified=0, pid=13933, just started
13933:C 13 Dec 2021 11:54:09.112 # Warning: no config file specified, using the default config. In order to specify a config file use ./redis-server /path/to/redis.conf
13933:M 13 Dec 2021 11:54:09.113 * Increased maximum number of open files to 10032 (it was originally set to 1024).
13933:M 13 Dec 2021 11:54:09.113 * monotonic clock: POSIX clock_gettime
_._
_.-``__ ''-._
_.-`` `. `_. ''-._ Redis 6.2.6 (00000000/0) 64 bit
.-`` .-```. ```\/ _.,_ ''-._
( ' , .-` | `, ) Running in standalone mode
|`-._`-...-` __...-.``-._|'` _.-'| Port: 6379
| `-._ `._ / _.-' | PID: 13933
`-._ `-._ `-./ _.-' _.-'
|`-._`-._ `-.__.-' _.-'_.-'|
| `-._`-._ _.-'_.-' | https://redis.io
`-._ `-._`-.__.-'_.-' _.-'
|`-._`-._ `-.__.-' _.-'_.-'|
| `-._`-._ _.-'_.-' |
`-._ `-._`-.__.-'_.-' _.-'
`-._ `-.__.-' _.-'
`-._ _.-'
`-.__.-'
13933:M 13 Dec 2021 11:54:09.113 # Server initialized
13933:M 13 Dec 2021 11:54:09.114 * Loading RDB produced by version 6.2.6
13933:M 13 Dec 2021 11:54:09.114 * RDB age 5 seconds
13933:M 13 Dec 2021 11:54:09.114 * RDB memory usage when created 0.84 Mb
13933:M 13 Dec 2021 11:54:09.114 # Done loading RDB, keys loaded: 0, keys expired: 0.
13933:M 13 Dec 2021 11:54:09.114 * DB loaded from disk: 0.000 seconds
13933:M 13 Dec 2021 11:54:09.114 * Ready to accept connections
//使用service控制Redis
[root@localhost ~]# cd /usr/src/
[root@localhost src]# ls
debug kernels redis-6.2.6 redis-6.2.6.tar.gz
[root@localhost src]# cd redis-6.2.6
[root@localhost redis-6.2.6]# ls
00-RELEASENOTES INSTALL runtest tests
BUGS make runtest-cluster TLS.md
CONDUCT Makefile runtest-moduleapi utils
CONTRIBUTING MANIFESTO runtest-sentinel
COPYING README.md sentinel.conf
deps redis.conf src
[root@localhost redis-6.2.6]# mkdir -p /usr/local/redis/conf
[root@localhost redis-6.2.6]# cp redis.conf /usr/local/redis/conf/
[root@localhost ~]# cat /usr/lib/systemd/system/redis_server.service
[Unit]
Description=Redis data structure server
Documentation=https://redis.io/documentation
Wants=network-online.target
After=network-online.target
[Service]
ExecStart=/usr/local/redis/bin/redis-server /usr/local/redis/conf/redis.conf --supervised systemd
Type=notify
[Install]
WantedBy=multi-user.target
[root@localhost ~]# pkill redis-server
[root@localhost ~]# systemctl daemon-reload
[root@localhost ~]# systemctl enable --now redis_server.service
Created symlink /etc/systemd/system/multi-user.target.wants/redis_server.service → /usr/lib/systemd/system/redis_server.service.
[root@localhost ~]# ss -anlt
State Recv-Q Send-Q Local Address:Port Peer Address:Port
LISTEN 0 128 0.0.0.0:22 0.0.0.0:*
LISTEN 0 511 127.0.0.1:6379 0.0.0.0:*
LISTEN 0 128 [::]:22 [::]:*
LISTEN 0 511 [::1]:6379 [::]:*
//给redis设置一个密码
[root@localhost ~]# vim /usr/local/redis/conf/redis.conf
901 requirepass 123
[root@localhost ~]# systemctl restart redis_server.service
[root@localhost ~]# cd /usr/local/redis/
[root@localhost redis]# ls
bin conf
[root@localhost redis]# cd bin/
[root@localhost bin]# ls
dump.rdb redis-check-aof redis-cli redis-server
redis-benchmark redis-check-rdb redis-sentinel
[root@localhost ~]# cd /usr/local/redis/bin/
[root@localhost bin]#
[root@localhost bin]#
[root@localhost bin]# ./redis-cli
127.0.0.1:6379> ping
(error) NOAUTH Authentication required.
127.0.0.1:6379> exit
[root@localhost bin]# ./redis-cli -a 123
Warning: Using a password with '-a' or '-u' option on the command line interface may not be safe.
127.0.0.1:6379> ping
PONG