Redis 没有官方的Windows版本,微软开源技术团队(Microsoft Open Tech group)开发和维护着 Win64 的版本,但并不建议用于生产环境。

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

安装

官方下载地址为: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-serverredis-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> 

可有些童鞋就疑惑了,为什么运行redis-server后,不能退出后台运行呢,如果关闭终端,不就连redis一起关闭了吗? 请继续往下看……

后台运行

拷贝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]# 

是不是可以后台启动了呢?

最后

有些童鞋可能还是有疑惑,如何能让redis开机启动?又如何能让redis像服务一样启动或停止,类似下面的操作呢?

#打开服务
service redisd start
#关闭服务
service redisd stop

让Redis以服务方式运行并开机启动