Redis——Redis入门和一些笔记

Posted Pakho`

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Redis——Redis入门和一些笔记相关的知识,希望对你有一定的参考价值。

Redis概述

Redis是什么?

  • Redis(Re mote Di ctionary S erver ),即远程字典服务
  • 是一个开源的使用ANSI C语言编写、支持网络、可基于内存亦可持久化的日志型、Key-Value数据库,并提供多种语言的API
  • redis会周期性的把更新的数据写入磁盘或者把修改操作写入追加的记录文件,并且在此基础上实现了master-slave(主从)同步
  • 免费和开源,当下最热门的NoSQL技术之一,也被称为结构化数据库
  • 读的速度是110000次/s,写的速度是81000次/s
    在这里插入图片描述

Redis能干什么?

  • 内存存储,持久化,内存中是断电即失,所以持久化很重要(rdb、aof)
  • 效率高,可用于高速缓存
  • 发布订阅系统
  • 地图信息分析
  • 计时器、计数器(微信、微博、浏览量)

Redis特性

  • 多样数据类型
  • 持久化
  • 集群
  • 事务

Redis安装

Redis推荐都是在Linux服务器上搭建

官网下载redis-6.2.4

#基本环境安装
[root@pakho ~]# gcc -v    #查看当前gcc版本
gcc version 4.8.5 20150623 (Red Hat 4.8.5-28) (GCC)
#将redis-6.2.4.tar.gz上传服务器/usr/src目录

#解压redis安装包
[root@pakho src]# tar xf redis-6.2.4.tar.gz
[root@pakho src]# cd redis-6.2.4
[root@pakho redis-6.2.4]# make
[root@pakho redis-6.2.4]# make install

redis默认安装路径/usr/local/bin
#创建一个config文件夹
[root@pakho bin]# mkdir redis_config

#将redis配置文件 复制到redis_config下
[root@pakho bin]# cp /usr/src/redis-6.2.4/redis.conf redis_config/
[root@pakho redis_config]# ls                           #之后就用这个配置文件进行启动,原生文件作为备份
redis.conf

#redis默认不是后台启动,修改配置文件
[root@pakho redis_config]# vim redis.conf

daemonize no > daemonize yes                            #开启后台启动

启动redis
[root@pakho bin]# redis-server redis_config/redis.conf  #redis通过指定配置文件启动

连接redis
[root@pakho bin]# redis-cli -p 6379                     #-h:表示主机默认本机 -p:端口
127.0.0.1:6379> ping
PONG                                                    #连接成功
127.0.0.1:6379> set name pakho
OK
127.0.0.1:6379> get name
"pakho"
127.0.0.1:6379> keys *                                  #获取redis中所有的key
1) "name"

至此安装完成
[root@pakho redis-6.2.4]# ps aux | grep redis           #查看redis进程是否开启(cli和redis-server)

Redis性能测试

redis-benchmark是redis官方自带的压力测试工具

选项描述默认值
-h指定服务器主机名127.0.0.1
-p指定服务器端口6379
-s指定服务器 socket
-c指定并发连接数50
-n指定请求数10000
简单测试
100个并发连接 每个并发100000请求
redis-benchmark -h localhost -p 6379 -c 100 -n 100000

在这里插入图片描述
在这里插入图片描述

Redis一些基础知识

关闭redis服务

127.0.0.1:6379> shutdown
not connected> exit

redis默认16个数据库
不同的数据库存放不同的值

[root@pakho redis_config]# vim redis.conf
databases 16

`默认使用第0个`

#可以使用select进行切换
127.0.0.1:6379> select 3
OK
#DBSIZE查看当前数据库大小
127.0.0.1:6379[3]> dbsize
(integer) 0

#查看数据库所有key
127.0.0.1:6379> keys *

#清空全部库的内容
127.0.0.1:6379[3]> flushall

#清空当前库
127.0.0.1:6379[3]> flushdb
OK

Redis是单线程的

  • Redis是很快的,官方表示,Redis基于内存操作,CPU部署Redis性能瓶颈,Redis的瓶颈是根据机器的内存和网络带宽

Redis为什么单线程还这么快?

  • Redis是C语言写的,官方数据为100000+的QPS,完全不比通样使用key-vale的Memcache差
  • Redis将所有的数据全部放在内存中的,所以说使用单线程去操作效率就是最高的(CPU上下文切换,耗时的操作)
  • 对于内存系统来说,如果没有上下文切换效率就是最高的,多次读写都是在一个CPU上,在内存情况下,就是最佳方案

添加信息
set name value

127.0.0.1:6379> set name wyc
OK

信息查询
get key

127.0.0.1:6379> get name
"wyc"

获取帮助
help

127.0.0.1:6379> help set

  SET key value [EX seconds|PX milliseconds|EXAT timestamp|PXAT milliseconds-timestamp|KEEPTTL] [NX|XX] [GET]
  summary: Set the string value of a key
  since: 1.0.0
  group: string

127.0.0.1:6379> help get

  GET key
  summary: Get the value of a key
  since: 1.0.0
  group: string

测试PING
ping

127.0.0.1:6379> ping
PONG

判断当前key是否存在
exists

127.0.0.1:6379> exists name
(integer) 1                                                             #返回为1表示存在这个值

127.0.0.1:6379> exists lhb                                              #不存在返回1
(integer) 0

移当前动Key
move

127.0.0.1:6379> move name 1
(integer) 1
127.0.0.1:6379> select 1
OK
127.0.0.1:6379[1]> get name
"wyc"

设置key的过期时间
expire 单位为秒

127.0.0.1:6379[1]> expire name 5
(integer) 1
127.0.0.1:6379[1]> ttl name                                              #ttl表示当前key剩余时间
(integer) 2
127.0.0.1:6379[1]> ttl name
(integer) 1
127.0.0.1:6379[1]> ttl name
(integer) -2                                                             #-2表示key已不存在
127.0.0.1:6379[1]> exists name
(integer) 0
127.0.0.1:6379[1]> get name
(nil)

type 查看 key类型
type

127.0.0.1:6379[1]> set name pakho
OK
127.0.0.1:6379[1]> type name                                             #查看当前key类型
string

官方命令帮助文档

Redis配置文件

配置文件 unit 单位对大小写不敏感

# 1k => 1000 bytes
# 1kb => 1024 bytes
# 1m => 1000000 bytes
# 1mb => 1024*1024 bytes
# 1g => 1000000000 bytes
# 1gb => 1024*1024*1024 bytes
#
# units are case insensitive so 1GB 1Gb 1gB are all the same.

整合多配置文件

################################## INCLUDES ###################################

# Include one or more other config files here.  This is useful if you
# have a standard template that goes to all Redis servers but also need
# to customize a few per-server settings.  Include files can include
# other files, so use this wisely.
#
# Note that option "include" won't be rewritten by command "CONFIG REWRITE"
# from admin or Redis Sentinel. Since Redis always uses the last processed
# line as value of a configuration directive, you'd better put includes
# at the beginning of this file to avoid overwriting config change at runtime.
#
# If instead you are interested in using includes to override configuration
# options, it is better to use include as the last line.
#
# include /path/to/local.conf
# include /path/to/other.conf

网络配置

bind 127.0.0.1 -::1 #绑定的IP
protected-mode yes  #保护模式
port 6379           #端口设置

通用配置

daemonize yes       #以守护进程方式运行,默认是NO,需自己开启为yes
# supervised auto   #管理守护进程,根据版本默认配置文件不同,可以不用动也可以No
pidfile /var/run/redis_6379.pid  #如果以守护进程方式运行,需指定pid文件

日志
# Specify the server verbosity level.
# This can be one of:
# debug (a lot of information, useful for development/testing)             #大量日志,测试和开发阶段
# verbose (many rarely useful info, but not a mess like the debug level)   #记录较多的日志信息
# notice (moderately verbose, what you want in production probably)        #通知,生产环境使用
# warning (only very important / critical messages are logged)             #警告,产生重要的信息
loglevel notice                                                            #一般不去设置
logfile ""                                                                 #日志生成文件位置名
databases 16                                                               #数据库数量,默认16个数据库
always-show-logo no                                                        #是否总是显示logo

快照

  • 持久化,在规定的时间内,执行了多少次操作,则会持久化到文件.rdb.aof
  • redis是内存数据库,如果没有持久化,数据断电即失
# save 3600 1                                                              #3600秒内如果至少一个key至少一次修改就进行持久化操作
# save 300 100
# save 60 10000                                                            #高并发
#我们之后会设置自己的

stop-writes-on-bgsave-error yes                                            #持久化出错后是否继续工作
rdbcompression yes                                                         #是否压缩rdb文件,需要消耗一些cpu资源
rdbchecksum yes                                                            #保寸rdb文件时检查校验
dir ./                                                                     #rdb文件保存目录

安全

################################## SECURITY ###################################
127.0.0.1:6379> config set requirepass "123456"  #设置密码
OK
127.0.0.1:6379> auth 123456
OK
127.0.0.1:6379> config get requirepass
1) "requirepass"
2) "123456"

客户端限制

################################### CLIENTS ####################################
# maxclients 10000              #设置能连接redis最大客户端数量

内存
############################## MEMORY MANAGEMENT ################################
# maxmemory <bytes>                 #redis设置最大内存容量
# maxmemory-policy noeviction       #内存达到上限后的处理策略
  • maxmemory-policy 六种方式
    1. volatile-lru:只对设置了过期时间的key进行LRU(默认值)
    2. allkeys-lru : 删除lru算法的key
    3. volatile-random:随机删除即将过期key
    4. allkeys-random:随机删除
    5. volatile-ttl : 删除即将过期的
    6. noeviction : 永不过期,返回错误

aof模式

############################## APPEND ONLY MODE ###############################
appendonly no                       #默认不开启aof模式,默认使用rdb方式持久化,大部分情况下rdb够用了
appendfilename "appendonly.aof"     #持久化文件名

# appendfsync always                #每次修改都会同步
appendfsync everysec                #没秒执行一次sync(同步),可能会丢失1s对的数据
# appendfsync no                    #不执行sync,这时操作系统自己同步速度是最快的

以上是关于Redis——Redis入门和一些笔记的主要内容,如果未能解决你的问题,请参考以下文章

Redis入门笔记

Redis入门笔记

[Redis]Redis入门笔记

redis入门指南-笔记-简介

Redis学习笔记1--入门篇

Redis入门学习笔记一