Redis初始

在这里插入图片描述

前言

NoSQL数据库

概述

NoSQL(NoSQL = Not Only SQL ),意即“不仅仅是SQL”,泛指非关系型的数据库

NoSQL 不依赖业务逻辑方式存储,而以简单的key-value模式存储。因此大大的增加了数据库的扩展能力。

特点

  • 不遵循SQL标准。

  • 不支持ACID。

  • 远超于SQL的性能。

使用场景

  • 对数据高并发的读写

  • 海量数据的读写

  • l对数据高可扩展性的

不适用场景

  • 需要事务支持

  • 基于sql的结构化查询存储,处理复杂的关系,需要及时查询。

(用不着sql的和用了sql也不行的情况,请考虑用NoSql

常见的NoSQL数据库

Memcache

在这里插入图片描述1. 很早出现的NoSql数据库
2.数据都在内存中,一般不持久化
3.支持简单的key-value模式,支持类型单一,只支持string
4. 一般是作为缓存数据库辅助持久化的数据库

Redis

在这里插入图片描述 1.几乎覆盖了Memcached的绝大部分功能
2.数据都在内存中,支持持久化,主要用作备份恢复
3.除了支持简单的key-value模式,还支持多种数据结构的存储,比如 list、set、hash、zset等。
4. 一般是作为缓存数据库辅助持久化的数据库

MongoDB

在这里插入图片描述1. 高性能、开源、模式自由(schema free)的文档型数据库
2.数据都在内存中, 如果内存不足,把不常用的数据保存到硬盘
3. 虽然是key-value模式,但是对value(尤其是json)提供了丰富的查询功能
4.支持二进制数据及大型对象
5. 可以根据数据的特点替代RDBMS ,成为独立的数据库。或者配合RDBMS,存储特定的数据。

1、Redis 介绍

1.1、定义

Redis 是一个开源的,单线程的,基于内存操作的,速度很快,它可以用作数据库、缓存和消息中间件。 它支持多种类型的数据结构,如字符串(strings)、散列(hashes)、列表(lists)、集合(sets)、有序集合(sorted sets)与范围查询等,每种数据类型有自己的专属命令,所以它通常也被称为数据结构服务器。

redis默认的端口号为6379,默认有16个数据库,默认使用的是第0个数据库,可以使用select进行切换。

redis的瓶颈是根据机器的字长和带宽决定的

在这里插入图片描述

1.2、Redis特点

  • Redis是一个开源的key-value存储系统。

  • 和Memcached类似,它支持存储的value类型相对更多,包括string(字符串)、list(链表)、set(集合)、zset(sorted set --有序集合)和hash(哈希类型)。

  • 这些数据类型都支持push/pop、add/remove及取交集并集和差集及更丰富的操作,而且这些操作都是原子性的。

  • 在此基础上,Redis支持各种不同方式的排序。

  • 与memcached一样,为了保证效率,数据都是缓存在内存中。

  • 区别的是Redis会周期性的把更新的数据写入磁盘或者把修改操作写入追加的记录文件。

  • 并且在此基础上实现了master-slave(主从)同步。

1.3、Redis的优缺点

1.3.1、优点

  1. 读写数据时速度很快,尤其是对大量的数据进行读写操作时,效果更加明显。
  2. 高可用性。
  3. 数据模型灵活,无需事先为要存储的数据建立字段,随时可以存储自定义的数据格式。
  4. 成本低廉,无论是学习成本还是使用成本都很低。
  5. 开源。

1.3.2、缺点

  1. 不支持事务,没有事务的ACID
  2. 不支持标准的 SQL,没有公认的 NoSQL 标准。
  3. 数据类型单一,不是那么多,基本上只有字符串类型,哈希类型、列表类型、集合类型、有序集合等。
  4. 没有关系型数据库的约束,大多数也没有索引的概念

1.3、Redis文件解释

  • redis-benchmark:性能测试工具,可以在自己本子运行,看看自己本子性能如何

  • redis-check-aof:修复有问题的AOF文件,rdb和aof后面讲

  • redis-check-dump:修复有问题的dump.rdb文件

  • redis-sentinel:Redis集群使用

  • redis-server:Redis服务器启动命令

  • redis-cli:客户端,操作入口

2、安装

Redis安装比较常用的有两种方式,一种是在windows系统下直接安装,另一种是在Liunx系统下安装,比较推荐在Linux系统下安装Redis。

2.1、在windows下安装redis

在windows下安装Redis的方式比较简单,大致可以分为以下几步:

  1. 首先进入官网下载或者Github上下载你想要安装的redis版本。

    官网地址:https://redis.io/

在这里插入图片描述

  1. 下载完成后进行解压。

    在这里插入图片描述

  2. 启动。

    在进行启动时要注意启动顺序。先启动redis-server.exe(服务端)再启动redis-cli.exe(客户端)

启动服务端

在这里插入图片描述

注意:redis的默认端口是6379

启动客户端

不要关闭服务端,再启动客户端

输入ping,如果显示PONG,则启动成功。

在这里插入图片描述

关闭客户端

在这里插入图片描述
此时服务器将会自动关闭。

2.2、在linux下安装redis(推荐)

在linux下安装比较机械的方式就是现下载好压缩包,然后使用xftp,把redis压缩包放入linux环境中,进行解压使用。另外一种就是直接使用命令,在linux系统中直接安装Redis。具体如下:

首先下载wget(前提)

[root@root itbestboy]# yum install wget
Loaded plugins: fastestmirror
Determining fastest mirrors
base                                                                                                                                   | 3.6 kB  00:00:00     
docker-ce-stable                                                                                                                       | 3.5 kB  00:00:00     
epel                                                                                                                                   | 4.7 kB  00:00:00     
extras                                                                                                                                 | 2.9 kB  00:00:00     
updates                                                                                                                                | 2.9 kB  00:00:00     
(1/4): epel/x86_64/updateinfo                                                                                                          | 1.0 MB  00:00:00     
(2/4): epel/x86_64/primary_db                                                                                                          | 6.9 MB  00:00:00     
(3/4): updates/7/x86_64/primary_db                                                                                                     | 9.6 MB  00:00:00     
(4/4): docker-ce-stable/7/x86_64/primary_db                                                                                            |  63 kB  00:00:00     
Package wget-1.14-18.el7_6.1.x86_64 already installed and latest version
Nothing to do

正是开始下载redis

1.首先去官网下载安装包

[root@root itbestboy]# wget http://download.redis.io/releases/redis-6.0.7.tar.gz
--2021-08-27 13:04:33--  http://download.redis.io/releases/redis-6.0.7.tar.gz
Resolving download.redis.io (download.redis.io)... 45.60.125.1
Connecting to download.redis.io (download.redis.io)|45.60.125.1|:80... connected.
HTTP request sent, awaiting response... 200 OK
Length: 2240490 (2.1M) [application/octet-stream]
Saving to: ‘redis-6.0.7.tar.gz’

100%[====================================================================================================================>] 2,240,490   47.5KB/s   in 45s    

2021-08-27 13:05:20 (48.8 KB/s) - ‘redis-6.0.7.tar.gz’ saved [2240490/2240490]

2.解压

#这个看自己,路径自己选择
[root@root itbestboy]# mv redis-6.0.7.tar.gz /opt/
[root@root itbestboy]# ls
apache-tomcat-9.0.48  apache-tomcat-9.0.48.tar.gz  hello.java  jdk-8u221-linux-x64.rpm  test01  test02
[root@root itbestboy]# cd /opt/
[root@root opt]# ls
containerd  redis-6.0.7.tar.gz
[root@root opt]# tar zxvf redis-6.0.7.tar.gz 
redis-6.0.7/

3.安装编译器

[root@root redis-6.0.7]# yum install gcc-c++
#或[root@root redis-6.0.7]# yum -y install gcc automake autoconf libtool make 
Loaded plugins: fastestmirror
Loading mirror speeds from cached hostfile
Package gcc-c++-4.8.5-44.el7.x86_64 already installed and latest version
Nothing to do
[root@root redis-6.0.7]# gcc -v
Using built-in specs.
COLLECT_GCC=gcc
COLLECT_LTO_WRAPPER=/usr/libexec/gcc/x86_64-redhat-linux/4.8.5/lto-wrapper
Target: x86_64-redhat-linux
Configured with: ../configure --prefix=/usr --mandir=/usr/share/man --infodir=/usr/share/info --with-bugurl=http://bugzilla.redhat.com/bugzilla --enable-bootstrap --enable-shared --enable-threads=posix --enable-checking=release --with-system-zlib --enable-__cxa_atexit --disable-libunwind-exceptions --enable-gnu-unique-object --enable-linker-build-id --with-linker-hash-style=gnu --enable-languages=c,c++,objc,obj-c++,java,fortran,ada,go,lto --enable-plugin --enable-initfini-array --disable-libgcj --with-isl=/builddir/build/BUILD/gcc-4.8.5-20150702/obj-x86_64-redhat-linux/isl-install --with-cloog=/builddir/build/BUILD/gcc-4.8.5-20150702/obj-x86_64-redhat-linux/cloog-install --enable-gnu-indirect-function --with-tune=generic --with-arch_32=x86-64 --build=x86_64-redhat-linux
Thread model: posix
gcc version 4.8.5 20150623 (Red Hat 4.8.5-44) (GCC) 

4.make

[root@root redis-6.0.7]# make
cd src && make all
make[1]: Entering directory `/opt/redis-6.0.7/src'
    CC Makefile.dep
make[1]: Leaving directory `/opt/redis-6.0.7/src'
make[1]: Entering directory `/opt/redis-6.0.7/src'

Hint: It's a good idea to run 'make test' ;)

make[1]: Leaving directory `/opt/redis-6.0.7/src'
[root@root redis-6.0.7]# make install
cd src && make install
make[1]: Entering directory `/opt/redis-6.0.7/src'

Hint: It's a good idea to run 'make test' ;)

    INSTALL install
    INSTALL install
    INSTALL install
    INSTALL install
    INSTALL install
make[1]: Leaving directory `/opt/redis-6.0.7/src'

5.修改配置文件,redis.conf

因为redis默认不会后台启动,所以此时我们需要允许后台启动。

redis配置文件的路径:/use/local/bin

[root@root redis-5.0.8]# cd /usr/local/bin
[root@root bin]# ls
chardetect  cloud-init      easy_install      easy_install-3.8  jsonpatch    jsonschema        mcrypt    redis-benchmark  redis-check-rdb  redis-sentinel
cloud-id    cloud-init-per  easy_install-3.6  jsondiff          jsonpointer  libmcrypt-config  mdecrypt  redis-check-aof  redis-cli        redis-server

#为了防止今后修改错误造成redis不可以使用,我们可以在复制一份redis.conf,及今后修改配置就在这里面修改,如果今后修改错误,就直接在复制一份干净的redis.conf
[root@root bin]# mkdir redisconfig
[root@root bin]# ls
chardetect  cloud-init-per    easy_install-3.8  jsonpointer       mcrypt           redis-check-aof  redisconfig
cloud-id    easy_install      jsondiff          jsonschema        mdecrypt         redis-check-rdb  redis-sentinel
cloud-init  easy_install-3.6  jsonpatch         libmcrypt-config  redis-benchmark  redis-cli        redis-server
[root@root bin]# cp /opt/redis-5.0.8/redis.conf redisconfig/
[root@root bin]# cd redisconfig/
[root@root redisconfig]# ls
redis.conf

#配置文件
#默认:
################################# GENERAL #####################################

# By default Redis does not run as a daemon. Use 'yes' if you need it.
# Note that Redis will write a pid file in /var/run/redis.pid when daemonized.
daemonize no

#需要修改位yes
daemonize yes


6.启动redis


[root@root bin]# pwd
/usr/local/bin
[root@root bin]# redis-server redisconfig/redis.conf 
25522:C 27 Aug 2021 13:34:10.968 # oO0OoO0OoO0Oo Redis is starting oO0OoO0OoO0Oo
25522:C 27 Aug 2021 13:34:10.968 # Redis version=5.0.8, bits=64, commit=00000000, modified=0, pid=25522, just started
25522:C 27 Aug 2021 13:34:10.968 # Configuration loaded
[root@root bin]# redis-cli -p 6379
127.0.0.1:6379> ping
PONG

127.0.0.1:6379> set name itbestboy
OK
127.0.0.1:6379> get name
"itbestboy"
#查看此时的进程信息
[root@root ~]# ps -ef|grep redis
root     25523     1  0 13:34 ?        00:00:00 redis-server 127.0.0.1:6379
root     25548 13717  0 13:34 pts/0    00:00:00 redis-cli -p 6379
root     26667 26613  0 13:44 pts/1    00:00:00 grep --color=auto redis
[root@root ~]# 

7.关闭


127.0.0.1:6379> shutdown
not connected>
[root@root ~]# ps -ef|grep redis
root     25548 13717  0 13:34 pts/0    00:00:00 redis-cli -p 6379
root     26734 26613  0 13:45 pts/1    00:00:00 grep --color=auto redis

not connected> exit
[root@root bin]# 
#再次查看进程信息
[root@root ~]# ps -ef|grep redis
root     26754 26613  0 13:45 pts/1    00:00:00 grep --color=auto redis
[root@root ~]# 

:安装redis的方式有很多,不仅限于这两种。
在这里插入图片描述

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值