这里主要介绍Centos下安装Redis,以及配置redis,后台运行以及自动启动等。

1.安装

官方下载地址为:https://redis.io/download

[root@localhost ~]# wget http://download.redis.io/releases/redis-4.0.6.tar.gz

解压

[root@localhost ~]# tar xzf redis-4.0.6.tar.gz

编译

[root@localhost ~]# cd redis-4.0.6
[root@localhost redis-4.0.6]# make

安装,官网并没有这个操作,执行这个的目的就是将可执行文件拷贝到/usr/local/bin/目录下,这样就可以直接执行redis-server 、redis-cli 等命令了。

[root@localhost redis-4.0.6]# make install

安装完成,查看版本

[root@localhost redis-4.0.6]# redis-server -v
Redis server v=4.0.6 sha=00000000:0 malloc=jemalloc-4.0.3 bits=64 build=4f1a60f0053726b7

启动 redis-server

[root@localhost redis-4.0.6]# redis-server 
9961:C 12 Dec 05:14:58.101 # oO0OoO0OoO0Oo Redis is starting oO0OoO0OoO0Oo
9961:C 12 Dec 05:14:58.101 # Redis version=4.0.6, bits=64, commit=00000000, modified=0, pid=9961, just started
9961:C 12 Dec 05:14:58.101 # Warning: no config file specified, using the default config. In order to specify a config file use redis-server /path/to/redis.conf
                _._                                                  
           _.-``__ ''-._                                             
      _.-``    `.  `_.  ''-._           Redis 4.0.6 (00000000/0) 64 bit
  .-`` .-```.  ```\/    _.,_ ''-._                                   
 (    '      ,       .-`  | `,    )     Running in standalone mode
 |`-._`-...-` __...-.``-._|'` _.-'|     Port: 6379
 |    `-._   `._    /     _.-'    |     PID: 9961
  `-._    `-._  `-./  _.-'    _.-'                                   
 |`-._`-._    `-.__.-'    _.-'_.-'|                                  
 |    `-._`-._        _.-'_.-'    |           http://redis.io        
  `-._    `-._`-.__.-'_.-'    _.-'                                   
 |`-._`-._    `-.__.-'    _.-'_.-'|                                  
 |    `-._`-._        _.-'_.-'    |                                  
  `-._    `-._`-.__.-'_.-'    _.-'                                   
      `-._    `-.__.-'    _.-'                                       
          `-._        _.-'                                           
              `-.__.-'                                               

9961:M 12 Dec 05:14:58.105 # WARNING: The TCP backlog setting of 511 cannot be enforced because /proc/sys/net/core/somaxconn is set to the lower value of 128.
9961:M 12 Dec 05:14:58.105 # Server initialized
9961:M 12 Dec 05:14:58.105 # 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.
9961:M 12 Dec 05:14:58.105 # 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.
9961:M 12 Dec 05:14:58.105 * Ready to accept connections

看到以上页面就启动成功啦,我们新开一个终端去连接redis。

[root@localhost ~]# redis-cli
127.0.0.1:6379> set name zhangsan
OK
127.0.0.1:6379> get name
"zhangsan"
127.0.0.1:6379> keys *
1) "name"
127.0.0.1:6379> 

2.后台运行

拷贝redis.conf配置文件到/etc/redis 在刚解压的redis根目录执行:

[root@localhost redis-4.0.6]# mkdir /etc/redis
[root@localhost redis-4.0.6]# cp redis.conf /etc/redis/

编辑配置文件

[root@localhost redis-4.0.6]# vim /etc/redis/redis.conf 

找到daemonize no 改为daemonize yes

# 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.
daemonize yes

指定配置文件启动redis

[root@localhost redis-4.0.6]# redis-server  /etc/redis/redis.conf 
9999:C 12 Dec 05:31:11.872 # oO0OoO0OoO0Oo Redis is starting oO0OoO0OoO0Oo
9999:C 12 Dec 05:31:11.872 # Redis version=4.0.6, bits=64, commit=00000000, modified=0, pid=9999, just started
9999:C 12 Dec 05:31:11.872 # Configuration loaded
[root@localhost redis-4.0.6]# 

3.以服务方式运行并开机启动

首先我们切换到解压后的Redis目录

cd redis-4.0.6

然后将utils/redis_init_script文件复制到/etc/init.d下命名为redisd

cp utils/redis_init_script /etc/init.d/redisd

编辑/etc/init.d/redisd文件

vim /etc/init.d/redisd

在第二行加入# chkconfig: 2345 80 90,目的是让redis开机启动,并修改里边的CONF,我的配置文件为/etc/redis/redis.conf ,大约在11行,其他的也可以根据实际情况修改

#!/bin/sh
# chkconfig: 2345 80 90  
#
# Simple Redis init.d script conceived to work on Linux systems
# as it does use of the /proc filesystem.

REDISPORT=6379
EXEC=/usr/local/bin/redis-server
CLIEXEC=/usr/local/bin/redis-cli

PIDFILE=/var/run/redis_${REDISPORT}.pid
CONF="/etc/redis/redis.conf"

添加至服务

chkconfig --add redisd

查看一下

[root@localhost redis-4.0.6]# chkconfig --list |grep redisd

注意:该输出结果只显示 SysV 服务,并不包含原生 systemd 服务。SysV 配置数据可能被原生 systemd 配置覆盖。 
      如果您想列出 systemd 服务,请执行 'systemctl list-unit-files'。
      欲查看对特定 target 启用的服务请执行
      'systemctl list-dependencies [target]'。

redisd          0:关    1:关    2:开    3:开    4:开    5:开    6:关

启动Redis

service redisd start

关闭Redis

service redisd stop