[TOC] ### 一、安装 Redis #### 1. 下载 Redis 源码包 --- Redis官方下载站:[https://redis.io](https://redis.io) 源码包的链接地址:http://download.redis.io/releases/redis-6.0.8.tar.gz ![](https://img.itqaq.com/art/content/b956c9c54cdec14e659dc0fe7069271c.png) #### 2. 上传到 linux 服务器上(我用的是vmware中的centos7 最小化安装) **方法一:使用 wget 命令** ![](https://img.itqaq.com/art/content/a51574ea34ff13bb873338f43283b5fd.png) 因为我用的是最小化安装,默认是没有安装 wget 命令,所以要使用 wget 命令必须先按照该命令的包 ``` yum install wget -y ``` 再执行该命令就可以将redis源码包下载下来,redis源码包很小(2.14 MB),使用 wget 下载命令很快的,推荐使用该方式 ![](https://img.itqaq.com/art/content/cdb0553a5d9bc4c17f35e424d12482a5.png) **方法二:使用 secureCRT 上传** 先将redis源码包下载到本地,然后打开 `SFTP窗口` ![](https://img.itqaq.com/art/content/931cb52cae9c2f3451afca5e2521d7d1.png) 将文件直接拖动到 `SFTP窗口` 即可 ![](https://img.itqaq.com/art/content/0635c247e45a9dd4fe498d052eff3084.png) #### 3. 解压 Redis 源码包 --- ``` tar xf redis-6.0.8.tar.gz ``` ![](https://img.itqaq.com/art/content/8ecff08e30485661ed9840be3cef3183.png) #### 4. 进入解压的目录,无需配置,直接编译进行安装 --- PREFIX 用于指定安装路径,必须大写,编译安装 nginx 时是小写,不要搞混了 ``` make PREFIX=/usr/local/redis install ``` ![](https://img.itqaq.com/art/content/38ad2dbf8fa5bb7c438d80dd5bed28e5.png) 执行以上命令如果出现以下错误,代表没有安装 gcc 环境 ![](https://img.itqaq.com/art/content/00056d555108405dc09221d465043c2f.png) yum 安装 gcc ``` yum install gcc gcc-c++ -y ``` 重新编译安装 ``` make distclean && make PREFIX=/usr/local/redis install ``` 报错原因:gcc 版本太低,centos7.6 yum 下载的gcc版本是 4.8.5 ![](https://img.itqaq.com/art/content/86bee8feddf1a55155e46f3040a85e63.png) gcc 版本太低的解决办法: ``` # 查看gcc版本是否在5.3以上,centos7.6默认安装4.8.5 gcc -v # 升级gcc到5.3及以上,如下: 升级到gcc 9.3: yum -y install centos-release-scl yum -y install devtoolset-9-gcc devtoolset-9-gcc-c++ devtoolset-9-binutils scl enable devtoolset-9 bash 需要注意的是scl命令启用只是临时的,退出shell或重启就会恢复原系统gcc版本。 如果要长期使用gcc 9.3的话: echo "source /opt/rh/devtoolset-9/enable" >>/etc/profile 这样退出shell重新打开就是新版的gcc了 以下其他版本同理,修改devtoolset版本号即可。 ``` 第二次重新编译安装 ``` make PREFIX=/usr/local/redis install ``` 命令执行结束显示以下内容代表编译安装成功 ![](https://img.itqaq.com/art/content/6b83c299ab5a9b7ebcd8d478053f8a5a.png) ### 二、启动 Redis 服务(redis-server) #### 1. 直接启动 Redis 进入 redis 的安装目录,运行 bin 目录下的 redis-server 文件 ``` cd /usr/local/redis ./bin/redis-server ``` 这种启动方式需要一直打开窗口,不能进行其他操作,不太方便。按 ctrl + c可以关闭窗口 #### 2. 以后台进程方式启动redis 将 redis 源码包目录中的 redis.conf 拷贝到 redis 安装目录下 ``` cp /usr/local/src/redis.conf /usr/local/redis/ ``` ![](https://img.itqaq.com/art/content/7231aa8892921c16af809d5eedeb11e5.png) 修改配置文件 /usr/local/redis/redis.conf 将 ``` daemonize no ``` 修改为 ``` daemonize yes ``` 指定 redis.conf 文件启动 ``` ./redis-server ../redis.conf ``` ![](https://img.itqaq.com/art/content/e05b28faf7b2d04cf592a82112b5488e.png) ### 三、关闭 Redis 进程 首先使用 `ps -aux | grep redis` 查看redis进程,再使用 kill 命令杀死进程 ``` ps -aux | grep redis kill 13251 ``` ![](https://img.itqaq.com/art/content/dd1b95e24f43d467966a0f26f625f7cd.png) ### 四、进入 Redis(redis-cli) 执行 Redis 安装目录下的bin目录下的 redis-cli 文件 ``` [root@localhost bin]# pwd /usr/local/redis/bin [root@localhost bin]# ll total 37960 -rw-r--r--. 1 root root 92 Sep 15 13:13 dump.rdb -rwxr-xr-x. 1 root root 4740344 Sep 15 12:46 redis-benchmark -rwxr-xr-x. 1 root root 9686736 Sep 15 12:46 redis-check-aof -rwxr-xr-x. 1 root root 9686736 Sep 15 12:46 redis-check-rdb -rwxr-xr-x. 1 root root 5060192 Sep 15 12:46 redis-cli lrwxrwxrwx. 1 root root 12 Sep 15 12:46 redis-sentinel -> redis-server -rwxr-xr-x. 1 root root 9686736 Sep 15 12:46 redis-server [root@localhost bin]# ./redis-cli 127.0.0.1:6379> ``` > 参考资料 https://www.bilibili.com/video/BV1zt4y1Q71k?p=2 https://blog.csdn.net/weidu01/article/details/105946606