Redis那些事-简介与安装

NoSql是非关系型数据库,全称 Not only sql,NoSql为了解决高并发、高可用、高可扩展,大数据存储等一系列问题而产生的数据 库解决方案,而Redis就是其中一种。

简介

Redis全称是Remote Dictionary Server,直意就是远程词典服务器,所以它是key-value形式存储。

Redis的性能优越主要来自于3个方面,首先 ,它是基于ANSIC语言编写的,接近于汇编语言的机器语言,运行十分快速。其次,它是基于内存的读/写,速度自然比数据库的磁盘读/写要快得多。最后,它的数据库结构只有 6 种数据类型,数据结构比较简单,因此规则较少,而数据库则是范式,完整性、规范性需要考虑的规则比较多,处理业务会比较复杂。

使用场景

一般而言Redis在Java Web应用中存在两个主要的场景, 一个是缓存常用的数据,另一个是在需要高速读/写的场合使用它快速读/写, 比如一些需要进行商品抢购和抢红包的场合。由于在高并发的情况下,需要对数据进行高速读/写的场景, 一个最为核心的问题是数据一致性和访问控制。

  • 缓存(数据查询、短连接、新闻内容、商品内容等等)。(使用最多)
  • 分布式集群架构中的session分离。
  • 聊天室的在线好友列表。
  • 任务队列。(秒杀、抢购、12306等等)
  • 应用排行榜。
  • 网站访问统计。
  • 过期数据处理。

使用Redis思考:

  • 业务数据常用吗?命中率如何?如果命中率很低 ,就没有必要写入缓存。
  • 该业务数据是读操作多,还是写操作多 ,如果写操作多 ,频繁需要写入数据库 ,也没有必要使用缓存。
  • 业务数据大小如何?如果要存储几百兆字节的文件 ,会给缓存带来很大的压力,有没有必要?

Redis的安装(Mac)

https://redis.io/download 下载Stable版本,下载完成解压到 /usr/local 目录下,然后执行命令编译:

cd /usr/local/redis-5.0.6
make
sudo make install

编译成功后,输入 redis-server 启动 redis 即可,默认端口是6379。

启动Redis

直接输入 redis-server 启动 redis 的方式是前端启动方式,缺点是启动后无法在控制台做任何操作了,所以推荐使用后台启动方式。

先把刚刚启动的Redis给停了:

redis-cli shutdown

然后cd到/usr/local/redis-5.0.6修改redis.conf

vim redis.conf

按i进入编辑,找到daemonize设为yes:

################################# 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 yes

按esc然后,输入:wq保存退出,然后cd到redis目录,输入redis-server redis.conf启动即可,查看是否启动,输入ps ‐aux | grep redis。

连接Redis

使用 redis-cli -h 127.0.0.1 -p 6379 连接。

macdeMacBook-Pro:redis-5.0.6 czd$ redis-cli -h 127.0.0.1 -p 6379
127.0.0.1:6379> set username dane
OK
127.0.0.1:6379> get username
"dane"
127.0.0.1:6379>

设置密码,编辑redis.conf设置requirepass:

################################## SECURITY ###################################

# Require clients to issue AUTH <PASSWORD> before processing any other
# commands.  This might be useful in environments in which you do not trust
# others with access to the host running redis-server.
#
# This should stay commented out for backward compatibility and because most
# people do not need auth (e.g. they run their own servers).
#
# Warning: since Redis is pretty fast an outside user can try up to
# 150k passwords per second against a good box. This means that you should
# use a very strong password otherwise it will be very easy to break.
#
requirepass 123456

然后重启Redis,连接后,第一次操作需要密码验证,输入auth 123456:

redis-cli -h 127.0.0.1 -p 6379
127.0.0.1:6379> get username
(error) NOAUTH Authentication required.
127.0.0.1:6379> auth 123456
OK
127.0.0.1:6379> get username
"dane"

Redis默认IP是127.0.0.1,为了能让远程连接,可以修改IP,同样是修改redis.conf:

~~~ WARNING ~~~ If the computer running Redis is directly exposed to the
# internet, binding to all the interfaces is dangerous and will expose the
# instance to everybody on the internet. So by default we uncomment the
# following bind directive, that will force Redis to listen only into
# the IPv4 loopback interface address (this means Redis will be able to
# accept connections only from clients running into the same computer it
# is running).
#
# IF YOU ARE SURE YOU WANT YOUR INSTANCE TO LISTEN TO ALL THE INTERFACES
# JUST COMMENT THE FOLLOWING LINE.
# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
bind 127.0.0.1

bind那里改成对应的IP就可以了。

©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • NOSQL类型简介键值对:会使用到一个哈希表,表中有一个特定的键和一个指针指向特定的数据,如redis,volde...
    MicoCube阅读 4,087评论 2 27
  • 一、Redis 简介 Redis 是完全开源免费的,遵守BSD协议,是一个高性能(NOSQL)的key-value...
    神奇作手阅读 504评论 0 1
  • 1 Redis介绍1.1 什么是NoSql为了解决高并发、高可扩展、高可用、大数据存储问题而产生的数据库解决方...
    克鲁德李阅读 5,393评论 0 36
  • Nosql概述 在介绍Redis之前,首先先要介绍Nosql的概念。 互联网架构发展 在90年代的时候,计算机访问...
    COKIDCC阅读 700评论 0 1
  • redis安装及使用 本次安装环境: centos6.8 redis-3.2.1 1、安装redis 下载redi...
    雄柱阅读 545评论 0 0