Redis介绍
NoSQL的引言
NoSQL( Not Only SQL
),意即不仅仅是SQL, 泛指非关系型的数据库。Nosql这个技术门类,早期就有人提出,发展至2009年趋势越发高涨。
RDBMS – 关系型数据库,SQL语句
为什么是NoSQL
随着互联网网站的兴起,传统的关系数据库在应付动态网站,特别是超大规模和高并发的纯动态网站已经显得力不从心,暴露了很多难以克服的问题。如商城网站中对商品数据频繁查询
、对热搜商品的排行统计
、订单超时问题
、以及微信朋友圈(音频,视频)存储等相关使用传统的关系型数据库实现就显得非常复杂,虽然能实现相应功能但是在性能上却不是那么乐观。nosql这个技术门类的出现,更好的解决了这些问题,它告诉了世界不仅仅是sql。
NoSQL四大分类
1. 键值(Key-Value)存储数据库
- 说明
- 一类数据库主要会使用到一个哈希表,这个表中有一个特定的键和一个指针指向特定的数据。
- 特点
- Key/value模型对于IT系统来说的优势在于简单、易部署。
- 但是如果DBA只对部分值进行查询或更新的时候,Key/value就显得效率低下了。
- 相关产品
- Tokyo Cabinet/Tyrant
- Redis – 数据在内存
- SSDB – 数据在硬盘
- Voldemort
- Oracle BDB
2. 列存储数据库
- 说明
- 这部分数据库通常是用来应对分布式存储的海量数据。
- 特点
- 键仍然存在,但是它们的特点是指向了多个列。这些列是由列家族来安排的。
- 相关产品
- Cassandra
- HBase
- Riak
3. 文档型数据库
- 说明
- 文档型数据库的灵感是来自于Lotus Notes办公软件的,而且它同第一种键值存储相类似该类型的数据模型是版本化的文档,半结构化的文档以特定的格式存储,比如JSON。文档型数据库可 以看作是键值数据库的升级版,允许之间嵌套键值。而且文档型数据库比键值数据库的查询效率更高
- 特点
- 以文档形式存储
- 相关产品
- MongoDB
- CouchDB
- MongoDb(4.x).
- 国内也有文档型数据库SequoiaDB,已经开源
4. 图形(Graph)数据库
- 说明
- 图形结构的数据库同其他行列以及刚性结构的SQL数据库不同,它是使用灵活的图形模型,并且能够扩展到多个服务器上。
- NoSQL数据库没有标准的查询语言(SQL),因此进行数据库查询需要制定数据模型。许多NoSQL数据库都有REST式的数据接口或者查询API。
- 特点
- NoSQL数据库没有标准的查询语句(SQL),因此进行数据库查询需要定制数据模型。许多NoSQL数据库都有REST式的数据接口或者查询API。
- 相关产品
- Neo4J
- InfoGrid
- Infinite Graph
NoSQL应用场景
- 数据模型比较简单
- 需要灵活性更强的IT系统(系统设计灵活,性能要求比较高)
- 对数据库性能要求较高
- 不需要高度的数据一致性(Nosql产品对于事务支持都不是特别良好)
Redis
Redis官方网站:https://redis.io
什么是Redis
Redis is an open source (BSD licensed), in-memory data structure store, used as a database, cache and message broker.
Redis 开源 遵循BSD 基于内存数据存储 被用于作为 数据库 缓存 消息中间件
Redis 数据存储在内存中,读写快,断电即消失(未进行数据持久化),机制:持久化机制,内存数据,定期写入到磁盘中
- 总结:redis是一个内存型的数据库
- 应用:缓存、消息中间件等
Redis特点
- Redis是一个高性能key/value内存型数据库
- Redis支持丰富的数据类型(String、List、Set、ZSet、Hash)
- Redis支持持久化 (内存数据持久化到硬盘中)
- Redis单线程,单进程(不支持并发,不存在线程安全的问题,Redis实现分布式锁)
Redis安装
准备环境
- CentOS 7.x+
下载redis源码包
拷贝下载连接
Linux系统下,下载完整源码包
1
2
3
4
5
6
7
8
9
10
11[root@localhost ~]# wget https://download.redis.io/releases/redis-6.2.1.tar.gz?_ga=2.236241992.874767586.1616555487-1821064826.1616555487
--2021-03-24 11:24:54-- https://download.redis.io/releases/redis-6.2.1.tar.gz?_ga=2.236241992.874767586.1616555487-1821064826.1616555487
正在解析主机 download.redis.io (download.redis.io)... 45.60.125.1
正在连接 download.redis.io (download.redis.io)|45.60.125.1|:443... 已连接。
已发出 HTTP 请求,正在等待回应... 200 OK
长度:2438367 (2.3M) [application/octet-stream]
正在保存至: “redis-6.2.1.tar.gz?_ga=2.236241992.874767586.1616555487-1821064826.1616555487”
100%[=====================================================================>] 2,438,367 2.02MB/s 用时 1.2s
2021-03-24 11:24:56 (2.02 MB/s) - 已保存 “redis-6.2.1.tar.gz?_ga=2.236241992.874767586.1616555487-1821064826.1616555487” [2438367/2438367])
解压缩文件
1
2
3
4
5
6
7
8
9
10
11
12
13[root@localhost ~]# ls
redis-6.2.1.tar.gz?_ga=2.236241992.874767586.1616555487-1821064826.1616555487
[root@localhost ~]# tar -zxvf redis-6.2.1.tar.gz\?_ga\=2.236241992.874767586.1616555487-1821064826.1616555487
redis-6.2.1/
redis-6.2.1/.github/
redis-6.2.1/.github/ISSUE_TEMPLATE/
redis-6.2.1/.github/ISSUE_TEMPLATE/bug_report.md
redis-6.2.1/.github/ISSUE_TEMPLATE/crash_report.md
redis-6.2.1/.github/ISSUE_TEMPLATE/feature_request.md
……
[root@localhost ~]# ls
redis-6.2.1.tar.gz?_ga=2.236241992.874767586.1616555487-1821064826.1616555487
redis-6.2.1
安装gcc
1
2
3
4
5
6
7[root@localhost ~]# yum install -y gcc
已加载插件:fastestmirror
Determining fastest mirrors
* base: mirrors.huaweicloud.com
* extras: mirrors.huaweicloud.com
* updates: mirrors.huaweicloud.com
……
进入解压缩目录执行如下命令进行编译
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15[root@localhost ~]# cd redis-6.2.1
[root@localhost redis-6.2.1]# make MALLOC=libc
cd src && make all
make[1]: 进入目录“/root/redis-6.2.1/src”
CC Makefile.dep
make[1]: 离开目录“/root/redis-6.2.1/src”
make[1]: 进入目录“/root/redis-6.2.1/src”
rm -rf redis-server redis-sentinel redis-cli redis-benchmark redis-check-rdb redis-check-aof *.o *.gcda *.gcno *.gcov redis.info lcov-html Makefile.dep dict-benchmark
……
INSTALL redis-check-rdb
INSTALL redis-check-aof
Hint: It's a good idea to run 'make test' ;)
make[1]: 离开目录“/root/redis-6.2.1/src”
编译完成后执行如下命令
1
2
3
4
5
6
7
8
9
10
11
12
13[root@localhost redis-6.2.1]# make install PREFIX=/usr/local/redis
cd src && make install
make[1]: 进入目录“/root/redis-6.2.1/src”
CC Makefile.dep
make[1]: 离开目录“/root/redis-6.2.1/src”
make[1]: 进入目录“/root/redis-6.2.1/src”
Hint: It's a good idea to run 'make test' ;)
INSTALL install
INSTALL install
INSTALL install
make[1]: 离开目录“/root/redis-6.2.1/src”
进入/usr/local/redis目录启动redis服务
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35[root@localhost redis-6.2.1]# cd /usr/local/redis/
[root@localhost redis]# ls
bin
[root@localhost redis]# cd bin/
[root@localhost bin]# ls
redis-benchmark redis-check-aof redis-check-rdb redis-cli redis-sentinel redis-server
[root@localhost bin]# ./redis-server
5715:C 24 Mar 2021 11:59:45.568 # oO0OoO0OoO0Oo Redis is starting oO0OoO0OoO0Oo
5715:C 24 Mar 2021 11:59:45.568 # Redis version=6.2.1, bits=64, commit=00000000, modified=0, pid=5715, just started
5715:C 24 Mar 2021 11:59:45.568 # Warning: no config file specified, using the default config. In order to specify a config file use ./redis-server /path/to/redis.conf
5715:M 24 Mar 2021 11:59:45.570 * Increased maximum number of open files to 10032 (it was originally set to 1024).
5715:M 24 Mar 2021 11:59:45.570 * monotonic clock: POSIX clock_gettime
_._
_.-``__ ''-._
_.-`` `. `_. ''-._ Redis 6.2.1 (00000000/0) 64 bit
.-`` .-```. ```\/ _.,_ ''-._
( ' , .-` | `, ) Running in standalone mode
|`-._`-...-` __...-.``-._|'` _.-'| Port: 6379
| `-._ `._ / _.-' | PID: 5715
`-._ `-._ `-./ _.-' _.-'
|`-._`-._ `-.__.-' _.-'_.-'|
| `-._`-._ _.-'_.-' | http://redis.io
`-._ `-._`-.__.-'_.-' _.-'
|`-._`-._ `-.__.-' _.-'_.-'|
| `-._`-._ _.-'_.-' |
`-._ `-._`-.__.-'_.-' _.-'
`-._ `-.__.-' _.-'
`-._ _.-'
`-.__.-'
5715:M 24 Mar 2021 11:59:45.571 # WARNING: The TCP backlog setting of 511 cannot be enforced because /proc/sys/net/core/somaxconn is set to the lower value of 128.
5715:M 24 Mar 2021 11:59:45.571 # Server initialized
5715:M 24 Mar 2021 11:59:45.571 # 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.
5715:M 24 Mar 2021 11:59:45.572 * Ready to accept connections启动界面,Redis服务端口默认是 6379
进入bin目录执行客户端连接操作,打开一个新的iTherm,使用客户端连接Redis
1
2
3
4
5
6
7
8
9[root@localhost ~]# cd /usr/local/redis/
[root@localhost redis]# ls
bin
[root@localhost redis]# cd bin/
[root@localhost bin]# ls
redis-benchmark redis-check-rdb redis-sentinel
redis-check-aof redis-cli redis-server
[root@localhost bin]# ./redis-cli -h localhost -p 6379
localhost:6379>连接成功出现上面界面连接成功
Redis配置
redis启动服务的细节
- 注意:直接使用./redis-server方式使用的是redis-server这个shell脚本中默认配置
如何在启动redis时指定配置文件启动
注意:默认在redis安装完成之后再安装目录没有任何配置文件,需要在源码目录中复制redis.conf配置文件到安装目录
1
2
3
4[root@localhost redis-6.2.1]# cp redis.conf /usr/local/redis/
[root@localhost redis-6.2.1]# cd /usr/local/redis/
[root@localhost redis]# ls
bin redis.conf将配置文件复制到redis目录下后,加载配置启动
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34[root@localhost redis]# cd bin/
[root@localhost bin]# ./redis-server ../redis.conf
4322:C 25 Mar 2021 12:32:08.516 # oO0OoO0OoO0Oo Redis is starting oO0OoO0OoO0Oo
4322:C 25 Mar 2021 12:32:08.516 # Redis version=6.2.1, bits=64, commit=00000000, modified=0, pid=4322, just started
4322:C 25 Mar 2021 12:32:08.516 # Configuration loaded
4322:M 25 Mar 2021 12:32:08.518 * Increased maximum number of open files to 10032 (it was originally set to 1024).
4322:M 25 Mar 2021 12:32:08.518 * monotonic clock: POSIX clock_gettime
_._
_.-``__ ''-._
_.-`` `. `_. ''-._ Redis 6.2.1 (00000000/0) 64 bit
.-`` .-```. ```\/ _.,_ ''-._
( ' , .-` | `, ) Running in standalone mode
|`-._`-...-` __...-.``-._|'` _.-'| Port: 6379
| `-._ `._ / _.-' | PID: 4322
`-._ `-._ `-./ _.-' _.-'
|`-._`-._ `-.__.-' _.-'_.-'|
| `-._`-._ _.-'_.-' | http://redis.io
`-._ `-._`-.__.-'_.-' _.-'
|`-._`-._ `-.__.-' _.-'_.-'|
| `-._`-._ _.-'_.-' |
`-._ `-._`-.__.-'_.-' _.-'
`-._ `-.__.-' _.-'
`-._ _.-'
`-.__.-'
4322:M 25 Mar 2021 12:32:08.520 # WARNING: The TCP backlog setting of 511 cannot be enforced because /proc/sys/net/core/somaxconn is set to the lower value of 128.
4322:M 25 Mar 2021 12:32:08.520 # Server initialized
4322:M 25 Mar 2021 12:32:08.520 # 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.
4322:M 25 Mar 2021 12:32:08.520 * Loading RDB produced by version 6.2.1
4322:M 25 Mar 2021 12:32:08.520 * RDB age 61 seconds
4322:M 25 Mar 2021 12:32:08.520 * RDB memory usage when created 0.84 Mb
4322:M 25 Mar 2021 12:32:08.520 * DB loaded from disk: 0.000 seconds
4322:M 25 Mar 2021 12:32:08.520 * Ready to accept connections
修改配置文件中的配置,例如:默认端口号,再指定配置文件重新启动redis
Redis可视化工具
工具:redis-desktop-manager,该工具收费
官方网站:https://rdm.dev
MacOS
在App Store中搜索,需要人民币98块
注意:远程连接需要开启Redis的远程连接,默认Redis服务器是没有开启远程连接的,也就是默认拒绝所有远程客户端连接
修改配置开启远程连接
1
vim redis.conf
修改如下配置
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31################################## NETWORK #####################################
# By default, if no "bind" configuration directive is specified, Redis listens
# for connections from all available network interfaces on the host machine.
# It is possible to listen to just one or multiple selected interfaces using
# the "bind" configuration directive, followed by one or more IP addresses.
# Each address can be prefixed by "-", which means that redis will not fail to
# start if the address is not available. Being not available only refers to
# addresses that does not correspond to any network interfece. Addresses that
# are already in use will always fail, and unsupported protocols will always BE
# silently skipped.
#
# Examples:
#
# bind 192.168.1.100 10.0.0.1 # listens on two specific IPv4 addresses
# bind 127.0.0.1 ::1 # listens on loopback IPv4 and IPv6
# bind * -::* # like the default, all available interfaces
#
# ~~~ WARNING ~~~ If the computer running Redis is directly exposed to the
# internet, binding to all the interfaces is dangerous and will expose the
# instance to everybody on the internet. So by default we uncomment the
# following bind directive, that will force Redis to listen only on the
# IPv4 and IPv6 (if available) loopback interface addresses (this means Redis
# will only be able to accept client connections from the same host that it is
# running on).
#
# IF YOU ARE SURE YOU WANT YOUR INSTANCE TO LISTEN TO ALL THE INTERFACES
# JUST COMMENT OUT THE FOLLOWING LINE.
# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
# bind 127.0.0.1 -::1
bind 0.0.0.0将配置文件中的bind 127.0.0.1 -::1修改为bind 0.0.0.0(允许一切客户端连接)
关闭防火墙
查看防火墙状态
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22[root@localhost ~]# systemctl status firewalld
● firewalld.service - firewalld - dynamic firewall daemon
Loaded: loaded (/usr/lib/systemd/system/firewalld.service; enabled; vendor preset: enabled)
Active: active (running) since 四 2021-03-25 12:22:30 CST; 4h 39min ago
Docs: man:firewalld(1)
Main PID: 2697 (firewalld)
Tasks: 2
Memory: 31.3M
CGroup: /system.slice/firewalld.service
└─2697 /usr/bin/python -Es /usr/sbin/firewalld --nofork --nopid
3月 25 12:22:34 localhost.localdomain firewalld[2697]: WARNING: COMMAND_FAILED: '/usr/sbin/iptables -w2 -t ...me.
3月 25 12:22:34 localhost.localdomain firewalld[2697]: WARNING: COMMAND_FAILED: '/usr/sbin/iptables -w2 -t ...?).
3月 25 12:22:34 localhost.localdomain firewalld[2697]: WARNING: COMMAND_FAILED: '/usr/sbin/iptables -w2 -t ...me.
3月 25 12:22:34 localhost.localdomain firewalld[2697]: WARNING: COMMAND_FAILED: '/usr/sbin/iptables -w2 -t ...?).
3月 25 12:22:34 localhost.localdomain firewalld[2697]: WARNING: COMMAND_FAILED: '/usr/sbin/iptables -w2 -t ...me.
3月 25 12:22:34 localhost.localdomain firewalld[2697]: WARNING: COMMAND_FAILED: '/usr/sbin/iptables -w2 -t ...?).
3月 25 12:22:34 localhost.localdomain firewalld[2697]: WARNING: COMMAND_FAILED: '/usr/sbin/iptables -w2 -t ...me.
3月 25 12:22:34 localhost.localdomain firewalld[2697]: WARNING: COMMAND_FAILED: '/usr/sbin/iptables -w2 -t ...me.
3月 25 12:22:34 localhost.localdomain firewalld[2697]: WARNING: COMMAND_FAILED: '/usr/sbin/iptables -w2 -t ...?).
3月 25 12:22:34 localhost.localdomain firewalld[2697]: WARNING: COMMAND_FAILED: '/usr/sbin/iptables -w2 -t ...me.
Hint: Some lines were ellipsized, use -l to show in full.active (running) :表示防火墙开启
关闭防火墙
1
[root@localhost ~]# systemctl stop firewalld.service
再次查看防火墙状态
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19[root@localhost ~]# systemctl status firewalld
● firewalld.service - firewalld - dynamic firewall daemon
Loaded: loaded (/usr/lib/systemd/system/firewalld.service; enabled; vendor preset: enabled)
Active: inactive (dead) since 四 2021-03-25 17:02:30 CST; 1s ago
Docs: man:firewalld(1)
Process: 2697 ExecStart=/usr/sbin/firewalld --nofork --nopid $FIREWALLD_ARGS (code=exited, status=0/SUCCESS)
Main PID: 2697 (code=exited, status=0/SUCCESS)
3月 25 12:22:34 localhost.localdomain firewalld[2697]: WARNING: COMMAND_FAILED: '/usr/sbin/iptables -w2 -t filter -C DOCKER-IS...name.
3月 25 12:22:34 localhost.localdomain firewalld[2697]: WARNING: COMMAND_FAILED: '/usr/sbin/iptables -w2 -t filter -C DOCKER-IS...in?).
3月 25 12:22:34 localhost.localdomain firewalld[2697]: WARNING: COMMAND_FAILED: '/usr/sbin/iptables -w2 -t filter -n -L DOCKER...name.
3月 25 12:22:34 localhost.localdomain firewalld[2697]: WARNING: COMMAND_FAILED: '/usr/sbin/iptables -w2 -t filter -C DOCKER-US...in?).
3月 25 12:22:34 localhost.localdomain firewalld[2697]: WARNING: COMMAND_FAILED: '/usr/sbin/iptables -w2 -t filter -C FORWARD -...name.
3月 25 12:22:34 localhost.localdomain firewalld[2697]: WARNING: COMMAND_FAILED: '/usr/sbin/iptables -w2 -t nat -C DOCKER -p tc...name.
3月 25 12:22:34 localhost.localdomain firewalld[2697]: WARNING: COMMAND_FAILED: '/usr/sbin/iptables -w2 -t filter -C DOCKER ! ...in?).
3月 25 12:22:34 localhost.localdomain firewalld[2697]: WARNING: COMMAND_FAILED: '/usr/sbin/iptables -w2 -t nat -C POSTROUTING ...name.
3月 25 17:02:29 localhost.localdomain systemd[1]: Stopping firewalld - dynamic firewall daemon...
3月 25 17:02:30 localhost.localdomain systemd[1]: Stopped firewalld - dynamic firewall daemon.
Hint: Some lines were ellipsized, use -l to show in full.
修改配置之后重启Redis服务,一定要加载配置文件启动
1
[root@localhost bin]# ./redis-server ../redis.conf
使用工具连接
连接成功后