Windows和Linux(Centos7)下的Redis安装及使用

一、简介

Redis 是一个开源(BSD许可)的,内存中的数据结构存储系统,它可以用作数据库、缓存和消息中间件。 它支持多种类型的数据结构,如 字符串(strings), 散列(hashes), 列表(lists), 集合(sets), 有序集合(sorted sets) 与范围查询, bitmaps, hyperloglogs 和 地理空间(geospatial) 索引半径查询。 Redis 内置了 复制(replication),LUA脚本(Lua scripting), LRU驱动事件(LRU eviction),事务(transactions) 和不同级别的 磁盘持久化(persistence), 并通过 Redis哨兵(Sentinel)和自动 分区(Cluster)提供高可用性(high availability)。[简介来源Redis中文官网]

二、Windows下安装

  1. Redis下载

    由于Redis还没正式支持Windows,但由于其是开源的,微软开放技术小组开发并维护了针对Win64的Windows接口程序,提供了一个windows版本的redis分支,目前已经不进行更新维护了。

    下载地址:https://github.com/microsoftarchive/redis/releases
    在这里插入图片描述

  2. 创建目录(目录随意,按照习惯,我放到了D盘:D:\Redis)用于存放解压后的文件
    解压后的内容如下:
    在这里插入图片描述

  3. 打开cmd窗口,进入D:\Redis目录(刚才的安装目录)下

    C:\Users>D:
    D:\>cd Redis
    D:\Redis>
    

    或者直接在刚才的D:\Redis目录下直接进入cmd窗口
    在这里插入图片描述

  4. 运行Redis
    输入命令redis-server.exe redis.windows.conf

    D:\Redis>redis-server.exe redis.windows.conf
    [9380] 21 Dec 20:12:21.137 # Warning: no config file specified, using the default config. In order to specify a config file use redis-server.exe /path/to/redis.conf
                    _._
               _.-``__ ''-._
          _.-``    `.  `_.  ''-._           Redis 3.2.100 (00000000/0) 64 bit
      .-`` .-```.  ```\/    _.,_ ''-._
     (    '      ,       .-`  | `,    )     Running in standalone mode
     |`-._`-...-` __...-.``-._|'` _.-'|     Port: 6379
     |    `-._   `._    /     _.-'    |     PID: 9380
      `-._    `-._  `-./  _.-'    _.-'
     |`-._`-._    `-.__.-'    _.-'_.-'|
     |    `-._`-._        _.-'_.-'    |           http://redis.io
      `-._    `-._`-.__.-'_.-'    _.-'
     |`-._`-._    `-.__.-'    _.-'_.-'|
     |    `-._`-._        _.-'_.-'    |
      `-._    `-._`-.__.-'_.-'    _.-'
          `-._    `-.__.-'    _.-'
              `-._        _.-'
                  `-.__.-'
    
    [9380] 21 Dec 20:12:21.143 # Server started, Redis version 3.2.100
    [9380] 21 Dec 20:12:21.143 * DB loaded from disk: 0.000 seconds
    [9380] 21 Dec 20:12:21.143 * The server is now ready to accept connections on port 6379
    

    1.可以把 redis 的路径(安装路径:D:\Redis)加到系统的环境变量里,这样就可以在任意目录下启动了
    2.后面的那个redis.windows.conf可以省略,会启用默认的
    在这里插入图片描述

  5. 简单使用
    (1)另启一个 cmd 窗口,原来的不要关闭,不然就无法访问服务端了
    (2)进入redis目录下(添加过环境变量的不需要进入redis目录)
    (3)连接Redis服务器

    redis-cli.exe -h 127.0.0.1 -p 6379
    

    (4)设置键值对

    set myKey myValue
    

    (5)取出键值对

    get myKey
    

    在这里插入图片描述

三、Linux(Centos7)下安装

  1. Redis下载

    下载地址:https://redis.io/download,最好下载最新稳定版本,我下载的时候最新版本为5.0

    (1)获取对应版本的下载链接
    在这里插入图片描述
    (2)使用wget下载Redis安装包

    [root@localhost Redis]# wget http://download.redis.io/releases/redis-5.0.7.tar.gz
    [root@localhost Redis]# tar xzf redis-5.0.7.tar.gz
    [root@localhost Redis]# cd redis-5.0.7/
    [root@localhost redis-5.0.7]# make
    
  2. 启动redis的三种方式
    第一种方式:直接启动Redis

    [root@localhost redis-5.0.7]# ./src/redis-server
    

    这种方式启动Redis 使用的是默认配置。也可以通过启动参数告诉Redis使用指定配置文件使用下面命令启动:./src/redis-server ./redis.conf

    第二种方式:以后台进程方式启动Redis
    修改redis.conf文件
    daemonize no修改为daemonize yes
    在这里插入图片描述

    [root@localhost redis-5.0.7]# ./src/redis-server ./redis.conf
    

    查看进程:ps -aux | grep redis
    在这里插入图片描述
    关闭进程:kill 进程id

    第三种方式:设置Redis开机自启动
    首先必须确保将daemonize no修改为daemonize yes,修改方法请看第二种方式
    在/etc目录下新建redis目录

    [root@localhost redis-5.0.7]# cd /etc/
    [root@localhost etc]# mkdir redis
    

    安装目录下的redis.conf 文件复制一份到/etc/redis目录下,并命名为6379.conf

    [root@localhost etc]# cd redis/ 
    [root@localhost redis]# cp /home/Redis/redis-5.0.7/redis.conf /etc/redis/6379.conf
    

    将Redis的启动脚本复制一份放到/etc/init.d目录下,并重命名为redisd

    [root@localhost redis]# cd /etc/init.d/
    [root@localhost init.d]# cp /home/Redis/redis-5.0.7/utils/redis_init_script /etc/init.d/redisd
    

    修改redisd文件里的两个路径

    [root@localhost init.d]# vim redisd
    

    我的Redis安装路径为/home/Redis/redis-5.0.7,所以我修改为:
    在这里插入图片描述

    此处如果不进行修改,在启动的时候会报错:/etc/init.d/redisd: line 28: /usr/local/bin/redis-server: No such file or directory,详细报错内容请参考:报错(/etc/init.d/redisd: line 28: /usr/local/bin/redis-server: No such file or directory)的解决办法

    设置redis开机自启动

    [root@localhost init.d]# chkconfig redisd on
    

    现在可以直接以服务的形式启动和关闭redis了
    启动:service redisd start

    [root@localhost init.d]# service redisd start
    Starting Redis server...
    14200:C 09 Dec 2019 21:17:50.133 # oO0OoO0OoO0Oo Redis is starting oO0OoO0OoO0Oo
    14200:C 09 Dec 2019 21:17:50.133 # Redis version=5.0.7, bits=64, commit=00000000, modified=0, pid=14200, just started
    14200:C 09 Dec 2019 21:17:50.133 # Configuration loaded
    [root@localhost init.d]# 
    

    关闭:service redisd stop

    [root@localhost init.d]# service redisd stop
    Stopping ...
    Redis stopped
    [root@localhost init.d]# 
    
  3. 简单使用

    [root@localhost init.d]# cd /home/Redis/redis-5.0.7/
    [root@localhost redis-5.0.7]# src/redis-cli
    127.0.0.1:6379> set myKey myValue
    OK
    127.0.0.1:6379> get myKey
    "myValue"
    127.0.0.1:6379> 
    

    常用操作命令:
    启动客户端:src/redis-cli
    设置键值对:set 键 值
    通过键获取值:get 键

欢迎关注同名微信公众号:程序猿杂记

程序猿杂记

技术|交流|福利
发布了63 篇原创文章 · 获赞 87 · 访问量 4万+

猜你喜欢

转载自blog.csdn.net/weixin_44110998/article/details/103454963