Skip to content

CentOs 7 安装 Redis

1.下载并解压安装包

进入 redis 官网下载

下载软件包

cd /usr/local/software
wget http://download.redis.io/releases/redis-5.0.8.tar.gz

解压缩文件并移动

tar -zxvf redis-5.0.8.tar.gz

2.编译安装

切换到 redis 目录,编译

cd /usr/local/software/redis-5.0.8
make

如果这个时候报错

You need tcl 8.5 or newer in order to run the Redis test

先安装 tcl

cd /usr/local/software
wget http://downloads.sourceforge.net/tcl/tcl8.6.1-src.tar.gz
tar xzvf tcl8.6.1-src.tar.gz  -C /usr/local/
cd  ./tcl8.6.1/unix/
./configure
make
make install

再编译一次,然后安装

cd /usr/local/redis-5.0.8
cd src && make install PREFIX=/usr/local/redis

启动 redis 服务

cd /usr/local/redis/bin
./redis-server

启动成功后

13247:C 14 Apr 2020 15:44:28.388 # oO0OoO0OoO0Oo Redis is starting oO0OoO0OoO0Oo
13247:C 14 Apr 2020 15:44:28.388 # Redis version=5.0.8, bits=64, commit=00000000, modified=0, pid=13247, just started
13247:C 14 Apr 2020 15:44:28.388 # Warning: no config file specified, using the default config. In order to specify a config file use ./redis-server /path/to/redis.conf
13247:M 14 Apr 2020 15:44:28.389 * Increased maximum number of open files to 10032 (it was originally set to 1024).
             _._
        _.-``__ ''-._
    _.-``    `.  `_.  ''-._           Redis 5.0.8 (00000000/0) 64 bit
.-`` .-```.  ```\/    _.,_ ''-._
(    '      ,       .-`  | `,    )     Running in standalone mode
|`-._`-...-` __...-.``-._|'` _.-'|     Port: 6379
|    `-._   `._    /     _.-'    |     PID: 13247
`-._    `-._  `-./  _.-'    _.-'
|`-._`-._    `-.__.-'    _.-'_.-'|
|    `-._`-._        _.-'_.-'    |           http://redis.io
`-._    `-._`-.__.-'_.-'    _.-'
|`-._`-._    `-.__.-'    _.-'_.-'|
|    `-._`-._        _.-'_.-'    |
`-._    `-._`-.__.-'_.-'    _.-'
    `-._    `-.__.-'    _.-'
        `-._        _.-'
            `-.__.-'

13247:M 14 Apr 2020 15:44:28.390 # WARNING: The TCP backlog setting of 511 cannot be enforced because /proc/sys/net/core/somaxconn is set to the lower value of 128.
13247:M 14 Apr 2020 15:44:28.390 # Server initialized
13247:M 14 Apr 2020 15:44:28.390 # WARNING overcommit_memory is set to 0! Background save may fail under low memory condition. To fix this issue add 'vm.overcommit_memory = 1' to /etc/sysctl.conf and then reboot or run the command 'sysctl vm.overcommit_memory=1' for this to take effect.
13247:M 14 Apr 2020 15:44:28.390 # WARNING you have Transparent Huge Pages (THP) support enabled in your kernel. This will create latency and memory usage issues with Redis. To fix this issue run the command 'echo never > /sys/kernel/mm/transparent_hugepage/enabled' as root, and add it to your /etc/rc.local in order to retain the setting after a reboot. Redis must be restarted after THP is disabled.
13247:M 14 Apr 2020 15:44:28.390 * Ready to accept connections

3.配置服务启动

从 redis 的源码目录中复制 redis.conf 到 redis 的安装目录

cp /usr/local/software/redis-5.0.8/redis.conf /usr/local/redis/bin/

修改 redis.conf

vi /usr/local/redis/bin/redis.conf

变更内容

bind 0.0.0.0 #支持远程连接
daemonize yes
requirepass dqT%#60h #配置验证密码

编辑一个 redis.service 文件

vi /etc/systemd/system/redis.service

编辑内容,ExecStart 路径不一样的自行修改

[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 start redis.service   #启动redis服务

systemctl stop redis.service   #停止redis服务

systemctl restart redis.service   #重新启动服务

systemctl status redis.service   #查看服务当前状态

systemctl enable redis.service   #设置开机自启动

systemctl disable redis.service   #停止开机自启动