探索企业级数据库新势力:Redis 基础与进阶

目录

一. 关系型数据库和 NoSQL 数据库

1.1. 数据库主要分为两大类:关系型数据库与 NoSQL 数据库

1.2. 为什么还要用 NoSQL 数据库呢?

二. Remote Dictionary Server 简介

2.1. 什么是redis

2.2. Redis特性

2.3. 单线程为何如此快?

2.4. Redis应用场景

三 Redis的安装

四. Redis的基本操作


一. 关系型数据库和 NoSQL 数据库

1.1. 数据库主要分为两大类:关系型数据库与 NoSQL 数据库

关系型数据库,是建立在关系模型基础上的数据库,其借助于集合代数等数学概念和方法来处理数据库中的数据主流的 MySQL、Oracle、MS SQL Server 和 DB2 都属于这类传统数据库。 NoSQL 数据库,全称为 Not Only SQL,意思就是适用关系型数据库的时候就使用关系型数据库,不适用的时候也没有必要非使用关系型数据库不可,可以考虑使用更加合适的数据存储。主要分为临时性键值存储(memcached、Redis)、永久性键值存储(ROMA、Redis)、面向文档的数据库 (MongoDB、CouchDB)、面向列的数据库(Cassandra、HBase),每种 NoSQL 都有其特有的使用场景及优点。

1.2. 为什么还要用 NoSQL 数据库呢?

主要是由于随着互联网发展,数据量越来越大,对性能要求越来越高,传统数据库存在着先天性的缺 陷,即单机(单库)性能瓶颈,并且扩展困难。这样既有单机单库瓶颈,却又扩展困难,自然无法满足日益增长的海量数据存储及其性能要求,所以才会出现了各种不同的 NoSQL 产品,NoSQL 根本性的优势在于在云计算时代,简单、易于大规模分布式扩展,并且读写性能非常高

二. Remote Dictionary Server 简介

中文官网 https://redis.cn

2.1. 什么是redis

Redis (Remote Dictionary Server) 在2009年发布,开发者是意大利的萨尔瓦多·桑菲利波普(Salvatore Sanfilippo),他本想为自己的公司 开发一个用于替换MySQL的产品Redis,但是没有想到他把Redis开源后大受欢迎,短短几年,Redis就有 了很大的用户群体,目前国内外使用的公司众多,比如:阿里,百度,新浪微博,知乎网,GitHub,Twitter 等 Redis是一个开源的、遵循BSD协议的、基于内存的而且目前比较流行的键值数据库(key-value database),是一个非关系型数据库,redis 提供将内存通过网络远程共享的一种服务,提供类似功能的 还有memcached,但相比memcached,redis还提供了易扩展、高性能、具备数据持久性等功能。 Redis 在高并发、低延迟环境要求比较高的环境使用量非常广泛

2.2. Redis特性

速度快: 10W QPS,基于内存,C语言实现 单线程 持久化 支持多种数据结构 支持多种编程语言 功能丰富: 支持Lua脚本,发布订阅,事务,pipeline等功能 简单: 代码短小精悍(单机核心代码只有23000行左右),单线程开发容易,不依赖外部库,使用简单 主从复制 支持高可用和分布式

2.3. 单线程为何如此快?

纯内存 非阻塞 避免线程切换和竞态消耗

2.4. Redis应用场景

Session 共享:常见于web集群中的Tomcat或者PHP中多web服务器session共享 缓存:数据查询、电商网站商品信息、新闻内容 计数器:访问排行榜、商品浏览数等和次数相关的数值统计场景 微博/微信社交场合:共同好友,粉丝数,关注,点赞评论等 消息队列:ELK的日志缓存、部分业务的订阅发布系统 地理位置: 基于GEO(地理信息定位),实现摇一摇,附近的人,外卖等功能

三 Redis的安装

官方下载地址:Index of /releases/

#rpm包方式安装
[root@redis-node1 ~]# dnf install redis -y

#源码安装
#解压源码包
[root@redis-node1 ~]# tar zxf redis-7.4.0.tar.gz
[root@redis-node1 ~]# ls
redis-7.4.0 redis-7.4.0.tar.gz
[root@node1 ~]# cd redis-7.4.0/
#安装编译工具
[root@redis-node1 redis-7.4.0]# dnf install make gcc initscripts -y
#执行编译命令
[root@redis-node1 redis-7.4.0]# make
[root@redis-node1 redis-7.4.0]# make install
#启动Redis
[root@redis-node1 redis-7.4.0]# cd utils/
[root@redis-node1 utils]# ./install_server.sh
Welcome to the redis service installer
This script will help you easily set up a running redis server
This systems seems to use systemd. #提示系统使用的是systemd的初始化方式
Please take a look at the provided example service unit files in this directory,
and adapt and install them. Sorry!
[root@redis-node1 utils]# vim install_server.sh     #解决报错问题
....
#bail if this system is managed by systemd
#_pid_1_exe="$(readlink -f /proc/1/exe)"
#if [ "${_pid_1_exe##*/}" = systemd ]
#then
# echo "This systems seems to use systemd."
# echo "Please take a look at the provided example service unit files in
this directory, and adapt and install them. Sorry!"
# exit 1
#fi
.....
[root@redis-node1 utils]# ./install_server.sh
Welcome to the redis service installer
This script will help you easily set up a running redis server
Please select the redis port for this instance: [6379]     #端口号
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] #命令
#路径
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/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!
Starting Redis server...
Installation successful!

