万隆的笔记 万隆的笔记
博文索引
笔试面试
  • 在线学站

    • 菜鸟教程 (opens new window)
    • 入门教程 (opens new window)
    • Coursera (opens new window)
  • 在线文档

    • w3school (opens new window)
    • Bootstrap (opens new window)
    • Vue (opens new window)
    • 阿里开发者藏经阁 (opens new window)
  • 在线工具

    • tool 工具集 (opens new window)
    • bejson 工具集 (opens new window)
    • 文档转换 (opens new window)
  • 更多在线资源
  • Changlog
  • Aboutme
GitHub (opens new window)
博文索引
笔试面试
  • 在线学站

    • 菜鸟教程 (opens new window)
    • 入门教程 (opens new window)
    • Coursera (opens new window)
  • 在线文档

    • w3school (opens new window)
    • Bootstrap (opens new window)
    • Vue (opens new window)
    • 阿里开发者藏经阁 (opens new window)
  • 在线工具

    • tool 工具集 (opens new window)
    • bejson 工具集 (opens new window)
    • 文档转换 (opens new window)
  • 更多在线资源
  • Changlog
  • Aboutme
GitHub (opens new window)
  • Redis

    • Redis简介
    • 缓存的认识
    • Linux安装Redis的正确方式
      • Centos 6.x 安装 Redis 5.x
      • 附:Docker安装
    • Redis数据类型
    • Redis底层数据结构
    • Redis回收策略与缓存过期
    • Redis持久化
    • Redis发布与订阅
    • Redis事务
    • Redis Lua脚本
    • Redis 慢查询
    • Redis 监视器
    • Redis通讯协议
    • Redis事件处理机制与NIO演进
    • Redis 常用命令
    • Redis与MyBatis整合
    • Spring、SpringBoot整合Redis
  • 集群架构

  • Redis
  • Redis
2022-03-31
目录

Linux安装Redis的正确方式

# Linux安装Redis的正确方式

Redis 最新版本已经出到 6.x,但是往往生产都是以稳定版本优先,这里我以安装5.0.14 (opens new window)为例。

# Centos 6.x 安装 Redis 5.x

网上很多安装的教程,都是下载已经编译过的安装包,直接解压配置环境等步骤,我这里是下载源码、编译、按照,并通过官方的utils安装多个redis实例,达到service redis的目的,步骤参考如下:

  1. 环境准备:下载redis源码

    # 如果安装过wget,可忽略
    sudo yum install wget
    # 下载源码,你也可以新建一个文件夹,以后下载的文件都放在一个目录下
    sudo wget http://download.redis.io/releases/redis-5.0.5.tar.gz
    
  2. 解压,并进入解压的目录(建议阅读README.md,不想阅读的进行下一步):

    tar zvxf redis-5.0.5.tar.gz
    cd redis-5.0.5/
    # 阅读README,里面有教你如何编译以及安装
    vi README.md 
    
  3. 编译准备:安装gcc,如果之前执行过 make,注意清除上次编译的环境:

    sudo yum install gcc
    sudo make distclean
    
  4. 编译源码,生成可执行文件

    # 进行编译生成可执行文件
    sudo make 
    ...
    Hint: It's a good idea to run 'make test' ;)
    # 进入src目录查看结果
    cd src
    
  5. 退回到解压的安装文件夹,安装 redis 到 /usr/local/redis5,本质上其实是复制文件到该安装路径

    cd ..
    # 安装redis,prefix指定安装的路径
    sudo make install PREFIX=/usr/local/redis5
    
  6. 编辑profile文件,配置redis环境;再通过utils安装一个实例

    # 配置`redis`环境
    sudo vi /etc/profile
    
    export REDIS_HOME=/usr/local/redis5
    export PATH=$PATH:$REDIS_HOME/bin
    
    source /etc/profile
    

    注意,这里如果你不是root用户,可以将配置配置到自己~/.bash_profile,或者切换到root执行下面步骤:

    # 通过`utils`安装一个实例
    cd utils
    [root@localhost utils]# ./install_server.sh
    Welcome to the redis service installer
    This script will help you easily set up a running redis server
    
    Please select the redis port for this instance: [6379] # 默认 6379, 多个实例用端口号区分
    Selecting default: 6379
    Please select the redis config file name [/etc/redis/6379.conf]
    Selected default - /etc/redis/6379.conf
    Please select the redis log file name [/var/log/redis_6379.log]
    Selected default - /var/log/redis_6379.log
    Please select the data directory for this instance [/var/lib/redis/6379]
    Selected default - /var/lib/redis/6379
    Please select the redis executable path [/usr/local/redis5/bin/redis-server] # 配置Redis环境成功,这里才会感知到执行路径
    Selected config:
    Port           : 6379
    Config file    : /etc/redis/6379.conf
    Log file       : /var/log/redis_6379.log
    Data dir       : /var/lib/redis/6379
    Executable     : /usr/local/redis5/bin/redis-server
    Cli Executable : /usr/local/redis5/bin/redis-cli
    Is this ok? Then press ENTER to go on or Ctrl-C to abort.
    Copied /tmp/6379.conf => /etc/init.d/redis_6379
    Installing service...
    Successfully added to chkconfig!
    Successfully added to runlevels 345!
    Starting Redis server...
    Installation successful!
    
    [root@localhost utils]# ps aux | grep redis
    root     13012  0.1  0.4 153828  7644 ?        Ssl  13:49   0:03 /usr/local/redis5/bin/redis-server 127.0.0.1:6379
    root     24384  0.0  0.0 112656   972 pts/1    S+   14:35   0:00 grep --color=auto redis
    

    至此,安装完成。后续如果有需要,我们可以通过utils安装多个Redis实例(进程),这种安装方式的优点:

    • 可执行文件在一个目录:/usr/local/redis5
    • 多个实例有自己的配置文件、数据文件等
    • 可以通过 service redis_xxx start/stop/status...的方式管理redis,xxx为端口号。
    • 系统启动时,会帮你启动redis实例。脚本在:/etc/init.d/redis_xxx

# 附:Docker安装

Docker 安装 Redis,docker-compose.yml配置文件如下,可参考 Docker Hub 的文档 (opens new window),当前 redis的lasted版本为6.2.6。

version: '3.1'
services:
  master:
    image: redis
    container_name: redis-master
    command: redis-server --port 6379 --requirepass dev@123456  --appendonly yes
    ports:
      - 6379:6379
    volumes:
      - /usr/local/docker/redis/master-data:/data

redis-server 命令说明:

  • --requirepass:指定redis密码
  • --appendonly yes :开启redis数据持久化
上次更新: 5/30/2023, 11:42:20 PM
Redis数据类型

Redis数据类型→

最近更新
01
2025
01-15
02
Elasticsearch面试题
07-17
03
Elasticsearch进阶
07-16
更多文章>
Theme by Vdoing
  • 跟随系统
  • 浅色模式
  • 深色模式
  • 阅读模式