本文主要介绍如果在Centos7下安装Redis。
1.安装依赖
redis是由C语言开发,因此安装之前必须要确保服务器已经安装了gcc,可以通过如下命令查看机器是否安装:
<span style="color:#000000"><span style="background-color:#ffffff"><code class="language-python">gcc -v
</code></span></span>
如果没有安装则通过以下命令安装:
<span style="color:#000000"><span style="background-color:#ffffff"><code class="language-python">yum install -y gcc
</code></span></span>
2.下载redis安装包并解压
<span style="color:#000000"><span style="background-color:#ffffff"><code class="language-python"><span style="color:#008000"># 下载,我是在root下执行的下载,所以我的下载目录为:/root/redis-6.2.6,这里按照自己的实际情况调整</span>
wget https://download.redis.io/releases/redis-<span style="color:#880000">6.2</span><span style="color:#880000">.6</span>.tar.gz
<span style="color:#008000"># 解压</span>
tar -zxvf redis-<span style="color:#880000">6.2</span><span style="color:#880000">.6</span>.tar.gz
</code></span></span>
3.进入解压目录并编译
<span style="color:#000000"><span style="background-color:#ffffff"><code class="language-python"><span style="color:#008000"># 进入解压目录</span>
cd redis-<span style="color:#880000">6.2</span><span style="color:#880000">.6</span>
<span style="color:#008000"># 编译</span>
make
</code></span></span>
4.指定安装目录并进行安装
<span style="color:#000000"><span style="background-color:#ffffff"><code class="language-python">make install PREFIX=/usr/local/redis
</code></span></span>
5.启动redis服务
5.1 直接启动(不建议使用)
<span style="color:#000000"><span style="background-color:#ffffff"><code class="language-python"><span style="color:#008000"># 进入redis安装目录</span>
cd /usr/local/redis/<span style="color:#0000ff">bin</span>/
<span style="color:#008000"># 启动服务</span>
./redis-server
</code></span></span>
5.2 通过守护进程方式启动
<span style="color:#000000"><span style="background-color:#ffffff"><code class="language-python"><span style="color:#008000"># 第一步:从 redis 的源码目录中复制 redis.conf 到 redis 的安装目录</span>
cp /root/redis-<span style="color:#880000">6.2</span><span style="color:#880000">.6</span>/redis.conf /usr/local/redis/<span style="color:#0000ff">bin</span>/
<span style="color:#008000"># 第二步:修改redis.conf配置文件</span>
cd /usr/local/redis/<span style="color:#0000ff">bin</span>/
vi redis.conf
<span style="color:#008000"># 修改内容如下:</span>
<span style="color:#008000">#daemonize 的值从 no 修改成 yes</span>
<span style="color:#008000"># 第三步:启动服务</span>
./redis-server redis.conf
<span style="color:#008000"># 第四步:查看进程来确定redis是否启动成功,非必须</span>
ps -ef |grep redis
</code></span></span>
备注:如果想要设置指定IP连接redis,只需要修改redis.conf文件中bind配置项即可。如果不限IP,将127.0.0.1修改成0.0.0.0即可。
6.设置开机自动启动
切换到/lib/systemd/system/目录,创建redis.service文件。命令如下:
<span style="color:#000000"><span style="background-color:#ffffff"><code class="language-python">cd /lib/systemd/system/
vim redis.service
</code></span></span>
文件内容如下:
<span style="color:#000000"><span style="background-color:#ffffff"><code class="language-python">[Unit]
Description=redis-server
After=network.target
[Service]
<span style="color:#a31515">Type</span>=forking
<span style="color:#008000"># ExecStart需要按照实际情况修改成自己的地址</span>
ExecStart=/usr/local/redis/<span style="color:#0000ff">bin</span>/redis-server /usr/local/redis/<span style="color:#0000ff">bin</span>/redis.conf
PrivateTmp=true
[Install]
WantedBy=multi-user.target
</code></span></span>
设置开启自动启动
<span style="color:#000000"><span style="background-color:#ffffff"><code class="language-python"><span style="color:#008000"># 开机自动启动</span>
systemctl enable redis.service
<span style="color:#008000"># 启动redis服务</span>
systemctl start redis.service
<span style="color:#008000"># 查看服务状态</span>
systemctl status redis.service
<span style="color:#008000"># 停止服务</span>
systemctl stop redis.service
<span style="color:#008000"># 取消开机自动启动(卸载服务)</span>
systemctl disabled redis.service</code></span></span>