目录
[一.SQL 与 NoSQL 的区别?](#一.SQL 与 NoSQL 的区别?)
[Redis 为什么那么快?](#Redis 为什么那么快?)
[redis-cli 命令行工具:](#redis-cli 命令行工具:)
[redis-benchmark 测试工具:](#redis-benchmark 测试工具:)
[四.Redis 数据库常用命令](#四.Redis 数据库常用命令)
一.SQL 与 NoSQL 的区别?
SQL:二维表格结构,纵向扩展(提升单机硬件性能),基于ACID原则,事务控制更稳定,细粒度更高,典型代表 :MySQL,SQL-Server
NoSQL:不固定的,通常存储在数据集中(比如键值对、文档、索引、图形结构、时间序列等),横向扩展(增加服务器节点数量),基于BASE原则,稳定性和细粒度控制方面不如SQL,典型代表 :Redis Memcached。
二.Redis
Redis :是 C语言开发的,开源的,基于内存运行的NoSQL。
数据存储结构 键值对(Key/Value KV)
默认端口号 TCP/6379
数据类型: 五大基础类型 String(字符串) List(列表) Hash(哈希/散列) Set(无序集合) Zset/Sorted Set(有序集合)
Redis 为什么那么快?
1)redis是基于内存运行的,数据读写都是在内存中完成的
2)数据读写采用单线程模式,避免了多线程切换带来的CPU性能消耗,同时也不要考虑各种锁的问题
3)采用IO多路复用模型,可以使线程处理更多的网络连接请求,提高并发能力
网站性能压测工具 :ab jmeter
redis 性能压测工具 : redis-benchmark -h <redis服务地址> -p <服务端口> -a <密码> -c <并发连接数> -n <请求数> -d <数据大小> -t <命令列表> -q
redis 客户端工具 : redis-cli -h <redis服务地址> -p <服务端口> -a <密码> [命令]
三.Redis的安装
开启一台服务器,修改内核配置文件:



安装redis:



创建redis工作目录:

复制配置文件:

创建用户redis:

给redis目录下的所有目录添加权限:


添加环境变量:



修改redis配置文件:
添加监听主机:

将本机访问保护模式设置no:

设置为守护进程,后台启动:

指定 PID 文件:

指定日志文件:

指定持久化文件所在目录:

增加一行,设置redis密码:

开启服务:

定义systemd服务管理脚本开启服务:



需要授权:


开启服务:

Redis 命令工具 :
redis-server:Redis 服务器启动命令
redis-benchmark:性能测试工具,用于检测 Redis 在本机的运行效率
redis-check-aof:修复有问题的 AOF 持久化文件
redis-check-rdb:修复有问题的 RDB 持久化文件
redis-cli:Redis 客户端命令行工具
redis-sentinel:Redis 哨兵集群使用
redis-cli 命令行工具:
语法:redis-cli -h host -p port [-a password]
-h :指定远程主机
-p :指定 Redis 服务的端口号
-a :指定密码,未设置数据库密码可以省略-a 选项
若不添加任何选项表示,则使用 127.0.0.1:6379 连接本机上的 Redis 数据库
redis-benchmark 测试工具:
基本的测试语法:redis-benchmark [选项] [选项值]
-h :指定服务器主机名。
-p :指定服务器端口。
-s :指定服务器 socket
-c :指定并发连接数。
-n :指定请求数。
-d :以字节的形式指定 SET/GET 值的数据大小。
-k :1=keep alive 0=reconnect 。
-r :SET/GET/INCR 使用随机 key, SADD 使用随机值。
-P :通过管道传输<numreq>请求。
-q :强制退出 redis。仅显示 query/sec 值。
--csv :以 CSV 格式输出。
-l :生成循环,永久执行测试。
-t :仅运行以逗号分隔的测试命令列表。
-I :Idle 模式。仅打开 N 个 idle 连接并等待。
redis-benchmark 是官方自带的 Redis 性能测试工具,可以有效的测试 Redis 服务的性能。
进行测试:

四.Redis 数据库常用命令
keys *:看所有键

select 序号:多数据库间切换

多数据库间移动数据:move 键值 序号

清除数据库内数据(慎用):flushdb

清空所有库(慎用):flushall


string(字符串)数据类型:
set 键 值:创建键
get 键:查看值

del 键:删除键


exists 键 判断该键是否存在,存在返回1,否则返回0:
set命令为键设置新值,并覆盖原有值:

strlen 键 :获取指定Key的字符长度

定义值:

incr 键:Key的值递增1

decr 键:Key的值递减1

decrby 键 数字:减少指定的整数

incrby 键 数字:增加指定的整数

setex 键 秒数 值:指定Key的过期时间为多少秒。


setnx 键 值 :键不存在,因此setnx命令执行成功,反馈1

mset 键1 值1 键2 值2 ......: 批量设置多个键
mget 键1 键2:批量获取键的值

msetnx 键1 值1 键2 值2 ....:批量设置键,键不存在执行成功并返回1

键存在,返回0,由于key5没有设置成功,所以返回nil:

List(列表)数据类型:
lpush 键 值1 值2 .... :创建list类型键,将参数从左到右依次插入

lrange 键 起始下标 终止下标: 0(第一个) -1(最后一个)

lpushx 键 值1 值2 ....:键不存在则不创建不成功,返回0

lpop 键:移除并返回键的第一个元素,从左取

llen 键:在执行lpop命令两次后,键值两个元素已经被弹出

lrem 键 数值 已有值 :从头部(left)向尾部(right)变量链表,删除值,返回值为实际删除的数量。

lindex 键 数字:获取指定值

lset 键 下标数字 新值:将下标的值,设为数值

linsert 键 before 已有值 新值:在已有值前添加新值

linsert 键 after 已有值 新值:在已有值后添加新值

rpush 键 值1 值2 ...: 插入参数,插入顺序是从右到左依次插入

rpushx 键 新值:在键尾部插入新值

rpop 键 :移除并返回键的第一个值,从右取

rpoplpush 键1 键2 : 将键1的尾部值移除,并插入到键2的开头
hash(散列)数据类型:
hset 键 字段1 值1 字段2 值2 ....:设置键


hlen 键:获取h键的字段数量

hexists 键 字段:看键中字段是否存在,存在返回1,不存在返回0

hdel 键 字段:删除键中的字段

hsetnx 键 字段 值:给键重新添加字段和值,字段不存在返回1,字段存在返回0


hincrby 键 字段 正数:给字段值加上正数值

hincrby 键 字段 负数:给字段值减数值

hmset 键 字段1 值1 字段2 值2 ....:一次性设置多个字段,显示没有设置的字段值为nil

hgetall 键:查看键的字段和值

hkeys 键 :查看键的所有字段

hvals 键:查看键的所有字段的值

Set数据类型(无序集合)
sadd 键 值1 值2 值3 ....:创建键

sismember 键 值:判断值是否存在,存在返回1,不存在返回0

smembers 键 :查看键无顺序显示

scard 键:查看键值的数量

srandmember 键:随机返回键的值

spop 键:随机的移除并返回的值

srem 键 值1 值2..:移除键的值,如有不存在的值则只返回存在值的总数

smove 键1 键2 键1的值:将键1的值移动到键2上,返回1成功,0失败


zset(有序集合)数据类型:
zadd 键 字段 '值':创建zset数据类型键

zrange 键 起始下标 末尾下标 withscores:查看键值 ,withscores选项表示返回的结果中包含字段值

zrank 键 值:查看值的下标

当值不存在返回nil:

zcard 键:查看键的值数量

zrem 键 值1 值2 ...:删除值


zscore 键 字段:查看字段值

查看没有的字段返回nil:

zincrby 键 正数 值:将值加为上数

zincrby 键 负数 值:将值减去数

zrangebyscore 键 开始下标 结尾下标:看键的最小值和最大值

zrangebyscore 键 (开始下标 结尾下标:看键的最大值,不包括最小值

zrangebyscore 键 -inf +inf limit 最小值 最大值:-inf表示第一个(位置索引值最低的,即0),+inf表示最后一个(位置索引值最高的)limit后面的参数用于限制返回值,从索引值为2开始往后取3个值。

zremrangebyscore 键 开始下标 结尾下标:删除下标最小的和最大的值


zrevrange 键 开始下标 结尾下标 withscores:查看键的字段和开始值到结尾值

zrevrank 键 值:从高到低的排序,返回键值的下标

zrevrangebyscore 键 数字 数字 limit 数字 数字: 从高到底的顺序输出。

zrevrangebyscore 键 +inf -inf limit 数字 数字:查看键的值