Cent0S7 安装Redis
1.安装前环境检查
1.1 查看redis的安装环境
redis是由C语言开发,因此安装之前必须要确保服务器已经安装了gcc,可以通过如下命令查看机器是否安装:
gcc -v
如果没有安装则通过以下命令安装:
yum install -y gcc
2.redis安装
2.1 创建Redis文件夹
sudo mkdir /volume1/Reids
2.2 下载Redis包到对应目录下
wget -P /volume1/ https://download.redis.io/releases/redis-6.2.8.tar.gz
2.3 解压下载下来的Redis包
tar -zxvf redis-6.2.8.tar.gz
2.4 进入解压目录
cd /volume1/redis-6.2.8
2.5编译Redis
make
2.6 前面创建了Redis目录,现在指定安装路径目录并进行安装
make install PREFIX=/volume1/redis
2.7 进入redis对应目录进行服务启动
cd /volume1/redis/bin
2.8 启动redis服务
./redis-server
3 配置Redis
3.1 从 redis 的源码目录中复制 redis.conf 到 redis 的安装目录
cp /volume1/redis-6.2.8/redis.conf /volume1/redis/bin/
3.2 进入对应目录,修改redis.conf配置文件
#进入reids目录
cd /volume1/redis/bin/#配置redis的配置文件
vi redis.conf
修改内容如下:
- daemonize 的值从 no 修改成 yes
- 将redis指定链接ip改成 不限ip链接,将127.0.0.1改成0.0.0.0
- daemonize 的值从 no 修改成 yes
# 在 这个下面
################################# 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 no
-
将redis指定链接ip改成 不限ip链接,将127.0.0.1改成0.0.0.0 |将 bind 127.0.0.1 -::1 改成 bind 0.0.0.0 -::1
# 在 这个下面 ############################### NETWORK ################################### # are already in use will always fail, and unsupported protocols will always BE # silently skipped. # # Examples: # # bind 192.168.1.100 10.0.0.1 # listens on two specific IPv4 addresses # bind 127.0.0.1 ::1 # listens on loopback IPv4 and IPv6 # bind * -::* # like the default, all available interfaces # # ~~~ WARNING ~~~ If the computer running Redis is directly exposed to the # internet, binding to all the interfaces is dangerous and will expose the # instance to everybody on the internet. So by default we uncomment the # following bind directive, that will force Redis to listen only on the # IPv4 and IPv6 (if available) loopback interface addresses (this means Redis # will only be able to accept client connections from the same host that it is # running on). # # IF YOU ARE SURE YOU WANT YOUR INSTANCE TO LISTEN TO ALL THE INTERFACES # JUST COMMENT OUT THE FOLLOWING LINE. # ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ bind 127.0.0.1 -::1
3.4 启动服务
./redis-server redis.conf
3.5 检查redis是否启动成功
ps -ef |grep redis
4.设置开机启动
4.1 进入到/lib/systemd/system/目录,创建redis.service文件
cd /lib/systemd/system/
4.2 创建redis.service文件
vim redis.service
4.3 编写文件内容
[Unit]
Description=redis-server
After=network.target[Service]
Type=forking
# ExecStart需要按照实际情况修改成自己的地址
ExecStart=/usr/local/redis/bin/redis-server /usr/local/redis/bin/redis.conf
PrivateTmp=true[Install]
WantedBy=multi-user.target
\\【例如我的配置:】
[Unit]
Description=redis-server
After=network.target[Service]
Type=forking
// ExecStart需要按照实际情况修改成自己的地址
ExecStart=/volume1/redis/bin/redis-server /volume1/redis/bin/redis.conf
PrivateTmp=true[Install]
WantedBy=multi-user.target
:wq! 保持并推出文件
4.4 使用Redis服务新命令
# 开机自动启动
systemctl enable redis.service# 启动redis服务
systemctl start redis.service# 查看服务状态
systemctl status redis.service# 停止服务
systemctl stop redis.service# 取消开机自动启动(卸载服务)
systemctl disabled redis.service