#配置redis
[root@redis-node1 utils]# vim /etc/redis/6379.conf
...
bind * -::*
...
# even if no authentication is configured
protected-mode no
...
[root@redis-node1 utils]# /etc/init.d/redis_6379 restart
Stopping ...
Redis stopped
Starting Redis server...
[root@redis-node1 utils]# netstat -antlpe | grep redis
tcp 0 0 0.0.0.0:6379 0.0.0.0:* LISTEN 0
67267 38417/redis-server
tcp6 0 0 :::6379

#查看信息
[root@redis-node1 utils]# redis-cli
127.0.0.1:6379> info

#其他机子配置
[root@node1 ~]# cd /usr/local/bin                #一定要在对应的目录才行。
[root@node1 bin]# rsync -al * root@172.25.254.20:/usr/local/bin 
[root@node1 ~]# scp -r redis-7.4.0 root@172.25.254.20:/root/    #一定要在对应的目录才行。

[root@node1 bin]# rsync -al * root@172.25.254.30:/usr/local/bin #一定要在对应的目录才行。
[root@node1 ~]# scp -r redis-7.4.0 root@172.25.254.30:/root/    #一定要在对应的目录才行。

#启动Redis
[root@node2 ~]# dnf install initscripts -y
[root@node1 ~]# cd redis-7.4.0/utils/
[root@redis-node1 utils]# ./install_server.sh
Welcome to the redis service installer
This script will help you easily set up a running redis server
Please select the redis port for this instance: [6379]     #端口号
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] #命令
#路径
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/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!
Starting Redis server...
Installation successful!

#配置redis
[root@redis-node1 utils]# vim /etc/redis/6379.conf
...
bind * -::*
...
# even if no authentication is configured
protected-mode no
...
[root@redis-node1 utils]# /etc/init.d/redis_6379 restart
Stopping ...
Redis stopped
Starting Redis server...
[root@redis-node1 utils]# netstat -antlpe | grep redis
tcp 0 0 0.0.0.0:6379 0.0.0.0:* LISTEN 0
67267 38417/redis-server
tcp6 0 0 :::6379

四. Redis的基本操作

config get *查看配置
select 1选择数据库
flushdb清空当前数据库
flushall清空所有数据库
move key 1移动key
del key删除
rename oldkey newkey改名
expire key 10设置过期时间
persist key设置持久化
keys user*查询
exists key判断是否存在
#查看配置
127.0.0.1:6379[1]> CONFIG GET bind
1) "bind"
2) "* -::*"
127.0.0.1:6379[1]> CONFIG GET *

#写入和读取数据
127.0.0.1:6379> SET name lee
OK
127.0.0.1:6379> GET name
"lee"
127.0.0.1:6379> set name lee ex 5
OK
127.0.0.1:6379> get name
"lee"
127.0.0.1:6379> get name
"lee"
127.0.0.1:6379> get name
"lee"
127.0.0.1:6379> get name
"lee"
127.0.0.1:6379> get name
(nil)

#如果没有设定数据过期时间会一直存在, /var/lib/redis/6379/dump.rdb内存快照中
127.0.0.1:6379> set name lee
OK
127.0.0.1:6379> KEYS * #查看所有key
1) "name"
1) "name"

#选择数据库 redisa中有0-15个数据库
127.0.0.1:6379> select 1
OK
127.0.0.1:6379[1]> get name
(nil)
127.0.0.1:6379> select 0
127.0.0.1:6379[1]> select 16
(error) ERR DB index is out of range

#移动数据
127.0.0.1:6379> set name lee
OK
127.0.0.1:6379> MOVE name 1
(integer) 1
127.0.0.1:6379> GET name
(nil)
127.0.0.1:6379> select 1
OK
127.0.0.1:6379[1]> get name
"lee"
#改变键名
127.0.0.1:6379[1]> RENAME name id
OK
127.0.0.1:6379[1]> get name
(nil)
127.0.0.1:6379[1]> get id
"lee"

#设定数据过期时间
127.0.0.1:6379> set name lee ex 10000
OK
127.0.0.1:6379> get name
"lee"
127.0.0.1:6379> EXPIRE name 3
(integer) 1
127.0.0.1:6379> get name
"lee"
127.0.0.1:6379> get name
(nil)

#删除
127.0.0.1:6379> set name lee
OK
127.0.0.1:6379> get name
"lee"
127.0.0.1:6379> del name
(integer) 1
127.0.0.1:6379> get name
(nil)

#持久化保存
127.0.0.1:6379> PERSIST name
(integer) 0

#判断key是否存在
127.0.0.1:6379> EXISTS name
(integer) 1
127.0.0.1:6379> EXISTS lee
(integer) 0

#清空当前库
127.0.0.1:6379> flushdb
OK
127.0.0.1:6379> GET name
(nil)

#清空所有库
127.0.0.1:6379[1]> FLUSHALL
OK
  • 25
    点赞
  • 19
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值