redis的官网在https://redis.io/。官网中的初始教程中有在不同操作系统下的安装方式;英文好的同学可以直接参照官网的教程来安装。 本文只介绍在CentOS7 下通过源码来安装的步骤。
下载Redis源码[root;ecs-Liunx download]# tar -xvf redis-6.2.6.tar.gz
解压完成后可以看到redis目录;
[root;ecs-Liunx redis-6.2.6]# ls -al
total 256
drwxrwxr-x 7 root root 4096 Oct 4 2021 .
drwxr-xr-x 8 root root 4096 May 19 15:14 ..
-rw-rw-r-- 1 root root 33624 Oct 4 2021 00-RELEASENOTES
-rw-rw-r-- 1 root root 51 Oct 4 2021 BUGS
-rw-rw-r-- 1 root root 5026 Oct 4 2021 CONDUCT
-rw-rw-r-- 1 root root 3384 Oct 4 2021 CONTRIBUTING
-rw-rw-r-- 1 root root 1487 Oct 4 2021 COPYING
drwxrwxr-x 7 root root 213 Apr 12 11:58 deps
drwxrwxr-x 4 root root 45 Oct 4 2021 .github
-rw-rw-r-- 1 root root 483 Oct 4 2021 .gitignore
-rw-rw-r-- 1 root root 11 Oct 4 2021 INSTALL
-rw-rw-r-- 1 root root 151 Oct 4 2021 Makefile
-rw-rw-r-- 1 root root 6888 Oct 4 2021 MANIFESTO
-rw-rw-r-- 1 root root 21567 Oct 4 2021 README.md
-rw-rw-r-- 1 root root 93724 Oct 4 2021 redis.conf
-rwxrwxr-x 1 root root 275 Oct 4 2021 runtest
-rwxrwxr-x 1 root root 279 Oct 4 2021 runtest-cluster
-rwxrwxr-x 1 root root 1079 Oct 4 2021 runtest-moduleapi
-rwxrwxr-x 1 root root 281 Oct 4 2021 runtest-sentinel
-rw-rw-r-- 1 root root 13768 Oct 4 2021 sentinel.conf
drwxrwxr-x 3 root root 12288 Apr 12 12:03 src
drwxrwxr-x 11 root root 182 Oct 4 2021 tests
-rw-rw-r-- 1 root root 3055 Oct 4 2021 TLS.md
drwxrwxr-x 9 root root 4096 Oct 4 2021 utils
安装C语言编译器gccyum list installed gcc
如果未安装gcc, 可以使用下面的命令安装。
yum install -y gcc
3.编译源文件
执行以下命令进入源文件目录并编译。
[root;ecs-Liunx download]# cd redis-6.2.6
[root;ecs-Liunx redis-6.2.6]# make
4.指定安装目录安装
执行以下命令安装redis到指定目录, 比如; /usr/local/redis
[root;ecs-Liunx redis-6.2.6]# make install PREFIX=/usr/local/redis
5.启动Redis服务
编译安装完Redis;可以通过守护进程方式启动Redis服务。
首先将源文件目录下Redis的配置文件拷贝到安装目录。
[root;ecs-Liunx redis-6.2.6]# cp /opt/download/redis-6.2.6/redis.conf /usr/local/redis/bin
其次修改配置文件
[root;ecs-Liunx bin]# cd /usr/local/redis/bin
[root;ecs-Liunx bin]# vi redis.conf
将其中daemonize 的值从 no 修改成 yes
# to unlimited size. The default size is 20480.
#
# tls-session-cache-size 5000
# Change the default timeout of cached TLS sessions. The default timeout is 300
# seconds.
#
# tls-session-cache-timeout 60
################################# 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.
# When Redis is supervised by upstart or systemd, this parameter has no impact.
daemonize yes
# If you run Redis from upstart or systemd, Redis can interact with your
# superVision tree. Options:
# supervised no - no supervision interaction
# supervised upstart - signal upstart by putting Redis into SIGSTOP mode
# requires ;expect stop; in your upstart job config
# supervised systemd - signal systemd by writing READY=1 to $NOTIFY_SOCKET
# on startup, and updating Redis status on a regular
# basis.
最后在命令行启动服务。
[root;ecs-Liunx bin]# ./redis-server redis.conf
6.设置开机自启动。
切换到/lib/systemd/system/目录;创建redis.service文件。命令如下;
[root;ecs-Liunx redis-6.2.6]# cd /lib/systemd/system/
[root;ecs-Liunx system]# vi redis.service
在该文件中输入以下内容;
[Unit]
Description=redis-server
After=network.target
[Service]
Type=forking
ExecStart=/usr/local/redis/bin/redis-server /usr/local/redis/bin/redis.conf
PrivateTmp=true
[Install]
WantedBy=multi-user.target
保存文件。
然后输入systemctl enable 命令打开自动启动服务;输入 systemctl start redis.service 启动服务。
systemctl enable redis.service
systemctl start redis.service
最后如果要停止服务可以输入;
systemctl stop redis.service