当前位置:网站首页>Redis-01.初识Redis
Redis-01.初识Redis
2022-07-05 06:20:00 【寒叶飘逸_】
NoSQL
SQL和NoSQL的区别
| SQL | NoSQL | |
|---|---|---|
| 数据结构 | 结构化 | 非结构化(键值、文档、列、图等)结构比较松散 |
| 数据关联 | 关联的 | 无关联的 |
| 查询方式 | SQL查询 | 非SQL |
| 事务特性 | ACID | BASE |
| 存储方式 | 磁盘 | 内存 |
| 扩展性 | 垂直 | 水平 |
| 使用场景 | 1)数据结构固定 2)对数据安全性、一致性要求较高 | 1)数据结构不固定 2)对一致性、安全性要求不高 3)对性能要求 |
Redis
特征:
- 键值型,value支持多种不同的数据结构
- 单线程,每个命令具有原子性
- 低延迟,速度快(基于内存、IO多路复用、良好的编码)
- 支持数据的持久化
- 支持主从集群、分片集群
- 支持多语言客户端
1.安装Redis
1.安装Redis依赖
Redis基于C语言实现,因此首先安装gcc依赖
yum install -y gcc tcl
2.上传安装包并解压
上传Redis安装包至/usr/local/src
进入该路径后解压
tar -xzf redis-6.2.6.tar.gz
解压完成后进入解压路径
cd redis-6.2.6
运行编译命令:
make && make install
运行成功后,就安装好了
默认的安装路径为/usr/local/bin/
该目录以及默认配置到环境变量,因此可以在任意目录下运行这些命令。其中:
- redis-cli:是redis提供的命令行客户端
- redis-server:是redis的服务端启动脚本
- redis-sentinel:是redis的哨兵启动脚本
3.启动
redis的启动方式有很多种,例如:
- 默认启动
- 指定配置启动
- 开机自启
1.默认启动
安装完成后,在任意目录输入redis-server命令即可启动Redis:
redis-server
这种启动属于前台启动,会阻塞整个会话窗口,窗口关闭或者按下CTRL + C则Redis停止。不推荐使用。
2.指定配置启动
如果要让Redis以后台方式启动,则必须修改Redis配置文件,就在我们之前解压的redis安装包下(/usr/local/src/redis-6.2.6),名字叫redis.conf
先将这个配置文件备份一份:
cp redis.conf redis.conf.bck
修改配置文件
vi redis.conf
然后修改redis.conf文件中的一些配置:
# 允许访问的地址,默认是127.0.0.1,会导致只能在本地访问。修改为0.0.0.0则可以在任意IP访问,生产环境不要设置为0.0.0.0
bind 0.0.0.0
# 守护进程,修改为yes后即可后台运行
daemonize yes
# 密码,设置后访问Redis必须输入密码
requirepass redispasswd
Redis的其它常见配置:
# 监听的端口
port 6379
# 工作目录,默认是当前目录,也就是运行redis-server时的命令,日志、持久化等文件会保存在这个目录
dir .
# 数据库数量,设置为1,代表只使用1个库,默认有16个库,编号0~15
databases 1
# 设置redis能够使用的最大内存
maxmemory 512mb
# 日志文件,默认为空,不记录日志,可以指定日志文件名
logfile "redis.log"
启动Redis:
# 进入redis安装目录
cd /usr/local/src/redis-6.2.6
# 启动
redis-server redis.conf
停止服务:
# 利用redis-cli来执行 shutdown 命令,即可停止 Redis 服务,
# 因为之前配置了密码,因此需要通过 -a 来指定密码
redis-cli -a 123321 shutdown
3.开机自启
我们也可以通过配置来实现开机自启。
首先,新建一个系统服务文件:
vi /etc/systemd/system/redis.service
内容如下:
[Unit]
Description=redis-server
After=network.target
[Service]
Type=forking
ExecStart=/usr/local/bin/redis-server /usr/local/src/redis-6.2.6/redis.conf
PrivateTmp=true
[Install]
WantedBy=multi-user.target
然后重载系统服务:
systemctl daemon-reload
现在,我们可以用下面这组命令来操作redis了:
# 启动
systemctl start redis
# 停止
systemctl stop redis
# 重启
systemctl restart redis
# 查看状态
systemctl status redis
执行下面的命令,可以让redis开机自启:
systemctl enable redis
2.Redis客户端
安装完成Redis,我们就可以操作Redis,实现数据的CRUD了。这需要用到Redis客户端,包括:
- 命令行客户端
- 图形化桌面客户端
- 编程客户端
1.Redis命令行客户端
Redis安装完成后就自带了命令行客户端:redis-cli,使用方式如下:
redis-cli [options] [commonds]
其中常见的options有:
-h 127.0.0.1:指定要连接的redis节点的IP地址,默认是127.0.0.1-p 6379:指定要连接的redis节点的端口,默认是6379-a 123321:指定redis的访问密码
其中的commonds就是Redis的操作命令,例如:
ping:与redis服务端做心跳测试,服务端正常会返回pong
不指定commond时,会进入redis-cli的交互控制台
首先启动redis客户端
redis-cli
然后输入密码
auth "123321"
ping一下
ping
ping成功的话会返回一个pong
可以使用set将数据以字典形式存入数据库
2.图形化桌面客户端
安装包仓库:https://github.com/lework/RedisDesktopManager-Windows/releases
源码:https://github.com/uglide/RedisDesktopManager
按照自己redis的配置进行设置即可连接

