Redis学习之一 安装

一、简介

定义

  Remote DIctionary Server(Redis) 是一个由Salvatore Sanfilippo写的key-value存储系统。

  Redis是一个开源的使用ANSI C语言编写、遵守BSD协议、支持网络、可基于内存亦可持久化的日志型、Key-Value数据库,并提供多种语言的API。

  redis通常被称为数据结构服务器。

  redis是基于key-value的no sql数据库,因此,先来了解一下关于key相关的知识点

  1、任何二进制的序列都可以作为key使用
  2、Redis有统一的规则来设计key
  3、对key-value允许的最大长度是512MB

    五种数据类型

  字符串(string), 哈希(map), 列表(list), 集合(set) 和 有序集合(zset[sorted set])

  

应用场景

  1、最常用的就是会话缓存
  2、消息队列,比如支付
  3、活动排行榜或计数
  4、发布、订阅消息(消息通知)
  5、商品列表、评论列表等

 

Redis 英文官网:https://redis.io/

Redis 中文官网:https://www.redis.net.cn/

Redis GitHub 开源地址:https://github.com/antirez/redis

但是官方没有windows版本

WINDOWS下载安装:https://github.com/microsoftarchive/redis/releases

二、配置文件

创建redis.conf文件:
这是一个配置文件,指定了redis的监听端口,timeout等。如下面有:port 6379。

配置:

 遇到问题:

[18892] 05 Jan 16:02:28.584 #
The Windows version of Redis allocates a memory mapped heap for sharing with
the forked process used for persistence operations. In order to share this
memory, Windows allocates from the system paging file a portion equal to the
size of the Redis heap. At this time there is insufficient contiguous free
space available in the system paging file for this operation (Windows error
0x5AF). To work around this you may either increase the size of the system
paging file, or decrease the size of the Redis heap with the --maxheap flag.
Sometimes a reboot will defragment the system paging file sufficiently for
this operation to complete successfully.

Please see the documentation included with the binary distributions for more
details on the --maxheap flag.

Redis can not continue. Exiting.

 

处理方法:

windows硬盘需要配置虚拟内存,如果还有问题,清理磁盘碎片
redis.windows.conf
maxheap 1024000000
daemonize no

  

更改redis的配置需要修改redis.conf文件,以下是它一些主要的配置注释:

#是否作为守护进程运行
daemonize no
#Redis 默认监听端口
port 6379
#客户端闲置多少秒后,断开连接
timeout 300
#日志显示级别
loglevel verbose
#指定日志输出的文件名,也可指定到标准输出端口
logfile redis.log
#设置数据库的数量,默认最大是16,默认连接的数据库是0,可以通过select N 来连接不同的数据库
databases 32
#Dump持久化策略
#当有一条Keys 数据被改变是,900 秒刷新到disk 一次
#save 900 1
#当有10 条Keys 数据被改变时,300 秒刷新到disk 一次
save 300 100
#当有1w 条keys 数据被改变时,60 秒刷新到disk 一次
save 6000 10000
#当dump     .rdb 数据库的时候是否压缩数据对象
rdbcompression yes
#dump 持久化数据保存的文件名
dbfilename dump.rdb
###########    Replication #####################
#Redis的主从配置,配置slaveof则实例作为从服务器
#slaveof 192.168.0.105 6379
#主服务器连接密码
# masterauth <master-password>
############## 安全性 ###########
#设置连接密码
#requirepass <password>
############### LIMITS ##############
#最大客户端连接数
# maxclients 128
#最大内存使用率
# maxmemory <bytes>
########## APPEND ONLY MODE #########
#是否开启日志功能
appendonly no
# AOF持久化策略
#appendfsync always
#appendfsync everysec
#appendfsync no
################ VIRTUAL MEMORY ###########
#是否开启VM 功能
#vm-enabled no
# vm-enabled yes
#vm-swap-file logs/redis.swap
#vm-max-memory 0
#vm-page-size 32
#vm-pages 134217728
#vm-max-threads 4

主从复制

在从服务器配置文件中配置slaveof ,填写服务器IP及端口即可,如果主服务器设置了连接密码,在masterauth后指定密码就行了。

持久化

  • redis提供了两种持久化文案,Dump持久化和AOF日志文件持久化。
  • Dump持久化是把内存中的数据完整写入到数据文件,由配置策略触发写入,如果在数据更改后又未达到触发条件而发生故障会造成部分数据丢失。
  • AOF持久化是日志存储的,是增量的形式,记录每一个数据操作动作,数据恢复时就根据这些日志来生成。

 

三、.命令行操作

使用CMD命令提示符,打开redis-cli连接redis服务器 ,也可以使用telnet客户端

# redis-cli -h 服务器 –p 端口 –a 密码

redis-cli.exe -h 127.0.0.1 -p 6379

连接成功后,就可对redis数据增删改查了,如字符串操作:

Windows环境下安装Redis体验谈_新客网

以下是一些服务器管理常用命令:

info   #查看服务器信息
select <dbsize> #选择数据库索引  select 1
flushall #清空全部数据
flushdb  #清空当前索引的数据库
slaveof <服务器> <端口>  #设置为从服务器
slaveof no one #设置为主服务器
shutdown  #关闭服务

 

附加几个 bat 批处理脚本,请根据需要灵活配置

注册/卸载系统服务

redis-server.exe --service-install redis.windows.conf --loglevel verbose  

redis-server --service-uninstall 

  卸载服务:redis-server --service-uninstall

  开启服务:redis-server --service-start

  停止服务:redis-server --service-stop

启动命令

redis-server.exe redis.windows.conf

 可以把 redis 的路径加到系统的环境变量里。命令后面的 redis.conf 可以省略,此时会启用默认参数。(Redis 默认使用 6379 端口)

安装RADIS

开启redis:

1.打开cmd,然后进入redis解压目录。

2.执行命令redis-server.exe redis.windows.conf 如果出现如下错误

则执行如下命令:

1)redis-cli.exe

2)shutdown

2)exit

3.如果出现错误,执行如上命令后,重新执行redis-server.exe redis.windows.conf命令,如果正常开启,会出现如下图示:

4.如果没出现错误,在解压后直接执行redis-server.exe,可看到上面服务端执行成功的截图,然后再执行redis-cli.exe,就可以进入下面的客户端界面,添加数据如下所示(常用命令):

 

 

推荐:http://www.redis.net.cn/

 

 参考文章

https://www.cnblogs.com/youkanyouxiao/p/9072248.html

https://www.cnblogs.com/sxdcgaq8080/p/7204878.html

https://blog.csdn.net/u010137839/article/details/80210328

https://www.cnblogs.com/cing/p/7724653.html win7下安装redis

 

posted @ 2015-01-05 15:25  心存善念  阅读(33300)  评论(0编辑  收藏  举报