redis系列一:下载与安装

redis是什么?

Redis(官网 https://redis.io/) 是一个开源(BSD 许可)的内存数据结构存储,用作数据库、缓存和消息代理。 Redis 提供数据结构,例如字符串、散列、列表、集合、具有范围查询的排序集合、位图、超日志、地理空间索引和流。 Redis 具有内置复制、Lua 脚本、LRU 驱逐、事务和不同级别的磁盘持久性,并通过 Redis Sentinel 和 Redis Cluster 自动分区提供高可用性。

下载指引

下载地址:https://redis.io/download
每次进入都会显示当前最新的版本,例如当前最新的版本是7.0-rc1,如果想下载5.0历史版本,需要在页面找到 【Other versions】,选择download5.0.14则可以下载
image

6.0.16下载地址:https://download.redis.io/releases/redis-6.0.16.tar.gz
5.0.14下载地址:https://download.redis.io/releases/redis-5.0.14.tar.gz
所有版本地址:https://download.redis.io/releases/

centos下载和安装

在centos版本是7.6 和 8.2都测试过,其他版本慎用本教程
下面使用centos7.6版本安装redis5.0.14

1. 下载

在 /usr/local下执行

wget https://download.redis.io/releases/redis-5.0.14.tar.gz

如果显示:

-bash: wget: command not found,

表示当前环境还没有安装wget,执行

yum -y install wget

进行安装
下载完后

-rw-r--r--. 1 root root 2000179 Oct  4 19:08 redis-5.0.14.tar.gz

2. 解压

执行指令

tar -zxvf redis-5.0.14.tar.gz 

3. 检查gcc版本

redis的安装需要依赖gcc编译器,安装gcc或者将版本升级到最新,执行指令

yum -y install gcc tcl

如果安装redis的gcc版本需要大于5.x版本,需要按顺序执行

yum -y install centos-release-scl
yum -y install devtoolset-9-gcc devtoolset-9-gcc-c++ devtoolset-9-binutils
scl enable devtoolset-9 bash

4. 编译

因为我的redis解压后目录就在 /urs/local下,所以我就进入redis后执行编译命令

make

如果解压目录不是在/usr/local下,又想安装在/usr/local/redis下,就进入redis目录后执行编译命令

make PREFIX=/usr/local/redis install

编译过程需要1-2分钟时间,成功后的结果有一行日志:

make[1]: Leaving directory /usr/local/redis-5.0.14/src

说明redis已经编译好了
编译后我的redis目录如下

[root@localhost redis-5.0.14]# ll
total 352
-rw-rw-r--.  1 root root 127554 Oct  4 18:58 00-RELEASENOTES
-rw-rw-r--.  1 root root     53 Oct  4 18:58 BUGS
-rw-rw-r--.  1 root root   2381 Oct  4 18:58 CONTRIBUTING
-rw-rw-r--.  1 root root   1487 Oct  4 18:58 COPYING
drwxrwxr-x.  6 root root    192 Feb 27 23:06 deps
-rw-rw-r--.  1 root root     11 Oct  4 18:58 INSTALL
-rw-rw-r--.  1 root root    151 Oct  4 18:58 Makefile
-rw-rw-r--.  1 root root   6888 Oct  4 18:58 MANIFESTO
-rw-rw-r--.  1 root root  20555 Oct  4 18:58 README.md
-rw-rw-r--.  1 root root  63086 Feb 27 23:15 redis.conf
-rw-r--r--.  1 root root  63088 Feb 27 23:08 redis.conf.bak
-rwxrwxr-x.  1 root root    275 Oct  4 18:58 runtest
-rwxrwxr-x.  1 root root    280 Oct  4 18:58 runtest-cluster
-rwxrwxr-x.  1 root root    373 Oct  4 18:58 runtest-moduleapi
-rwxrwxr-x.  1 root root    281 Oct  4 18:58 runtest-sentinel
-rw-rw-r--.  1 root root   9710 Oct  4 18:58 sentinel.conf
drwxrwxr-x.  3 root root   8192 Feb 27 23:07 src
drwxrwxr-x. 11 root root    182 Oct  4 18:58 tests
drwxrwxr-x.  8 root root   4096 Oct  4 18:58 utils

5. 修改配置文件

在redis目录下找到redis.conf文件,这个文件配置着启动的信息

# 哪个ip可以访问,默认是127.0.0.1修改为0.0.0.0没有限制
bind 0.0.0.0
#默认是设置成yes的, 防止了远程访问,修改为no
protected-mode no
#守护线程启动,即后台运行,默认是no,修改为yes
daemonize yes
#端口,不修改默认是6379
port 6379

6. 运行

在redsi根目录下执行

./src/redis-server redis.conf

执行成功后显示

[root@localhost redis-5.0.14]# ./src/redis-server redis.conf
19718:C 27 Feb 2022 23:17:06.393 # oO0OoO0OoO0Oo Redis is starting oO0OoO0OoO0Oo
19718:C 27 Feb 2022 23:17:06.393 # Redis version=5.0.14, bits=64, commit=00000000, modified=0, pid=19718, just started
19718:C 27 Feb 2022 23:17:06.393 # Configuration loaded

7. 检查是否已经启动

在redis根目录下执行

./src/redis-cli

显示如下则说明已经后台运行redis,如需退出当前客户端,执行quit回车即可

[root@localhost redis-5.0.14]# ./src/redis-cli 
127.0.0.1:6379> 

开机启动

1. 前置条件

redis服务的启动文件redis-server在/usr/local/redis-5.0.14/src目录下
启动的配置文件redis.conf在/usr/local/redis-5.0.14目录下

2. 创建配置文件

/usr/lib/systemd/system下执行

vi redisd.service

输入内容

[Unit]
Description=Redis
After=network.target

[Service]
Type=forking
ExecStart=/usr/local/redis-5.0.14/src/redis-server /usr/local/redis-5.0.14/redis.conf
ExecReload=/usr/local/redis-5.0.14/src/redis-server -s reload
ExecStop=/usr/local/redis-5.0.14/src/redis-server -s stop
PrivateTmp=true

[Install]
WantedBy=multi-user.target

保存退出
执行开机启动

systemctl enable redisd

3. 常用的命令

#启动redis服务
systemctl start redisd

#重启redis服务
systemctl restart redisd

#停止redis服务
systemctl stop redisd

#禁止开机自启
systemctl disable redisd

#查看状态
systemctl status redisd
posted @   匿名用户甲  阅读(535)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· 无需6万激活码!GitHub神秘组织3小时极速复刻Manus,手把手教你使用OpenManus搭建本
· C#/.NET/.NET Core优秀项目和框架2025年2月简报
· 葡萄城 AI 搜索升级:DeepSeek 加持,客户体验更智能
· 什么是nginx的强缓存和协商缓存
· 一文读懂知识蒸馏
点击右上角即可分享
微信分享提示