可以发现,之前存入redis的数据成功保存在了redis的数据库中
参考资料:
- https://www.bilibili.com/video/BV1cr4y1671t?p=7&spm_id_from=pageDriver
- https://blog.csdn.net/Mr_wangB0/article/details/102741624
- https://blog.csdn.net/qq_26710805/article/details/80171101
边栏推荐
- Overview of variable resistors - structure, operation and different applications
- What's wrong with this paragraph that doesn't work? (unresolved)
- Leetcode-9: palindromes
- [rust notes] 17 concurrent (Part 2)
- 1.14 - assembly line
- Leetcode-31: next spread
- Chapter 6 relational database theory
- Operator priority, one catch, no doubt
- 博弈论 AcWing 891. Nim游戏
- 博弈论 AcWing 893. 集合-Nim游戏
猜你喜欢

MySQL怎么运行的系列(八)14张图说明白MySQL事务原子性和undo日志原理

Operator priority, one catch, no doubt

P2575 master fight

Quickly use Amazon memorydb and build your own redis memory database

SQL三种连接:内连接、外连接、交叉连接

Series of how MySQL works (VIII) 14 figures explain the atomicity of MySQL transactions and the principle of undo logging

4.Oracle-重做日志文件管理

【LeetCode】Easy | 20. Valid parentheses

1.14 - assembly line

Is it impossible for lamda to wake up?
随机推荐
论文阅读报告
Record the process of configuring nccl and horovod in these two days (original)
Data visualization chart summary (I)
4. Object mapping Mapster
做 SQL 性能优化真是让人干瞪眼
Bash exercise 17 writing scripts to install the server side of FRP reverse proxy software
P3265 [jloi2015] equipment purchase
2048项目实现
RGB LED infinite mirror controlled by Arduino
How to make water ripple effect? This wave of water ripple effect pulls full of retro feeling
MySQL advanced part 2: MySQL architecture
Sorting out the latest Android interview points in 2022 to help you easily win the offer - attached is the summary of Android intermediate and advanced interview questions in 2022
[leetcode] day95 effective Sudoku & matrix zeroing
[rust notes] 15 string and text (Part 1)
TCP's understanding of three handshakes and four waves
Single chip computer engineering experience - layered idea
WordPress switches the page, and the domain name changes back to the IP address
Simple selection sort of selection sort
[leetcode] day94 reshape matrix
MySQL advanced part 2: the use of indexes